Repository navigation
Closed
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.
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_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>.
Reacted by Dave Halter and Wojciech Kordalski
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.
Activity
Robbepop commentedon Apr 18, 2021
I am in need of this API for my library in order to be able to have an efficient implementation that avoids unsafe Rust code.
I wonder, why is the goal to have
try_mapinstead offilter_mapsince I can see a great value in having both APIs available.So if
filter_mapis already good to go we could stabilize that one and later addtry_mapas soon as all open questions for it are resolved.hniksic commentedon Apr 24, 2021
The name
filter_map()seems unusual for something that returns aResult- for example,Iterator::filter_map()returns an iterator with filtering built in. The equivalent method already exists in the Mutex implementations oftokioandparking_lot, under the nametry_map(). In both cases the closure it accepts returnsOption<&U>and not the more generalimpl Try. So maybe the nametry_mapshould be reconsidered in that light.lucassardois commentedon Jun 29, 2021
I'm also interested in the stabilization of this API for the same reason as @Robbepop. However, I also agree that
filter_map()should be namedtry_map()since it returns aResult. As an API user it makes more sense to me.tony612 commentedon Sep 13, 2021
I wonder if we can have a generic filter & map for any inner enum instead of just Option.
For example in this code
As the code show,
dataref is compared twice to get the result. Is there any way to make it simpler?m-ou-se commentedon Mar 16, 2022
@rfcbot fcp merge
rfcbot commentedon Mar 16, 2022
Team member @m-ou-se has proposed to merge this. The next step is review by the rest of the tagged team members:
No concerns currently listed.
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!
See this document for info about what commands tagged team members can give me.
rfcbot commentedon Mar 19, 2022
🔔 This is now entering its final comment period, as per the review above. 🔔
rfcbot commentedon Mar 29, 2022
The final comment period, with a disposition to merge, as per the review above, is now complete.
As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.
This will be merged soon.
Rollup merge of rust-lang#97308 - JohnTitor:stabilize-cell-filter-map…
Rollup merge of #97308 - JohnTitor:stabilize-cell-filter-map, r=Mark-…