Skip to content

Tracking Issue for cell_filter_map #81061

Closed

Description

@KodrAus

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>.

Activity

  1. added
    T-libs-api
    C-tracking-issue
    on Jan 16, 2021
  2. Robbepop commented on Apr 18, 2021

    @Robbepop
    Contributor

    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_map instead of filter_map since I can see a great value in having both APIs available.
    So if filter_map is already good to go we could stabilize that one and later add try_map as soon as all open questions for it are resolved.

  3. hniksic commented on Apr 24, 2021

    @hniksic
    Contributor

    The name filter_map() seems unusual for something that returns a Result - for example, Iterator::filter_map() returns an iterator with filtering built in. The equivalent method already exists in the Mutex implementations of tokio and parking_lot, under the name try_map(). In both cases the closure it accepts returns Option<&U> and not the more general impl Try. So maybe the name try_map should be reconsidered in that light.

  4. lucassardois commented on Jun 29, 2021

    @lucassardois

    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 named try_map() since it returns a Result. As an API user it makes more sense to me.

  5. tony612 commented on Sep 13, 2021

    @tony612

    I wonder if we can have a generic filter & map for any inner enum instead of just Option.

    For example in this code

    pub enum Foo<T> {
        A(),
        B(T),
    }
    
    pub struct Bar {}
    
    pub struct User {
        pub data: RefCell<Foo<Bar>>,
    }
    
    impl User {
        pub fn get_data(&self) -> std::result::Result<Ref<'_, Bar>, String> {
            let v = self.data.borrow();
            if let Foo::B(_) = *v {
                Ok(Ref::map(v, |x| match x {
                    Foo::B(b) => b,
                    _ => unreachable!(),
                }))
            } else {
                Err(String::from("error"))
            }
        }
    }
    

    As the code show, data ref is compared twice to get the result. Is there any way to make it simpler?

  6. added
    I-libs-api-nominated
    on Mar 9, 2022
  7. m-ou-se commented on Mar 16, 2022

    @m-ou-se
    Member

    @rfcbot fcp merge

  8. rfcbot commented on Mar 16, 2022

    @rfcbot

    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.

  9. added
    proposed-final-comment-period
    disposition-merge
    final-comment-period
    and removed
    proposed-final-comment-period
    on Mar 16, 2022
  10. rfcbot commented on Mar 19, 2022

    @rfcbot

    🔔 This is now entering its final comment period, as per the review above. 🔔

  11. added
    finished-final-comment-period
    and removed
    final-comment-period
    on Mar 29, 2022
  12. rfcbot commented on Mar 29, 2022

    @rfcbot

    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.

  13. added
    to-announce
    on Mar 29, 2022
  14. removed
    to-announce
    on Mar 31, 2022
  15. removed
    I-libs-api-nominated
    on Apr 6, 2022
  16. added a commit that references this issue on May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueT-libs-apidisposition-mergefinished-final-comment-period

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions