-
Notifications
You must be signed in to change notification settings - Fork 36
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
make rng seed extendable #26
Conversation
does this PR make sense to you? |
|
||
// Seed abstraction, so users can specify their own | ||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct CellSeed(Cell<u64>); |
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.
I don't think the new-type struct abstraction is necessary, it imo should be omitted. It might be a good idea to replace it with a type alias.
pub type CellSeed = Cell<u64>;
As mentioned in #31 it might be more beneficial to get rid of interior mutability altogether and instead let the user wrap it in whatever interior mutability abstraction they want to use. |
fn with_seed(seed: u64) -> Rng<Self> where Self: Sized { | ||
Rng::from(AtomicSeed(AtomicU64::new(seed))) | ||
} | ||
|
||
fn get(&self) -> u64 { | ||
self.0.load(Ordering::Relaxed) | ||
} | ||
|
||
fn set(&self, new_seed: u64) { | ||
self.0.store(new_seed, Ordering::Relaxed) | ||
} |
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.
If thread B reads a value between the time thread A reads it and the time it stores the value, thread A and thread B will get the same value from rng. I guess there are both use cases where that is ok and use cases where it is not ok, but I'm not positive about having an API that is easy to misuse.
(If you want to create atomic rng, I think you need to replace get/set in gen_u64 with fetch_add)
Cell
is notSend
thus it cannot be used as struct field in async environment.I tried to implement cursor, which needs to have its' data shuffled in a deterministic manner.
example: