pub fn flush_to<W>(&mut self, sink: &mut W) -> Result<usize>
    where
        W: Write,
    {
        // Nothing to do, if this buffer holds no data.
        if self.is_empty() {
            return Ok(0);
        }

        // Buffer tail, as an offset into the buffer data slice.
        let tail_ofs = self.tail.0 as usize % Self::SIZE;

        // Flushing the buffer can take either one or two writes:
        // - one write, if the tail doesn't need to wrap around to reach the head; or
        // - two writes, if the tail would wrap around: tail to slice end, then slice end to
        //   head.

        // First write length: the lesser of tail to slice end, or tail to head.
        let len_to_write = std::cmp::min(Self::SIZE - tail_ofs, self.len());

        // It's safe to unwrap here, since we've already checked if the buffer was empty.
        let data = self.data.as_ref().unwrap();

        // Issue the first write and absorb any `WouldBlock` error (we can just try again
        // later).
        let written = sink
            .write(&data[tail_ofs..(tail_ofs + len_to_write)])
            .map_err(Error::TxBufFlush)?;

        // Move the buffer tail ahead by the amount (of bytes) we were able to flush out.
        self.tail += Wrapping(written as u32);

        // If we weren't able to flush out as much as we tried, there's no point in attempting
        // our second write.
        if written < len_to_write {
            return Ok(written);
        }

        // Attempt our second write. This will return immediately if a second write isn't
        // needed, since checking for an empty buffer is the first thing we do in this
        // function.
        //
        // Interesting corner case: if we've already written some data in the first pass,
        // and then the second write fails, we will consider the flush action a success
        // and return the number of bytes written in the first pass.
        Ok(written + self.flush_to(sink).unwrap_or(0))
    }
/// Flush the contents of the ring-buffer to a writable stream.
///
/// Return the number of bytes that have been transferred out of the ring-buffer and into
/// the writable stream.
Flush the contents of the ring-buffer to a writable stream.
Return the number of bytes that have been transferred out of the ring-buffer and into
the writable stream.
Flush the contents of the ring - buffer to a writable stream . Return the number of bytes that have been transferred out of the ring - buffer and into the writable stream .
pub fn flush_to<W>(&mut self, sink: &mut W) -> Result<usize>
    where
        W: Write,
    {
        // Nothing to do, if this buffer holds no data.
        if self.is_empty() {
            return Ok(0);
        }
        // Buffer tail, as an offset into the buffer data slice.
        let tail_ofs = self.tail.0 as usize % Self::SIZE;
        // Flushing the buffer can take either one or two writes:
        // - one write, if the tail doesn't need to wrap around to reach the head; or
        // - two writes, if the tail would wrap around: tail to slice end, then slice end to
        //   head.
        // First write length: the lesser of tail to slice end, or tail to head.
        let len_to_write = std::cmp::min(Self::SIZE - tail_ofs, self.len());
        // It's safe to unwrap here, since we've already checked if the buffer was empty.
        let data = self.data.as_ref().unwrap();
        // Issue the first write and absorb any `WouldBlock` error (we can just try again
        // later).
        let written = sink
            .write(&data[tail_ofs..(tail_ofs + len_to_write)])
            .map_err(Error::TxBufFlush)?;
        // Move the buffer tail ahead by the amount (of bytes) we were able to flush out.
        self.tail += Wrapping(written as u32);
        // If we weren't able to flush out as much as we tried, there's no point in attempting
        // our second write.
        if written < len_to_write {
            return Ok(written);
        }
        // Attempt our second write. This will return immediately if a second write isn't
        // needed, since checking for an empty buffer is the first thing we do in this
        // function.
        //
        // Interesting corner case: if we've already written some data in the first pass,
        // and then the second write fails, we will consider the flush action a success
        // and return the number of bytes written in the first pass.
        Ok(written + self.flush_to(sink).unwrap_or(0))
    }
pub fn flush_to < W > ( & mut self , sink : & mut W ) -> Result < usize > where W : Write , { // Nothing to do, if this buffer holds no data. if self . is_empty ( ) { return Ok ( 0 ) ; } // Buffer tail, as an offset into the buffer data slice. let tail_ofs = self . tail . 0 as usize % Self :: SIZE ; // Flushing the buffer can take either one or two writes: // - one write, if the tail doesn't need to wrap around to reach the head; or // - two writes, if the tail would wrap around: tail to slice end, then slice end to //   head. // First write length: the lesser of tail to slice end, or tail to head. let len_to_write = std :: cmp :: min ( Self :: SIZE - tail_ofs , self . len ( ) ) ; // It's safe to unwrap here, since we've already checked if the buffer was empty. let data = self . data . as_ref ( ) . unwrap ( ) ; // Issue the first write and absorb any `WouldBlock` error (we can just try again // later). let written = sink . write ( & data [ tail_ofs .. ( tail_ofs + len_to_write ) ] ) . map_err ( Error :: TxBufFlush ) ? ; // Move the buffer tail ahead by the amount (of bytes) we were able to flush out. self . tail += Wrapping ( written as u32 ) ; // If we weren't able to flush out as much as we tried, there's no point in attempting // our second write. if written < len_to_write { return Ok ( written ) ; } // Attempt our second write. This will return immediately if a second write isn't // needed, since checking for an empty buffer is the first thing we do in this // function. // // Interesting corner case: if we've already written some data in the first pass, // and then the second write fails, we will consider the flush action a success // and return the number of bytes written in the first pass. Ok ( written + self . flush_to ( sink ) . unwrap_or ( 0 ) ) }
Flush the contents of the ring-buffer to a writable stream.
Flush the contents of the ring - buffer to a writable stream .
&mut self
sink
&mut W
&mut self
sink
&mut W