1
1
use std:: rc:: Rc ;
2
2
3
- use anyhow:: Result ;
4
3
use cid:: { multihash, Cid } ;
5
4
6
5
pub mod tracking;
@@ -15,26 +14,28 @@ pub use block::*;
15
14
///
16
15
/// The cgo blockstore adapter implements this trait.
17
16
pub trait Blockstore {
17
+ type Error : std:: error:: Error + std:: fmt:: Debug ;
18
+
18
19
/// 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 > ;
20
21
21
22
/// Put a block with a pre-computed cid.
22
23
///
23
24
/// If you don't yet know the CID, use put. Some blockstores will re-compute the CID internally
24
25
/// even if you provide it.
25
26
///
26
27
/// 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 > ;
28
29
29
30
/// 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 > {
31
32
Ok ( self . get ( k) ?. is_some ( ) )
32
33
}
33
34
34
35
/// Puts the block into the blockstore, computing the hash with the specified multicodec.
35
36
///
36
37
/// 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 >
38
39
where
39
40
Self : Sized ,
40
41
D : AsRef < [ u8 ] > ,
@@ -55,7 +56,7 @@ pub trait Blockstore {
55
56
/// let blocks = vec![Block::new(0x55, vec![0, 1, 2])];
56
57
/// bs.put_many(blocks.iter().map(|b| (Blake2b256, b.into()))).unwrap();
57
58
/// ```
58
- fn put_many < D , I > ( & self , blocks : I ) -> Result < ( ) >
59
+ fn put_many < D , I > ( & self , blocks : I ) -> Result < ( ) , Self :: Error >
59
60
where
60
61
Self : Sized ,
61
62
D : AsRef < [ u8 ] > ,
@@ -68,7 +69,7 @@ pub trait Blockstore {
68
69
/// Bulk-put pre-keyed blocks into the blockstore.
69
70
///
70
71
/// 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 >
72
73
where
73
74
Self : Sized ,
74
75
D : AsRef < [ u8 ] > ,
@@ -82,34 +83,36 @@ pub trait Blockstore {
82
83
}
83
84
84
85
pub trait Buffered : Blockstore {
85
- fn flush ( & self , root : & Cid ) -> Result < ( ) > ;
86
+ fn flush ( & self , root : & Cid ) -> Result < ( ) , Self :: Error > ;
86
87
}
87
88
88
89
impl < BS > Blockstore for & BS
89
90
where
90
91
BS : Blockstore ,
91
92
{
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 > {
93
96
( * self ) . get ( k)
94
97
}
95
98
96
- fn put_keyed ( & self , k : & Cid , block : & [ u8 ] ) -> Result < ( ) > {
99
+ fn put_keyed ( & self , k : & Cid , block : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
97
100
( * self ) . put_keyed ( k, block)
98
101
}
99
102
100
- fn has ( & self , k : & Cid ) -> Result < bool > {
103
+ fn has ( & self , k : & Cid ) -> Result < bool , Self :: Error > {
101
104
( * self ) . has ( k)
102
105
}
103
106
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 >
105
108
where
106
109
Self : Sized ,
107
110
D : AsRef < [ u8 ] > ,
108
111
{
109
112
( * self ) . put ( mh_code, block)
110
113
}
111
114
112
- fn put_many < D , I > ( & self , blocks : I ) -> Result < ( ) >
115
+ fn put_many < D , I > ( & self , blocks : I ) -> Result < ( ) , Self :: Error >
113
116
where
114
117
Self : Sized ,
115
118
D : AsRef < [ u8 ] > ,
@@ -118,7 +121,7 @@ where
118
121
( * self ) . put_many ( blocks)
119
122
}
120
123
121
- fn put_many_keyed < D , I > ( & self , blocks : I ) -> Result < ( ) >
124
+ fn put_many_keyed < D , I > ( & self , blocks : I ) -> Result < ( ) , Self :: Error >
122
125
where
123
126
Self : Sized ,
124
127
D : AsRef < [ u8 ] > ,
@@ -132,27 +135,29 @@ impl<BS> Blockstore for Rc<BS>
132
135
where
133
136
BS : Blockstore ,
134
137
{
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 > {
136
141
( * * self ) . get ( k)
137
142
}
138
143
139
- fn put_keyed ( & self , k : & Cid , block : & [ u8 ] ) -> Result < ( ) > {
144
+ fn put_keyed ( & self , k : & Cid , block : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
140
145
( * * self ) . put_keyed ( k, block)
141
146
}
142
147
143
- fn has ( & self , k : & Cid ) -> Result < bool > {
148
+ fn has ( & self , k : & Cid ) -> Result < bool , Self :: Error > {
144
149
( * * self ) . has ( k)
145
150
}
146
151
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 >
148
153
where
149
154
Self : Sized ,
150
155
D : AsRef < [ u8 ] > ,
151
156
{
152
157
( * * self ) . put ( mh_code, block)
153
158
}
154
159
155
- fn put_many < D , I > ( & self , blocks : I ) -> Result < ( ) >
160
+ fn put_many < D , I > ( & self , blocks : I ) -> Result < ( ) , Self :: Error >
156
161
where
157
162
Self : Sized ,
158
163
D : AsRef < [ u8 ] > ,
@@ -161,7 +166,7 @@ where
161
166
( * * self ) . put_many ( blocks)
162
167
}
163
168
164
- fn put_many_keyed < D , I > ( & self , blocks : I ) -> Result < ( ) >
169
+ fn put_many_keyed < D , I > ( & self , blocks : I ) -> Result < ( ) , Self :: Error >
165
170
where
166
171
Self : Sized ,
167
172
D : AsRef < [ u8 ] > ,
0 commit comments