-
-
Notifications
You must be signed in to change notification settings - Fork 461
impl<R: RngCore> RngCore for Rc<RefCell<R>> #604
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
It's unclear what CI dislikes about that second commit, but it's also unclear yet if it does exactly what we want. Also verify if that |
The failure is on nightly only and inside Could you please add a test exercising the newly added |
use std::ops::Deref; | ||
use distributions::{Distribution, Standard}; | ||
// let rng = rng(110); | ||
let mut r = & *::std::rc::Rc::new(::std::cell::RefCell::new(rng(110))); |
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.
We would not require the & *
here if we had an actual impl for Rc<RefCell<R>>
but navigating rustc to a &mut &RefCel<R>
is tricky.
I do like this |
In fact, we cannot consider this form strictly more general, even if
Add back the |
If you can provide both implementations and they don't conflict, that does sound more general. Failing that, I would err on the side of implementing the form most likely to be used. Do you have a use-case for this BTW? |
I've no use case myself right now because I do protocol work in which I need reproducibility and precise control over sampling. I did not suggest doing this with I think both forms suffice for the standard use case in which you want to pass the same |
I say keep |
As an aside, an interesting research problem would be designing a non-locking reentrant numerical PRNG, albeit with poor reproducability. In essence, you'd pay slightly more computational cost than some numerical PRNGs employ some argument that no matter how the parallel executions interacted with themselves via nasty rampant undefined behavior the resulting parallel permutation still gives a sensible numerical PRNG. I expect all references into such a PRNG would require their own unique seed, which the PRNG should use to prevent operations from different threads from inverting one another. Also you'd require a bound on the number of parallel executions. |
@dhardy Personally, I would err on the side of caution and not add features without use cases. |
@burdges so a partially-local, partially-shared PRNG? Intriguing, but as you say, more a research problem than something we should worry about solving here, and considering there are reasonably good small, fast PRNGs with 128 bits of state, I see very little motivation except as a CSPRNG (where the correctness proofs become even more important). I think then I will close this as postponed lacking use-case; those with an interest are of course welcome to comment. |
Yeah, the issue is here now so anyone who actually wants this can maybe find it and ask for it. |
I'd think
RefCell
adjusting its borrow counter sounds more cache friendly than forkingRng
s.We cannot avoid
RefCell
by usingUnsafeCell
, etc. here because we cannot prevent user supplied methods likenext_u32
from referencing us again. See the theCopy
requirement on Cell::update.I've tried to permit users to bypass the
Rc
counter and allocation when possible by instead providingwhich should still support
Rc<RefCell<R>
viaRc: Deref
. If you want to use this withoutRc
then you must remember that all methods want&mut &RefCell<R>
, which may require some cajoling.