1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! Synchronous COBS encoding and decoding between in-memory slices.
//!
//! This module is always available, including in `no_std` builds. It needs
//! no executor or heap allocation. [`Reliable`](crate::Reliable) uses the
//! same framing rules internally, but accepts only delimiter-terminated
//! frames.
//!
//! | Operation | API |
//! |---|---|
//! | Encode a complete payload as an undelimited body | [`encode_from_slice`] |
//! | Encode a payload with surrounding zero delimiters | [`encode_from_slice_including_sentinels`] |
//! | Decode one frame from the start of a slice | [`decode_to_slice`] |
//!
//! Size encoding buffers with [`max_encoding_length`](crate::max_encoding_length),
//! adding two bytes for delimiters. Decoding requires capacity only for the
//! decoded payload.
//!
//! # Example
//!
//! Decode consecutive frames from one buffer, using
//! [`DecodedFrame::consumed`] to locate the next frame.
//!
//! ```
//! use cobs_io_async::{max_encoding_length, sync};
//!
//! let mut stream = [0u8; 2 * (max_encoding_length(3) + 2)];
//! let first = sync::encode_from_slice_including_sentinels(&[7, 0, 8], &mut stream).unwrap();
//! let second =
//! sync::encode_from_slice_including_sentinels(&[9], &mut stream[first..]).unwrap();
//! let mut input = &stream[..first + second];
//!
//! let mut payload = [0u8; 3];
//! let frame = sync::decode_to_slice(input, &mut payload).unwrap();
//! assert_eq!(&payload[..frame.len], &[7, 0, 8]);
//! input = &input[frame.consumed..];
//!
//! let frame = sync::decode_to_slice(input, &mut payload).unwrap();
//! assert_eq!(&payload[..frame.len], &[9]);
//! assert!(input[frame.consumed..].is_empty());
//! ```
use Infallible;
use cratedecode_slice;
use crate::;
/// Result of successfully decoding one frame with [`decode_to_slice`].
/// Encodes all of `source` as one complete, undelimited COBS body into `dest`.
///
/// Returns the encoded length; the body occupies `dest[..len]`. Payload zeros
/// are encoded as data. Empty input produces `[1]`. A terminal full block of
/// 254 nonzero payload bytes does not receive a redundant trailing code byte.
///
/// A destination of [`max_encoding_length(source.len())`](crate::max_encoding_length)
/// bytes is always sufficient.
///
/// # Errors
///
/// Returns [`SeekableError::OutOfBounds`] if `dest` is too small. The
/// destination may then have been partially overwritten.
///
/// # Example
///
/// ```
/// use cobs_io_async::sync::encode_from_slice;
///
/// let mut encoded = [0u8; 4];
/// let len = encode_from_slice(&[7, 0, 8], &mut encoded).unwrap();
/// assert_eq!(&encoded[..len], &[2, 7, 2, 8]);
/// ```
/// Encodes `source` with one leading and one trailing zero delimiter.
///
/// On success, `dest[..len]` contains `[0, encoded_body..., 0]`. Empty input
/// produces `[0, 1, 0]`. Payload zeros remain data and do not split the frame.
///
/// Reserve [`max_encoding_length(source.len())`](crate::max_encoding_length)
/// plus two bytes.
///
/// # Errors
///
/// Returns [`SeekableError::OutOfBounds`] if `dest` is too small. The
/// destination may then have been partially overwritten.
/// Decodes the first COBS frame in `source` into the beginning of `dest`.
///
/// Leading zeros are ignored as padding. Decoding stops after the zero
/// delimiter completing the frame; bytes after it are not examined, and
/// [`DecodedFrame::consumed`] identifies where they begin.
///
/// If `source` ends before a delimiter, the frame is accepted when one has
/// started and its current COBS block is structurally complete. Both `[1]`
/// and `[1, 0]` decode to an empty payload. This end-of-input acceptance
/// cannot detect truncation at a COBS block boundary; check that `consumed`
/// includes a delimiter (`source[consumed - 1] == 0`) when the protocol
/// requires delimited frames.
///
/// On success, bytes of `dest` after the decoded payload remain unchanged.
///
/// # Errors
///
/// - `DecodeError::Destination(SeekableError::OutOfBounds)`: `dest` cannot
/// hold the decoded payload.
/// - [`DecodeError::InvalidFrame`]: a delimiter arrived inside an incomplete
/// block. The attached progress counts the invalidating delimiter, so the
/// next frame starts at `source[progress.consumed as usize..]`.
/// - [`DecodeError::UnexpectedSourceEof`]: `source` ended inside an
/// incomplete block.
/// - [`DecodeError::EmptyFrame`]: `source` is empty or contains only padding.
///
/// Errors may leave `dest` partially overwritten. Source errors are never
/// returned.
///
/// # Example
///
/// ```
/// use cobs_io_async::sync::decode_to_slice;
///
/// let source = [0, 2, 7, 2, 8, 0, 2, 9, 0];
/// let mut output = [0x80u8; 5];
/// let frame = decode_to_slice(&source, &mut output).unwrap();
///
/// assert_eq!(frame.len, 3);
/// assert_eq!(output, [7, 0, 8, 0x80, 0x80]);
/// assert_eq!(&source[frame.consumed..], &[2, 9, 0]);
/// ```