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

make rng seed extendable #26

Closed
wants to merge 1 commit into from
Closed

make rng seed extendable #26

wants to merge 1 commit into from

Conversation

Yneth
Copy link

@Yneth Yneth commented May 15, 2022

Cell is not Send 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:

struct Cursor {
  idx: usize,
  buf: Vec<T>,
  rng: Rng,
  seed: u64
}

impl Cursor {
  async fn next() {
      // when idx > buf.len(), refresh, shuffle, continue
  }
}

@Yneth
Copy link
Author

Yneth commented May 16, 2022

does this PR make sense to you?


// Seed abstraction, so users can specify their own
#[derive(Debug, PartialEq, Eq)]
pub struct CellSeed(Cell<u64>);
Copy link
Member

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>;

@fogti fogti mentioned this pull request Sep 13, 2022
@fogti
Copy link
Member

fogti commented Sep 13, 2022

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.

Comment on lines +128 to +138
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)
}
Copy link
Collaborator

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants