Skip to content

Tracking Issue for cell_filter_map #81061

Closed
@KodrAus

Description

@KodrAus
Contributor

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

added
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
on Jan 16, 2021
Robbepop

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.

hniksic

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.

lucassardois

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.

tony612

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?

m-ou-se

m-ou-se commented on Mar 16, 2022

@m-ou-se
Member

@rfcbot fcp merge

rfcbot

rfcbot commented on Mar 16, 2022

@rfcbot
Collaborator

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.

added
proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.
final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
and removed
proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
on Mar 16, 2022

11 remaining items

Loading
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-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @m-ou-se@hniksic@tony612@apiraino@KodrAus

      Issue actions

        Tracking Issue for cell_filter_map · Issue #81061 · rust-lang/rust