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

Introduce <&[_]>::split_off and <&str>::split_off #49173

Closed
wants to merge 1 commit into from

Conversation

nox
Copy link
Contributor

@nox nox commented Mar 19, 2018

No description provided.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @withoutboats (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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 19, 2018
@kennytm kennytm added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Mar 19, 2018
@Centril
Copy link
Contributor

Centril commented Mar 19, 2018

This seems nice to have 👍
I'm kinda saddened that libcore can't use these (and existing methods in the same impl..) -- hopefully we can fix that someday.

@cramertj
Copy link
Member

cramertj commented Mar 19, 2018

+1-- I've written these a handful of times.

I'd also like split_off_front and split_off_front_mut.

@nox
Copy link
Contributor Author

nox commented Mar 19, 2018

@cramertj I plan to make a separate PR for a more general "cutting" concept, with "anchored ranges". I'll probably do that at the end of the week.

https://play.rust-lang.org/?gist=54e0077190f33596d9f8fa705bed0a66&version=nightly


#[inline]
fn split_off_mut<'a>(self: &mut &'a mut Self, at: usize) -> &'a mut str {
let (rest, split) = mem::replace(self, unsafe { from_utf8_unchecked_mut(&mut []) })
Copy link
Contributor

Choose a reason for hiding this comment

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

This is trivially safe since an empty string slice is UTF-8 by definition, but a comment to that effect would be nice here ;)
(Generally I think all uses of unsafe elimination blocks should come with a comment explaining the reason why it is safe.)

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 finally added the damn comment. :3

@shepmaster shepmaster added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 23, 2018
@cramertj
Copy link
Member

ping @withoutboats

@nox
Copy link
Contributor Author

nox commented Apr 5, 2018

@withoutboats Ping.

@SimonSapin SimonSapin added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 6, 2018
@SimonSapin
Copy link
Contributor

The implementation looks fine to me but the self: &mut &'a Self parameter is unusual. @rust-lang/libs what do you think?

@alexcrichton
Copy link
Member

The method name here could be shadowed by Vec::split_off so we may want to rename?

We've also got methods like <[_]>::split_{first,last}, so I wonder if we'd want to provide similar treatment to those methods as well? (aka have all the split_* methods with a &mut &mut [T] version)

@bors
Copy link
Contributor

bors commented Apr 12, 2018

☔ The latest upstream changes (presumably #49698) made this pull request unmergeable. Please resolve the merge conflicts.

@pietroalbini
Copy link
Member

Ping from triage! Can @withoutboats (or someone else from @rust-lang/libs) review this?

@withoutboats
Copy link
Contributor

Code looks good, I agree with Alex that the potential shadowing with the Vec API seems bad and it would be good to rename.

In regard to @SimonSapin's point about the return type, I think this is just an example of the kinds of new APIs arbitrary_self_types enables.

@nox
Copy link
Contributor Author

nox commented Apr 20, 2018

What should I rename them to?

@Centril
Copy link
Contributor

Centril commented Apr 20, 2018

@nox How about: steal_from or take_from? This hints at the fact that the return value "steals" or takes something from the receiver starting from at and leaves the rest in place.

@nox
Copy link
Contributor Author

nox commented Apr 20, 2018

I would expect such a method to set self to the rest of the slice, not the beginning.

@TimNN
Copy link
Contributor

TimNN commented Apr 24, 2018

@nox / @alexcrichton: It looks like the main blocker here is deciding on names for the new methods. How do you want to move forward with this?


If this does essentially the same as existing split_off methods, I think it should be named similarly. If shadowing with Vec is a concern, maybe split_off_slice? Or keep split_off for now and discuss again prior to stabilization?

@alexcrichton
Copy link
Member

I don't have a particular personal preference on name, but I do think it just needs to be different

@pietroalbini pietroalbini added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 30, 2018
@pietroalbini
Copy link
Member

Ping from triage! What's the status of this? S-waiting-on-bikeshed?

@alexcrichton
Copy link
Member

Indeed!

@pietroalbini
Copy link
Member

Sure.

@pietroalbini pietroalbini added the S-waiting-on-bikeshed Status: Awaiting a decision on trivial things. label Apr 30, 2018
@cramertj
Copy link
Member

cramertj commented Apr 30, 2018

How about split_tail or take_tail?

@shepmaster shepmaster removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 6, 2018
@pietroalbini
Copy link
Member

Ping from triage @nox @alexcrichton! What we should do with this PR?

@Centril
Copy link
Contributor

Centril commented Jun 4, 2018

I propose that we just pick one of the suboptimal names proposed for now and optimize it during stabilization; maybe we'll have better ideas by then?

@Centril
Copy link
Contributor

Centril commented Jun 4, 2018

Also; I noticed that impl Default for &'_ mut str so the unsafe block can/should be replaced with a call to default.

@shepmaster
Copy link
Member

pick one of the suboptimal names proposed for now and optimize it during stabilization; maybe we'll have better ideas by then

Can we say that explicitly in the docs somehow? "If you don't like this name, pop on over to #XXXXX and paint the bikeshed"

@Centril
Copy link
Contributor

Centril commented Jun 4, 2018

@shepmaster SGTM 👍

@alexcrichton
Copy link
Member

Looks like this hasn't been updated in awhile so I'm going to close, but feel free to resubmit of course! I think @Centril's idea above makes sense to me too!

@nox
Copy link
Contributor Author

nox commented Jul 26, 2019

I completely forgot this PR. I'm currently doing a crate for slices represented as a pair of start and end pointers (like slice::Iter) and I'm thinking that split_off should rather live on slice::Iter, given its representation fits the use case better.

@cramertj
Copy link
Member

@nox I started on a revival of it in #62282

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bikeshed Status: Awaiting a decision on trivial things. 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.