11use std:: rc:: Rc ;
22
3- use anyhow:: Result ;
43use cid:: { multihash, Cid } ;
54
65pub mod tracking;
@@ -15,26 +14,28 @@ pub use block::*;
1514///
1615/// The cgo blockstore adapter implements this trait.
1716pub trait Blockstore {
17+ type Error : std:: error:: Error + std:: fmt:: Debug ;
18+
1819 /// Gets the block from the blockstore.
19- fn get ( & self , k : & Cid ) -> Result < Option < Vec < u8 > > > ;
20+ fn get ( & self , k : & Cid ) -> Result < Option < Vec < u8 > > , Self :: Error > ;
2021
2122 /// Put a block with a pre-computed cid.
2223 ///
2324 /// If you don't yet know the CID, use put. Some blockstores will re-compute the CID internally
2425 /// even if you provide it.
2526 ///
2627 /// If you _do_ already know the CID, use this method as some blockstores _won't_ recompute it.
27- fn put_keyed ( & self , k : & Cid , block : & [ u8 ] ) -> Result < ( ) > ;
28+ fn put_keyed ( & self , k : & Cid , block : & [ u8 ] ) -> Result < ( ) , Self :: Error > ;
2829
2930 /// Checks if the blockstore has the specified block.
30- fn has ( & self , k : & Cid ) -> Result < bool > {
31+ fn has ( & self , k : & Cid ) -> Result < bool , Self :: Error > {
3132 Ok ( self . get ( k) ?. is_some ( ) )
3233 }
3334
3435 /// Puts the block into the blockstore, computing the hash with the specified multicodec.
3536 ///
3637 /// By default, this defers to put.
37- fn put < D > ( & self , mh_code : multihash:: Code , block : & Block < D > ) -> Result < Cid >
38+ fn put < D > ( & self , mh_code : multihash:: Code , block : & Block < D > ) -> Result < Cid , Self :: Error >
3839 where
3940 Self : Sized ,
4041 D : AsRef < [ u8 ] > ,
@@ -55,7 +56,7 @@ pub trait Blockstore {
5556 /// let blocks = vec![Block::new(0x55, vec![0, 1, 2])];
5657 /// bs.put_many(blocks.iter().map(|b| (Blake2b256, b.into()))).unwrap();
5758 /// ```
58- fn put_many < D , I > ( & self , blocks : I ) -> Result < ( ) >
59+ fn put_many < D , I > ( & self , blocks : I ) -> Result < ( ) , Self :: Error >
5960 where
6061 Self : Sized ,
6162 D : AsRef < [ u8 ] > ,
@@ -68,7 +69,7 @@ pub trait Blockstore {
6869 /// Bulk-put pre-keyed blocks into the blockstore.
6970 ///
7071 /// By default, this defers to put_keyed.
71- fn put_many_keyed < D , I > ( & self , blocks : I ) -> Result < ( ) >
72+ fn put_many_keyed < D , I > ( & self , blocks : I ) -> Result < ( ) , Self :: Error >
7273 where
7374 Self : Sized ,
7475 D : AsRef < [ u8 ] > ,
@@ -82,34 +83,36 @@ pub trait Blockstore {
8283}
8384
8485pub trait Buffered : Blockstore {
85- fn flush ( & self , root : & Cid ) -> Result < ( ) > ;
86+ fn flush ( & self , root : & Cid ) -> Result < ( ) , Self :: Error > ;
8687}
8788
8889impl < BS > Blockstore for & BS
8990where
9091 BS : Blockstore ,
9192{
92- fn get ( & self , k : & Cid ) -> Result < Option < Vec < u8 > > > {
93+ type Error = BS :: Error ;
94+
95+ fn get ( & self , k : & Cid ) -> Result < Option < Vec < u8 > > , Self :: Error > {
9396 ( * self ) . get ( k)
9497 }
9598
96- fn put_keyed ( & self , k : & Cid , block : & [ u8 ] ) -> Result < ( ) > {
99+ fn put_keyed ( & self , k : & Cid , block : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
97100 ( * self ) . put_keyed ( k, block)
98101 }
99102
100- fn has ( & self , k : & Cid ) -> Result < bool > {
103+ fn has ( & self , k : & Cid ) -> Result < bool , Self :: Error > {
101104 ( * self ) . has ( k)
102105 }
103106
104- fn put < D > ( & self , mh_code : multihash:: Code , block : & Block < D > ) -> Result < Cid >
107+ fn put < D > ( & self , mh_code : multihash:: Code , block : & Block < D > ) -> Result < Cid , Self :: Error >
105108 where
106109 Self : Sized ,
107110 D : AsRef < [ u8 ] > ,
108111 {
109112 ( * self ) . put ( mh_code, block)
110113 }
111114
112- fn put_many < D , I > ( & self , blocks : I ) -> Result < ( ) >
115+ fn put_many < D , I > ( & self , blocks : I ) -> Result < ( ) , Self :: Error >
113116 where
114117 Self : Sized ,
115118 D : AsRef < [ u8 ] > ,
@@ -118,7 +121,7 @@ where
118121 ( * self ) . put_many ( blocks)
119122 }
120123
121- fn put_many_keyed < D , I > ( & self , blocks : I ) -> Result < ( ) >
124+ fn put_many_keyed < D , I > ( & self , blocks : I ) -> Result < ( ) , Self :: Error >
122125 where
123126 Self : Sized ,
124127 D : AsRef < [ u8 ] > ,
@@ -132,27 +135,29 @@ impl<BS> Blockstore for Rc<BS>
132135where
133136 BS : Blockstore ,
134137{
135- fn get ( & self , k : & Cid ) -> Result < Option < Vec < u8 > > > {
138+ type Error = BS :: Error ;
139+
140+ fn get ( & self , k : & Cid ) -> Result < Option < Vec < u8 > > , Self :: Error > {
136141 ( * * self ) . get ( k)
137142 }
138143
139- fn put_keyed ( & self , k : & Cid , block : & [ u8 ] ) -> Result < ( ) > {
144+ fn put_keyed ( & self , k : & Cid , block : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
140145 ( * * self ) . put_keyed ( k, block)
141146 }
142147
143- fn has ( & self , k : & Cid ) -> Result < bool > {
148+ fn has ( & self , k : & Cid ) -> Result < bool , Self :: Error > {
144149 ( * * self ) . has ( k)
145150 }
146151
147- fn put < D > ( & self , mh_code : multihash:: Code , block : & Block < D > ) -> Result < Cid >
152+ fn put < D > ( & self , mh_code : multihash:: Code , block : & Block < D > ) -> Result < Cid , Self :: Error >
148153 where
149154 Self : Sized ,
150155 D : AsRef < [ u8 ] > ,
151156 {
152157 ( * * self ) . put ( mh_code, block)
153158 }
154159
155- fn put_many < D , I > ( & self , blocks : I ) -> Result < ( ) >
160+ fn put_many < D , I > ( & self , blocks : I ) -> Result < ( ) , Self :: Error >
156161 where
157162 Self : Sized ,
158163 D : AsRef < [ u8 ] > ,
@@ -161,7 +166,7 @@ where
161166 ( * * self ) . put_many ( blocks)
162167 }
163168
164- fn put_many_keyed < D , I > ( & self , blocks : I ) -> Result < ( ) >
169+ fn put_many_keyed < D , I > ( & self , blocks : I ) -> Result < ( ) , Self :: Error >
165170 where
166171 Self : Sized ,
167172 D : AsRef < [ u8 ] > ,
0 commit comments