Skip to main content

ax_io/iobuf/
mod.rs

1mod ext;
2mod impls;
3
4pub use self::ext::{IoBufExt, IoBufMutExt};
5
6/// A trait for byte buffers that can be used as a source of bytes to read.
7///
8/// This is an optional extension to [`Read`](crate::Read). A reader may not have a deterministic
9/// length, but an `IoBuf` can report how many bytes are remaining to be read.
10pub trait IoBuf {
11    /// Returns the number of bytes between the current position and the end of the buffer.
12    fn remaining(&self) -> usize;
13
14    /// Returns `true` if there are no remaining bytes in the buffer.
15    #[inline]
16    fn is_empty(&self) -> bool {
17        self.remaining() == 0
18    }
19}
20
21/// A trait for byte buffers that can be used as a destination for bytes to write.
22///
23/// This is an optional extension to [`Write`](crate::Write). A writer may not have a deterministic
24/// length, but an `IoBufMut` can report how much space is remaining to be written.
25pub trait IoBufMut {
26    /// Returns the number of bytes that can be written from the current position until the end of
27    /// the buffer is reached.
28    fn remaining_mut(&self) -> usize;
29
30    /// Returns `true` if there is no remaining space in the buffer.
31    #[inline]
32    fn is_full(&self) -> bool {
33        self.remaining_mut() == 0
34    }
35}