typhoon/bytes/common.rs
1/// Immutable byte buffer trait for read-only access to buffer data.
2/// Implemented by both OwnedByteBuffer (immutable, Send+Sync) and ManagedByteBuffer (mutable, Send).
3pub trait ByteBuffer: AsRef<[u8]> + Clone + Send {
4 /// Returns the length of the current view in bytes.
5 fn len(&self) -> usize;
6
7 /// Returns true if buffer length is zero.
8 #[inline]
9 fn is_empty(&self) -> bool {
10 self.len() == 0
11 }
12
13 /// Get byte at `at` index. Panics if out of bounds.
14 fn get(&self, at: usize) -> &u8;
15
16 /// Get immutable slice of entire buffer.
17 fn slice(&self) -> &[u8];
18
19 /// Get immutable slice from `start` offset to end.
20 fn slice_start(&self, start: usize) -> &[u8];
21
22 /// Get immutable slice from beginning to `end` offset.
23 fn slice_end(&self, end: usize) -> &[u8];
24
25 /// Get immutable slice from `start` to `end` offset.
26 fn slice_both(&self, start: usize, end: usize) -> &[u8];
27
28 /// Split into two immutable slices at `divide` point. Returns (left, right).
29 fn split(&self, divide: usize) -> (&[u8], &[u8]);
30}
31
32/// Mutable byte buffer trait for read-write access to buffer data.
33/// Only implemented by ManagedByteBuffer (pool-attached, mutable, Send).
34pub trait ByteBufferMut: ByteBuffer + AsMut<[u8]> {
35 /// Set byte at `at` index to `value`. Panics if out of bounds.
36 fn set(&self, at: usize, value: u8);
37
38 /// Get mutable slice of entire buffer.
39 #[allow(clippy::mut_from_ref)]
40 fn slice_mut(&self) -> &mut [u8];
41
42 /// Get mutable slice from `start` offset to end.
43 #[allow(clippy::mut_from_ref)]
44 fn slice_start_mut(&self, start: usize) -> &mut [u8];
45
46 /// Get mutable slice from beginning to `end` offset.
47 #[allow(clippy::mut_from_ref)]
48 fn slice_end_mut(&self, end: usize) -> &mut [u8];
49
50 /// Get mutable slice from `start` to `end` offset.
51 #[allow(clippy::mut_from_ref)]
52 fn slice_both_mut(&self, start: usize, end: usize) -> &mut [u8];
53
54 /// Split into two mutable slices at `divide` point. Returns (left, right).
55 #[allow(clippy::mut_from_ref)]
56 fn split_mut(&self, divide: usize) -> (&mut [u8], &mut [u8]);
57
58 /// Create view with start shifted forward by `start` bytes.
59 fn rebuffer_start(&self, start: usize) -> Self;
60
61 /// Create view with end at `end` offset from current start.
62 fn rebuffer_end(&self, end: usize) -> Self;
63
64 /// Create view with both `start` and `end` adjusted from current start.
65 fn rebuffer_both(&self, start: usize, end: usize) -> Self;
66
67 /// Expand view backward by `size` bytes (into header space).
68 fn expand_start(&self, size: usize) -> Self;
69
70 /// Expand view forward by `size` bytes (into trailer space).
71 fn expand_end(&self, size: usize) -> Self;
72
73 /// Split into two buffer views at `divide` point. Returns (left, right).
74 fn split_buf_start(&self, divide: usize) -> (Self, Self);
75
76 /// Split into two buffer views at `len(buffer) - divide` point. Returns (left, right).
77 fn split_buf_end(&self, divide: usize) -> (Self, Self);
78
79 /// Append `other` slice to end. Returns expanded view.
80 fn append(&self, other: &[u8]) -> Self;
81
82 /// Prepend `other` slice to start. Returns expanded view.
83 fn prepend(&self, other: &[u8]) -> Self;
84
85 /// Ensure buffer has at least `size` bytes, expanding or shrinking as needed.
86 fn ensure_size(&self, size: usize) -> Self;
87}