cobs2 0.1.4

Consistent Overhead Byte Stuffing — COBS — and variant COBS/R
Documentation
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//! Consistent Overhead Byte Stuffing—Reduced (COBS/R)
//!
//! This module contains functions for a variant of COBS, called COBS/R. Its purpose is to save one
//! byte from the encoded form in some cases. Plain COBS encoding always has a +1 byte encoding
//! overhead. See the references for details. COBS/R can often avoid the +1 byte, which can be a
//! useful savings if it is mostly small messages that are being encoded.
//!
//! In plain COBS, the last length code byte in the message has some inherent redundancy: if it is
//! greater than the number of remaining bytes, this is detected as an error.
//!
//! In COBS/R, instead we opportunistically replace the final length code byte with the final data
//! byte, whenever the value of the final data byte is greater than or equal to what the final
//! length value would normally be. This variation can be unambiguously decoded: the decoder
//! notices that the length code is greater than the number of remaining bytes.
//!
//! ### Examples
//!
//! The byte values in the examples are in hex.
//!
//! #### First example
//!
//! Input:
//!
//! > 2F A2 00 92 73 02
//!
//! This example is encoded the same in COBS and COBS/R. Encoded (length code bytes are
//! highlighted):
//!
//! > **03** 2F A2 **04** 92 73 02
//!
//! #### Second example
//!
//! The second example is almost the same, except the final data byte value is greater than what
//! the length byte would be.
//!
//! Input:
//!
//! > 2F A2 00 92 73 26
//!
//! Encoded in plain COBS (length code bytes are highlighted):
//!
//! > **03** 2F A2 **04** 92 73 26
//!
//! Encoded in COBS/R:
//!
//! > **03** 2F A2 **26** 92 73
//!
//! Because the last data byte (`26`) is greater than the usual length code (`04`), the last data
//! byte can be inserted in place of the length code, and removed from the end of the sequence.
//! This avoids the usual +1 byte overhead of the COBS encoding.
//!
//! The decoder detects this variation on the encoding simply by detecting that the length code is
//! greater than the number of remaining bytes. That situation would be a decoding error in regular
//! COBS, but in COBS/R it is used to save one byte in the encoded message.


/// Calculate the minimum possible COBS/R encoded output size, for a given size of input data.
pub const fn encode_min_output_size(input_len: usize) -> usize {
    if input_len == 0 {
        1
    } else {
        input_len
    }
}

/// Calculate the maximum possible COBS/R encoded output size, for a given size of input data.
pub const fn encode_max_output_size(input_len: usize) -> usize {
    if input_len == 0 {
        1
    } else if input_len >= usize::max_value() - 253 {
        usize::max_value()
    } else {
        let increase = (input_len + 253) / 254;
        if input_len >= usize::max_value() - increase {
            usize::max_value()
        } else {
            input_len + increase
        }
    }
}

/// Common function for converting an iterator encoder's input iterator size hint to an output size hint.
fn encode_size_hint(in_hint: (usize, Option<usize>)) -> (usize, Option<usize>) {
    let lower_bound = encode_min_output_size(in_hint.0);
    let upper_bound = in_hint.1.map(|x| encode_max_output_size(x));
    (lower_bound, upper_bound)
}

/// Calculate the minimum possible decoded output size, for a given size of COBS-encoded input.
///
/// Worst-case is for decoding output from a naive COBS encoder. So, same as for COBS decoding.
pub const fn decode_min_output_size(input_len: usize) -> usize {
    if input_len >= 1 {
        let increase = (input_len - 1) / 255;
        input_len - 1 - increase
    } else {
        0
    }
}

/// Calculate the maximum possible decoded output size, for a given size of COBS/R-encoded input.
pub const fn decode_max_output_size(input_len: usize) -> usize {
    input_len
}

/// Common function for converting an iterator decoder's input iterator size hint to an output size hint.
fn decode_size_hint(in_hint: (usize, Option<usize>)) -> (usize, Option<usize>) {
    let lower_bound = decode_min_output_size(in_hint.0);
    let upper_bound = in_hint.1.map(|x| decode_max_output_size(x));
    (lower_bound, upper_bound)
}

/// Encode data into COBS/R encoded form, writing output to the given output buffer.
///
/// The output data is COBS-encoded, containing no zero-bytes.
///
/// The caller must provide a reference to a suitably-sized output buffer.
/// [encode_max_output_size()] calculates the required output buffer size, for a given input
/// size.
///
/// The return value is a [Result] that in the `Ok` case is a slice of the valid data in the
/// output buffer.
///
///     let mut cobs_buf = [0x55_u8; 1000];
///     let data = b"ABC\0ghij\0xyz";
///     let data_cobs = cobs2::cobsr::encode_array(&mut cobs_buf, data);
///     assert_eq!(data_cobs.unwrap(), b"\x04ABC\x05ghijzxy");
pub fn encode_array<'a>(out_buf: &'a mut [u8], in_buf: &[u8]) -> crate::Result<&'a [u8]> {
    let mut code_i = 0;
    let mut out_i = 1;
    let mut last_value = 0_u8;

    if code_i >= out_buf.len() {
        return Err(crate::Error::OutputBufferTooSmall);
    }
    for x in in_buf {
        if out_i - code_i >= 0xFF {
            out_buf[code_i] = 0xFF;
            code_i = out_i;
            if code_i >= out_buf.len() {
                return Err(crate::Error::OutputBufferTooSmall);
            }
            out_i = code_i + 1;
        }
        if *x == 0 {
            out_buf[code_i] = (out_i - code_i) as u8;
            code_i = out_i;
            if code_i >= out_buf.len() {
                return Err(crate::Error::OutputBufferTooSmall);
            }
            out_i = code_i + 1;
            last_value = 0;
        } else {
            last_value = *x;
            if out_i >= out_buf.len() {
                return Err(crate::Error::OutputBufferTooSmall);
            }
            out_buf[out_i] = last_value;
            out_i += 1;
        }
    }

    // We've reached the end of the source data.
    // Finalise the remaining output. In particular, write the code (length) byte.
    // Update the pointer to calculate the final output length.
    if last_value >= (out_i - code_i) as u8 {
        out_buf[code_i] = last_value;
        out_i -= 1;
    } else {
        out_buf[code_i] = (out_i - code_i) as u8;
    }

    Ok(&out_buf[..out_i])
}

/// Encode data into COBS/R encoded form, returning output as a vector of `u8`.
///
/// The output data is COBS/R-encoded, containing no zero-bytes.
///
/// The return value is a [Result] that in the `Ok` case is a vector of `u8`.
///
///     let data = b"ABC\0ghij\0xyz";
///     let data_cobs = cobs2::cobsr::encode_vector(data);
///     assert_eq!(data_cobs.unwrap(), b"\x04ABC\x05ghijzxy".to_vec());
#[cfg(feature = "alloc")]
pub fn encode_vector(in_buf: &[u8]) -> crate::Result<alloc::vec::Vec<u8>> {
    let mut code_i = 0;
    let mut run_len = 0_u8;
    let mut last_value = 0_u8;
    let mut out_vec = alloc::vec::Vec::<u8>::with_capacity(encode_max_output_size(in_buf.len()));

    for x in in_buf {
        if run_len == 0xFF {
            out_vec[code_i] = 0xFF;
            code_i += 0xFF;
            run_len = 0;
        }
        if *x == 0 {
            if run_len == 0 {
                out_vec.push(1);
                code_i += 1;
            } else {
                out_vec[code_i] = run_len;
                code_i += run_len as usize;
            }
            run_len = 0;
            last_value = 0;
        } else {
            if run_len == 0 {
                out_vec.push(0xFF);
                run_len = 1;
            }
            last_value = *x;
            out_vec.push(last_value);
            run_len += 1;
        }
    }

    // We've reached the end of the source data.
    // Finalise the remaining output. In particular, write the code (length) byte.
    if run_len == 0 {
        out_vec.push(1);
    } else if last_value >= run_len {
        out_vec[code_i] = last_value;
        out_vec.pop();
    } else {
        out_vec[code_i] = run_len;
    }

    Ok(out_vec)
}

struct EncodeIterator<I>
where
    I: Iterator<Item = u8>,
{
    in_iter: I,
    in_lookahead: Option<Option<u8>>,
    eof: bool,
    last_run_0xff: bool,
    hold_write_i: u8,
    hold_read_i: u8,
    hold_buf: [u8; 255],
}

impl<I> EncodeIterator<I>
where
    I: Iterator<Item = u8>,
{
    fn new(i: I) -> EncodeIterator<I> {
        return EncodeIterator {
            in_iter: i,
            in_lookahead: None,
            eof: false,
            last_run_0xff: false,
            hold_write_i: 0,
            hold_read_i: 0,
            hold_buf: [1; 255],
        };
    }
}

impl<I> Iterator for EncodeIterator<I>
where
    I: Iterator<Item = u8>,
{
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item> {
        let mut last_byte: u8 = 0;

        if self.hold_write_i != 0 {
            if self.hold_read_i < self.hold_write_i {
                let byte_val = self.hold_buf[self.hold_read_i as usize];
                self.hold_read_i += 1;
                return Some(byte_val);
            } else {
                self.hold_read_i = 0;
                self.hold_write_i = 0;
                // else drop through to loop below.
            }
        }
        if self.eof {
            return None;
        }
        loop {
            if self.hold_write_i == 0xFE {
                self.last_run_0xff = true;
                let in_iter_next = self.in_iter.next();
                if in_iter_next.is_none() {
                    self.eof = true;
                    if last_byte >= 0xFF {
                        self.hold_write_i -= 1;
                    }
                }
                self.in_lookahead = Some(in_iter_next);
                return Some(0xFF);
            } else {
                let in_iter_next = if self.in_lookahead.is_some() {
                    self.in_lookahead.take().unwrap()
                } else {
                    self.in_iter.next()
                };
                let byte_val = in_iter_next.unwrap_or_else(|| {
                    self.eof = true;
                    0
                });
                if self.last_run_0xff {
                    self.last_run_0xff = false;
                    if self.eof {
                        return None;
                    }
                }
                if byte_val == 0 {
                    let run_len = self.hold_write_i + 1;
                    let count_byte = if self.eof && self.hold_write_i > 0 && last_byte >= run_len {
                        self.hold_write_i -= 1;
                        last_byte
                    } else {
                        run_len
                    };
                    return Some(count_byte);
                } else {
                    last_byte = byte_val;
                    self.hold_buf[self.hold_write_i as usize] = byte_val;
                    self.hold_write_i += 1;
                }
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let in_iter_size_hint = self.in_iter.size_hint();
        encode_size_hint(in_iter_size_hint)
    }
}

/// Encode data into COBS/R encoded form, getting data from a `u8` iterator, and providing the output as an iterator.
///
/// The output data is COBS/R-encoded, containing no zero-bytes.
///
/// The caller must provide a `u8` iterator.
///
/// The return value is a `u8` iterator. This is suitable to `collect()` into a byte container.
///
///     let data = b"ABC\0ghij\0xyz".to_vec();
///     let data_cobs: Vec<u8> = cobs2::cobsr::encode_iter(data.into_iter()).collect();
///     assert_eq!(data_cobs, b"\x04ABC\x05ghijzxy".to_vec());
pub fn encode_iter<I>(i: I) -> impl Iterator<Item = u8>
where
    I: Iterator<Item = u8>,
{
    EncodeIterator::<I>::new(i)
}

/// Encode data into COBS/R encoded form, getting data from a `&u8` iterator, and providing the output as an iterator.
///
/// The output data is COBS/R-encoded, containing no zero-bytes.
///
/// The caller must provide a `&u8` iterator.
///
/// The return value is a `u8` iterator. This is suitable to `collect()` into a byte container.
///
///     let data = b"ABC\0ghij\0xyz".to_vec();
///     let data_cobs: Vec<u8> = cobs2::cobsr::encode_ref_iter(data.iter()).collect();
///     assert_eq!(data_cobs, b"\x04ABC\x05ghijzxy".to_vec());
pub fn encode_ref_iter<'a, I>(i: I) -> impl Iterator<Item = u8> + 'a
where
    I: Iterator<Item = &'a u8> + 'a,
{
    EncodeIterator::<_>::new(i.copied())
}

/// Decode COBS/R-encoded data, writing decoded data to the given output buffer.
///
/// The caller must provide a reference to a suitably-sized output buffer.
/// [decode_max_output_size()] calculates the required output buffer size, for a given input
/// size.
///
/// The return value is a [Result] that in the `Ok` case is a slice of the decoded data in the
/// output buffer.
///
///     let mut decode_buf = [0x55_u8; 1000];
///     let data_cobs = b"\x04ABC\x05ghijzxy";
///     let decode_data = cobs2::cobsr::decode_array(&mut decode_buf, data_cobs);
///     assert_eq!(decode_data.unwrap(), b"ABC\0ghij\0xyz");
pub fn decode_array<'a>(out_buf: &'a mut [u8], in_buf: &[u8]) -> crate::Result<&'a [u8]> {
    let mut code_i = 0;
    let mut out_i = 0;

    if !in_buf.is_empty() {
        loop {
            let code = in_buf[code_i];
            if code == 0 {
                return Err(crate::Error::ZeroInEncodedData);
            }
            for in_i in (code_i + 1)..(code_i + code as usize) {
                if out_i >= out_buf.len() {
                    return Err(crate::Error::OutputBufferTooSmall);
                }
                if in_i >= in_buf.len() {
                    // End of data, where length code is greater than remaining data.
                    // Output the length code as the last output byte.
                    out_buf[out_i] = code;
                    out_i += 1;
                    break;
                }
                let in_byte = in_buf[in_i];
                if in_byte == 0 {
                    return Err(crate::Error::ZeroInEncodedData);
                }
                out_buf[out_i] = in_byte;
                out_i += 1;
            }
            code_i += code as usize;
            if code_i >= in_buf.len() {
                // End of data. Exit, without outputting a trailing zero for the end of the data.
                break;
            }
            if code < 0xFF {
                // Output trailing zero.
                if out_i >= out_buf.len() {
                    return Err(crate::Error::OutputBufferTooSmall);
                }
                out_buf[out_i] = 0;
                out_i += 1;
            }
        }
    }
    Ok(&out_buf[..out_i])
}

/// Decode COBS/R-encoded data, returning output as a vector of `u8`.
///
/// The return value is a [Result] that in the `Ok` case is a vector of `u8`.
///
///     let data_cobs = b"\x04ABC\x05ghijzxy";
///     let decode_data = cobs2::cobsr::decode_vector(data_cobs);
///     assert_eq!(decode_data.unwrap(), b"ABC\0ghij\0xyz");
#[cfg(feature = "alloc")]
pub fn decode_vector(in_buf: &[u8]) -> crate::Result<alloc::vec::Vec<u8>> {
    let mut code_i = 0;
    let mut out_vec = alloc::vec::Vec::<u8>::with_capacity(decode_max_output_size(in_buf.len()));

    if !in_buf.is_empty() {
        loop {
            let code = in_buf[code_i];
            if code == 0 {
                return Err(crate::Error::ZeroInEncodedData);
            }
            for in_i in (code_i + 1)..(code_i + code as usize) {
                if in_i >= in_buf.len() {
                    // End of data, where length code is greater than remaining data.
                    // Output the length code as the last output byte.
                    out_vec.push(code);
                    break;
                }
                let in_byte = in_buf[in_i];
                if in_byte == 0 {
                    return Err(crate::Error::ZeroInEncodedData);
                }
                out_vec.push(in_byte);
            }
            code_i += code as usize;
            if code_i >= in_buf.len() {
                // End of data. Exit, without outputting a trailing zero for the end of the data.
                break;
            }
            if code < 0xFF {
                // Output trailing zero.
                out_vec.push(0);
            }
        }
    }
    Ok(out_vec)
}

struct DecodeIterator<I>
where
    I: Iterator<Item = u8>,
{
    in_iter: I,
    eof: bool,
    last_run: u8,
    count_run: u8,
}

impl<I> DecodeIterator<I>
where
    I: Iterator<Item = u8>,
{
    fn new(i: I) -> DecodeIterator<I> {
        return DecodeIterator {
            in_iter: i,
            eof: false,
            last_run: 0,
            count_run: 0,
        };
    }
}

impl<I> Iterator for DecodeIterator<I>
where
    I: Iterator<Item = u8>,
{
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if self.eof {
                self.eof = false;
                return None;
            }
            let in_iter_next = self.in_iter.next();
            let byte_val = in_iter_next.unwrap_or(0);
            if byte_val == 0 {
                if self.count_run != 0 {
                    self.eof = true;
                    return Some(self.last_run);
                } else {
                    return None;
                }
            }
            if self.count_run == 0 {
                let last_run = self.last_run;
                self.last_run = byte_val;
                self.count_run = byte_val - 1;
                if last_run != 0 && last_run != 0xFF {
                    return Some(0);
                }
            } else {
                self.count_run -= 1;
                return Some(byte_val);
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let in_iter_size_hint = self.in_iter.size_hint();
        decode_size_hint(in_iter_size_hint)
    }
}

/// Decode COBS/R-encoded data, getting data from a `u8` iterator, and providing the output as an iterator.
///
/// The input data should be COBS/R-encoded, containing no zero-bytes.
///
/// The caller must provide a `u8` iterator.
///
/// The return value is a `u8` iterator. This is suitable to `collect()` into a byte container.
///
/// Unlike the other decode functions, no errors are returned by this function. Rather, decoding is
/// best-effort. In the event of any zero in the input, this will be regarded as end-of-data.
///
///     let data_cobs = b"\x04ABC\x05ghijzxy".to_vec();
///     let decode_data: Vec<u8> = cobs2::cobsr::decode_iter(data_cobs.into_iter()).collect();
///     assert_eq!(decode_data, b"ABC\0ghij\0xyz");
pub fn decode_iter<I>(i: I) -> impl Iterator<Item = u8>
where
    I: Iterator<Item = u8>,
{
    DecodeIterator::<I>::new(i)
}

/// Decode COBS/R-encoded data, getting data from a `&u8` iterator, and providing the output as an iterator.
///
/// The input data should be COBS/R-encoded, containing no zero-bytes.
///
/// The caller must provide a `&u8` iterator.
///
/// The return value is a `u8` iterator. This is suitable to `collect()` into a byte container.
///
/// Unlike the other decode functions, no errors are returned by this function. Rather, decoding is
/// best-effort. In the event of any zero in the input, this will be regarded as end-of-data.
///
///     let data_cobs = b"\x04ABC\x05ghijzxy".to_vec();
///     let decode_data: Vec<u8> = cobs2::cobsr::decode_ref_iter(data_cobs.iter()).collect();
///     assert_eq!(decode_data, b"ABC\0ghij\0xyz");
pub fn decode_ref_iter<'a, I>(i: I) -> impl Iterator<Item = u8> + 'a
where
    I: Iterator<Item = &'a u8> + 'a,
{
    DecodeIterator::<_>::new(i.copied())
}

struct DecodeResultIterator<I>
where
    I: Iterator<Item = u8>,
{
    in_iter: I,
    eof: bool,
    last_run: u8,
    count_run: u8,
}

impl<I> DecodeResultIterator<I>
where
    I: Iterator<Item = u8>,
{
    fn new(i: I) -> DecodeResultIterator<I> {
        return DecodeResultIterator {
            in_iter: i,
            eof: false,
            last_run: 0,
            count_run: 0,
        };
    }
}

impl<I> Iterator for DecodeResultIterator<I>
where
    I: Iterator<Item = u8>,
{
    type Item = crate::Result<u8>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if self.eof {
                return None;
            }
            let in_iter_next = self.in_iter.next();
            if in_iter_next.is_none() {
                self.eof = true;
                if self.count_run != 0 {
                    return Some(Ok(self.last_run));
                } else {
                    return None;
                }
            }
            let byte_val = in_iter_next.unwrap();
            if byte_val == 0 {
                self.eof = true;
                return Some(Err(crate::Error::ZeroInEncodedData));
            }
            if self.count_run == 0 {
                let last_run = self.last_run;
                self.last_run = byte_val;
                self.count_run = byte_val - 1;
                if last_run != 0 && last_run != 0xFF {
                    return Some(Ok(0));
                }
            } else {
                self.count_run -= 1;
                return Some(Ok(byte_val));
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let in_iter_size_hint = self.in_iter.size_hint();
        decode_size_hint(in_iter_size_hint)
    }
}

/// Decode COBS/R-encoded data, getting data from a `u8` iterator, and providing the output as an iterator.
///
/// The input data should be COBS/R-encoded, containing no zero-bytes.
///
/// The caller must provide a `u8` iterator.
///
/// The return value is a `crate::Result<u8>` iterator. This is suitable to `collect()` into a
/// byte container wrapped in `crate::Result<>`.
///
///     let data_cobs = b"\x04ABC\x05ghijzxy".to_vec();
///     let decode_data: cobs2::Result<Vec<u8>> = cobs2::cobsr::decode_result_iter(data_cobs.into_iter()).collect();
///     assert_eq!(decode_data.unwrap(), b"ABC\0ghij\0xyz");
pub fn decode_result_iter<I>(i: I) -> impl Iterator<Item = crate::Result<u8>>
where
    I: Iterator<Item = u8>,
{
    DecodeResultIterator::<I>::new(i)
}

/// Decode COBS/R-encoded data, getting data from a `&u8` iterator, and providing the output as an iterator.
///
/// The input data should be COBS/R-encoded, containing no zero-bytes.
///
/// The caller must provide a `&u8` iterator.
///
/// The return value is a `crate::Result<u8>` iterator. This is suitable to `collect()` into a
/// byte container wrapped in `crate::Result<>`.
///
///     let data_cobs = b"\x04ABC\x05ghijzxy".to_vec();
///     let decode_data: cobs2::Result<Vec<u8>> = cobs2::cobsr::decode_result_ref_iter(data_cobs.iter()).collect();
///     assert_eq!(decode_data.unwrap(), b"ABC\0ghij\0xyz");
pub fn decode_result_ref_iter<'a, I>(i: I) -> impl Iterator<Item = crate::Result<u8>> + 'a
where
    I: Iterator<Item = &'a u8> + 'a,
{
    DecodeResultIterator::<_>::new(i.copied())
}