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
Unresolved Questions
- Naming and signature: We wanted to call this method
try_map and accept a closure like FnOnce(&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 it filter_map instead, with the closure returning a Option<&U>.
Feature gate:
#![feature(cell_filter_map)]This is a tracking issue for
Ref::filter_mapandRefMut::filter_map, which lets you optionally project data inside aReforRefMut, wrapped in aReforRefMut. If the projection returnsNonethen these methods return aErr(Self)so that you can still get the originalReforRefMutback out.Public API
Steps / History
Unresolved Questions
try_mapand 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_mapinstead, with the closure returning aOption<&U>.