-
Notifications
You must be signed in to change notification settings - Fork 185
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
Add getrandom::array()
#293
Conversation
3 early comments on this PR:
I put my above proposed implementation in this Godbolt Example. All three implementations of |
Maybe. At a minimum, such a function needs to be marked as I have some other ideas for a simpler implementation using
The goal isn't really to support nested byte arrays, but to make it easy to construct arrays of other types in a way that doesn't require us to manually add support to new types on a regular basis. In the case of ECC we need random
In most cases I imagine one will want to write a
So, the solution I proposed is ugly but AFAIK it's the prettiest and most minimal solution that doesn't force the user to write ugly, tedious code using
It would often become one line that avoids the need to define a function:
It may be the case that |
I think it's better to use a different, more descriptive feature name.
I think the current implementation is clean enough, though it could be worth to find a better name for But I do not understand why the following approach was not used for the trait (implementation of the function stays the same): // The trait name is just a brief placeholder
pub unsafe trait Pod {}
unsafe impl Pod for u8 {}
unsafe impl Pod for u16 {}
unsafe impl Pod for u32 {}
unsafe impl Pod for u64 {}
unsafe impl Pod for u128 {}
unsafe impl Pod for usize {}
// It's safe to write into padding bytes, so this impl correct for all types,
// including with alignment bigger than size.
unsafe impl<T: Pod, const N: usize> Pod for [T; N] {} It should automatically provide support for
I think it's better to use |
This is looking very similar to
I would prefer the second approach to the first, but both approaches also solve the issue of needing something like If we just want something standalone for this crate, I think we should only deal with arrays of |
I would be fine with an opt-in feature like this. We would then just document that the MSRV for this feature is higher than the crate's default MSRV. |
I think the general advice for Rust is to prefer |
It would be awesome if we could get something like this to work. When I did: let x: [u64; 8] = getrandom_array()?.map(u64::from_ne_bytes); I kept getting a EDIT: doing something like: pub unsafe trait Bytes {}
unsafe impl Bytes for u8 {}
unsafe impl<B: Bytes, const N: usize> Bytes for [B; N] {}
pub fn getrandom_array<B: Bytes, const N: usize>() -> Result<[B; N], Error> { ... } allows the above code to compile without any type annotations. |
2dace63
to
d5c4f85
Compare
getrandom_arrays
07a9d4f
to
ad0af77
Compare
Regarding the naming of I clean up the draft a bit to take into account the suggestions above. I do not think it is a good idea to try to maintain a list of types to support; I'd rather not maintain a subset of |
56d3282
to
11181db
Compare
Personally, I view them as variants of the |
Personally, I think this is out of scope for this crate. I think that it would be more idiomatic for this method to just be implemented as a method for |
@notgull |
Implement `getrandom::array`. It requires Rust 1.51 and the user must enable the "array" feature explicitly.
11181db
to
42f0d22
Compare
I know this is an old PR, perhaps bit-rotted
Actually this is allowed specifically because of the MaybeUninit. Any of the bytes can be uninit, which is why getting the T out of the MaybeUninit wrapper is itself also unsafe. |
Closing in favor of #381. |
Implement
getrandom_array
. It requires Rust 1.51 and the user must enable the "array" feature explicitly.