@@ -15,16 +15,17 @@ use crate::{
1515 HypercoreError ,
1616} ;
1717
18+ /// Supertrait for Storage
19+ pub trait StorageTraits : RandomAccess + Debug { }
20+ impl < T : RandomAccess + Debug > StorageTraits for T { }
21+
1822/// Save data to a desired storage backend.
1923#[ derive( Debug ) ]
20- pub struct Storage < T >
21- where
22- T : RandomAccess + Debug ,
23- {
24- tree : T ,
25- data : T ,
26- bitfield : T ,
27- oplog : T ,
24+ pub struct Storage {
25+ tree : Box < dyn StorageTraits + Send > ,
26+ data : Box < dyn StorageTraits + Send > ,
27+ bitfield : Box < dyn StorageTraits + Send > ,
28+ oplog : Box < dyn StorageTraits + Send > ,
2829}
2930
3031pub ( crate ) fn map_random_access_err ( err : RandomAccessError ) -> HypercoreError {
@@ -51,17 +52,18 @@ pub(crate) fn map_random_access_err(err: RandomAccessError) -> HypercoreError {
5152 }
5253}
5354
54- impl < T > Storage < T >
55- where
56- T : RandomAccess + Debug + Send ,
57- {
55+ impl Storage {
5856 /// Create a new instance. Takes a callback to create new storage instances and overwrite flag.
5957 pub async fn open < Cb > ( create : Cb , overwrite : bool ) -> Result < Self , HypercoreError >
6058 where
6159 Cb : Fn (
6260 Store ,
6361 ) -> std:: pin:: Pin <
64- Box < dyn std:: future:: Future < Output = Result < T , RandomAccessError > > + Send > ,
62+ Box <
63+ dyn std:: future:: Future <
64+ Output = Result < Box < dyn StorageTraits + Send > , RandomAccessError > ,
65+ > + Send ,
66+ > ,
6567 > ,
6668 {
6769 let mut tree = create ( Store :: Tree ) . await . map_err ( map_random_access_err) ?;
@@ -235,39 +237,45 @@ where
235237 Ok ( ( ) )
236238 }
237239
238- fn get_random_access ( & mut self , store : & Store ) -> & mut T {
240+ fn get_random_access ( & mut self , store : & Store ) -> & mut Box < dyn StorageTraits + Send > {
239241 match store {
240242 Store :: Tree => & mut self . tree ,
241243 Store :: Data => & mut self . data ,
242244 Store :: Bitfield => & mut self . bitfield ,
243245 Store :: Oplog => & mut self . oplog ,
244246 }
245247 }
246- }
247248
248- impl Storage < RandomAccessMemory > {
249249 /// New storage backed by a `RandomAccessMemory` instance.
250250 #[ instrument( err) ]
251251 pub async fn new_memory ( ) -> Result < Self , HypercoreError > {
252- let create = |_| async { Ok ( RandomAccessMemory :: default ( ) ) } . boxed ( ) ;
252+ let create = |_| {
253+ async { Ok ( Box :: new ( RandomAccessMemory :: default ( ) ) as Box < dyn StorageTraits + Send > ) }
254+ . boxed ( )
255+ } ;
253256 // No reason to overwrite, as this is a new memory segment
254257 Self :: open ( create, false ) . await
255258 }
256- }
257259
258- #[ cfg( not( target_arch = "wasm32" ) ) ]
259- impl Storage < RandomAccessDisk > {
260260 /// New storage backed by a `RandomAccessDisk` instance.
261+ #[ cfg( not( target_arch = "wasm32" ) ) ]
261262 #[ instrument( err) ]
262263 pub async fn new_disk ( dir : & PathBuf , overwrite : bool ) -> Result < Self , HypercoreError > {
263264 let storage = |store : Store | {
264- let name = match store {
265- Store :: Tree => "tree" ,
266- Store :: Data => "data" ,
267- Store :: Bitfield => "bitfield" ,
268- Store :: Oplog => "oplog" ,
269- } ;
270- RandomAccessDisk :: open ( dir. as_path ( ) . join ( name) ) . boxed ( )
265+ let dir = dir. clone ( ) ;
266+ async move {
267+ let name = match store {
268+ Store :: Tree => "tree" ,
269+ Store :: Data => "data" ,
270+ Store :: Bitfield => "bitfield" ,
271+ Store :: Oplog => "oplog" ,
272+ } ;
273+ Ok (
274+ Box :: new ( RandomAccessDisk :: open ( dir. as_path ( ) . join ( name) ) . await ?)
275+ as Box < dyn StorageTraits + Send > ,
276+ )
277+ }
278+ . boxed ( )
271279 } ;
272280 Self :: open ( storage, overwrite) . await
273281 }
0 commit comments