Uh oh!
There was an error while loading. Please reload this page.
Implement TryFrom<&[T]> for &[T; N] - #44764
Conversation
There are many cases where a buffer with a static compile-time size is preferred over a slice with a dynamic size. This allows for performing a checked conversion from &[T] to &[T; N]. This may also lead to compile-time optimizations involving [T; N] such as loop unrolling.
rust-highfive
commented
Sep 21, 2017
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @BurntSushi (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. |
shepmaster
commented
Sep 22, 2017
Thanks for the awesome PR, @nvzqz! We'll wrangle up a top-notch reviewer for you in a short amount of time! |
jethrogb
commented
Sep 24, 2017
I'd love to make arrays better, but shouldn't we wait for const generics? |
shepmaster
commented
Sep 24, 2017
Is there a reason this would conflict or not be implementable with const generics? Otherwise it seems to leave us in no worse shape than we are now. |
jethrogb
commented
Sep 24, 2017
Not that I'm aware of. People have previously been cautious about metadata bloat though. |
nvzqz
commented
Sep 24, 2017
This change adds 66 new implementations of |
BurntSushi
commented
Sep 25, 2017
I haven't been following |
alexcrichton
commented
Sep 28, 2017
ping @sfackler, mind taking a look at this? |
| /// The error type returned when a conversion from a slice to an array fails. | ||
| #[unstable(feature = "try_from", issue = "33417")] | ||
| #[derive(Debug, Copy, Clone)] | ||
| pub struct TryFromSliceError(()); |
There was a problem hiding this comment.
This should probably implement Error in libstd.
There was a problem hiding this comment.
Should the Display implementation also be in libstd or libcore?
sfackler
commented
Sep 28, 2017
I'm on board with these existing, but let's see if everyone is okay with more ad-hoc array stuff. @rfcbot fcp merge |
sfackler
commented
Sep 28, 2017
Oops, forgot to libs tag it @rfcbot fcp merge |
Team member @sfackler has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
bluss
commented
Sep 29, 2017
|
I believe that the let values = [1,2,3,4];let three:&[i32;3] = values[..3].try_into().unwrap();// Or even:let last:&[i32;2] = values[2..4].try_into().unwrap(); |
sfackler
commented
Sep 29, 2017
In other similar situations like copy_from_slice and clone_from_slice, we require that the lengths match exactly. |
bors
commented
Sep 30, 2017
☔ The latest upstream changes (presumably #44174) made this pull request unmergeable. Please resolve the merge conflicts. |
carols10cents
commented
Oct 9, 2017
ping @BurntSushi and @aturon for your ticky boxes here! |
scottmcm
commented
Oct 9, 2017
If |
SimonSapin
commented
Oct 9, 2017
|
shepmaster
commented
Oct 15, 2017
nvzqz
commented
Oct 22, 2017
Is this just waiting on @aturon? It's been 3 weeks now 😕 |
carols10cents
commented
Oct 23, 2017
@aturon was out for two weeks, but hopefully should be back now, but he is traveling a bit again this week :) i'll ping him on IRC. |
| impl fmt::Display for TryFromSliceError { | ||
| #[inline] | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| fmt::Display::fmt(self.__description(), f) |
There was a problem hiding this comment.
Why not use the description method from the Error trait?
There was a problem hiding this comment.
Error is declined in libstd, not libcore
rfcbot
commented
Oct 24, 2017
🔔 This is now entering its final comment period, as per the review above. 🔔 |
alexcrichton
commented
Oct 31, 2017
Egad sorry for the delay! This can now be approved after we've all signed off -- @bors: r+ |
bors
commented
Oct 31, 2017
📌 Commit 4c853ad has been approved by |
bors
commented
Oct 31, 2017
Implement TryFrom<&[T]> for &[T; N] There are many cases where a buffer with a static compile-time size is preferred over a slice with a dynamic size. This allows for performing a checked conversion from `&[T]` to `&[T; N]`. This may also lead to compile-time optimizations involving `[T; N]` such as loop unrolling. This is my first PR to Rust, so I'm not sure if discussion of this change should happen here or does it need its own RFC? I figured these changes would be a subset of #33417.
bors
commented
Nov 1, 2017
☀️ Test successful - status-appveyor, status-travis |
This commit serves to provide the same benefits as PR rust-lang#44764 except for boxes that refer to owned arrays allocated in the heap.
jethrogb
commented
Apr 26, 2019
We're missing |
There are many cases where a buffer with a static compile-time size is preferred over a slice with a dynamic size. This allows for performing a checked conversion from
&[T]to&[T; N]. This may also lead to compile-time optimizations involving[T; N]such as loop unrolling.This is my first PR to Rust, so I'm not sure if discussion of this change should happen here or does it need its own RFC? I figured these changes would be a subset of #33417.