Skip to content

Commit

Permalink
Merge 'Added random number generation to I/O trait for simulation' fr…
Browse files Browse the repository at this point in the history
…om mason

Took a stab at the RNG portion of this [issue](#272), do let me know if there's anything that I can improve on/rework!

Thanks!

Closes #301
  • Loading branch information
penberg committed Aug 22, 2024
2 parents 9e16385 + 68e54c2 commit 0a9f2ab
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 2 deletions.
10 changes: 10 additions & 0 deletions bindings/wasm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ impl limbo_core::IO for PlatformIO {
fn run_once(&self) -> Result<()> {
Ok(())
}

fn generate_random_number(&self) -> i64 {
let random_f64 = Math_random();
(random_f64 * i64::MAX as f64) as i64
}
}

#[wasm_bindgen]
extern "C" {
fn Math_random() -> f64;
}

pub struct DatabaseStorage {
Expand Down
6 changes: 6 additions & 0 deletions core/io/darwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ impl IO for DarwinIO {
}
Ok(())
}

fn generate_random_number(&self) -> i64 {
let mut buf = [0u8; 8];
getrandom::getrandom(&mut buf).unwrap();
i64::from_ne_bytes(buf)
}
}

enum CompletionCallback {
Expand Down
6 changes: 6 additions & 0 deletions core/io/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ impl IO for GenericIO {
fn run_once(&self) -> Result<()> {
Ok(())
}

fn generate_random_number(&self) -> i64 {
let mut buf = [0u8; 8];
getrandom::getrandom(&mut buf).unwrap();
i64::from_ne_bytes(buf)
}
}

pub struct GenericFile {
Expand Down
6 changes: 6 additions & 0 deletions core/io/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ impl IO for LinuxIO {
}
Ok(())
}

fn generate_random_number(&self) -> i64 {
let mut buf = [0u8; 8];
getrandom::getrandom(&mut buf).unwrap();
i64::from_ne_bytes(buf)
}
}

pub struct LinuxFile {
Expand Down
2 changes: 2 additions & 0 deletions core/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub trait IO {
fn open_file(&self, path: &str) -> Result<Rc<dyn File>>;

fn run_once(&self) -> Result<()>;

fn generate_random_number(&self) -> i64;
}

pub type Complete = dyn Fn(Rc<RefCell<Buffer>>);
Expand Down
6 changes: 6 additions & 0 deletions core/io/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ impl IO for WindowsIO {
fn run_once(&self) -> Result<()> {
Ok(())
}

fn generate_random_number(&self) -> i64 {
let mut buf = [0u8; 8];
getrandom::getrandom(&mut buf).unwrap();
i64::from_ne_bytes(buf)
}
}

pub struct WindowsFile {
Expand Down
11 changes: 9 additions & 2 deletions simulator/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
};
println!("Seed: {}", seed);
let mut rng = ChaCha8Rng::seed_from_u64(seed);
let io = Arc::new(SimulatorIO::new().unwrap());
let io = Arc::new(SimulatorIO::new(seed).unwrap());
let db = match Database::open_file(io.clone(), "./testing/testing.db") {
Ok(db) => db,
Err(_) => todo!(),
Expand Down Expand Up @@ -56,17 +56,20 @@ struct SimulatorIO {
inner: Box<dyn IO>,
fault: RefCell<bool>,
files: RefCell<Vec<Rc<SimulatorFile>>>,
rng: RefCell<ChaCha8Rng>,
}

impl SimulatorIO {
fn new() -> Result<Self> {
fn new(seed: u64) -> Result<Self> {
let inner = Box::new(PlatformIO::new()?);
let fault = RefCell::new(false);
let files = RefCell::new(Vec::new());
let rng = RefCell::new(ChaCha8Rng::seed_from_u64(seed));
Ok(Self {
inner,
fault,
files,
rng,
})
}

Expand Down Expand Up @@ -106,6 +109,10 @@ impl IO for SimulatorIO {
self.inner.run_once().unwrap();
Ok(())
}

fn generate_random_number(&self) -> i64 {
self.rng.borrow_mut().next_u64() as i64
}
}

struct SimulatorFile {
Expand Down

0 comments on commit 0a9f2ab

Please sign in to comment.