Uh oh!
There was an error while loading. Please reload this page.
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 |
briansmith
commented
Oct 20, 2022
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 placeholderpubunsafetraitPod{}unsafeimplPodforu8{}unsafeimplPodforu16{}unsafeimplPodforu32{}unsafeimplPodforu64{}unsafeimplPodforu128{}unsafeimplPodforusize{}// It's safe to write into padding bytes, so this impl correct for all types,// including with alignment bigger than size.unsafeimpl<T:Pod,constN:usize>Podfor[T;N]{}It should automatically provide support for
I think it's better to use |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
josephlr
commented
Oct 20, 2022
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 |
josephlr
commented
Oct 20, 2022
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. |
josephlr
commented
Oct 20, 2022
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: pubunsafetraitBytes{}unsafeimplBytesforu8{}unsafeimpl<B:Bytes,constN:usize>Bytesfor[B;N]{}pubfngetrandom_array<B:Bytes,constN:usize>() -> Result<[B;N],Error>{ ...}allows the above code to compile without any type annotations. |
2dace63 to
d5c4f85Comparegetrandom_arrays07a9d4f to
ad0af77Comparebriansmith
commented
Oct 21, 2022
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
11181dbCompare
Personally, I view them as variants of the |
notgull
commented
Oct 21, 2022
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 |
newpavlov
commented
Oct 22, 2022
@notgull |
Implement `getrandom::array`. It requires Rust 1.51 and the user must enable the "array" feature explicitly.
11181db to
42f0d22CompareLokathor
commented
Sep 1, 2023
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. |
newpavlov
commented
Nov 8, 2023
Closing in favor of #381. |
Implement
getrandom_array. It requires Rust 1.51 and the user must enable the "array" feature explicitly.