@@ -3,6 +3,7 @@ use std::path::Path;
3
3
use std:: sync:: Arc ;
4
4
5
5
use chrono:: { DateTime , Utc } ;
6
+ use rand:: { rngs:: ThreadRng , thread_rng, Rng } ;
6
7
use uuid:: Uuid ;
7
8
8
9
pub use self :: file:: FileExt ;
@@ -14,6 +15,7 @@ pub mod file;
14
15
pub trait Io : Send + Sync + ' static {
15
16
type File : FileExt ;
16
17
type TempFile : FileExt ;
18
+ type Rng : Rng ;
17
19
18
20
fn create_dir_all ( & self , path : & Path ) -> io:: Result < ( ) > ;
19
21
/// 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 {
30
32
fn now ( & self ) -> DateTime < Utc > ;
31
33
fn uuid ( & self ) -> Uuid ;
32
34
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 ;
33
37
}
34
38
35
39
#[ derive( Default , Debug , Clone , Copy ) ]
@@ -38,6 +42,7 @@ pub struct StdIO(pub(crate) ());
38
42
impl Io for StdIO {
39
43
type File = std:: fs:: File ;
40
44
type TempFile = std:: fs:: File ;
45
+ type Rng = ThreadRng ;
41
46
42
47
fn create_dir_all ( & self , path : & Path ) -> io:: Result < ( ) > {
43
48
std:: fs:: create_dir_all ( path)
@@ -72,11 +77,18 @@ impl Io for StdIO {
72
77
fn hard_link ( & self , src : & Path , dst : & Path ) -> io:: Result < ( ) > {
73
78
std:: fs:: hard_link ( src, dst)
74
79
}
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
+ }
75
86
}
76
87
77
88
impl < T : Io > Io for Arc < T > {
78
89
type File = T :: File ;
79
90
type TempFile = T :: TempFile ;
91
+ type Rng = T :: Rng ;
80
92
81
93
fn create_dir_all ( & self , path : & Path ) -> io:: Result < ( ) > {
82
94
self . as_ref ( ) . create_dir_all ( path)
@@ -107,6 +119,11 @@ impl<T: Io> Io for Arc<T> {
107
119
fn hard_link ( & self , src : & Path , dst : & Path ) -> io:: Result < ( ) > {
108
120
self . as_ref ( ) . hard_link ( src, dst)
109
121
}
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
+ }
110
127
}
111
128
112
129
pub struct Inspect < W , F > {
0 commit comments