Skip to content
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

Provide Entry-like API for Option #39289

Merged
merged 1 commit into from
Feb 5, 2017
Merged

Conversation

shahn
Copy link
Contributor

@shahn shahn commented Jan 25, 2017

This implements #39288.

I am wondering whether to use std::intrinsics::unreachable!() here. Both seems fine to me (the second match optimizes away in release mode).

@rust-highfive
Copy link
Collaborator

r? @brson

(rust_highfive has picked a reviewer for you, use r? to override)

Copy link
Member

@steveklabnik steveklabnik left a comment

Choose a reason for hiding this comment

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

mostly formatting nits

/////////////////////////////////////////////////////////////////////////

/// Inserts `v` into the option if it is None, then returns a reference to
/// the contained value
Copy link
Member

Choose a reason for hiding this comment

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

needs a period at the end

// Entry-like operations to insert if None and return a reference
/////////////////////////////////////////////////////////////////////////

/// Inserts `v` into the option if it is None, then returns a reference to
Copy link
Member

Choose a reason for hiding this comment

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

None should also be in backticks



/// Inserts a value computed from `f` into the option if it is None, then
/// returns a reference to the contained value
Copy link
Member

Choose a reason for hiding this comment

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

same two issues as above

}

/// Inserts `v` into the option if it is None, then
/// returns a mutable reference to the contained value
Copy link
Member

Choose a reason for hiding this comment

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

same as above

}

/// Inserts a value computed from `f` into the option if it is None, then
/// returns a mutable reference to the contained value
Copy link
Member

Choose a reason for hiding this comment

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

same as above

///
/// let mut x = None;
/// let y: &mut u32 = x.get_mut_or_insert(5);
/// assert_eq!(y, &5);
Copy link
Member

Choose a reason for hiding this comment

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

could you put new lines before and after this, give it a little space?

/// let mut x = None;
/// let y: &mut u32 = x.get_mut_or_insert(5);
/// assert_eq!(y, &5);
/// *y = 7;
Copy link
Member

Choose a reason for hiding this comment

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

should there be another assert_eq after this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't do it on purpose, because the only thing that was interesting here was the mutability of the returned value and I wanted the example shorter. But I am happy to add it if you think that's better?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, this gave me an idea for something hopefully even better. Running tests locally before pushing new version :)

/// let mut x = None;
/// let y: &mut u32 = x.get_mut_or_insert_with(|| 5);
/// assert_eq!(y, &5);
/// *y = 7;
Copy link
Member

Choose a reason for hiding this comment

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

same issue as above

/// ```
/// #![feature(option_entry)]
///
/// let mut x = None;
Copy link
Member

Choose a reason for hiding this comment

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

could you put a newline after this please?

/// ```
/// #![feature(option_entry)]
///
/// let mut x = None;
Copy link
Member

Choose a reason for hiding this comment

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

could you put a newline after this please?

@shahn
Copy link
Contributor Author

shahn commented Jan 25, 2017

I think I addressed all your review comments, let me know if you like it. I can also squash the changes if that's preferable.

Copy link
Member

@steveklabnik steveklabnik left a comment

Choose a reason for hiding this comment

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

two tiny more. this is great 👍 squash is probably good, but feel free to wait until someone signs off on the implementations too

///
/// *y = 7;
/// }
/// assert_eq!(x, Some(7));
Copy link
Member

Choose a reason for hiding this comment

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

one more newline above this?

this example is much better 👍

///
/// *y = 7;
/// }
/// assert_eq!(x, Some(7));
Copy link
Member

Choose a reason for hiding this comment

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

same here

/// *y = 7;
///
/// {
/// let y: &mut u32 = x.get_mut_or_insert_with(|| 5);
Copy link
Member

Choose a reason for hiding this comment

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

same here

/// *y = 7;
///
/// {
/// let y: &mut u32 = x.get_mut_or_insert(5);
Copy link
Member

Choose a reason for hiding this comment

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

all this code should be indented another two spaces; four is the standard style.

@shahn
Copy link
Contributor Author

shahn commented Jan 25, 2017

changes made, thanks :)

@JordiPolo
Copy link
Contributor

I'm just learning rust but isn't this a huge api (4 new functions) for what seems like a trivial problem? Option does not have so many functions and I can guess most of the people just learn them all. This is just more to learn.

@shahn
Copy link
Contributor Author

shahn commented Jan 25, 2017

To me, this should be a part of the stdlib because for the user it's very hard to reason about the unreachable path and whether that'll be optimized out or not. Entry-style APIs should be ubiquitous, imho.

Edit: And with that comment, I think if this is deemed acceptable it should use the intrinsics unreachable for the implementation.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 25, 2017

Why are the immutable versions required at all? You can always use the mutable reference like an immutable one.

@shahn
Copy link
Contributor Author

shahn commented Jan 25, 2017

Oh indeed.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 25, 2017

And now you can remove the _mut, since there's no immutable alternative ^^

@frewsxcv frewsxcv added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Jan 25, 2017
@shahn
Copy link
Contributor Author

shahn commented Feb 1, 2017

Ping, should I be doing anything here? Like providing a rebase or something? Hope I'm not accidentally blocking this by messing up a required step.

@alexcrichton
Copy link
Member

@bors: r+

Looks good to me, thanks @shahn!

@bors
Copy link
Contributor

bors commented Feb 4, 2017

📌 Commit 25c94c3 has been approved by alexcrichton

@shahn
Copy link
Contributor Author

shahn commented Feb 4, 2017

Sure I shouldn't rebase it? But yay anyway :)

@alexcrichton
Copy link
Member

Nah this is still mergeable (no need for a rebase). If you want to squash down the commits though feel free and I'll re-r+

This implements rust-lang#39288.

Thanks to @steveklabnik and @oli-obk for their review comments :)
@shahn
Copy link
Contributor Author

shahn commented Feb 4, 2017

I squashed it up, now less messy. Thanks

@sfackler
Copy link
Member

sfackler commented Feb 5, 2017

@bors r=alexcrichton

@bors
Copy link
Contributor

bors commented Feb 5, 2017

📌 Commit 8e02ad0 has been approved by alexcrichton

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Feb 5, 2017
Provide Entry-like API for Option

This implements rust-lang#39288.

I am wondering whether to use std::intrinsics::unreachable!() here. Both seems fine to me (the second match optimizes away in release mode).
bors added a commit that referenced this pull request Feb 5, 2017
@bors bors merged commit 8e02ad0 into rust-lang:master Feb 5, 2017
@bors
Copy link
Contributor

bors commented Feb 5, 2017

⌛ Testing commit 8e02ad0 with merge 9c8cdb2...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants