-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Make PRNGs implement Clone instead of Copy #20199
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
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.
r? @pcwalton (rust_highfive has picked a reviewer for you, use r? to override) |
|
I agree with @thestinger here. It is somewhat of a footgun if you accidentally copy your RNG, but |
It’s a fair point and, honestly, I can’t think of counter-argument to that… |
I'm not sure how much sense it makes to use most RNGs with In any case, it would be good to have these structs implement (It might be good to have some tests that check that |
i likewise agree with @huonw that not implementing |
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. See: * http://internals.rust-lang.org/t/copy-impl-for-random-number-generators/1218 * rust-lang/rust#20199 [breaking-change]. Use `clone` method implemented on the relevant RNGs instead.
Remove derivations of
Copy
trait fromChaChaRng
,IsaacRng
,Isaac64Rng
and
StdRng
and implement theClone
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 onlybe 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 thederived
Copy
implementations did.Although the
Copy
implementations of these random number generators aresuspected to not be used much, this is a [breaking-change].
Fixes #20170.