You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The filter_map method introduced in 1.63 extended the existing map method by allowing the conversion to be fallible. However, in case of a failure, the conversion doesn't have a straight-forward way to return any value.
Do we need to return the original RefMut in case of failure:
Pros:
More general, potentially covers more use-cases.
Consistent with filter_map.
Cons:
You have to .map_err(|(_, e)| e) to use ?
Need to return 2 values in Result::Err.
What do we put in Resut:Err in return value?
A struct (pros: field order doesn't matter, cons: Occam's razor)
A tuple (pros: more ergonomic, cons: have to decided on the order of values)
Can we generalize this using Try / Residual?
I have considered generalizing this to use the Try & Residual just like #79711 does for array::try_map, but it does not seem to be possible: we want to essentially .map_err(|e| (orig, e)) but this does not seem to be supported with Try. (Plus I am not even sure if it is possible to express the fact that &U in Try::Output would have to have the same lifetime as the &T input of F.)
[src]
I also have a need of this feature which would greatly help with easier error handling when dealing with Ref/RefMut.
Well, this will probably take ages before it's accepted (and even more before it's stabilized), just as everything that has to do with libs-api, but meanwhile you can use one of the workaround from the OP.
Proposal: Add
{Ref,RefMut}::try_map
methodProblem statement
The
filter_map
method introduced in 1.63 extended the existingmap
method by allowing the conversion to be fallible. However, in case of a failure, the conversion doesn't have a straight-forward way to return any value.Motivating examples or use cases
Using
try_map
it can be rewritten as follows:This removes the need to use 2 unwraps.
Solution sketch
https://github.com/rust-lang/rust/pull/118087/files
Design considerations
RefMut
in case of failure:filter_map
..map_err(|(_, e)| e)
to use?
Result::Err
.Resut:Err
in return value?Try
/Residual
?Alternatives
External implementation using
Option
andunwrap
External implementation using
unsafe
(Might be incorrect)
Links and related work
rust-lang/rust#118087
The text was updated successfully, but these errors were encountered: