-
-
Couldn't load subscription status.
- Fork 477
rand_core: introduce Seed trait
#1670
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
|
Currently it breaks |
rand_core/src/seed.rs
Outdated
| } | ||
| } | ||
|
|
||
| impl Seed for u128 { |
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.
I wonder if we should also implement this trait for [u32/u64/u128; N] types. For example, for PCG32 [u64; 2] is a "natural" seed, while for ChaCha20 it would be [u32; 8].
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.
I was assuming this was the purpose of Seed. Also why you deleted the "Default for large arrays" doc.
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.
My main purpose was to support non-u8 seeds, support for large arrays is a great side benefit.
rand_core/src/seed.rs
Outdated
| fn try_from_bytes<E>(fill: impl FnOnce(&mut [u8]) -> Result<(), E>) -> Result<Self, E>; | ||
|
|
||
| /// Create seed from an infallible closure which fills the provided buffer. | ||
| fn from_bytes(fill: impl FnOnce(&mut [u8])) -> Self { |
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.
Ideally, we would use impl FnOnce(&mut [u8; { Self::SIZE }]), but it does not work on the current stable Rust.
rand_core/src/seed.rs
Outdated
| /// Create seed from a fallible closure which fills the provided buffer. | ||
| fn try_from_bytes<E>(fill: impl FnOnce(&mut [u8]) -> Result<(), E>) -> Result<Self, E>; |
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 should be named try_from_fill(buf: impl ...).
But I'm wondering why we actually want it. The only advantage I can see over a simple fn from_bytes(bytes: [u8; Self::SIZE]) is that it does not rely on the optimiser to elide a move.
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.
As I wrote in the comment above, [u8; Self::SIZE] does not work because of the const generics limitations.
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.
Ah.. this limitation is not a side-effect of using a closure.
We could use fn from_bytes(bytes: &[u8]) -> Self; but we'd lose assertion of the size.
|
So, I'm not really convinced we need this. If someone wants to use a |
|
It looks similar to |
This isn't really unwieldy; it just means "like
This could easily be misused.
We should get a proper fix from Rust soon: rust-lang/rust#61415 which won't force usage of alternative types like |
And it allows for unrelated types like It may have been a different story if we were able to use
How is it different from using
Well, "soon" here may mean year or more until stabilization. |
|
It looks like the main difference in our opinions is whether we should allow non- I guess we should see what others think about it. |
Yes, PRNGs normally use something like I've seen people use bad seeds before. |
|
There's a second reason I'm against this: it is a significant re-working of what has been a stable part of Mostly I see this PR as unnecessary churn (plus some additional complexity). I would prefer to wait until we have const generics then replace
Is this important? It would enable
This could be a significant advantage, except that nothing really requires larger keys than
This PR would not permit usage of
This comment was a mistake in my part: since the RNG fixes the seed type (and would likely use In summary: technically (I don't think) either approach is significantly better than the other. The churn and the implied need for an extended stabilisation period are however significant reasons not to do this. |
I meant it as a disadvantage of the current API, since
Since no one else has voiced support for this approach, I am going to close this PR. |
This resolves the issue with longer arrays, makes the code slightly more concise, and allows to use
u32/u64/u128as seeds.Discussed in #1643 and #1666.