-
Notifications
You must be signed in to change notification settings - Fork 20
ACP: add slice::split_once #102
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
What about a more general form? pub fn array_split<const N: usize>(&'a self, pred: F) -> Option<[&[T]; N]>
where F: FnMut(&T) -> bool Could be used the same way by requesting an array of length 2, but would allow for usage in cases where one wants to split exactly twice, or exactly thrice, etc |
I wouldn't necessarily be opposed to that, but I still think
match s.splitn(delimier, 2).collect_array::<2>() {
Some([prefix, suffix]) => todo!(),
None => todo!(),
}
I think I agree that, even now that we have const generics, splitting into exactly two slices is often a special case, and a dedicated method for it is more clear. |
How does this differ from the things proposed in #69? |
This seems fine. Feel free to open a tracking issue and open a PR to rust-lang/rust to add it as an unstable feature. |
Proposal
Add
slice::split_once
andslice::rsplit_once
methods, analogous to the existingstr::split_once
andstr::rsplit_once
methods.Problem statement
When doing ad-hoc parsing of a format that isn't guaranteed to be valid unicode, it's often useful to split byte slices on the first occurrence a specific delimiter. There isn't currently an API that expresses this directly for byte slices, although there is for strings.
Motivation, use-cases
There are some examples in aprs-parser-rs crate, which is being refactored from treating its input data as strings to using byte-slices. APRS packets consist of a header and a body, separated by a
b':'
byte. This is currently being parsed like this:Solution sketches
There are currently two options to do this in stable rust.
Using
position
to find the delimiter's index, then splitting it on that index and explicitly rejecting the first byte of the second slice (which contains the delimiter):Using
splitn
:These options are okay, but not great. They're both relatively verbose and don't express the actual intention very directly. They also have the issue that mistakes aren't necessarily going to show up in the type system.
With strings, there is currently a
split_once
method, that handles this exact use case:A similar method could be added for slices:
Along with an
rsplit_once
equivalent. I also think it might make sense to addsplit_once_mut
andrsplit_once_mut
, however those don't currently exist forstr
.Links and related work
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.
The text was updated successfully, but these errors were encountered: