-
Notifications
You must be signed in to change notification settings - Fork 19
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: Provide TryFrom<&[T]>
implementation for &[[T; N]]
#201
Comments
TryFrom<&[T]>
implementation for &[[T; N]]
Not sure why this is more useful than the method that returns both. At least with |
It’s useful if length of the slice not being divisible by the length of a chunk is an error. If you want to go through all the available chunks regardless of remainder than indeed you wouldn’t use this. |
How do you propose that this impl handles the |
Ideally it wouldn’t compile (which also makes me realise that suggested solution doesn’t currently do that but that can be addressed with a simple compile-time assert). |
@mina86 We reviewed this in today's @rust-lang/libs-api meeting, and there were objections to adding a |
Also, separately, |
This is the objection I shared in the meeting: I don't think we should have (Try)From implementations that change the "shape" of a data structure like this. Such conversions imply that nested (If we accept this ACP, would we also have to add all kind of other (Try)From impls that do various forms of flattening and (re-)grouping?) Having a method for this, however, sounds good to me!
To clarify, this wasn't a full team decision, but additions like these require team consensus, which cannot be reached when there are objections from team members. Adding and stabilizing a method for this still requires a team decision (through FCP), but based on the temperature check in the meeting, it seems likely that'd be accepted. |
The meeting notes mention the possibility of (It's also nice in that it can talk about how And +1 to not being fond of this as a trait. I think under the https://doc.rust-lang.org/std/convert/trait.From.html#when-to-implement-from vernacular, it's not "value-preserving" because the "conceptual kind" of the answer is different. |
The It would make the fn pairs(slice: &[u8]) -> Option<&[[u8; 2]]> {
slice.as_chunks_exact()
} |
Proposal
Problem statement
Provide
TryFrom<&[T]>
implementation for&[[T; N]]
as a more convenient wrapper around[T]::as_chunks
.Motivation, use-cases
Unstable
[T]::as_chunks
method allows converting&[T]
into&[[T; N]]
. However, to handle slices whose length doesn’t divide byN
the method returns(chunks, remainder)
tuple. If one wants to assert an empty remainder they may need to resort to destructuring the tuple and introducingif
statement. Similarly, it doesn’t mesh nicely with method chaining. A chain of methods needs to be stopped to verify the second element of the returned tuple before proceeding.Introducing
TryFrom<&[T]>
implementation for&[[T; N]]
offers aResult
-based interface for chunking a slice.Result
integrates with many interfaces of the language which results in more concise code.Compare:
Solution sketches
Solution is implemented in rust-lang/rust#105316 and has the form:
The text was updated successfully, but these errors were encountered: