@@ -335,9 +335,10 @@ impl TryFrom<SegmentView> for IterSegment {
335335 type Error = io:: Error ;
336336
337337 fn try_from ( view : SegmentView ) -> Result < Self , Self :: Error > {
338+ let segment = view. offset ( ) ;
338339 File :: try_from ( view)
339340 . map ( BufReader :: new)
340- . map ( |file| IterSegment { file } )
341+ . map ( |file| IterSegment { segment , read : 0 , file } )
341342 }
342343}
343344
@@ -354,10 +355,25 @@ impl TryFrom<SegmentView> for File {
354355/// Created by [`SegmentView::try_iter`].
355356#[ must_use = "iterators are lazy and do nothing unless consumed" ]
356357pub struct IterSegment {
358+ segment : u64 ,
359+ read : u64 ,
357360 file : BufReader < File > ,
358361}
359362
360363impl IterSegment {
364+ /// Return the id of the segment being iterated over.
365+ ///
366+ /// The segment id is the `min_offset`, but that information is not
367+ /// meaningful here -- the value returned should be treated as opaque.
368+ pub fn segment ( & self ) -> u64 {
369+ self . segment
370+ }
371+
372+ /// Return the number of bytes read from the segment file so far.
373+ pub fn bytes_read ( & self ) -> u64 {
374+ self . read
375+ }
376+
361377 fn read_exact_or_none ( & mut self , buf : & mut [ u8 ] ) -> Option < io:: Result < ( ) > > {
362378 match self . file . read_exact ( buf) {
363379 Err ( e) if e. kind ( ) == io:: ErrorKind :: UnexpectedEof => None ,
@@ -375,12 +391,14 @@ impl Iterator for IterSegment {
375391 if let Err ( e) = self . read_exact_or_none ( & mut buf) ? {
376392 return Some ( Err ( e) ) ;
377393 }
394+ self . read += HEADER_SIZE as u64 ;
378395
379396 let message_len = u32:: from_le_bytes ( buf) ;
380397 let mut buf = vec ! [ 0 ; message_len as usize ] ;
381398 if let Err ( e) = self . read_exact_or_none ( & mut buf) ? {
382399 return Some ( Err ( e) ) ;
383400 }
401+ self . read += message_len as u64 ;
384402
385403 Some ( Ok ( buf) )
386404 }
0 commit comments