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

Random number generators should not implement Copy #20170

Closed
nagisa opened this issue Dec 23, 2014 · 2 comments
Closed

Random number generators should not implement Copy #20170

nagisa opened this issue Dec 23, 2014 · 2 comments

Comments

@nagisa
Copy link
Member

nagisa commented Dec 23, 2014

Currently there’s a few random number generators that implement Copy:

  • StdRng
  • Isaac*Rng
  • ChaChaRng

All of these random number generators have a bad property of also copying their seed/internal data implicitly, rather than splitting their seed between original and copied generators. This means that Rng in question will start generating the same sequence values when generator is passed by value.

These generators should not implement Copy (implicit) and implement Clone (explicit) instead in case copying generators is intended (which is almost never a case), like XorShiftRng does. Maybe I just missed the use-case of Copyable PRNGs?

@sfackler
Copy link
Member

I'd agree that they probably shouldn't be Copy.

nagisa added a commit to nagisa/rust that referenced this issue Dec 24, 2014
Remove derivations of `Copy` trait from `ChaChaRng`, `IsaacRng`, `Isaac64Rng`
and `StdRng` and implement the `Clone` trait instead.

Copying is an implicit operation and the copy shares its internal state with
the original generator. This means the the two generators will yield exactly
the same sequence of values. This behaviour is considerably easy to trigger
by, for example, passing the generator by value to a function.

`Clone` trait, on the other hand, does no implicit copying. The user will only
be able to either move the generator or ask for a copy explicitly, via the
`clone` method.

The only downside to this patch is the fact that the implementations of
`Clone::clone` methods do not optimise down to a single memcpy, like the
derived `Copy` implementations did.

Although the `Copy` implementations of these random number generators are
suspected to not be used much, this is a [breaking-change].

Fixes rust-lang#20170.
@huonw
Copy link
Member

huonw commented Dec 24, 2014

NB. This is not a question of Copy versus Clone, since we should implement Clone for sure, and an implementation of Copy is mostly orthogonal to this.

@nagisa nagisa closed this as completed Jan 2, 2015
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 a pull request may close this issue.

3 participants