Add `try_map` functions to `Ref` and `RefMut`

It would be nice to bubble up errors in the Ref::map and RefMut::map functions. I think this is a pretty non-controversial addition:

impl<'b, T> Ref<'b, T>
where 
    T: ?Sized,
{
    pub fn try_map<U, E, F>(orig: Ref<'b, T>, f: F) -> Result<Ref<'b, U>, E>
    where
         F: FnOnce(&T) -> Result<&U, E>
         U: ?Sized,
    { ... }
}

Does anyone see any issues with this? Like I said, it seems pretty non-controversial.

I suggest making it generic over Try, like how Iterator::try_find is. See Make `array::{try_from_fn, try_map}` and `Iterator::try_find` generic over `Try` by scottmcm · Pull Request #91286 · rust-lang/rust · GitHub for how to do that.

But yes, this seems plausible. Since it's just a method you can send a PR for it, if you'd like.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.