88
99use std:: {
1010 iter,
11- path:: PathBuf ,
12- sync:: {
13- mpsc:: { self , Receiver , Sender } ,
14- Mutex ,
15- } ,
11+ sync:: mpsc:: { self , Receiver , Sender } ,
1612 thread,
1713} ;
1814
@@ -28,95 +24,54 @@ use crate::{
2824 ClientImplementation ,
2925} ;
3026
31- pub use store:: { Filesystem , Ram , StoreProvider } ;
27+ pub use store:: { StorageConfig , StoreConfig } ;
3228pub use ui:: UserInterface ;
3329
3430pub type Client < ' a , D = CoreOnly > = ClientImplementation < ' a , Syscall , D > ;
3531
36- // We need this mutex to make sure that:
37- // - TrussedInterchange is not used concurrently (panics if violated)
38- // - the Store is not used concurrently
39- static MUTEX : Mutex < ( ) > = Mutex :: new ( ( ) ) ;
40-
41- pub fn with_platform < S , R , F > ( store : S , f : F ) -> R
32+ pub fn with_platform < R , F > ( mut store : StoreConfig , f : F ) -> R
4233where
43- S : StoreProvider ,
44- F : FnOnce ( Platform < S > ) -> R ,
34+ F : FnOnce ( Platform < ' _ > ) -> R ,
4535{
46- let _guard = MUTEX . lock ( ) . unwrap_or_else ( |err| err. into_inner ( ) ) ;
47- unsafe {
48- store. reset ( ) ;
49- }
50- // causing a regression again
51- // let rng = chacha20::ChaCha8Rng::from_rng(rand_core::OsRng).unwrap();
52- let platform = Platform {
53- rng : ChaCha8Rng :: from_seed ( [ 42u8 ; 32 ] ) ,
54- _store : store,
55- ui : UserInterface :: new ( ) ,
56- } ;
57- f ( platform)
36+ store. with_store ( |store| {
37+ // causing a regression again
38+ // let rng = chacha20::ChaCha8Rng::from_rng(rand_core::OsRng).unwrap();
39+ let platform = Platform {
40+ rng : ChaCha8Rng :: from_seed ( [ 42u8 ; 32 ] ) ,
41+ store,
42+ ui : UserInterface :: new ( ) ,
43+ } ;
44+ f ( platform)
45+ } )
5846}
5947
60- pub fn with_client < S , R , F > ( store : S , client_id : & str , f : F ) -> R
48+ pub fn with_client < R , F > ( store : StoreConfig , client_id : & str , f : F ) -> R
6149where
62- S : StoreProvider ,
6350 F : FnOnce ( Client ) -> R ,
6451{
6552 with_platform ( store, |platform| platform. run_client ( client_id, f) )
6653}
6754
68- pub fn with_fs_client < P , R , F > ( internal : P , client_id : & str , f : F ) -> R
69- where
70- P : Into < PathBuf > ,
71- F : FnOnce ( Client ) -> R ,
72- {
73- with_client ( Filesystem :: new ( internal) , client_id, f)
74- }
75-
76- pub fn with_ram_client < R , F > ( client_id : & str , f : F ) -> R
77- where
78- F : FnOnce ( Client ) -> R ,
79- {
80- with_client ( Ram :: default ( ) , client_id, f)
81- }
82-
83- pub fn with_clients < S , R , F , const N : usize > ( store : S , client_ids : [ & str ; N ] , f : F ) -> R
84- where
85- S : StoreProvider ,
86- F : FnOnce ( [ Client ; N ] ) -> R ,
87- {
88- with_platform ( store, |platform| platform. run_clients ( client_ids, f) )
89- }
90-
91- pub fn with_fs_clients < P , R , F , const N : usize > ( internal : P , client_ids : [ & str ; N ] , f : F ) -> R
92- where
93- P : Into < PathBuf > ,
94- F : FnOnce ( [ Client ; N ] ) -> R ,
95- {
96- with_clients ( Filesystem :: new ( internal) , client_ids, f)
97- }
98-
99- /// Run a function with multiple clients using the RAM for the filesystem.
100- ///
55+ /// Run a function with multiple clients.
10156///
10257/// Const generics are used to allow easy deconstruction in the callback arguments
10358///
10459/// ```rust
10560///# use trussed::client::{Ed255, CryptoClient};
10661///# use trussed::types::{Location, Mechanism};
10762///# use trussed::syscall;
108- ///# use trussed::virt::with_ram_clients ;
109- /// with_ram_clients( ["client1", "client2"], |[mut client1, mut client2]| {
63+ ///# use trussed::virt::{with_clients, StoreConfig} ;
64+ /// with_clients(StoreConfig::ram(), ["client1", "client2"], |[mut client1, mut client2]| {
11065/// let key = syscall!(client1.generate_ed255_private_key(Location::Internal)).key;
11166/// // The clients are distinct
11267/// assert!(!syscall!(client2.exists(Mechanism::Ed255, key)).exists);
11368/// })
11469/// ```
115- pub fn with_ram_clients < R , F , const N : usize > ( client_ids : [ & str ; N ] , f : F ) -> R
70+ pub fn with_clients < R , F , const N : usize > ( store : StoreConfig , client_ids : [ & str ; N ] , f : F ) -> R
11671where
11772 F : FnOnce ( [ Client ; N ] ) -> R ,
11873{
119- with_clients ( Ram :: default ( ) , client_ids, f)
74+ with_platform ( store , |platform| platform . run_clients ( client_ids, f) )
12075}
12176
12277pub struct Syscall ( Sender < ( ) > ) ;
@@ -184,13 +139,13 @@ impl<'a, I: 'static, C: Default> Runner<'a, I, C> {
184139 }
185140}
186141
187- pub struct Platform < S : StoreProvider > {
142+ pub struct Platform < ' a > {
188143 rng : ChaCha8Rng ,
189- _store : S ,
144+ store : store :: Store < ' a > ,
190145 ui : UserInterface ,
191146}
192147
193- impl < S : StoreProvider > Platform < S > {
148+ impl Platform < ' _ > {
194149 pub fn run_client < R > ( self , client_id : & str , test : impl FnOnce ( Client ) -> R ) -> R {
195150 self . run_client_with_backends ( client_id, CoreOnly , & [ ] , test)
196151 }
@@ -255,9 +210,9 @@ impl<S: StoreProvider> Platform<S> {
255210 }
256211}
257212
258- unsafe impl < S : StoreProvider > platform:: Platform for Platform < S > {
213+ unsafe impl < ' a > platform:: Platform for Platform < ' a > {
259214 type R = ChaCha8Rng ;
260- type S = S :: Store ;
215+ type S = store :: Store < ' a > ;
261216 type UI = UserInterface ;
262217
263218 fn user_interface ( & mut self ) -> & mut Self :: UI {
@@ -269,6 +224,6 @@ unsafe impl<S: StoreProvider> platform::Platform for Platform<S> {
269224 }
270225
271226 fn store ( & self ) -> Self :: S {
272- unsafe { S :: store ( ) }
227+ self . store
273228 }
274229}
0 commit comments