-
Notifications
You must be signed in to change notification settings - Fork 20
ACP: Slice to array version of split_off
#543
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
Comments
Aren't these the same as the |
These are similar, but my proposed methods modify |
I don't think it makes sense to provide methods that require let mut slice = [0, 1, 2, 3].as_slice();
// reassign `slice`
let val;
(val, slice) = slice.split_first_chunk::<2>().unwrap();
// or just shadow
let (val, slice) = slice.split_first_chunk::<2>().unwrap(); |
They don't require |
So these methods would be to the existing |
Proposal
Problem statement
Often, I have a slice containing some amount of elements, and I'd like the split off the first
N
elements as an array and leave behind the remainder.Motivating examples or use cases
This gets a little easier with
slice::split_off
, which is FCP'ed for stabilization, but still nothing there lets you avoid the.try_into().unwrap()
conversion from a slice to an array.Solution sketch
Each function removes
N
elements from the slice, either in front or in back, returning the removed elements as an array (or returningNone
and leavingself
unchanged ifself.len() < N
).My motivating example snippet would become:
Edge cases
If
N == 0
, then it'll always returnSome
, with the array being zero-length and having its address at the start or end of the slice (i.e.slice: &[u8]
andslice.split_off_chunk_first<0>()
have the same address).If
N == self.len()
, then the returned array will be the full original contents of the slice, and the slice will now be empty, and will point to the end/beginning (same address as if the opposite method were called withN == 0
).Alternatives
I could write my own implementations of these functions as wrappers around
slice::split_off
, but this feels to me like something that belongs in the standard library.I've only ever written code that would use the shared reference versions, not the mutable reference versions, so we could skip those two, but I feel like it'd be better to keep the shared/mutable symmetry a lot of slice methods already have.
These methods were originally suggested as
take_chunk
instead ofsplit_off_chunk
, to fit with what was then the name for the methods on this feature. There are other names that could be considered, but I think matching the name of those methods makes sense. We could bikeshed whether chunk comes before, after, or between first/last and mut in the name (I weakly prefer the order I wrote).Links and related work
These methods have been mentioned on rust-lang/rust#62280, which is FCP'ed to stabilize similar methods but with a gap that I think this fulfills. I wanted to add these methods there (and I wasn't the only one to mention them), but it has been FCP'ed without response from anyone who seems official, so I'm writing them in a new ACP. I'm assuming the lack of comment meant "we're not adding these methods here" and not "we're not adding these methods".
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: