@@ -17,7 +17,8 @@ use std::{
1717 io,
1818 marker:: PhantomData ,
1919 sync:: Arc ,
20- time:: Instant , u64,
20+ time:: Instant ,
21+ u64,
2122} ;
2223
2324use crate :: {
@@ -89,26 +90,32 @@ pub trait BitfieldSubscription: std::fmt::Debug + Send + 'static {
8990pub type BoxedBitfieldSubscription = Box < dyn BitfieldSubscription > ;
9091
9192/// Events from observing a local bitfield
92- #[ derive( Debug , PartialEq , Eq ) ]
93+ #[ derive( Debug , PartialEq , Eq , derive_more :: From ) ]
9394pub enum BitfieldEvent {
9495 /// 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 ) ,
10397 /// 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 ,
112119}
113120
114121/// A download request
@@ -325,8 +332,14 @@ impl BitfieldSubscription for TestBitfieldSubscription {
325332 }
326333 } ;
327334 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 ( ) ) ,
330343 )
331344 }
332345}
@@ -353,9 +366,13 @@ impl<S> SimpleBitfieldSubscription<S> {
353366async fn get_valid_ranges_local < S : Store > ( hash : & Hash , store : S ) -> anyhow:: Result < BitfieldEvent > {
354367 if let Some ( entry) = store. get_mut ( hash) . await ? {
355368 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 ( ) )
357370 } 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 ( ) )
359376 }
360377}
361378
@@ -368,7 +385,7 @@ async fn get_valid_ranges_remote(
368385 let ( size, _) = crate :: get:: request:: get_verified_size ( & conn, hash) . await ?;
369386 let chunks = ( size + 1023 ) / 1024 ;
370387 let ranges = ChunkRanges :: from ( ChunkNum ( 0 ) ..ChunkNum ( chunks) ) ;
371- Ok ( BitfieldEvent :: State { ranges, size } )
388+ Ok ( BitfieldState { ranges, size } . into ( ) )
372389}
373390
374391impl < S : Store > BitfieldSubscription for SimpleBitfieldSubscription < S > {
@@ -406,10 +423,11 @@ impl<S: Store> BitfieldSubscription for SimpleBitfieldSubscription<S> {
406423 async move {
407424 let event = match recv. await {
408425 Ok ( ev) => ev,
409- Err ( _) => BitfieldEvent :: State {
426+ Err ( _) => BitfieldState {
410427 ranges : ChunkRanges :: empty ( ) ,
411428 size : u64:: MAX ,
412- } ,
429+ }
430+ . into ( ) ,
413431 } ;
414432 event
415433 }
0 commit comments