saluki_io/buf/
mod.rs

1use std::collections::VecDeque;
2
3use bytes::{Buf, BufMut, Bytes};
4
5mod vec;
6pub use self::vec::{BytesBuffer, FixedSizeVec};
7
8/// An I/O buffer that can be read from.
9pub trait ReadIoBuffer: Buf {
10    fn capacity(&self) -> usize;
11}
12
13impl ReadIoBuffer for &[u8] {
14    fn capacity(&self) -> usize {
15        self.len()
16    }
17}
18
19impl ReadIoBuffer for Bytes {
20    fn capacity(&self) -> usize {
21        self.len()
22    }
23}
24
25impl ReadIoBuffer for VecDeque<u8> {
26    fn capacity(&self) -> usize {
27        self.capacity()
28    }
29}
30
31/// An I/O buffer that can be written to.
32pub trait WriteIoBuffer: BufMut {}
33
34impl<T> WriteIoBuffer for T where T: BufMut {}
35
36/// An I/O buffer that can be read from and written to.
37pub trait ReadWriteIoBuffer: ReadIoBuffer + WriteIoBuffer {}
38
39impl<T> ReadWriteIoBuffer for T where T: ReadIoBuffer + WriteIoBuffer {}
40
41/// An I/O buffer that can be "collapsed" to regain unused capacity.
42pub trait CollapsibleReadWriteIoBuffer: ReadWriteIoBuffer {
43    /// Collapses the buffer, shifting any remaining data to the beginning of the buffer.
44    ///
45    /// This allows for more efficient use of the buffer, as remaining chunks that have yet to be read can be shifted to
46    /// the left to allow for subsequent reads to take place in the remaining capacity. This is particularly useful for
47    /// dealing with framed protocols, where multiple payloads may be present in a single buffer, and partial reads need
48    /// to be completed in order to extract the next frame.
49    fn collapse(&mut self);
50}
51
52/// A buffer that can be cleared.
53pub trait ClearableIoBuffer {
54    /// Clears the buffer, setting it back to its initial state.
55    fn clear(&mut self);
56}