-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement FixedSizeArray
for all fixed size arrays
#28088
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
Conversation
r? @pcwalton (rust_highfive has picked a reviewer for you, use r? to override) |
Code from this reddit post by @eddyb. |
This is quite amazing. Ideally the element type should be an associated type, but I know the impl isn't allowed that way. |
impl<T, A: Unsize<[T]>> FixedSizeArray<T> for A { | ||
#[inline] | ||
fn as_slice(&self) -> &[T] { | ||
unsafe { &*(self as *const A as *const [T]) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be just self as &[T]
or in fact just self
, and let coercion do the rest?
(It'd be great to have a more descriptive commit message or maybe some comments: I can definitely see people looking at this and wondering what's going on in future.) |
How well does it work with coherence? |
@eddyb No, it unfortunately doesn't work with coherence at all. |
@tbu- I tried making it smarter at some point: it should be possible to teach coherence that |
Using this for other blanket impls would make me a little uneasy, but this is an unstable trait anyway so I think this is fine. Could you squash the commits together and expand the first commit message like @huonw mentioned? Otherwise looks good to me! |
Do so by using the fact that fixed size arrays (like `[u8; 8]` can be coerced to slices `&[u8]`, this is expressed through the trait `Unsize<[T]>` that all fixed size arrays implement.
5174841
to
4d2709d
Compare
@alexcrichton Squashed and expanded the first commit message. |
Yeah I don't believe this is what we'd want in the future (it's not how trait support for fixed size arrays would look if we had designed for it). |
Does anybody actually use
like it was initially planned due to coherence restrictions, and without it it's almost useless, probably I should have killed it in the same PR that introduced it. |
@petrochenkov The trait is useful for abstractions over arrays, such as stack-allocated vectors, hybrid vectors, etc. |
⌛ Testing commit 4d2709d with merge d9a55d0... |
💔 Test failed - auto-linux-64-nopt-t |
@bors:retry On Mon, Aug 31, 2015 at 2:34 PM, bors [email protected] wrote:
|
@alexcrichton I believe there's a space missing after the colon in |
@bors retry |
⌛ Testing commit 4d2709d with merge 8f8b129... |
💔 Test failed - auto-win-gnu-64-nopt-t |
@bors: retry On Tue, Sep 1, 2015 at 4:58 AM, bors [email protected] wrote:
|
No description provided.