-
Notifications
You must be signed in to change notification settings - Fork 432
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
Docs for implementing SeedableRng
for large seeds
#381
Conversation
The failure on nightly seems unrelated. |
rand_core/src/lib.rs
Outdated
/// pub struct MyRngSeed(pub [u8; N]); | ||
/// | ||
/// impl Default for MyRngSeed { ... } | ||
/// impl AsMut<u8> for MyRngSeed { ... } |
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.
With just a few more lines the example can be made complete. Maybe it is worth it to do so? (and then also does not need to be ignored)
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 expanded the example to actually compile. What do you think?
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.
Can you use fn default() -> MyRngSeed { MyRngSeed([0u8; 64]) }
instead?
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.
Done.
rand_core/src/lib.rs
Outdated
@@ -307,6 +329,25 @@ pub trait SeedableRng: Sized { | |||
/// seeds it is preferable to map these to alternative constant value(s), | |||
/// for example `0xBAD5EEDu32` or `0x0DDB1A5E5BAD5EEDu64` ("odd biases? bad | |||
/// seed"). This is assuming only a small number of values must be rejected. | |||
/// Alternatively, the newtype pattern can be used to make sure only valid | |||
/// seeds can be constructed: |
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 don't really like this as a pattern. RNG implementations can do as they wish, but we shouldn't advertise it here i.m.o.
It is useful for generic code to have from_seed
work with all simple slices. The default implementation of from_rng
relies on this, and something like from_hashable
would too.
Also, does it really work? With the AsMut
implementation wouldn't it be possible to modify the newtype outside new()
?
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.
Also, does it really work? With the AsMut implementation wouldn't it be possible to modify the newtype outside new()?
Good point! I removed the suggestion to use the newtype pattern to work around invalid seeds.
This does not work, because the seed has to implement `AsMut<[u8]>`, so any invariants enforced by the constructor can be circumvented.
rand_core/src/lib.rs
Outdated
/// type Seed = MyRngSeed; | ||
/// | ||
/// fn from_seed(seed: MyRngSeed) -> MyRng { | ||
/// MyRng(MyRngSeed) |
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.
Should be MyRng(seed)
(reason of the CI failure)
Looks good to me! Let's wait until @dhardy had a chance to look over it to before merging. |
Yep, looks good! 👍 @pitdicker can you sort out the merging? |
I restarted the Travis build. @pitdicker if you have a Travis account I think you should be able to do that too. |
I already restarted travis, but that was not enough |
Fixes #354.