Skip to content

Add option::get_mut_ref() and friends. #7394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bblum opened this issue Jun 26, 2013 · 6 comments
Closed

Add option::get_mut_ref() and friends. #7394

bblum opened this issue Jun 26, 2013 · 6 comments
Labels
A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@bblum
Copy link
Contributor

bblum commented Jun 26, 2013

With the new borrow-checker, &mut T is noncopyable, so the following functions are now safe:

fn get_mut_ref<'a,T>(x: &'a mut Option<T>) -> &'a mut T { 
    match x { 
        &Some(ref mut inner) => inner,
        &None => fail!(),
    }   
}
fn map_mut<T,U>(x: &mut Option<T>, blk: &fn(&mut T) -> U) -> Option<U> {
    match x { 
        &Some(ref mut inner) => Some(blk(inner)),
        &None => None
    }   
}

@bblum bblum mentioned this issue Jun 26, 2013
@alexcrichton
Copy link
Member

I know that get_mut_ref already exists at least, and it seems like map_mut is more powerfully enabled by map_consume?

@thestinger
Copy link
Contributor

It seems we actually already have get_mut_ref. The map_mut method might not be needed since we have mutate and mutate_default for in-place mutation.

@bblum
Copy link
Contributor Author

bblum commented Jun 26, 2013

Oh cool, I didn't know get_mut_ref existed. map_mut would be needed for times when you don't have ownership of the data, only a mutable reference.

@alexcrichton
Copy link
Member

@bblum, but in map_mut you're replacing the data immediately, so why not move out into the blk parameter (which is what map_consume does)

@bblum
Copy link
Contributor Author

bblum commented Jun 27, 2013

@alexcrichton My thinking is that sometimes you can't use map_consume because all you have to start is an &mut Option<T>.

@alexcrichton
Copy link
Member

Oh my I never saw that, disregard me...

flip1995 pushed a commit to flip1995/rust that referenced this issue Jul 1, 2021
Update opener.

This updates the opener dependency, to try to reduce the duplicate dependencies in the rust-lang/rust repo.  The changelog is [here](https://github.com/Seeker14491/opener/blob/master/CHANGELOG.md#050---2021-06-11), and I don't expect it to have much of a change to anyone.

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

3 participants