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