Skip to content

Docs for implementing SeedableRng for large seeds#381

Merged
pitdicker merged 5 commits intorust-random:masterfrom
vks:large-seed
Apr 11, 2018
Merged

Docs for implementing SeedableRng for large seeds#381
pitdicker merged 5 commits intorust-random:masterfrom
vks:large-seed

Conversation

@vks
Copy link
Copy Markdown
Contributor

@vks vks commented Apr 9, 2018

Fixes #354.

@vks
Copy link
Copy Markdown
Contributor Author

vks commented Apr 9, 2018

The failure on nightly seems unrelated.

Comment thread rand_core/src/lib.rs Outdated
/// pub struct MyRngSeed(pub [u8; N]);
///
/// impl Default for MyRngSeed { ... }
/// impl AsMut<u8> for MyRngSeed { ... }
Copy link
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread rand_core/src/lib.rs Outdated
/// 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
Copy Markdown
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
Copy Markdown
Contributor 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.
Comment thread rand_core/src/lib.rs Outdated
/// type Seed = MyRngSeed;
///
/// fn from_seed(seed: MyRngSeed) -> MyRng {
/// MyRng(MyRngSeed)
Copy link
Copy Markdown
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
Copy Markdown
Contributor

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

@dhardy
Copy link
Copy Markdown
Member

dhardy commented Apr 11, 2018

Yep, looks good! 👍

@pitdicker can you sort out the merging?

@dhardy
Copy link
Copy Markdown
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
Copy Markdown
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