Skip to content
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

Merged
merged 5 commits into from
Apr 11, 2018

Conversation

vks
Copy link
Collaborator

@vks vks commented Apr 9, 2018

Fixes #354.

@vks
Copy link
Collaborator Author

vks commented Apr 9, 2018

The failure on nightly seems unrelated.

/// pub struct MyRngSeed(pub [u8; N]);
///
/// impl Default for MyRngSeed { ... }
/// impl AsMut<u8> for MyRngSeed { ... }
Copy link
Contributor

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)

Copy link
Collaborator Author

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?

Copy link
Contributor

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -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:
Copy link
Contributor

@pitdicker pitdicker Apr 9, 2018

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_rngrelies on this, and something like from_hashablewould too.

Also, does it really work? With the AsMut implementation wouldn't it be possible to modify the newtype outside new()?

Copy link
Collaborator Author

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.

vks added 3 commits April 10, 2018 10:58
This does not work, because the seed has to implement `AsMut<[u8]>`, so
any invariants enforced by the constructor can be circumvented.
/// type Seed = MyRngSeed;
///
/// fn from_seed(seed: MyRngSeed) -> MyRng {
/// MyRng(MyRngSeed)
Copy link
Contributor

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)

@pitdicker
Copy link
Contributor

Looks good to me! Let's wait until @dhardy had a chance to look over it to before merging.

@dhardy
Copy link
Member

dhardy commented Apr 11, 2018

Yep, looks good! 👍

@pitdicker can you sort out the merging?

@dhardy
Copy link
Member

dhardy commented Apr 11, 2018

I restarted the Travis build. @pitdicker if you have a Travis account I think you should be able to do that too.

@pitdicker
Copy link
Contributor

I already restarted travis, but that was not enough

@pitdicker pitdicker merged commit d0003ed into rust-random:master Apr 11, 2018
@vks vks deleted the large-seed branch April 11, 2018 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants