Closed
Description
Feature gate: #![feature(cell_filter_map)]
This is a tracking issue for Ref::filter_map
and RefMut::filter_map
, which lets you optionally project data inside a Ref
or RefMut
, wrapped in a Ref
or RefMut
. If the projection returns None
then these methods return a Err(Self)
so that you can still get the original Ref
or RefMut
back out.
Public API
impl<'b, T: ?Sized> Ref<'b, T> {
pub fn filter_map<U: ?Sized, F>(orig: Ref<'b, T>, f: F) -> Result<Ref<'b, U>, Self>
where
F: FnOnce(&T) -> Option<&U> {}
}
impl<'b, T: ?Sized> RefMut<'b, T> {
pub fn filter_map<U: ?Sized, F>(orig: RefMut<'b, T>, f: F) -> Result<RefMut<'b, U>, Self>
where
F: FnOnce(&mut T) -> Option<&mut U> {}
}
Steps / History
- Implementation: Introduce {Ref, RefMut}::try_map for optional projections in RefCell #78455
- Final commenting period (FCP)
- Stabilization PR
Unresolved Questions
- Naming and signature: We wanted to call this method
try_map
and accept a closure likeFnOnce(&T) -> R where R: Try<Ok = &U>
, but that's not sound with bound lifetimes. This method is only ok when the mapping closure uses HRTB. So we opted to call itfilter_map
instead, with the closure returning aOption<&U>
.