@@ -3,6 +3,7 @@ use std::path::Path;
33use std:: sync:: Arc ;
44
55use chrono:: { DateTime , Utc } ;
6+ use rand:: { rngs:: ThreadRng , thread_rng, Rng } ;
67use uuid:: Uuid ;
78
89pub use self :: file:: FileExt ;
@@ -14,6 +15,7 @@ pub mod file;
1415pub 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) ());
3842impl 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
7788impl < 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
112129pub struct Inspect < W , F > {
0 commit comments