Debian Codegraph @

src/dec.rs

raw symbols SCIP
1 /// The [`CobsDecoder`] type is used to decode a stream of bytes to a
2 /// given mutable output slice. This is often useful when heap data
3 /// structures are not available, or when not all message bytes are
4 /// received at a single point in time.
5 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
6 pub struct CobsDecoderrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#<'a> {
7 /// Destination slice for decoded message
8 destrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#dest.: &'a mut [u8],
9
10 /// Index of next byte to write in `dest`
11 dest_idxrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#dest_idx.: usize,
12
13 /// Decoder state as an enum
14 staterust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#state.: DecoderStaterust-analyzer cargo cobs 0.2.3 dec/DecoderState#,
15 }
16
17 /// The [`DecoderState`] is used to track the current state of a
18 /// streaming decoder. This struct does not contain the output buffer
19 /// (or a reference to one), and can be used when streaming the decoded
20 /// output to a custom data type.
21 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
22 pub enum DecoderStaterust-analyzer cargo cobs 0.2.3 dec/DecoderState# {
23 /// State machine has not received any non-zero bytes
24 Idlerust-analyzer cargo cobs 0.2.3 dec/DecoderState#Idle#,
25
26 /// 1-254 bytes, can be header or 00
27 Grabrust-analyzer cargo cobs 0.2.3 dec/DecoderState#Grab#(u8),
28
29 /// 255 bytes, will be a header next
30 GrabChainrust-analyzer cargo cobs 0.2.3 dec/DecoderState#GrabChain#(u8),
31 }
32
33 fn addrust-analyzer cargo cobs 0.2.3 dec/add().(to: &mut [u8], idx: usize, data: u8) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), ()> {
34 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().to.get_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get_mut().(idx)
35 .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| ())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). = data;
36 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
37 }
38
39 /// [`DecodeResult`] represents the possible non-error outcomes of
40 /// pushing an encoded data byte into the [`DecoderState`] state machine
41 pub enum DecodeResultrust-analyzer cargo cobs 0.2.3 dec/DecodeResult# {
42 /// The given input byte did not prompt an output byte, either because the
43 /// state machine is still idle, or we have just processed a header byte.
44 /// More data is needed to complete the message.
45 NoDatarust-analyzer cargo cobs 0.2.3 dec/DecodeResult#NoData#,
46
47 /// We have received a complete and well-encoded COBS message. The
48 /// contents of the associated output buffer may now be used
49 DataCompleterust-analyzer cargo cobs 0.2.3 dec/DecodeResult#DataComplete#,
50
51 /// The following byte should be appended to the current end of the decoded
52 /// output buffer.
53 /// More data is needed to complete the message.
54 DataContinuerust-analyzer cargo cobs 0.2.3 dec/DecodeResult#DataContinue#(u8),
55 }
56
57 impl DecoderStaterust-analyzer cargo cobs 0.2.3 dec/DecoderState# {
58 /// Push a single encoded byte into the state machine. If the input was
59 /// unexpected, such as an early end of a framed message segment, an Error will
60 /// be returned, and the current associated output buffer contents should be discarded.
61 ///
62 /// If a complete message is indicated, the decoding state machine will automatically
63 /// reset itself to the Idle state, and may be used to begin decoding another message.
64 ///
65 /// NOTE: Sentinel value must be included in the input to this function for the
66 /// decoding to complete
67 pub fn feedrust-analyzer cargo cobs 0.2.3 dec/impl#[DecoderState]feed().(&mut self, data: u8) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<DecodeResultrust-analyzer cargo cobs 0.2.3 dec/DecodeResult#, ()> {
68 use DecoderStaterust-analyzer cargo cobs 0.2.3 dec/DecoderState#::*;
69 use DecodeResultrust-analyzer cargo cobs 0.2.3 dec/DecodeResult#::*;
70 let (ret, state) = match (&self, data) {
71 // Currently Idle, received a terminator, ignore, stay idle
72 (Idlerust-analyzer cargo cobs 0.2.3 dec/DecoderState#Idle#, 0x00) => (Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(NoDatarust-analyzer cargo cobs 0.2.3 dec/DecodeResult#NoData#), Idlerust-analyzer cargo cobs 0.2.3 dec/DecoderState#Idle#),
73
74 // Currently Idle, received a byte indicating the
75 // next 255 bytes have no zeroes, so we will have 254 unmodified
76 // data bytes, then an overhead byte
77 (Idlerust-analyzer cargo cobs 0.2.3 dec/DecoderState#Idle#, 0xFF) => (Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(NoDatarust-analyzer cargo cobs 0.2.3 dec/DecodeResult#NoData#), GrabChainrust-analyzer cargo cobs 0.2.3 dec/DecoderState#GrabChain#(0xFE)),
78
79 // Currently Idle, received a byte indicating there will be a
80 // zero that must be modified in the next 1..=254 bytes
81 (Idlerust-analyzer cargo cobs 0.2.3 dec/DecoderState#Idle#, n) => (Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(NoDatarust-analyzer cargo cobs 0.2.3 dec/DecodeResult#NoData#), Grabrust-analyzer cargo cobs 0.2.3 dec/DecoderState#Grab#(n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().1)),
82
83 // We have reached the end of a data run indicated by an overhead
84 // byte, AND we have recieved the message terminator. This was a
85 // well framed message!
86 (Grabrust-analyzer cargo cobs 0.2.3 dec/DecoderState#Grab#(0), 0x00) => (Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DataCompleterust-analyzer cargo cobs 0.2.3 dec/DecodeResult#DataComplete#), Idlerust-analyzer cargo cobs 0.2.3 dec/DecoderState#Idle#),
87
88 // We have reached the end of a data run indicated by an overhead
89 // byte, and the next segment of 254 bytes will have no modified
90 // sentinel bytes
91 (Grabrust-analyzer cargo cobs 0.2.3 dec/DecoderState#Grab#(0), 0xFF) => {
92 (Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DataContinuerust-analyzer cargo cobs 0.2.3 dec/DecodeResult#DataContinue#(0)), GrabChainrust-analyzer cargo cobs 0.2.3 dec/DecoderState#GrabChain#(0xFE))
93 },
94
95 // We have reached the end of a data run indicated by an overhead
96 // byte, and we will treat this byte as a modified sentinel byte.
97 // place the sentinel byte in the output, and begin processing the
98 // next non-sentinel sequence
99 (Grabrust-analyzer cargo cobs 0.2.3 dec/DecoderState#Grab#(0), n) => {
100 (Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DataContinuerust-analyzer cargo cobs 0.2.3 dec/DecodeResult#DataContinue#(0)), Grabrust-analyzer cargo cobs 0.2.3 dec/DecoderState#Grab#(n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().1))
101 },
102
103 // We were not expecting the sequence to terminate, but here we are.
104 // Report an error due to early terminated message
105 (Grabrust-analyzer cargo cobs 0.2.3 dec/DecoderState#Grab#(_), 0) => {
106 (Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(()), Idlerust-analyzer cargo cobs 0.2.3 dec/DecoderState#Idle#)
107 }
108
109 // We have not yet reached the end of a data run, decrement the run
110 // counter, and place the byte into the decoded output
111 (Grabrust-analyzer cargo cobs 0.2.3 dec/DecoderState#Grab#(i), n) => {
112 (Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DataContinuerust-analyzer cargo cobs 0.2.3 dec/DecodeResult#DataContinue#(n)), Grabrust-analyzer cargo cobs 0.2.3 dec/DecoderState#Grab#(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().1))
113 },
114
115 // We have reached the end of a data run indicated by an overhead
116 // byte, AND we have recieved the message terminator. This was a
117 // well framed message!
118 (GrabChainrust-analyzer cargo cobs 0.2.3 dec/DecoderState#GrabChain#(0), 0x00) => {
119 (Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DataCompleterust-analyzer cargo cobs 0.2.3 dec/DecodeResult#DataComplete#), Idlerust-analyzer cargo cobs 0.2.3 dec/DecoderState#Idle#)
120 }
121
122 // We have reached the end of a data run, and we will begin another
123 // data run with an overhead byte expected at the end
124 (GrabChainrust-analyzer cargo cobs 0.2.3 dec/DecoderState#GrabChain#(0), 0xFF) => (Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(NoDatarust-analyzer cargo cobs 0.2.3 dec/DecodeResult#NoData#), GrabChainrust-analyzer cargo cobs 0.2.3 dec/DecoderState#GrabChain#(0xFE)),
125
126 // We have reached the end of a data run, and we will expect `n` data
127 // bytes unmodified, followed by a sentinel byte that must be modified
128 (GrabChainrust-analyzer cargo cobs 0.2.3 dec/DecoderState#GrabChain#(0), n) => (Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(NoDatarust-analyzer cargo cobs 0.2.3 dec/DecodeResult#NoData#), Grabrust-analyzer cargo cobs 0.2.3 dec/DecoderState#Grab#(n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().1)),
129
130 // We were not expecting the sequence to terminate, but here we are.
131 // Report an error due to early terminated message
132 (GrabChainrust-analyzer cargo cobs 0.2.3 dec/DecoderState#GrabChain#(_), 0) => {
133 (Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(()), Idlerust-analyzer cargo cobs 0.2.3 dec/DecoderState#Idle#)
134 }
135
136 // We have not yet reached the end of a data run, decrement the run
137 // counter, and place the byte into the decoded output
138 (GrabChainrust-analyzer cargo cobs 0.2.3 dec/DecoderState#GrabChain#(i), n) => {
139 (Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DataContinuerust-analyzer cargo cobs 0.2.3 dec/DecodeResult#DataContinue#(n)), GrabChainrust-analyzer cargo cobs 0.2.3 dec/DecoderState#GrabChain#(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().1))
140 },
141 };
142
143 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self = state;
144 ret
145 }
146 }
147
148 impl<'a> CobsDecoderrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#<'a> {
149
150 /// Create a new streaming Cobs Decoder. Provide the output buffer
151 /// for the decoded message to be placed in
152 pub fn newrust-analyzer cargo cobs 0.2.3 dec/impl#[`CobsDecoder<'a>`]new().(dest: &'a mut [u8]) -> CobsDecoderrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#<'a> {
153 CobsDecoderrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder# {
154 destrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#dest.,
155 dest_idxrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#dest_idx.: 0,
156 staterust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#state.: DecoderStaterust-analyzer cargo cobs 0.2.3 dec/DecoderState#::Idlerust-analyzer cargo cobs 0.2.3 dec/DecoderState#Idle#,
157 }
158 }
159
160 /// Push a single byte into the streaming CobsDecoder. Return values mean:
161 ///
162 /// * Ok(None) - State machine okay, more data needed
163 /// * Ok(Some(N)) - A message of N bytes was successfully decoded
164 /// * Err(M) - Message decoding failed, and M bytes were written to output
165 ///
166 /// NOTE: Sentinel value must be included in the input to this function for the
167 /// decoding to complete
168 pub fn feedrust-analyzer cargo cobs 0.2.3 dec/impl#[`CobsDecoder<'a>`]feed().(&mut self, data: u8) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>, usize> {
169 match self.staterust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#state..feedrust-analyzer cargo cobs 0.2.3 dec/impl#[DecoderState]feed().(data) {
170 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(()) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(self.dest_idxrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#dest_idx.),
171 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DecodeResultrust-analyzer cargo cobs 0.2.3 dec/DecodeResult#::NoDatarust-analyzer cargo cobs 0.2.3 dec/DecodeResult#NoData#) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#),
172 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DecodeResultrust-analyzer cargo cobs 0.2.3 dec/DecodeResult#::DataContinuerust-analyzer cargo cobs 0.2.3 dec/DecodeResult#DataContinue#(n)) => {
173 addrust-analyzer cargo cobs 0.2.3 dec/add().(self.destrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#dest., self.dest_idxrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#dest_idx., n).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| self.dest_idxrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#dest_idx.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
174 self.dest_idxrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#dest_idx. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
175 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)
176 }
177 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DecodeResultrust-analyzer cargo cobs 0.2.3 dec/DecodeResult#::DataCompleterust-analyzer cargo cobs 0.2.3 dec/DecodeResult#DataComplete#) => {
178 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(self.dest_idxrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#dest_idx.))
179 }
180 }
181 }
182
183 /// Push a slice of bytes into the streaming CobsDecoder. Return values mean:
184 ///
185 /// * Ok(None) - State machine okay, more data needed
186 /// * Ok(Some((N, M))) - A message of N bytes was successfully decoded,
187 /// using M bytes from `data` (and earlier data)
188 /// * Err(J) - Message decoding failed, and J bytes were written to output
189 ///
190 /// NOTE: Sentinel value must be included in the input to this function for the
191 /// decoding to complete
192 pub fn pushrust-analyzer cargo cobs 0.2.3 dec/impl#[`CobsDecoder<'a>`]push().(&mut self, data: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(usize, usize)>, usize> {
193 for (consumed_idx, d) in data.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() {
194 let x = self.feedrust-analyzer cargo cobs 0.2.3 dec/impl#[`CobsDecoder<'a>`]feed().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().d);
195 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(decoded_bytes_ct) = x?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). {
196 // convert from index to number of bytes consumed
197 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((decoded_bytes_ct, consumed_idx rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1)));
198 }
199 }
200
201 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)
202 }
203 }
204
205 // This needs to be a macro because `src` and `dst` could be the same or different.
206 macro_rules! decode_rawrust-analyzer cargo cobs 0.2.3 dec/decode_raw! (
207 ($src:ident, $dst:ident) => ({
208 let mut source_index = 0;
209 let mut dest_index = 0;
210
211 // Stop at the first terminator, if any
212 let src_end = if let Some(end) = $src.iter().position(|b| *b == 0) {
213 end
214 } else {
215 $src.len()
216 };
217
218 while source_index < src_end {
219 let code = $src[source_index];
220
221 if source_index + code as usize > src_end && code != 1 {
222 return Err(());
223 }
224
225 source_index += 1;
226
227 // TODO: There are potential `panic!`s in these dest_index offsets
228 for _ in 1..code {
229 $dst[dest_index] = $src[source_index];
230 source_index += 1;
231 dest_index += 1;
232 }
233
234 if 0xFF != code && source_index < src_end {
235 $dst[dest_index] = 0;
236 dest_index += 1;
237 }
238 }
239
240 DecodeReport {
241 dst_used: dest_index,
242 src_used: source_index,
243 }
244 })
245 );
246
247 /// Decodes the `source` buffer into the `dest` buffer.
248 ///
249 /// This function uses the typical sentinel value of 0.
250 ///
251 /// # Failures
252 ///
253 /// This will return `Err(())` if there was a decoding error. Otherwise,
254 /// it will return `Ok(n)` where `n` is the length of the decoded message.
255 pub fn decoderust-analyzer cargo cobs 0.2.3 dec/decode().(source: &[u8], dest: &mut[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, ()> {
256 let mut dec = CobsDecoderrust-analyzer cargo cobs 0.2.3 dec/CobsDecoder#::newrust-analyzer cargo cobs 0.2.3 dec/impl#[`CobsDecoder<'a>`]new().(dest);
257
258 // Did we decode a message, using some or all of the buffer?
259 match dec.pushrust-analyzer cargo cobs 0.2.3 dec/impl#[`CobsDecoder<'a>`]push().(source).orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]or().(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). {
260 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((d_used, _s_used)) => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(d_used),
261 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {},
262 }
263
264 // If we consumed the entire buffer, but did NOT get a message,
265 // AND the message did not end with a zero, try providing one to
266 // complete the decoding.
267 if source.lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&0) {
268 // Explicitly push sentinel of zero
269 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((d_used, _s_used)) = dec.pushrust-analyzer cargo cobs 0.2.3 dec/impl#[`CobsDecoder<'a>`]push().(&[0]).orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]or().(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). {
270 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(d_used)
271 }
272 }
273
274 // Nope, no early message, no missing terminator, just failed to decode
275 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(())
276 }
277
278 /// A report of the source and destination bytes used during in-place decoding
279 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
280 pub struct DecodeReportrust-analyzer cargo cobs 0.2.3 dec/DecodeReport# {
281 // The number of source bytes used, NOT INCLUDING the sentinel byte,
282 // if there was one.
283 pub src_usedrust-analyzer cargo cobs 0.2.3 dec/DecodeReport#src_used.: usize,
284
285 // The number of bytes of the source buffer that now include the
286 // decoded result
287 pub dst_usedrust-analyzer cargo cobs 0.2.3 dec/DecodeReport#dst_used.: usize,
288 }
289
290 /// Decodes a message in-place.
291 ///
292 /// This is the same function as `decode_in_place`, but provides a report
293 /// of both the number of source bytes consumed as well as the size of the
294 /// destination used.
295 pub fn decode_in_place_reportrust-analyzer cargo cobs 0.2.3 dec/decode_in_place_report().(buff: &mut[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<DecodeReportrust-analyzer cargo cobs 0.2.3 dec/DecodeReport#, ()> {
296 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(decode_rawrust-analyzer cargo cobs 0.2.3 dec/decode_raw!!(buff, buff))
297 }
298
299 /// Decodes a message in-place.
300 ///
301 /// This is the same function as `decode`, but replaces the encoded message
302 /// with the decoded message instead of writing to another buffer.
303 ///
304 /// The returned `usize` is the number of bytes used for the DECODED value,
305 /// NOT the number of source bytes consumed during decoding.
306 pub fn decode_in_placerust-analyzer cargo cobs 0.2.3 dec/decode_in_place().(buff: &mut[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, ()> {
307 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(decode_rawrust-analyzer cargo cobs 0.2.3 dec/decode_raw!!(buff, buff).dst_usedrust-analyzer cargo cobs 0.2.3 dec/DecodeReport#dst_used.)
308 }
309
310 /// Decodes the `source` buffer into the `dest` buffer using an arbitrary sentinel value.
311 ///
312 /// This is done by XOR-ing each byte of the source message with the chosen sentinel value,
313 /// which transforms the message into the same message encoded with a sentinel value of 0.
314 /// Then the regular decoding transformation is performed.
315 ///
316 /// The returned `usize` is the number of bytes used for the DECODED value,
317 /// NOT the number of source bytes consumed during decoding.
318 pub fn decode_with_sentinelrust-analyzer cargo cobs 0.2.3 dec/decode_with_sentinel().(source: &[u8], dest: &mut[u8], sentinel: u8) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, ()> {
319 for (x, y) in source.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(dest.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().()) {
320 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().y = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitXor<Self>`]bitxor().sentinel;
321 }
322 decode_in_placerust-analyzer cargo cobs 0.2.3 dec/decode_in_place().(dest)
323 }
324
325 /// Decodes a message in-place using an arbitrary sentinel value.
326 ///
327 /// The returned `usize` is the number of bytes used for the DECODED value,
328 /// NOT the number of source bytes consumed during decoding.
329 pub fn decode_in_place_with_sentinelrust-analyzer cargo cobs 0.2.3 dec/decode_in_place_with_sentinel().(buff: &mut[u8], sentinel: u8) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, ()> {
330 for x in buff.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().() {
331 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitXorAssign<Self>`]bitxor_assign().^=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitXorAssign<Self>`]bitxor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitXorAssign<Self>`]bitxor_assign().sentinel;
332 }
333 decode_in_placerust-analyzer cargo cobs 0.2.3 dec/decode_in_place().(buff)
334 }
335
336 #[cfg(feature = "use_std")]
337 /// Decodes the `source` buffer into a vector.
338 pub fn decode_vecrust-analyzer cargo cobs 0.2.3 dec/decode_vec().(source: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, ()> {
339 let mut decoded = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; source.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()];
340 match decoderust-analyzer cargo cobs 0.2.3 dec/decode().(source, &mut decoded[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().) {
341 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(n) => {
342 decoded.truncaterust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]truncate().(n);
343 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(decoded)
344 },
345 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(()) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(()),
346 }
347 }
348
349 #[cfg(feature = "use_std")]
350 /// Decodes the `source` buffer into a vector with an arbitrary sentinel value.
351 pub fn decode_vec_with_sentinelrust-analyzer cargo cobs 0.2.3 dec/decode_vec_with_sentinel().(source: &[u8], sentinel: u8) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, ()> {
352 let mut decoded = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; source.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()];
353 match decode_with_sentinelrust-analyzer cargo cobs 0.2.3 dec/decode_with_sentinel().(source, &mut decoded[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()., sentinel) {
354 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(n) => {
355 decoded.truncaterust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]truncate().(n);
356 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(decoded)
357 },
358 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(()) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(()),
359 }
360 }