Skip to content

Commit 4de86da

Browse files
committed
add with_rng to Io
1 parent 45045dd commit 4de86da

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

libsql-wal/src/io/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::path::Path;
33
use std::sync::Arc;
44

55
use chrono::{DateTime, Utc};
6+
use rand::{rngs::ThreadRng, thread_rng, Rng};
67
use uuid::Uuid;
78

89
pub use self::file::FileExt;
@@ -14,6 +15,7 @@ pub mod file;
1415
pub trait Io: Send + Sync + 'static {
1516
type File: FileExt;
1617
type TempFile: FileExt;
18+
type Rng: Rng;
1719

1820
fn create_dir_all(&self, path: &Path) -> io::Result<()>;
1921
/// TODO: when adding an async variant make sure all places where async is needed are replaced
@@ -30,6 +32,8 @@ pub trait Io: Send + Sync + 'static {
3032
fn now(&self) -> DateTime<Utc>;
3133
fn uuid(&self) -> Uuid;
3234
fn hard_link(&self, src: &Path, dst: &Path) -> io::Result<()>;
35+
fn with_rng<F, R>(&self, f: F) -> R
36+
where F: FnOnce(&mut Self::Rng) -> R;
3337
}
3438

3539
#[derive(Default, Debug, Clone, Copy)]
@@ -38,6 +42,7 @@ pub struct StdIO(pub(crate) ());
3842
impl Io for StdIO {
3943
type File = std::fs::File;
4044
type TempFile = std::fs::File;
45+
type Rng = ThreadRng;
4146

4247
fn create_dir_all(&self, path: &Path) -> io::Result<()> {
4348
std::fs::create_dir_all(path)
@@ -72,11 +77,18 @@ impl Io for StdIO {
7277
fn hard_link(&self, src: &Path, dst: &Path) -> io::Result<()> {
7378
std::fs::hard_link(src, dst)
7479
}
80+
81+
fn with_rng<F, R>(&self, f: F) -> R
82+
where F: FnOnce(&mut Self::Rng) -> R,
83+
{
84+
f(&mut thread_rng())
85+
}
7586
}
7687

7788
impl<T: Io> Io for Arc<T> {
7889
type File = T::File;
7990
type TempFile = T::TempFile;
91+
type Rng = T::Rng;
8092

8193
fn create_dir_all(&self, path: &Path) -> io::Result<()> {
8294
self.as_ref().create_dir_all(path)
@@ -107,6 +119,11 @@ impl<T: Io> Io for Arc<T> {
107119
fn hard_link(&self, src: &Path, dst: &Path) -> io::Result<()> {
108120
self.as_ref().hard_link(src, dst)
109121
}
122+
123+
fn with_rng<F, R>(&self, f: F) -> R
124+
where F: FnOnce(&mut Self::Rng) -> R {
125+
self.as_ref().with_rng(f)
126+
}
110127
}
111128

112129
pub struct Inspect<W, F> {

libsql-wal/tests/flaky_fs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl FlakyIo {
110110
impl Io for FlakyIo {
111111
type File = FlakyFile;
112112
type TempFile = FlakyFile;
113+
type Rng = rand_chacha::ChaCha8Rng;
113114

114115
fn create_dir_all(&self, path: &std::path::Path) -> std::io::Result<()> {
115116
self.with_random_failure(|| std::fs::create_dir_all(path))
@@ -150,6 +151,11 @@ impl Io for FlakyIo {
150151
fn hard_link(&self, _src: &Path, _dst: &Path) -> std::io::Result<()> {
151152
todo!()
152153
}
154+
155+
fn with_rng<F, R>(&self, f: F) -> R
156+
where F: FnOnce(&mut Self::Rng) -> R {
157+
f(&mut self.rng.lock())
158+
}
153159
}
154160

155161
macro_rules! assert_not_corrupt {

0 commit comments

Comments
 (0)