rmp/encode/
buffer.rs
1use super::RmpWrite;
4use alloc::vec::Vec;
5#[cfg(not(feature = "std"))]
6use core::fmt::{self, Display, Formatter};
7
8#[derive(Debug)]
17#[cfg(not(feature = "std"))]
18#[doc(hidden)]
19pub struct FixedBufCapacityOverflow {
20 _priv: (),
21}
22
23#[cfg(feature = "std")]
27#[doc(hidden)]
28pub type FixedBufCapacityOverflow = std::io::Error;
29
30#[cfg(not(feature = "std"))]
31impl Display for FixedBufCapacityOverflow {
32 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
33 f.write_str("Capacity overflow for fixed-size byte buffer")
36 }
37}
38#[cfg(not(feature = "std"))]
39impl crate::encode::RmpWriteErr for FixedBufCapacityOverflow {}
40
41#[cfg(not(feature = "std"))]
46impl<'a> RmpWrite for &'a mut [u8] {
47 type Error = FixedBufCapacityOverflow;
48
49 #[inline]
50 fn write_bytes(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
51 let to_write = buf.len();
52 let remaining = self.len();
53 if to_write <= remaining {
54 self[..to_write].copy_from_slice(buf);
55 unsafe {
56 *self = core::slice::from_raw_parts_mut(
58 self.as_mut_ptr().add(to_write),
59 remaining - to_write,
60 )
61 }
62 Ok(())
63 } else {
64 Err(FixedBufCapacityOverflow { _priv: () })
65 }
66 }
67}
68
69#[derive(Debug, Clone, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
79pub struct ByteBuf {
80 bytes: Vec<u8>,
81}
82impl ByteBuf {
83 #[inline]
85 #[must_use]
86 pub fn new() -> Self {
87 ByteBuf { bytes: Vec::new() }
88 }
89 #[inline]
93 #[must_use]
94 pub fn with_capacity(capacity: usize) -> Self {
95 ByteBuf {
96 bytes: Vec::with_capacity(capacity),
97 }
98 }
99 #[inline]
101 #[must_use]
102 pub fn into_vec(self) -> Vec<u8> {
103 self.bytes
104 }
105 #[inline]
107 #[must_use]
108 pub fn from_vec(bytes: Vec<u8>) -> Self {
109 ByteBuf { bytes }
110 }
111 #[inline]
113 #[must_use]
114 pub fn as_vec(&self) -> &Vec<u8> {
115 &self.bytes
116 }
117 #[inline]
119 pub fn as_mut_vec(&mut self) -> &mut Vec<u8> {
120 &mut self.bytes
121 }
122 #[inline]
124 #[must_use]
125 pub fn as_slice(&self) -> &[u8] {
126 &self.bytes
127 }
128}
129impl AsRef<[u8]> for ByteBuf {
130 fn as_ref(&self) -> &[u8] {
131 &self.bytes
132 }
133}
134impl AsRef<Vec<u8>> for ByteBuf {
135 #[inline]
136 fn as_ref(&self) -> &Vec<u8> {
137 &self.bytes
138 }
139}
140impl AsMut<Vec<u8>> for ByteBuf {
141 #[inline]
142 fn as_mut(&mut self) -> &mut Vec<u8> {
143 &mut self.bytes
144 }
145}
146impl From<ByteBuf> for Vec<u8> {
147 #[inline]
148 fn from(buf: ByteBuf) -> Self {
149 buf.bytes
150 }
151}
152impl From<Vec<u8>> for ByteBuf {
153 #[inline]
154 fn from(bytes: Vec<u8>) -> Self {
155 ByteBuf { bytes }
156 }
157}
158
159impl RmpWrite for ByteBuf {
160 type Error = core::convert::Infallible;
161
162 #[inline]
163 fn write_u8(&mut self, val: u8) -> Result<(), Self::Error> {
164 self.bytes.push(val);
165 Ok(())
166 }
167
168 #[inline]
169 fn write_bytes(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
170 self.bytes.extend_from_slice(buf);
171 Ok(())
172 }
173}
174#[cfg(not(feature = "std"))]
175impl<'a> RmpWrite for Vec<u8> {
176 type Error = core::convert::Infallible;
177
178 #[inline]
179 fn write_u8(&mut self, val: u8) -> Result<(), Self::Error> {
180 self.push(val);
181 Ok(())
182 }
183
184 #[inline]
185 fn write_bytes(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
186 self.extend_from_slice(buf);
187 Ok(())
188 }
189}