-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Make FixedSizeArray an unsafe trait #28538
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
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @pcwalton (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. 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. |
Can you put This sounds good to me, but I don't think I can make the decision. |
@bluss I added it to the PR message, hopefully correctly... |
Hm, if you break it anyway maybe you can make |
@petrochenkov Seems reasonable except I'm not able to write this such that it comiles atm:
Results in a compile error citing E0207 -- that |
@alexcrichton I'd like us to do this |
@alevy |
Can you also expand the documentation to indicate why this trait is I'm a little wary to continue to tweak perma-unstable traits like this because they're permanently unstable and lots of tweaks mean that the abstraction is useful and should perhaps live externally on crates.io first before moving into the standard library. This trait was added some time ago and there's no current usage in-tree, so there's no real reason this can't be external. For now though an incremental improvement is fine. |
@alexcrichton I expanded the documentation. Happy to revise if needed, though. Thanks! |
[breaking-change] `FixedSizeArray` is meant to be implemented for arrays of fixed size only, but can be implemented for anything at the moment. Marking the trait unsafe would make it more reasonable to write unsafe code which operates on fixed size arrays of any size. For example, using `uninitialized` to create a fixed size array and immediately filling it with a fixed value is externally safe: ``` pub fn init_with_nones<T, A: FixedSizeArray<Option<T>>>() -> A { let mut res = unsafe { mem::uninitialized() }; for elm in res.as_mut_slice().iter_mut() { *elm = None; } res } ``` But the same code is not safe if `FixedSizeArray` is implemented for other types: ``` struct Foo { foo: usize } impl FixedSizeArray<Option<usize>> for Foo { fn as_slice(&self) -> &[usize] { &[] } fn as_mut_slice(&self) -> &mut [usize] { &mut [] } } ``` now `init_with_nones() : Foo` returns a `Foo` with an undefined value for the field `foo`.
[breaking-change]
FixedSizeArray
is meant to be implemented for arrays of fixed size only, but can be implemented for anything at the moment. Marking the trait unsafe would make it more reasonable to write unsafe code which operates on fixed size arrays of any size.For example, using
uninitialized
to create a fixed size array and immediately filling it with a fixed value is externally safe:But the same code is not safe if
FixedSizeArray
is implemented for other types:now
init_with_nones() : Foo
returns aFoo
with an undefined value for the fieldfoo
.