-
Notifications
You must be signed in to change notification settings - Fork 20
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
ACP: Introduce checked_split_at{,_mut} methods #308
Comments
Why |
I followed naming of integer methods, e.g. |
Well there's already |
+1 for Or |
Arithmetic types have |
We briefly discussed this in last week's libs-API meeting. We're happy to see this as an unstable feature. The exact name can be figured out as an open question on the tracking issue before stabilization. Feel free to open a tracking issue and add it to your PR. Thanks! |
@m-ou-se, great news, thanks! Quick question though: rust-lang/rust#118578 adds the methods to Edit: Went with a). |
core: introduce split_at{,_mut}_checked Introduce split_at_checked and split_at_mut_checked methods to slices types (including str) which are non-panicking versions of split_at and split_at_mut respectively. This is analogous to get method being non-panicking version of indexing. - rust-lang/libs-team#308 - rust-lang#119128
core: introduce split_at{,_mut}_checked Introduce split_at_checked and split_at_mut_checked methods to slices types (including str) which are non-panicking versions of split_at and split_at_mut respectively. This is analogous to get method being non-panicking version of indexing. - rust-lang/libs-team#308 - rust-lang#119128
core: introduce split_at{,_mut}_checked Introduce split_at_checked and split_at_mut_checked methods to slices types (including str) which are non-panicking versions of split_at and split_at_mut respectively. This is analogous to get method being non-panicking version of indexing. - rust-lang/libs-team#308 - rust-lang#119128
core: introduce split_at{,_mut}_checked Introduce split_at_checked and split_at_mut_checked methods to slices types (including str) which are non-panicking versions of split_at and split_at_mut respectively. This is analogous to get method being non-panicking version of indexing. - rust-lang/libs-team#308 - rust-lang#119128
Rollup merge of rust-lang#118578 - mina86:c, r=dtolnay core: introduce split_at{,_mut}_checked Introduce split_at_checked and split_at_mut_checked methods to slices types (including str) which are non-panicking versions of split_at and split_at_mut respectively. This is analogous to get method being non-panicking version of indexing. - rust-lang/libs-team#308 - rust-lang#119128
Proposal
Problem statement
While indexing of a slice has a non-panicking counterpart in the form of get method, no such counterpart exists for split_at method. This forces users (who wish not to panic) to explicitly check for length which is something that the split_at function is already doing. This makes caller code more verbose, marginally more error-prone as the mid point is passed twice and potentially harder to reason about panicking behaviours.
Motivating examples or use cases
Proposed methods don’t offer any kind of ground breaking feature and the same effect can be achieved by just a couple additional statements so the motivating examples are by no means impressive. Nonetheless, they represent that occasionally codebases need to check the length.
From nearcore:
Could be rewritten as:
From Solana:
Could be rewritten as:
From CosmWasm:
Could be rewritten as:
I’m currently working on code which looks as follows:
There,
bytes
argument must by at least lengthbytes_len
and tail must be all zeros.Solution sketch
Addition of checked_split_at{,_mut} methods to slice and str types. See rust-lang/rust#118578.
Alternatives
(mid <= slice.len()).then(|| slice.split_at(mid)
however priori art for convenience non-panicking methods exists (namelyget
method) and, at least to me, it feels like split_at falls within same category.Links and related work
This was mentioned in an old thread though the discussion fizzled out without any conclusion.
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: