Open GrigorenkoPV opened 7 months ago
I also have a need of this feature which would greatly help with easier error handling when dealing with Ref/RefMut.
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
https://github.com/rust-lang/rust/pull/118087