feat(nix-compat/wire/bytes/reader): expose the remaining data length
The API is a bit odd here, because we don't have a distinct type for a known-length reader. Change-Id: I4a1dd07fbed0a400004dbe4aa2095c51898ad3bd Reviewed-on: https://cl.tvl.fyi/c/depot/+/11538 Tested-by: BuildkiteCI Reviewed-by: flokli <flokli@flokli.de> Reviewed-by: Brian Olsen <me@griff.name>
This commit is contained in:
		
							parent
							
								
									5070f9eff7
								
							
						
					
					
						commit
						44bd9543a6
					
				
					 2 changed files with 31 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -89,6 +89,20 @@ where
 | 
			
		|||
            },
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Remaining data length, ie not including data already read.
 | 
			
		||||
    ///
 | 
			
		||||
    /// If the size has not been read yet, this is [None].
 | 
			
		||||
    #[allow(clippy::len_without_is_empty)] // if size is unknown, we can't answer that
 | 
			
		||||
    pub fn len(&self) -> Option<u64> {
 | 
			
		||||
        match self.state {
 | 
			
		||||
            State::Size { .. } => None,
 | 
			
		||||
            State::Body {
 | 
			
		||||
                consumed, user_len, ..
 | 
			
		||||
            } => Some(user_len - consumed),
 | 
			
		||||
            State::Trailer(ref r) => Some(r.len() as u64),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<R: AsyncRead + Unpin> AsyncRead for BytesReader<R> {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -81,6 +81,12 @@ pub(crate) fn read_trailer<R: AsyncRead + Unpin, T: Tag>(
 | 
			
		|||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<R, T: Tag> ReadTrailer<R, T> {
 | 
			
		||||
    pub fn len(&self) -> u8 {
 | 
			
		||||
        self.data_len
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<R: AsyncRead + Unpin, T: Tag> Future for ReadTrailer<R, T> {
 | 
			
		||||
    type Output = io::Result<Trailer>;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -141,6 +147,17 @@ impl<R: AsyncRead + Unpin> TrailerReader<R> {
 | 
			
		|||
    pub fn new(reader: R, data_len: u8) -> Self {
 | 
			
		||||
        Self::Reading(read_trailer(reader, data_len))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn len(&self) -> u8 {
 | 
			
		||||
        match self {
 | 
			
		||||
            TrailerReader::Reading(fut) => fut.len(),
 | 
			
		||||
            &TrailerReader::Releasing {
 | 
			
		||||
                off,
 | 
			
		||||
                data: Trailer { data_len, .. },
 | 
			
		||||
            } => data_len - off,
 | 
			
		||||
            TrailerReader::Done => 0,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<R: AsyncRead + Unpin> AsyncRead for TrailerReader<R> {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue