firedbg_rust_debugger/
bytes.rs1use sea_streamer::Buffer;
2use std::str::Utf8Error;
3
4#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5#[repr(transparent)]
6pub struct Bytes(Vec<u8>);
10
11impl Default for Bytes {
12 fn default() -> Self {
13 Self::new()
14 }
15}
16
17impl Bytes {
18 pub fn new() -> Self {
19 Self(Default::default())
20 }
21
22 pub fn len(&self) -> usize {
23 self.0.len()
24 }
25
26 pub fn get(&self, i: usize) -> u8 {
27 self.0[i]
28 }
29
30 pub fn slice(&self, p: usize, q: usize) -> &[u8] {
31 &self.0[p..q]
32 }
33
34 pub fn push_byte(&mut self, byte: u8) {
35 self.0.push(byte);
36 }
37
38 pub fn push_bytes(&mut self, mut bytes: Self) {
39 self.0.append(&mut bytes.0);
40 }
41
42 pub fn push_slice(&mut self, bytes: &[u8]) {
43 self.0.extend_from_slice(bytes);
44 }
45
46 pub fn push_string(&mut self, string: String) {
47 self.0.append(&mut string.into_bytes());
48 }
49
50 pub fn push_str(&mut self, str: &str) {
51 self.0.extend_from_slice(str.as_bytes());
52 }
53
54 pub fn clear(&mut self) {
55 self.0.clear();
56 }
57
58 pub fn as_bytes(&self) -> &[u8] {
59 &self.0
60 }
61
62 pub fn into_bytes(self) -> Vec<u8> {
63 self.0
64 }
65}
66
67impl From<Vec<u8>> for Bytes {
68 fn from(bytes: Vec<u8>) -> Self {
69 Self(bytes)
70 }
71}
72
73macro_rules! impl_into_bytes {
74 ($ty: ty) => {
75 impl From<$ty> for Bytes {
76 fn from(v: $ty) -> Self {
77 v.to_ne_bytes().to_vec().into()
78 }
79 }
80 };
81}
82
83impl_into_bytes!(u8);
84impl_into_bytes!(i8);
85impl_into_bytes!(u16);
86impl_into_bytes!(i16);
87impl_into_bytes!(u32);
88impl_into_bytes!(i32);
89impl_into_bytes!(u64);
90impl_into_bytes!(i64);
91impl_into_bytes!(u128);
92impl_into_bytes!(i128);
93impl_into_bytes!(usize);
94impl_into_bytes!(isize);
95impl_into_bytes!(f32);
96impl_into_bytes!(f64);
97
98impl Buffer for Bytes {
99 fn size(&self) -> usize {
100 self.0.len()
101 }
102
103 fn into_bytes(self) -> Vec<u8> {
104 self.0
105 }
106
107 fn as_bytes(&self) -> &[u8] {
108 &self.0
109 }
110
111 fn as_str(&self) -> Result<&str, Utf8Error> {
112 std::str::from_utf8(&self.0)
113 }
114}