seq-str 0.1.4

Flat collections of strings etc.
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
use alloc::vec::Vec;
use core::fmt;

/// A sequence of `&[u8]`, stored contiguously
///
/// This can be used as a drop-in replacement for `Vec<Vec<u8>>` in some cases,
/// with better memory locality and fewer memory allocations.
///
/// When using `SeqBytes` instead of `Vec<Vec<u8>>`, the individual byte strings
/// cannot be resized, but when this isn't needed there isn't much downside otherwise.
///
/// The container also supports "emplace"-style APIs like `in_place_writer`, which allow you to
/// write the next element directly into the contiguous buffer with minimal overhead.
#[derive(Clone, Default, Eq, PartialEq, Hash)]
pub struct SeqBytes {
    data: Vec<u8>,
    offsets: Vec<usize>,
}

impl SeqBytes {
    /// Create a new SeqBytes
    pub fn new() -> Self {
        Self::default()
    }

    /// Check if the sequence is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get the number of slices in the sequence
    pub fn len(&self) -> usize {
        self.offsets.len()
    }

    /// Reserve capacity for more slices
    pub fn reserve(&mut self, extra: usize) {
        self.offsets.reserve(extra);
        // Guess how much data to reserve based on existing usage
        if !self.is_empty() && extra > 0 {
            let a = extra * 4;
            let b = self.data.len() * 4;
            // estimate data.len * extra / offsets.len
            let c = (self.data.len() * extra)
                >> ((usize::BITS - 1) - self.offsets.len().leading_zeros());

            #[allow(clippy::collapsible_else_if)]
            let median = if a <= b {
                if a <= c { core::cmp::min(b, c) } else { a }
            } else {
                if a <= c { a } else { core::cmp::max(b, c) }
            };

            self.data.reserve(median);
        } else {
            self.data.reserve(4 * extra);
        }
    }

    /// Shrink container to fit the current data
    pub fn shrink_to_fit(&mut self) {
        self.data.shrink_to_fit();
        self.offsets.shrink_to_fit();
    }

    /// Get the sum of the lengths of the byte strings
    pub fn num_bytes(&self) -> usize {
        self.data.len()
    }

    /// Get the i'th element of the sequence in a checked manner
    pub fn get(&self, idx: usize) -> Option<&[u8]> {
        let first = self.offsets.get(idx)?;
        match self.offsets.get(idx + 1) {
            Some(second) => Some(&self.data[*first..*second]),
            None => Some(&self.data[*first..]),
        }
    }

    /// Get the i'th element of the sequence in a checked manner
    pub fn get_mut(&mut self, idx: usize) -> Option<&mut [u8]> {
        let first = self.offsets.get(idx)?;
        match self.offsets.get(idx + 1) {
            Some(second) => Some(&mut self.data[*first..*second]),
            None => Some(&mut self.data[*first..]),
        }
    }

    /// Check if the sequence contains a particular element
    pub fn contains(&self, s: impl AsRef<[u8]>) -> bool {
        let s = s.as_ref();
        self.iter().any(|b| b == s)
    }

    /// Push a &[u8] onto the sequence
    pub fn push(&mut self, s: impl AsRef<[u8]>) {
        self.offsets.push(self.data.len());
        self.data.extend(s.as_ref().iter());
    }

    /// Get the last &[u8] of the sequence
    pub fn last(&self) -> Option<&[u8]> {
        match self.offsets.last() {
            Some(o) => Some(&self.data[*o..]),
            None => None,
        }
    }

    /// Pop the last element of the container
    /// Note that we can't return it because of lifetimes, so call [last] before popping.
    pub fn pop(&mut self) {
        if let Some(o) = self.offsets.pop() {
            self.data.truncate(o);
        }
    }

    /// Iterate over the sequence of &[u8]
    pub fn iter(&self) -> SeqBytesIter<'_> // impl ExactSizeIterator<Item = &[u8]>
    {
        SeqBytesIter {
            data: &self.data[..],
            offsets: &self.offsets[..],
        }
    }

    /// Iterate over the sequence of &mut [u8]
    pub fn iter_mut(&mut self) -> SeqBytesIterMut<'_> // impl ExactSizeIterator<Item = &mut[u8]>
    {
        SeqBytesIterMut {
            data: &mut self.data[..],
            offsets: &self.offsets[..],
        }
    }

    /// Iterate over the sequence of &[u8], in chunks of given size.
    /// Returns an iterator which yields one iterator for each chunk.
    /// The last chunk may be smaller.
    pub fn chunks(&self, chunk_size: usize) -> SeqBytesChunksIter<'_> {
        SeqBytesChunksIter {
            chunk_size,
            iter: self.iter(),
        }
    }

    // Helper to convert range bounds object to a range
    fn range_bounds_to_range(
        &self,
        range_bounds: impl core::ops::RangeBounds<usize>,
    ) -> (usize, usize) {
        use core::ops::Bound;

        let start_idx = match range_bounds.start_bound() {
            Bound::Included(s) => *s,
            Bound::Excluded(s) => s + 1,
            Bound::Unbounded => 0,
        };

        let end_idx = match range_bounds.end_bound() {
            Bound::Included(e) => e + 1,
            Bound::Excluded(e) => *e,
            Bound::Unbounded => self.offsets.len(),
        };

        (start_idx, end_idx)
    }

    /// Iterate over a range of the sequence of `&[u8]`
    ///
    /// This resembles [std::collections::BTreeMap::range], and is needed becuase like `BTreeMap`,
    /// we can't implement `Deref` or `SliceIndex<Range>` and produce a slice of our contents.
    /// See also [as_vec].
    pub fn range(&self, range_bounds: impl core::ops::RangeBounds<usize>) -> SeqBytesIter<'_> {
        let (start_idx, end_idx) = self.range_bounds_to_range(range_bounds);
        let data_end = self
            .offsets
            .get(end_idx)
            .cloned()
            .unwrap_or(self.data.len());

        SeqBytesIter {
            data: &self.data[0..data_end],
            offsets: &self.offsets[start_idx..end_idx],
        }
    }

    /// Iterate over a range of the sequence of `&mut [u8]`
    pub fn range_mut(
        &mut self,
        range_bounds: impl core::ops::RangeBounds<usize>,
    ) -> SeqBytesIterMut<'_> {
        let (start_idx, end_idx) = self.range_bounds_to_range(range_bounds);
        let data_end = self
            .offsets
            .get(end_idx)
            .cloned()
            .unwrap_or(self.data.len());

        SeqBytesIterMut {
            data: &mut self.data[0..data_end],
            offsets: &self.offsets[start_idx..end_idx],
        }
    }

    /// Truncate to at most the first n slices
    pub fn truncate(&mut self, new_size: usize) {
        if let Some(off) = self.offsets.get(new_size) {
            self.data.truncate(*off);
            self.offsets.truncate(new_size);
        }
    }

    /// Resize to contain only the first n `&[u8]`, or pad up to n slices, with empty slices added
    pub fn resize(&mut self, new_size: usize) {
        if let Some(off) = self.offsets.get(new_size) {
            self.data.truncate(*off);
            self.offsets.truncate(new_size);
        } else {
            // Push empty slices until offsets has length new_size
            let d = new_size - self.offsets.len();
            self.offsets.reserve(d);
            for _ in 0..d {
                self.offsets.push(self.data.len());
            }
        }
    }

    /// Retain only those slices satisfying a predicate.
    /// The slices are always visited in order, similar to [std::vec::Vec::retain].
    pub fn retain(&mut self, mut pred: impl FnMut(&[u8]) -> bool) {
        self.retain_mut(|elem| pred(elem))
    }

    /// Retain only those slices satisfying a predicate.
    /// The slices are always visited in order, similar to [std::vec::Vec::retain_mut].
    pub fn retain_mut(&mut self, mut pred: impl FnMut(&mut [u8]) -> bool) {
        let (data, offsets) = (&mut self.data, &mut self.offsets);

        let mut offset_write_idx = 0;
        let mut kept_bytes = 0;

        // Invariant:
        // Offsets is not reduced in length during this loop
        // Data is not reduced in length during this loop
        for offset_idx in 0..offsets.len() {
            let start = offsets[offset_idx];
            let end = offsets.get(offset_idx + 1).cloned().unwrap_or(data.len());

            let outcome = pred(&mut data[start..end]);
            if outcome {
                // We will preserve kept_len additional bytes,
                // write their starting offset first.
                let kept_len = end - start;
                offsets[offset_write_idx] = kept_bytes;
                offset_write_idx += 1;

                // Move from data[start..end] to
                // data[kept_bytes, kept_bytes + kept_len]
                // if start == kept bytes we don't have to do anything
                if kept_bytes != start {
                    for byte_idx in 0..kept_len {
                        data[kept_bytes + byte_idx] = data[start + byte_idx];
                    }
                }
                kept_bytes += kept_len;
            }
        }
        drop(pred);

        // Truncate both offsets and data to what was actually retained
        offsets.truncate(offset_write_idx);
        data.truncate(kept_bytes);
    }

    /// Get an `impl std::io::Write` which can be used to write the next slice
    /// directly into the buffer without copying.
    #[cfg(feature = "std")]
    pub fn in_place_writer(&mut self) -> impl std::io::Write {
        // Correctness:
        // If we push a new offset on, then we have conceptually added a new string.
        // If the only thing that happens after that is that data is appended to self.data,
        // then the final state is correct and offsets doesn't need further adjusting.
        //
        // The only thing they can do with std::io::Write is push more bytes.
        // And no other changes can be made to SeqBytes until that writer is dropped,
        // because it captures &mut self.
        //
        // So we don't need to return an object with a Drop impl, we will always end
        // up in the correct state.
        self.offsets.push(self.data.len());
        &mut self.data
    }

    /// Version of `in_place_writer` that doesn't require `std::io::Write` trait
    ///
    /// Any bytes passed to the result of this function get concatenated to produce the
    /// newest byte string in the sequence. The new item is final when the writer is dropped.
    pub fn in_place_writer_no_std(&mut self) -> impl FnMut(&[u8]) {
        self.offsets.push(self.data.len());

        let dat = &mut self.data;

        move |b: &[u8]| {
            dat.extend(b);
        }
    }

    /// Express as a Vec<&[u8]>. The main reason that this may be useful is that there are
    /// useful methods on slice types `&[&[u8]]`, for example, [core::slice::binary_search],
    /// but `SeqBytes` itself doesn't implement `Deref` the way that `Vec` does and can
    /// only produce such a slice by allocating.
    ///
    /// Note: The trade-offs are that we would need more memory and `in_place_writer` would have
    /// to be more complicated and slower if we wanted our internal representation of the offsets
    /// to be a `Vec<&[u8]>`, which would allow such a `Deref` implementation.
    /// The direction we've taken is to add useful functions from `Vec` and slice
    /// as needed directly to this type instead, if they can't be obtained in a simpler way.
    pub fn as_vec(&self) -> Vec<&[u8]> {
        self.iter().collect()
    }

    /// Concatenate the `&[u8]` in the sequence into one `&[u8]`
    pub fn concat(&self) -> &[u8] {
        &self.data[..]
    }
}

impl core::ops::Index<usize> for SeqBytes {
    type Output = [u8];

    fn index(&self, index: usize) -> &[u8] {
        let first = self.offsets[index];
        match self.offsets.get(index + 1) {
            Some(second) => &self.data[first..*second],
            None => &self.data[first..],
        }
    }
}

impl core::ops::IndexMut<usize> for SeqBytes {
    fn index_mut(&mut self, index: usize) -> &mut [u8] {
        let first = self.offsets[index];
        match self.offsets.get(index + 1) {
            Some(second) => &mut self.data[first..*second],
            None => &mut self.data[first..],
        }
    }
}

impl fmt::Debug for SeqBytes {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_list().entries(self.iter()).finish()
    }
}

/// An iterator over a SeqBytes object
#[derive(Clone)]
pub struct SeqBytesIter<'a> {
    data: &'a [u8],
    offsets: &'a [usize],
}

impl<'a> Iterator for SeqBytesIter<'a> {
    type Item = &'a [u8];

    fn next(&mut self) -> Option<&'a [u8]> {
        let first = self.offsets.first()?;
        self.offsets = &self.offsets[1..];

        let second = self.offsets.first().cloned().unwrap_or(self.data.len());
        Some(&self.data[*first..second])
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.offsets.len();
        (remaining, Some(remaining))
    }
}

impl<'a> ExactSizeIterator for SeqBytesIter<'a> {}

impl<'a> DoubleEndedIterator for SeqBytesIter<'a> {
    fn next_back(&mut self) -> Option<&'a [u8]> {
        let last = *self.offsets.last()?;
        self.offsets = &self.offsets[..self.offsets.len() - 1];

        let (left, right) = self.data.split_at(last);
        self.data = left;

        Some(right)
    }
}

/// A mutable iterator over a SeqBytes object
pub struct SeqBytesIterMut<'a> {
    data: &'a mut [u8],
    offsets: &'a [usize],
}

impl<'a> Iterator for SeqBytesIterMut<'a> {
    type Item = &'a mut [u8];

    fn next(&mut self) -> Option<&'a mut [u8]> {
        let first = self.offsets.first()?;
        self.offsets = &self.offsets[1..];

        let second = self.offsets.first().cloned().unwrap_or(self.data.len());

        // Some(&mut self.data[*first..second])
        // Work around borrow checker issue
        let slice = unsafe {
            let start = self.data.as_mut_ptr().add(*first);
            core::slice::from_raw_parts_mut(start, second - first)
        };

        Some(slice)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.offsets.len();
        (remaining, Some(remaining))
    }
}

impl<'a> ExactSizeIterator for SeqBytesIterMut<'a> {}

impl<'a> DoubleEndedIterator for SeqBytesIterMut<'a> {
    fn next_back(&mut self) -> Option<&'a mut [u8]> {
        let last = *self.offsets.last()?;
        self.offsets = &self.offsets[..self.offsets.len() - 1];

        let (left, right) = self.data.split_at_mut(last);
        // Work around borrow checker issue
        self.data = unsafe {
            let len = left.len();
            let ptr = left.as_mut_ptr();
            core::slice::from_raw_parts_mut(ptr, len)
        };

        // Work around borrow checker issue
        let slice = unsafe {
            let len = right.len();
            let ptr = right.as_mut_ptr();
            core::slice::from_raw_parts_mut(ptr, len)
        };

        Some(slice)
    }
}

impl<A: AsRef<[u8]>> Extend<A> for SeqBytes {
    fn extend<T>(&mut self, iter: T)
    where
        T: IntoIterator<Item = A>,
    {
        let iter = iter.into_iter();
        self.reserve(iter.size_hint().0);
        for item in iter {
            self.push(item);
        }
    }
}

impl<A: AsRef<[u8]>> FromIterator<A> for SeqBytes {
    // Required method
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = A>,
    {
        let mut result = SeqBytes::default();
        result.extend(iter);
        result
    }
}

// IntoIterator can only be implemented for &'a SeqBytes,
// otherwise the buffer doesn't live long enough.
impl<'a> IntoIterator for &'a SeqBytes {
    type Item = &'a [u8];
    type IntoIter = SeqBytesIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

// A chunks iterator produces SeqBytesIter of a few items at a time
#[derive(Clone)]
pub struct SeqBytesChunksIter<'a> {
    chunk_size: usize,
    iter: SeqBytesIter<'a>,
}

impl<'a> Iterator for SeqBytesChunksIter<'a> {
    type Item = SeqBytesIter<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.iter.offsets.is_empty() {
            return None;
        }
        if self.iter.offsets.len() <= self.chunk_size {
            let iter = self.iter.clone();
            self.iter.offsets = &[];
            return Some(iter);
        }

        let (left, right) = self.iter.offsets.split_at(self.chunk_size);

        let data_end = right.first().copied().unwrap_or(self.iter.data.len());

        self.iter.offsets = right;

        Some(SeqBytesIter {
            data: &self.iter.data[..data_end],
            offsets: left,
        })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.iter.offsets.len().div_ceil(self.chunk_size);
        (remaining, Some(remaining))
    }
}

impl<'a> ExactSizeIterator for SeqBytesChunksIter<'a> {}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::{borrow::ToOwned, vec};

    #[test]
    fn vec_slice_conversions() {
        let vec_b = vec![b"1", b"2", b"3"];

        let seq_b: SeqBytes = vec_b.into_iter().collect();

        assert_eq!(seq_b.len(), 3);
        assert_eq!(&seq_b[0], b"1");
        assert_eq!(&seq_b[1], b"2");
        assert_eq!(&seq_b[2], b"3");

        let vec_str2 = seq_b.iter().map(ToOwned::to_owned).collect::<Vec<_>>();

        assert_eq!(vec_str2.len(), 3);
        assert_eq!(vec_str2[0], b"1");
        assert_eq!(vec_str2[1], b"2");
        assert_eq!(vec_str2[2], b"3");
    }

    #[test]
    fn vec_vec_b_conversions() {
        let vec_string = vec![b"1".to_owned(), b"2".to_owned(), b"3".to_owned()];

        let seq_b: SeqBytes = vec_string.into_iter().collect();

        assert_eq!(seq_b.len(), 3);
        assert_eq!(&seq_b[0], b"1");
        assert_eq!(&seq_b[1], b"2");
        assert_eq!(&seq_b[2], b"3");

        let vec_str2 = seq_b.iter().map(ToOwned::to_owned).collect::<Vec<_>>();

        assert_eq!(vec_str2.len(), 3);
        assert_eq!(vec_str2[0], b"1");
        assert_eq!(vec_str2[1], b"2");
        assert_eq!(vec_str2[2], b"3");
    }

    #[test]
    fn iter_rev() {
        let vec_b = vec![b"1", b"2", b"3"];

        let seq_b: SeqBytes = vec_b.into_iter().collect();

        assert_eq!(seq_b.len(), 3);
        assert_eq!(&seq_b[0], b"1");
        assert_eq!(&seq_b[1], b"2");
        assert_eq!(&seq_b[2], b"3");

        let seq_b2: SeqBytes = seq_b.into_iter().rev().collect();

        assert_eq!(seq_b2.len(), 3);
        assert_eq!(&seq_b2[0], b"3");
        assert_eq!(&seq_b2[1], b"2");
        assert_eq!(&seq_b2[2], b"1");
    }

    #[test]
    fn contains() {
        let vec_b: Vec<&[u8]> = vec![b"123", b"45", b"6", b"", b"7", b"89"];
        let seq_b: SeqBytes = vec_b.iter().collect();

        assert!(seq_b.contains(b"123"));
        assert!(!seq_b.contains(b"12"));
        assert!(!seq_b.contains(b"1"));
        assert!(seq_b.contains(b""));
        assert!(seq_b.contains(b"6"));
    }

    #[test]
    fn iter_mut() {
        let vec_b: Vec<&[u8]> = vec![b"123", b"45", b"6", b"", b"7", b"89"];
        let mut seq_b: SeqBytes = vec_b.iter().collect();

        for b in seq_b.iter_mut() {
            if b.len() > 0 {
                b[0] = b"a"[0];
            }
        }

        assert_eq!(seq_b.len(), 6);
        assert_eq!(&seq_b[0], b"a23");
        assert_eq!(&seq_b[1], b"a5");
        assert_eq!(&seq_b[2], b"a");
        assert_eq!(&seq_b[3], b"");
        assert_eq!(&seq_b[4], b"a");
        assert_eq!(&seq_b[5], b"a9");
    }

    #[test]
    fn retain() {
        let vec_b: Vec<&[u8]> = vec![b"123", b"45", b"6", b"", b"7", b"89"];
        let mut seq_b: SeqBytes = vec_b.iter().collect();

        assert_eq!(seq_b.len(), 6);
        assert_eq!(&seq_b[0], b"123");
        assert_eq!(&seq_b[1], b"45");
        assert_eq!(&seq_b[2], b"6");
        assert_eq!(&seq_b[3], b"");
        assert_eq!(&seq_b[4], b"7");
        assert_eq!(&seq_b[5], b"89");

        seq_b.retain(|b| !b.is_empty());

        assert_eq!(seq_b.len(), 5);
        assert_eq!(&seq_b[0], b"123");
        assert_eq!(&seq_b[1], b"45");
        assert_eq!(&seq_b[2], b"6");
        assert_eq!(&seq_b[3], b"7");
        assert_eq!(&seq_b[4], b"89");

        seq_b.retain(|b| b.len() >= 2);

        assert_eq!(seq_b.len(), 3);
        assert_eq!(&seq_b[0], b"123");
        assert_eq!(&seq_b[1], b"45");
        assert_eq!(&seq_b[2], b"89");

        seq_b.retain(|b| b.len() <= 2);

        assert_eq!(seq_b.len(), 2);
        assert_eq!(&seq_b[0], b"45");
        assert_eq!(&seq_b[1], b"89");

        seq_b.push(b"123");

        assert_eq!(seq_b.len(), 3);
        assert_eq!(&seq_b[0], b"45");
        assert_eq!(&seq_b[1], b"89");
        assert_eq!(&seq_b[2], b"123");

        seq_b.retain(|b| b.len() >= 3);

        assert_eq!(seq_b.len(), 1);
        assert_eq!(&seq_b[0], b"123");

        seq_b.resize(3);

        assert_eq!(seq_b.len(), 3);
        assert_eq!(&seq_b[0], b"123");
        assert_eq!(&seq_b[1], b"");
        assert_eq!(&seq_b[2], b"");

        seq_b.truncate(5);

        assert_eq!(seq_b.len(), 3);
        assert_eq!(&seq_b[0], b"123");
        assert_eq!(&seq_b[1], b"");
        assert_eq!(&seq_b[2], b"");

        seq_b.truncate(2);

        assert_eq!(seq_b.len(), 2);
        assert_eq!(&seq_b[0], b"123");
        assert_eq!(&seq_b[1], b"");
    }

    #[test]
    fn chunks() {
        let vec_b: Vec<&[u8]> = vec![b"123", b"45", b"6", b"", b"7", b"89"];
        let seq_b: SeqBytes = vec_b.iter().collect();

        let chunked: Vec<Vec<&[u8]>> = seq_b.chunks(2).map(|chunk| chunk.collect()).collect();

        assert_eq!(chunked.len(), 3);
        assert_eq!(chunked[0].len(), 2);
        assert_eq!(chunked[0][0], b"123");
        assert_eq!(chunked[0][1], b"45");
        assert_eq!(chunked[1].len(), 2);
        assert_eq!(chunked[1][0], b"6");
        assert_eq!(chunked[1][1], b"");
        assert_eq!(chunked[2].len(), 2);
        assert_eq!(chunked[2][0], b"7");
        assert_eq!(chunked[2][1], b"89");

        let chunked: Vec<Vec<&[u8]>> = seq_b.chunks(4).map(|chunk| chunk.collect()).collect();

        assert_eq!(chunked.len(), 2);
        assert_eq!(chunked[0].len(), 4);
        assert_eq!(chunked[0][0], b"123");
        assert_eq!(chunked[0][1], b"45");
        assert_eq!(chunked[0][2], b"6");
        assert_eq!(chunked[0][3], b"");
        assert_eq!(chunked[1].len(), 2);
        assert_eq!(chunked[1][0], b"7");
        assert_eq!(chunked[1][1], b"89");
    }
}