-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Implement Random for array
#136732
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
Implement Random for array
#136732
Changes from 1 commit
f1580f3
80d776e
5435be8
b7d7fc6
b33af51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,7 @@ use crate::ops::{ | |||||||||||||||||||||||||||
| ChangeOutputType, ControlFlow, FromResidual, Index, IndexMut, NeverShortCircuit, Residual, Try, | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| use crate::ptr::{null, null_mut}; | ||||||||||||||||||||||||||||
| use crate::random::{Random, RandomSource}; | ||||||||||||||||||||||||||||
| use crate::slice::{Iter, IterMut}; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| mod ascii; | ||||||||||||||||||||||||||||
|
|
@@ -426,6 +427,18 @@ impl<T: Clone, const N: usize> Clone for [T; N] { | |||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #[unstable(feature = "random", issue = "130703")] | ||||||||||||||||||||||||||||
| impl<T: Random, const N: usize> Random for [T; N] { | ||||||||||||||||||||||||||||
| fn random(source: &mut (impl RandomSource + ?Sized)) -> Self { | ||||||||||||||||||||||||||||
| let mut buf = [const { MaybeUninit::uninit() }; N]; | ||||||||||||||||||||||||||||
| for elem in &mut buf { | ||||||||||||||||||||||||||||
| elem.write(T::random(source)); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| // SAFETY: all elements of the array were initialized. | ||||||||||||||||||||||||||||
| unsafe { mem::transmute_copy(&buf) } | ||||||||||||||||||||||||||||
|
Urgau marked this conversation as resolved.
Outdated
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's a version that uses
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unsound for generic Ts. There's no guarantee that a Type's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @the8472 I did notice that, so we would need to restrict this to types that can hold arbitrary bytes (which currently includes every type implementing |
||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| trait SpecArrayClone: Clone { | ||||||||||||||||||||||||||||
| fn clone<const N: usize>(array: &[Self; N]) -> [Self; N]; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.