fn consume<F>(&mut self, count: usize, f: F) -> io::Result<usize>
    where
        F: FnOnce(&[VolatileSlice]) -> io::Result<usize>,
    {
        let mut buflen = 0;
        let mut bufs = Vec::with_capacity(self.buffers.len());
        for &buf in &self.buffers {
            if buflen >= count {
                break;
            }

            bufs.push(buf);

            let rem = count - buflen;
            if rem < buf.len() {
                buflen += rem;
            } else {
                buflen += buf.len() as usize;
            }
        }

        if bufs.is_empty() {
            return Ok(0);
        }

        let bytes_consumed = f(&*bufs)?;

        // This can happen if a driver tricks a device into reading/writing more data than
        // fits in a `usize`.
        let total_bytes_consumed =
            self.bytes_consumed
                .checked_add(bytes_consumed)
                .ok_or_else(|| {
                    io::Error::new(io::ErrorKind::InvalidData, Error::DescriptorChainOverflow)
                })?;

        let mut rem = bytes_consumed;
        while let Some(buf) = self.buffers.pop_front() {
            if rem < buf.len() {
                // Split the slice and push the remainder back into the buffer list. Safe because we
                // know that `rem` is not out of bounds due to the check and we checked the bounds
                // on `buf` when we added it to the buffer list.
                self.buffers.push_front(buf.offset(rem).unwrap());
                break;
            }

            // No need for checked math because we know that `buf.size() <= rem`.
            rem -= buf.len();
        }

        self.bytes_consumed = total_bytes_consumed;

        Ok(bytes_consumed)
    }
/// Consumes at most `count` bytes from the `DescriptorChain`. Callers must provide a function
/// that takes a `&[VolatileSlice]` and returns the total number of bytes consumed. This
/// function guarantees that the combined length of all the slices in the `&[VolatileSlice]` is
/// less than or equal to `count`.
///
/// # Errors
///
/// If the provided function returns any error then no bytes are consumed from the buffer and
/// the error is returned to the caller.
Consumes at most `count` bytes from the `DescriptorChain`. Callers must provide a function
that takes a `&[VolatileSlice]` and returns the total number of bytes consumed. This
function guarantees that the combined length of all the slices in the `&[VolatileSlice]` is
less than or equal to `count`.
Consumes at most ` count ` bytes from the ` DescriptorChain ` . Callers must provide a function that takes a ` & [ VolatileSlice ] ` and returns the total number of bytes consumed . This function guarantees that the combined length of all the slices in the ` & [ VolatileSlice ] ` is less than or equal to ` count ` .
fn consume<F>(&mut self, count: usize, f: F) -> io::Result<usize>
    where
        F: FnOnce(&[VolatileSlice]) -> io::Result<usize>,
    {
        let mut buflen = 0;
        let mut bufs = Vec::with_capacity(self.buffers.len());
        for &buf in &self.buffers {
            if buflen >= count {
                break;
            }
            bufs.push(buf);
            let rem = count - buflen;
            if rem < buf.len() {
                buflen += rem;
            } else {
                buflen += buf.len() as usize;
            }
        }
        if bufs.is_empty() {
            return Ok(0);
        }
        let bytes_consumed = f(&*bufs)?;
        // This can happen if a driver tricks a device into reading/writing more data than
        // fits in a `usize`.
        let total_bytes_consumed =
            self.bytes_consumed
                .checked_add(bytes_consumed)
                .ok_or_else(|| {
                    io::Error::new(io::ErrorKind::InvalidData, Error::DescriptorChainOverflow)
                })?;
        let mut rem = bytes_consumed;
        while let Some(buf) = self.buffers.pop_front() {
            if rem < buf.len() {
                // Split the slice and push the remainder back into the buffer list. Safe because we
                // know that `rem` is not out of bounds due to the check and we checked the bounds
                // on `buf` when we added it to the buffer list.
                self.buffers.push_front(buf.offset(rem).unwrap());
                break;
            }
            // No need for checked math because we know that `buf.size() <= rem`.
            rem -= buf.len();
        }
        self.bytes_consumed = total_bytes_consumed;
        Ok(bytes_consumed)
    }
fn consume < F > ( & mut self , count : usize , f : F ) -> io :: Result < usize > where F : FnOnce ( & [ VolatileSlice ] ) -> io :: Result < usize > , { let mut buflen = 0 ; let mut bufs = Vec :: with_capacity ( self . buffers . len ( ) ) ; for & buf in & self . buffers { if buflen >= count { break ; } bufs . push ( buf ) ; let rem = count - buflen ; if rem < buf . len ( ) { buflen += rem ; } else { buflen += buf . len ( ) as usize ; } } if bufs . is_empty ( ) { return Ok ( 0 ) ; } let bytes_consumed = f ( & * bufs ) ? ; // This can happen if a driver tricks a device into reading/writing more data than // fits in a `usize`. let total_bytes_consumed = self . bytes_consumed . checked_add ( bytes_consumed ) . ok_or_else ( | | { io :: Error :: new ( io :: ErrorKind :: InvalidData , Error :: DescriptorChainOverflow ) } ) ? ; let mut rem = bytes_consumed ; while let Some ( buf ) = self . buffers . pop_front ( ) { if rem < buf . len ( ) { // Split the slice and push the remainder back into the buffer list. Safe because we // know that `rem` is not out of bounds due to the check and we checked the bounds // on `buf` when we added it to the buffer list. self . buffers . push_front ( buf . offset ( rem ) . unwrap ( ) ) ; break ; } // No need for checked math because we know that `buf.size() <= rem`. rem -= buf . len ( ) ; } self . bytes_consumed = total_bytes_consumed ; Ok ( bytes_consumed ) }
Consumes at most `count` bytes from the `DescriptorChain`.
Consumes at most ` count ` bytes from the ` DescriptorChain ` .
&mut self
count
usize
f
F
&mut self
count
usize
f
F
errors
If the provided function returns any error then no bytes are consumed from the buffer and
the error is returned to the caller.
If the provided function returns any error then no bytes are consumed from the buffer and the error is returned to the caller .