@@ -17,7 +17,8 @@ use std::{
17
17
io,
18
18
marker:: PhantomData ,
19
19
sync:: Arc ,
20
- time:: Instant , u64,
20
+ time:: Instant ,
21
+ u64,
21
22
} ;
22
23
23
24
use crate :: {
@@ -89,26 +90,32 @@ pub trait BitfieldSubscription: std::fmt::Debug + Send + 'static {
89
90
pub type BoxedBitfieldSubscription = Box < dyn BitfieldSubscription > ;
90
91
91
92
/// Events from observing a local bitfield
92
- #[ derive( Debug , PartialEq , Eq ) ]
93
+ #[ derive( Debug , PartialEq , Eq , derive_more :: From ) ]
93
94
pub enum BitfieldEvent {
94
95
/// The full state of the bitfield
95
- State {
96
- /// The entire bitfield
97
- ranges : ChunkRanges ,
98
- /// The most precise known size of the blob.
99
- ///
100
- /// If I know nothing about the blob, this is u64::MAX.
101
- size : u64 ,
102
- } ,
96
+ State ( BitfieldState ) ,
103
97
/// An update to the bitfield
104
- Update {
105
- /// The ranges that were added
106
- added : ChunkRanges ,
107
- /// The ranges that were removed
108
- removed : ChunkRanges ,
109
- /// A refinement of the size of the blob.
110
- size : u64 ,
111
- } ,
98
+ Update ( BitfieldUpdate ) ,
99
+ }
100
+
101
+ /// An update to a bitfield
102
+ #[ derive( Debug , PartialEq , Eq ) ]
103
+ pub struct BitfieldUpdate {
104
+ /// The ranges that were added
105
+ pub added : ChunkRanges ,
106
+ /// The ranges that were removed
107
+ pub removed : ChunkRanges ,
108
+ /// The total size of the bitfield in bytes
109
+ pub size : u64 ,
110
+ }
111
+
112
+ /// The state of a bitfield
113
+ #[ derive( Debug , PartialEq , Eq ) ]
114
+ pub struct BitfieldState {
115
+ /// The ranges that are set
116
+ pub ranges : ChunkRanges ,
117
+ /// The total size of the bitfield in bytes
118
+ pub size : u64 ,
112
119
}
113
120
114
121
/// A download request
@@ -325,8 +332,14 @@ impl BitfieldSubscription for TestBitfieldSubscription {
325
332
}
326
333
} ;
327
334
Box :: pin (
328
- futures_lite:: stream:: once ( BitfieldEvent :: State { ranges, size : 1024 * 1024 * 1024 * 1024 * 1024 } )
329
- . chain ( futures_lite:: stream:: pending ( ) ) ,
335
+ futures_lite:: stream:: once (
336
+ BitfieldState {
337
+ ranges,
338
+ size : 1024 * 1024 * 1024 * 1024 * 1024 ,
339
+ }
340
+ . into ( ) ,
341
+ )
342
+ . chain ( futures_lite:: stream:: pending ( ) ) ,
330
343
)
331
344
}
332
345
}
@@ -353,9 +366,13 @@ impl<S> SimpleBitfieldSubscription<S> {
353
366
async fn get_valid_ranges_local < S : Store > ( hash : & Hash , store : S ) -> anyhow:: Result < BitfieldEvent > {
354
367
if let Some ( entry) = store. get_mut ( hash) . await ? {
355
368
let ( ranges, size) = crate :: get:: db:: valid_ranges_and_size :: < S > ( & entry) . await ?;
356
- Ok ( BitfieldEvent :: State { ranges, size } )
369
+ Ok ( BitfieldState { ranges, size } . into ( ) )
357
370
} else {
358
- Ok ( BitfieldEvent :: State { ranges : ChunkRanges :: empty ( ) , size : u64:: MAX } )
371
+ Ok ( BitfieldState {
372
+ ranges : ChunkRanges :: empty ( ) ,
373
+ size : u64:: MAX ,
374
+ }
375
+ . into ( ) )
359
376
}
360
377
}
361
378
@@ -368,7 +385,7 @@ async fn get_valid_ranges_remote(
368
385
let ( size, _) = crate :: get:: request:: get_verified_size ( & conn, hash) . await ?;
369
386
let chunks = ( size + 1023 ) / 1024 ;
370
387
let ranges = ChunkRanges :: from ( ChunkNum ( 0 ) ..ChunkNum ( chunks) ) ;
371
- Ok ( BitfieldEvent :: State { ranges, size } )
388
+ Ok ( BitfieldState { ranges, size } . into ( ) )
372
389
}
373
390
374
391
impl < S : Store > BitfieldSubscription for SimpleBitfieldSubscription < S > {
@@ -406,10 +423,11 @@ impl<S: Store> BitfieldSubscription for SimpleBitfieldSubscription<S> {
406
423
async move {
407
424
let event = match recv. await {
408
425
Ok ( ev) => ev,
409
- Err ( _) => BitfieldEvent :: State {
426
+ Err ( _) => BitfieldState {
410
427
ranges : ChunkRanges :: empty ( ) ,
411
428
size : u64:: MAX ,
412
- } ,
429
+ }
430
+ . into ( ) ,
413
431
} ;
414
432
event
415
433
}
0 commit comments