Skip to content

Improve Option::cloned method to allow mutable references #36137

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
wants to merge 1 commit into from

Conversation

therustmonk
Copy link
Contributor

@therustmonk therustmonk commented Aug 30, 2016

Old behavior:

--> main.rs:7:17
  |
7 |     Option::cloned(u.as_mut());
  |                    ^^^^^^^^^^ values differ in mutability
  |
  = note: expected type `std::option::Option<&_>`
  = note:    found type `std::option::Option<&mut U>`

--> main.rs:8:17
  |
8 |     Option::cloned(None::<Box<U>>);
  |                    ^^^^^^^^^^^^^^ expected reference, found box
  |
  = note: expected type `std::option::Option<&_>`
  = note:    found type `std::option::Option<Box<U>>`

This PR makes possible to use options with mutable references or boxes:

fn main() {
    // Works
    let val = 1;
    let val_ref = &val;
    let opt_none: Option<&'static u32> = None;
    let opt_ref = Some(&val);
    let opt_ref_ref = Some(&val_ref);
    assert_eq!(opt_none.clone(), None);
    assert_eq!(opt_none.cloned(), None);
    assert_eq!(opt_ref.clone(), Some(&val));
    assert_eq!(opt_ref.cloned(), Some(1));
    assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
    assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
    assert_eq!(opt_ref_ref.cloned().cloned(), Some(1));

    // Didn't work before
    let mut opt_mut = Some(val);
    let opt_box: Option<Box<u32>> = Some(Box::new(val));
    assert_eq!(opt_mut.as_mut().cloned(), Some(1));
    assert_eq!(opt_box.cloned(), Some(1));
}

At playground: https://play.rust-lang.org/?gist=06be36d6106550ce782b6841e902a732&version=nightly&backtrace=0
Reason: https://users.rust-lang.org/t/option-cloned-disowns-mut/7080

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @aturon (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@jonas-schievink
Copy link
Contributor

I'm not sure if Deref is the right trait to use here

assert_eq!(opt_mut.as_mut().cloned(), Some(1));

// Deep Deref works
assert_eq!(opt_box.cloned(), None);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand this correctly and the result of opt_box.cloned() is an Option<u32>?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I'll make it explicit.

@Eh2406
Copy link
Contributor

Eh2406 commented Aug 30, 2016

Is changing the associated types, and removing a lifetime a breaking change?

@therustmonk
Copy link
Contributor Author

Local tests passed sucessfully. I don't think it's a breaking change, because lifetime becomes implicit and associated types are compatible.

@alexcrichton
Copy link
Member

Originally added in #19060 but must have later been removed (link to removal is stale). Also this has been previously proposed but closed.

The libs team has become quite conservative over time about using Deref for this kind of generic programming as it it's always what you want.

@therustmonk
Copy link
Contributor Author

Oh, that's old story. I should close it, because now I see that's wrong to adapt current (good readable) API.

A lot of generics make tools more powerful (maybe), but less ergonomic. That's why Rust is more useful to me than congeners.

Actually, that's a good strategy to keep API explicit as you do. I think we all were confused because there isn't cloned_mut companion, as we see with as_ref and as_mut.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants