@@ -121,7 +121,7 @@ where
121
121
pub mod outboard_with_progress {
122
122
use std:: {
123
123
future:: Future ,
124
- io:: { self , BufReader , Read } ,
124
+ io:: { self , BufReader , Read } , sync :: mpsc ,
125
125
} ;
126
126
127
127
use bao_tree:: {
@@ -135,40 +135,85 @@ pub mod outboard_with_progress {
135
135
use blake3:: guts:: parent_cv;
136
136
use smallvec:: SmallVec ;
137
137
138
- pub trait Progress {
138
+ pub trait Progress < Item > {
139
139
type Error ;
140
- fn progress (
140
+ fn send (
141
141
& mut self ,
142
- offset : ChunkNum ,
142
+ value : Item ,
143
143
) -> impl Future < Output = std:: result:: Result < ( ) , Self :: Error > > ;
144
+
145
+ fn with_map < F , U > (
146
+ self ,
147
+ f : F ,
148
+ ) -> WithMap < Self , F >
149
+ where
150
+ Self : Sized ,
151
+ F : Fn ( Item ) -> U + Send + ' static ,
152
+ {
153
+ WithMap {
154
+ inner : self ,
155
+ f,
156
+ }
157
+ }
158
+ }
159
+
160
+ impl < T > Progress < T > for tokio:: sync:: mpsc:: Sender < T > {
161
+ type Error = tokio:: sync:: mpsc:: error:: SendError < T > ;
162
+
163
+ async fn send (
164
+ & mut self ,
165
+ value : T ,
166
+ ) -> std:: result:: Result < ( ) , Self :: Error > {
167
+ tokio:: sync:: mpsc:: Sender :: send ( self , value) . await
168
+ }
169
+ }
170
+
171
+ pub struct WithMap < P , F > {
172
+ inner : P ,
173
+ f : F ,
174
+ }
175
+
176
+ impl < P , F , T , U > Progress < T > for WithMap < P , F >
177
+ where
178
+ P : Progress < U > ,
179
+ F : Fn ( T ) -> U + Send + ' static ,
180
+ {
181
+ type Error = P :: Error ;
182
+
183
+ async fn send (
184
+ & mut self ,
185
+ value : T ,
186
+ ) -> std:: result:: Result < ( ) , Self :: Error > {
187
+ self . inner . send ( ( self . f ) ( value) ) . await
188
+ }
144
189
}
145
190
146
191
pub struct NoProgress ;
147
192
148
193
impl < Item > n0_future:: Sink < Item > for NoProgress {
149
- type Error = anyhow :: Error ;
194
+ type Error = io :: Error ;
150
195
151
196
fn poll_ready ( self : std:: pin:: Pin < & mut Self > , cx : & mut std:: task:: Context < ' _ > ) -> std:: task:: Poll < Result < ( ) , Self :: Error > > {
152
- std:: task:: Poll :: Ready ( anyhow :: Result :: Ok ( ( ) ) )
197
+ std:: task:: Poll :: Ready ( io :: Result :: Ok ( ( ) ) )
153
198
}
154
199
155
200
fn start_send ( self : std:: pin:: Pin < & mut Self > , item : Item ) -> Result < ( ) , Self :: Error > {
156
- anyhow :: Result :: Ok ( ( ) )
201
+ io :: Result :: Ok ( ( ) )
157
202
}
158
203
159
204
fn poll_flush ( self : std:: pin:: Pin < & mut Self > , cx : & mut std:: task:: Context < ' _ > ) -> std:: task:: Poll < Result < ( ) , Self :: Error > > {
160
- std:: task:: Poll :: Ready ( anyhow :: Result :: Ok ( ( ) ) )
205
+ std:: task:: Poll :: Ready ( io :: Result :: Ok ( ( ) ) )
161
206
}
162
207
163
208
fn poll_close ( self : std:: pin:: Pin < & mut Self > , cx : & mut std:: task:: Context < ' _ > ) -> std:: task:: Poll < Result < ( ) , Self :: Error > > {
164
- std:: task:: Poll :: Ready ( anyhow :: Result :: Ok ( ( ) ) )
209
+ std:: task:: Poll :: Ready ( io :: Result :: Ok ( ( ) ) )
165
210
}
166
211
}
167
212
168
- impl Progress for NoProgress {
213
+ impl < T > Progress < T > for NoProgress {
169
214
type Error = io:: Error ;
170
215
171
- async fn progress ( & mut self , _offset : ChunkNum ) -> std:: result:: Result < ( ) , Self :: Error > {
216
+ async fn send ( & mut self , _offset : T ) -> std:: result:: Result < ( ) , Self :: Error > {
172
217
io:: Result :: Ok ( ( ) )
173
218
}
174
219
}
@@ -181,7 +226,7 @@ pub mod outboard_with_progress {
181
226
where
182
227
W : WriteAt ,
183
228
R : Read ,
184
- P : Progress ,
229
+ P : Progress < ChunkNum > ,
185
230
{
186
231
// wrap the reader in a buffered reader, so we read in large chunks
187
232
// this reduces the number of io ops
@@ -203,7 +248,7 @@ pub mod outboard_with_progress {
203
248
) -> io:: Result < std:: result:: Result < ( ) , P :: Error > >
204
249
where
205
250
W : WriteAt ,
206
- P : Progress ,
251
+ P : Progress < ChunkNum > ,
207
252
{
208
253
// do not allocate for small trees
209
254
let mut stack = SmallVec :: < [ blake3:: Hash ; 10 ] > :: new ( ) ;
@@ -223,7 +268,7 @@ pub mod outboard_with_progress {
223
268
start_chunk,
224
269
..
225
270
} => {
226
- if let Err ( err) = progress. progress ( start_chunk) . await {
271
+ if let Err ( err) = progress. send ( start_chunk) . await {
227
272
return Ok ( Err ( err) ) ;
228
273
}
229
274
let buf = & mut buffer[ ..size] ;
0 commit comments