/// Scans up to `len` bytes in a file and sends data to the given consumer.
/// Returns the number of bytes successfully read.
Scans up to `len` bytes in a file and sends data to the given consumer.
Returns the number of bytes successfully read.
Scans up to ` len ` bytes in a file and sends data to the given consumer . Returns the number of bytes successfully read .
fn scan<F: FnMut(&[u8])>(
    stream: &mut impl Read,
    len: FileLen,
    buf_len: usize,
    mut consumer: F,
) -> io::Result<u64> {
    BUF.with(|buf| {
        let mut buf = buf.borrow_mut();
        let new_len = max(buf.len(), buf_len);
        buf.resize(new_len, 0);
        let mut read: u64 = 0;
        let len = len.into();
        while read < len {
            let remaining = len - read;
            let to_read = min(remaining, buf.len() as u64) as usize;
            let buf = &mut buf[..to_read];
            match stream.read(buf) {
                Ok(0) => break,
                Ok(actual_read) => {
                    read += actual_read as u64;
                    (consumer)(&buf[..actual_read]);
                }
                Err(e) => {
                    return Err(e);
                }
            }
        }
        Ok(read)
    })
}
fn scan < F : FnMut ( & [ u8 ] ) > ( stream : & mut impl Read , len : FileLen , buf_len : usize , mut consumer : F , ) -> io :: Result < u64 > { BUF . with ( | buf | { let mut buf = buf . borrow_mut ( ) ; let new_len = max ( buf . len ( ) , buf_len ) ; buf . resize ( new_len , 0 ) ; let mut read : u64 = 0 ; let len = len . into ( ) ; while read < len { let remaining = len - read ; let to_read = min ( remaining , buf . len ( ) as u64 ) as usize ; let buf = & mut buf [ .. to_read ] ; match stream . read ( buf ) { Ok ( 0 ) => break , Ok ( actual_read ) => { read += actual_read as u64 ; ( consumer ) ( & buf [ .. actual_read ] ) ; } Err ( e ) => { return Err ( e ) ; } } } Ok ( read ) } ) }
Scans up to `len` bytes in a file and sends data to the given consumer.
Scans up to ` len ` bytes in a file and sends data to the given consumer .
stream
&mut impl Read
len
FileLen
buf_len
usize
consumer
F