solana_bytes_reader/traits.rs
1pub trait ReadBytes {
2 type Error;
3
4 /// # Safety
5 /// The caller is responsible for ensuring that the range `[self.offset..self.offset + 8]`
6 /// is within bounds of the `bytes` slice.
7 fn read_u64(&mut self) -> Result<u64, Self::Error>;
8
9 /// # Safety
10 /// The caller is responsible for ensuring that the range `[self.offset..self.offset + 8]`
11 /// is within bounds of the `bytes` slice.
12 fn read_i64(&mut self) -> Result<i64, Self::Error>;
13
14 /// # Safety
15 /// The caller is responsible for ensuring that the range `[self.offset..self.offset + 4]`
16 /// is within bounds of the `bytes` slice.
17 fn read_u32(&mut self) -> Result<u32, Self::Error>;
18
19 /// # Safety
20 /// The caller is responsible for ensuring that the range `[self.offset..self.offset + 4]`
21 /// is within bounds of the `bytes` slice.
22 fn read_i32(&mut self) -> Result<i32, Self::Error>;
23
24 /// # Safety
25 /// The caller is responsible for ensuring that the range `[self.offset..self.offset + 2]`
26 /// is within bounds of the `bytes` slice.
27 fn read_u16(&mut self) -> Result<u16, Self::Error>;
28
29 /// # Safety
30 /// The caller is responsible for ensuring that the range `[self.offset..self.offset + 2]`
31 /// is within bounds of the `bytes` slice.
32 fn read_i16(&mut self) -> Result<i16, Self::Error>;
33
34 /// # Safety
35 /// This function returns an error instead of panicking if the index is out of bounds.
36 /// Valid values are 0 (false) and 1 (true). Any other value results in an error.
37 fn read_bool(&mut self) -> Result<bool, Self::Error>;
38
39 /// # Safety
40 /// This function returns an error instead of panicking if the index is out of bounds.
41 fn read_u8(&mut self) -> Result<u8, Self::Error>;
42
43 /// # Safety
44 /// This function returns an error instead of panicking if the index is out of bounds.
45 fn read_i8(&mut self) -> Result<i8, Self::Error>;
46
47 /// Reads `const N` amount of bytes.
48 ///
49 /// # Safety
50 /// The caller is responsible for ensuring that the range `[self.offset..self.offset + N]`
51 /// is within bounds of the `bytes` slice.
52 fn read_bytes<const N: usize>(&mut self) -> Result<[u8; N], Self::Error>;
53
54 /// Increments `self.offset` by `bytes_to_skip`.
55 ///
56 /// # Safety
57 /// The caller is responsible for ensuring that the `self.offset + bytes_to_skip`
58 /// is within bounds of the `bytes` slice
59 fn skip(&mut self, bytes_to_skip: usize);
60}
61
62/// Methods defined under this trait DO NOT increment the offset.
63pub trait PeekIntoBytes {
64 type Error;
65
66 /// # Safety
67 /// The caller is responsible for ensuring that the range `[self.offset..self.offset + 8]`
68 /// is within bounds of the `bytes` slice.
69 fn peek_u64(&self) -> Result<u64, Self::Error>;
70
71 /// # Safety
72 /// The caller is responsible for ensuring that the range `[self.offset..self.offset + 8]`
73 /// is within bounds of the `bytes` slice.
74 fn peek_i64(&self) -> Result<i64, Self::Error>;
75
76 /// # Safety
77 /// The caller is responsible for ensuring that the range `[self.offset..self.offset + 4]`
78 /// is within bounds of the `bytes` slice.
79 fn peek_u32(&self) -> Result<u32, Self::Error>;
80
81 /// # Safety
82 /// The caller is responsible for ensuring that the range `[self.offset..self.offset + 4]`
83 /// is within bounds of the `bytes` slice.
84 fn peek_i32(&self) -> Result<i32, Self::Error>;
85
86 /// # Safety
87 /// The caller is responsible for ensuring that the range `[self.offset..self.offset + 2]`
88 /// is within bounds of the `bytes` slice.
89 fn peek_u16(&self) -> Result<u16, Self::Error>;
90
91 /// # Safety
92 /// The caller is responsible for ensuring that the range `[self.offset..self.offset + 2]`
93 /// is within bounds of the `bytes` slice.
94 fn peek_i16(&self) -> Result<i16, Self::Error>;
95
96 /// # Safety
97 /// This function returns an error instead of panicking if the index is out of bounds.
98 /// Valid values are 0 (false) and 1 (true). Any other value results in an error.
99 fn peek_bool(&self) -> Result<bool, Self::Error>;
100
101 /// # Safety
102 /// This function returns an error instead of panicking if the index is out of bounds.
103 fn peek_u8(&self) -> Result<u8, Self::Error>;
104
105 /// # Safety
106 /// This function returns an error instead of panicking if the index is out of bounds.
107 fn peek_i8(&self) -> Result<i8, Self::Error>;
108
109 /// Peeks into `const N` amount of bytes.
110 ///
111 /// # Safety
112 /// The caller is responsible for ensuring that the range `[self.offset..self.offset + N]`
113 /// is within bounds of the `bytes` slice.
114 fn peek_bytes<const N: usize>(&self) -> Result<[u8; N], Self::Error>;
115}