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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
use heap_data::{TaggedPtr, HeapAllocation};
use std::fmt::{self, Write};
use std::hash;
use std::iter::FromIterator;
use std::io;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::ptr;
use std::slice;
use u32_to_usize;
use usize_to_u32;

/// A “zero copy” bytes buffer.
///
/// See [crate documentation](index.html) for an overview.
#[derive(Clone)]
pub struct BytesBuf(Inner);

/// The memory representation of `Inner` is one of two cases:
/// the heap-allocated case and the inline case.
/// The inline case is used for small buffers
/// (15 bytes or less on 64-bit platforms, 11 bytes or less on 32-bit).
///
/// * In the heap-allocated case, the fields are what their type says:
///   a pointer and two integers.
///   Because of the memory alignment requirement of `HeapData`,
///   `ptr`’s lower two bits are zero.
///
/// * In the inline-case, these same lower two bits of `ptr` are set to a non-zero value.
///   This serves as a tag to distinguish the two cases.
///   The rest of `ptr`’s lower byte stores the buffer’s length.
///   (4 bits would suffice for this since that length can not be more than 15.)
///   Finally the rest of `Inner`’s bytes are used to store the buffer’s content, inline.
///
///   To make this inline buffer an uninterrupted slice,
///   the metadata byte (that contains the tag and the length, `ptr`’s lower byte)
///   must be at an "edge" of `Inner`.
///   For this reason we use a different layout
///   on little-endian platforms (metadata byte at the start of `Inner`)
///   and on big-endian platforms (metadata byte at the end of `Inner`).
#[cfg(target_endian = "little")]
#[derive(Clone)]
#[repr(C)]  // Don’t re-order fields
struct Inner {
    ptr: TaggedPtr,
    start: u32,
    len: u32,
}

#[cfg(target_endian = "big")]
#[derive(Clone)]
#[repr(C)]  // Don’t re-order fields
struct Inner {
    start: u32,
    len: u32,
    ptr: TaggedPtr,
}

/// Offset from the start of `Inner` to the start of inline buffer data.
/// On little-endian the metadata byte is at the start, so inline data starts after that.
/// On big-endian the metadata byte is at the end of `Inner`.
#[cfg(target_endian = "little")]
const INLINE_DATA_OFFSET_BYTES: isize = 1;

#[cfg(target_endian = "big")]
const INLINE_DATA_OFFSET_BYTES: isize = 0;

const INLINE_LENGTH_MASK: usize = 0b_1111_1100;
const INLINE_LENGTH_OFFSET_BITS: usize = 2;

fn inline_length(metadata: usize) -> usize {
    (metadata & INLINE_LENGTH_MASK) >> INLINE_LENGTH_OFFSET_BITS
}

fn set_inline_length(metadata: usize, new_len: usize) -> usize {
    debug_assert!(new_len <= INLINE_CAPACITY);
    let without_len = metadata & !INLINE_LENGTH_MASK;
    let with_new_len = without_len | (new_len << INLINE_LENGTH_OFFSET_BITS);
    with_new_len
}

/// `size_of::<Inner>()`, except `size_of` can not be used in a constant expression.
#[cfg(target_pointer_width = "32")]
const SIZE_OF_INNER: usize = 4 + 4 + 4;

#[cfg(target_pointer_width = "64")]
const SIZE_OF_INNER: usize = 8 + 4 + 4;

#[allow(dead_code)]
unsafe fn static_assert(x: Inner) {
    mem::transmute::<Inner, [u8; SIZE_OF_INNER]>(x);  // Assert that SIZE_OF_INNER is correct
}

/// How many bytes can be stored inline, leaving one byte of metadata.
const INLINE_CAPACITY: usize = SIZE_OF_INNER - 1;

impl BytesBuf {
    /// Return a new, empty, inline buffer.
    #[inline]
    pub fn new() -> Self {
        let metadata = 0;  // Includes bits for `length = 0`
        BytesBuf(Inner {
            ptr: TaggedPtr::new_inline_data(metadata),
            start: 0,
            len: 0,
        })
    }

    /// Return a new buffer with capacity for at least (typically more than)
    /// the given number of bytes.
    ///
    /// ## Panics
    ///
    /// Panics if the requested capacity is greater than `std::u32::MAX` (4 gigabytes).
    ///
    /// ## Examples
    ///
    /// ```
    /// # use zbuf::BytesBuf;
    /// assert!(BytesBuf::with_capacity(17).capacity() >= 17);
    /// ```
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        if capacity <= INLINE_CAPACITY {
            Self::new()
        } else {
            BytesBuf(Inner {
                ptr: TaggedPtr::allocate(capacity),
                start: 0,
                len: 0,
            })
        }
    }

    #[inline]
    fn as_allocated(&self) -> Result<&HeapAllocation, usize> {
        self.0.ptr.as_allocated()
    }

    /// Return the length of this buffer, in bytes.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use zbuf::BytesBuf;
    /// assert_eq!(BytesBuf::from("🎉".as_bytes()).len(), 4);
    /// ```
    #[inline]
    pub fn len(&self) -> usize {
        match self.as_allocated() {
            Ok(_) => u32_to_usize(self.0.len),
            Err(metadata) => inline_length(metadata),
        }
    }

    /// Return whether this buffer is empty.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use zbuf::BytesBuf;
    /// assert_eq!(BytesBuf::new().is_empty(), true);
    /// assert_eq!(BytesBuf::from(b"abc".as_ref()).is_empty(), false);
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    fn data_and_uninitialized_tail(&mut self) -> (&mut [u8], *mut [u8]) {
        if self.0.ptr.is_shared_allocation() {
            *self = {
                let slice: &[u8] = self;
                Self::from(slice)
            }
        }

        if let Ok(metadata) = self.0.ptr.get_inline_data() {
            let len = inline_length(metadata);
            let struct_ptr: *mut Inner = &mut self.0;
            // Safety relies on INLINE_DATA_OFFSET_BYTES and INLINE_CAPACITY being correct
            // to give a slice within the memory layout of `Inner`.
            // Inline data is never uninitialized.
            unsafe {
                let data_ptr = (struct_ptr as *mut u8).offset(INLINE_DATA_OFFSET_BYTES);
                let inline = slice::from_raw_parts_mut(data_ptr, INLINE_CAPACITY);
                let (initialized, tail) = inline.split_at_mut(len);
                return (initialized, tail)
            }
        }

        let heap_allocation = self.0.ptr.as_owned_allocated_mut()
            .expect("expected owned allocation");

        let start = u32_to_usize(self.0.start);
        let len = u32_to_usize(self.0.len);
        let data = heap_allocation.data_mut();
        // Safety: the start..(start+len) range is known to be initialized.
        unsafe {
            let (initialized, tail) = (*data)[start..].split_at_mut(len);
            return (initialized, tail)
        }
    }

    /// Return the capacity of this buffer: the length to which it can grow
    /// without re-allocating.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use zbuf::BytesBuf;
    /// assert!(BytesBuf::with_capacity(17).capacity() >= 17);
    /// ```
    #[inline]
    pub fn capacity(&self) -> usize {
        if let Ok(heap_allocation) = self.as_allocated() {
            let capacity = if heap_allocation.is_owned() {
                heap_allocation.data_capacity().checked_sub(self.0.start)
                    .expect("data_capacity < start ??")
            } else {
                // This heap data is shared, we can’t write to it.
                // So we want `self.reserve(additional)` to reallocate if `additional > 0`,
                // but at the same time avoid `self.capacity() < self.len()`
                self.0.len
            };
            u32_to_usize(capacity)
        } else {
            INLINE_CAPACITY
        }
    }

    /// Remove the given number of bytes from the front (the start) of the buffer.
    ///
    /// This takes `O(1)` time and does not copy any heap-allocated data.
    ///
    /// ## Panics
    ///
    /// Panics if `bytes` is out of bounds.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use zbuf::BytesBuf;
    /// let mut buf = BytesBuf::from(b"hello".as_ref());
    /// buf.pop_front(2);
    /// assert_eq!(buf, b"llo");
    /// ```
    pub fn pop_front(&mut self, bytes: usize) {
        if let Ok(_) = self.as_allocated() {
            let bytes = usize_to_u32(bytes);
            match self.0.len.checked_sub(bytes) {
                None => panic!("tried to pop {} bytes, only {} are available", bytes, self.0.len),
                Some(new_len) => {
                    self.0.len = new_len;
                    self.0.start = self.0.start.checked_add(bytes).expect("overflow");
                }
            }
        } else {
            // `self` was small enough to be inline, so the new buffer will too.
            *self = Self::from(&self[bytes..])
        }
    }

    /// Remove the given number of bytes from the back (the end) of the buffer.
    ///
    /// This takes `O(1)` time and does not copy any heap-allocated data.
    ///
    /// ## Panics
    ///
    /// Panics if `bytes` is out of bounds.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use zbuf::BytesBuf;
    /// let mut buf = BytesBuf::from(b"hello".as_ref());
    /// buf.pop_back(2);
    /// assert_eq!(buf, b"hel");
    /// ```
    pub fn pop_back(&mut self, bytes: usize) {
        let len = self.len();
        match len.checked_sub(bytes) {
            None => panic!("tried to pop {} bytes, only {} are available", bytes, len),
            Some(new_len) => self.truncate(new_len)
        }
    }

    /// Split the buffer into two at the given index.
    ///
    /// Return a new buffer that contains bytes `[at, len)`,
    /// while `self` contains bytes `[0, at)`.
    ///
    /// # Panics
    ///
    /// Panics if `at` is out of bounds.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use zbuf::BytesBuf;
    /// let mut buf = BytesBuf::from(b"hello".as_ref());
    /// let tail = buf.split_off(2);
    /// assert_eq!(buf, b"he");
    /// assert_eq!(tail, b"llo");
    /// ```
    pub fn split_off(&mut self, at: usize) -> BytesBuf {
        let mut tail;
        if let Ok(_) = self.as_allocated() {
            let _: &[u8] = &self[at..];  // Check bounds
            let at = usize_to_u32(at);
            tail = self.clone();
            tail.0.start += at;
            tail.0.len -= at;
        } else {
            tail = Self::from(&self[at..])
        }
        self.truncate(at);
        tail
    }

    /// This makes the buffer empty but, unless it is shared, does not change its capacity.
    ///
    /// If potentially freeing memory is preferable, consider `buf = BytesBuf::empty()` instead.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use zbuf::BytesBuf;
    /// let mut buf = BytesBuf::from(b"hello".as_ref());
    /// assert_eq!(buf, b"hello");
    /// buf.clear();
    /// assert_eq!(buf, b"");
    /// assert!(buf.capacity() > 0);
    /// ```
    #[inline]
    pub fn clear(&mut self) {
        self.truncate(0)
    }

    /// Shortens the buffer to the specified length.
    ///
    /// If `new_len` is greater than the buffer’s current length, this has no effect.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use zbuf::BytesBuf;
    /// let mut buf = BytesBuf::from(b"hello".as_ref());
    /// buf.truncate(2);
    /// assert_eq!(buf, b"he");
    /// ```
    pub fn truncate(&mut self, new_len: usize) {
        if new_len < self.len() {
            // Safety: 0..len is known to be initialized
            unsafe {
                self.set_len(new_len)
            }
        }
    }

    /// Unsafe: 0..new_len data must be initialized
    unsafe fn set_len(&mut self, new_len: usize) {
        match self.as_allocated() {
            Ok(_) => {
                self.0.len = usize_to_u32(new_len)
            }
            Err(metadata) => {
                self.0.ptr = TaggedPtr::new_inline_data(set_inline_length(metadata, new_len))
            }
        }
    }

    /// Ensures that the buffer has capacity for at least (typically more than)
    /// `additional` bytes beyond its current length.
    ///
    /// This copies the data if this buffer is shared or if the existing capacity is insufficient.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use zbuf::BytesBuf;
    /// let mut buf = BytesBuf::from("abc".repeat(10).as_bytes());
    /// assert!(buf.capacity() < 100);
    /// buf.reserve(100);
    /// assert!(buf.capacity() >= 130);
    /// ```
    pub fn reserve(&mut self, additional: usize) {
        let new_capacity = self.len().checked_add(additional).expect("overflow");
        // self.capacity() already caps at self.len() for shared (not owned) heap-allocated buffers.
        if new_capacity > self.capacity() {
            let mut copy = Self::with_capacity(new_capacity);
            // Safety: copy_into_prefix’s contract
            unsafe {
                copy.write_to_uninitialized_tail(|uninit| copy_into_prefix(self, uninit))
            }
            *self = copy
        }
    }

    /// Extend this buffer by writing to its existing capacity.
    ///
    /// The closure is given a potentially-uninitialized mutable bytes slice,
    /// and returns the number of consecutive bytes written from the start of the slice.
    /// The buffer’s length is increased by that much.
    ///
    /// If `self.reserve(additional)` is called immediately before this method,
    /// the slice is at least `additional` bytes long.
    /// Without a `reserve` call the slice can be any length, including zero.
    ///
    /// This copies the existing data if there are other references to this buffer.
    ///
    /// ## Safety
    ///
    /// The closure must not *read* from the given slice, which may be uninitialized.
    /// It must initialize the `0..written` range, where `written` is the return value.
    ///
    /// ## Panics
    ///
    /// Panics if the value returned by the closure is larger than the given closure’s length.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use zbuf::BytesBuf;
    /// let mut buf = BytesBuf::from(b"hello".as_ref());
    /// buf.reserve(10);
    /// unsafe {
    ///     buf.write_to_uninitialized_tail(|uninitialized| {
    ///         for byte in &mut uninitialized[..3] {
    ///             *byte = b'!'
    ///         }
    ///         3
    ///     })
    /// }
    /// assert_eq!(buf, b"hello!!!");
    /// ```
    pub unsafe fn write_to_uninitialized_tail<F>(&mut self, f: F)
    where F: FnOnce(&mut [u8]) -> usize {
        let (_, tail) = self.data_and_uninitialized_tail();
        let written = f(&mut *tail);
        let new_len = self.len().checked_add(written).expect("overflow");
        assert!(written <= (*tail).len());
        // Safety relies on the closure returning a correct value:
        self.set_len(new_len)
    }

    /// Extend this buffer by writing to its existing capacity.
    ///
    /// The closure is given a mutable bytes slice
    /// that has been overwritten with zeros (which takes `O(n)` extra time).
    /// The buffer’s length is increased by the closure’s return value.
    ///
    /// If `self.reserve(additional)` is called immediately before this method,
    /// the slice is at least `additional` bytes long.
    /// Without a `reserve` call the slice can be any length, including zero.
    ///
    /// This copies the existing data if there are other references to this buffer.
    ///
    /// ## Panics
    ///
    /// Panics if the value returned by the closure is larger than the given closure’s length.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use zbuf::BytesBuf;
    /// let mut buf = BytesBuf::from(b"hello".as_ref());
    /// buf.reserve(10);
    /// buf.write_to_zeroed_tail(|zeroed| {
    ///     for byte in &mut zeroed[..3] {
    ///         *byte = b'!'
    ///     }
    ///     5
    /// });
    /// assert_eq!(buf, b"hello!!!\0\0");
    /// ```
    pub fn write_to_zeroed_tail<F>(&mut self, f: F)
    where F: FnOnce(&mut [u8]) -> usize {
        unsafe {
            self.write_to_uninitialized_tail(|tail| {
                ptr::write_bytes(tail.as_mut_ptr(), 0, tail.len());
                f(tail)
            })
        }
    }

    /// Extend this buffer by writing to its existing capacity, reading from an I/O stream.
    ///
    /// `Read::read` is called once with a potentially-uninitialized mutable bytes slice.
    ///
    /// If `self.reserve(additional)` is called immediately before this method,
    /// the slice is at least `additional` bytes long.
    /// Without a `reserve` call the slice can be any length, including zero.
    ///
    /// This copies the existing data if there are other references to this buffer.
    ///
    /// ## Safety
    ///
    /// The `Read` implementation must be “well-behaved”:
    /// never read from the slice it is given,
    /// and actually write the number of byte that the return value says were written.
    ///
    /// ## Examples
    ///
    /// ```
    /// #     use zbuf::{BytesBuf, StrBuf};
    /// #     fn do_io() -> std::io::Result<()> {
    /// let mut file = std::fs::File::open("bytesbuf.rs")?;
    /// let mut source = BytesBuf::with_capacity(file.metadata()?.len() as usize);
    /// unsafe {
    ///     while source.read_into_unititialized_tail_from(&mut file)? > 0 {}
    /// }
    /// // Self-referential test:
    /// assert!(StrBuf::from_utf8(source)?.contains("This string is unique"));
    /// #     Ok(())
    /// #     }
    /// #     do_io().unwrap()
    /// ```
    pub unsafe fn read_into_unititialized_tail_from<R>(&mut self, mut reader: R)
                                                       -> io::Result<usize>
    where R: io::Read {
        let mut result = Ok(0);
        self.write_to_uninitialized_tail(|tail| {
            let r = reader.read(tail);
            let written = match r {
                Ok(bytes) => bytes,
                // We don’t know how many bytes were actually written,
                // conservatively assume none:
                Err(_) => 0,
            };
            result = r;
            written
        });
        result
    }

    /// Appends the given bytes slice onto the end of this buffer.
    ///
    /// This copies the existing data if this buffer is shared
    /// or if the existing capacity is insufficient.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use zbuf::BytesBuf;
    /// let mut buf = BytesBuf::from(b"hello".as_ref());
    /// buf.push_slice(b" world!");
    /// assert_eq!(buf, b"hello world!");
    /// ```
    pub fn push_slice(&mut self, slice: &[u8]) {
        self.reserve(slice.len());
        // Safety: copy_into_prefix’s contract
        unsafe {
            self.write_to_uninitialized_tail(|uninit| copy_into_prefix(slice, uninit))
        }
    }

    /// Appends the given bytes buffer onto the end of this buffer.
    ///
    /// This is similar to [`push_slice`](#method.push_slice), but sometimes more efficient.
    ///
    /// ## Examples
    ///
    /// This allocates only once:
    ///
    /// ```
    /// # use zbuf::BytesBuf;
    /// let string = "abc".repeat(20);
    /// let mut buf = BytesBuf::from(string.as_bytes());
    /// let tail = buf.split_off(50);
    /// assert_eq!(buf.len(), 50);
    /// assert_eq!(tail.len(), 10);
    /// buf.push_buf(&tail);
    /// assert_eq!(buf, string.as_bytes());
    /// ```
    pub fn push_buf(&mut self, other: &BytesBuf) {
        if self.is_empty() {
            *self = other.clone();
            return
        }

        // FIXME: remove when borrows are non-lexical
        fn raw<T>(x: &T) -> *const T { x }

        if let (Ok(a), Ok(b)) = (self.as_allocated().map(raw), other.as_allocated().map(raw)) {
            // Two heap-allocated buffers…
            if ptr::eq(a, b) {
                // … that share the same heap allocation…
                if (self.0.start + self.0.len) == other.0.start {
                    // … and are contiguous
                    self.0.len += other.0.len;
                    return
                }
            }
        }
        self.push_slice(other)
    }
}

/// Copy `source` entirely at the start of `dest`. Return the number of bytes copied.
#[inline]
unsafe fn copy_into_prefix(source: &[u8], dest: *mut [u8]) -> usize {
    let len = source.len();
    (*dest)[..len].copy_from_slice(source);
    len
}

impl Deref for BytesBuf {
    type Target = [u8];

    fn deref(&self) -> &[u8] {
        match self.as_allocated() {
            Ok(heap_allocation) => {
                let start = u32_to_usize(self.0.start);
                let len = u32_to_usize(self.0.len);
                // Safety: start..(start+len) is known to be initialized
                unsafe {
                    &(*heap_allocation.data())[start..][..len]
                }
            }
            Err(metadata) => {
                let len = inline_length(metadata);
                let struct_ptr: *const Inner = &self.0;
                let struct_ptr = struct_ptr as *const u8;
                // Safety relies on INLINE_DATA_OFFSET_BYTES being correct
                // and set_inline_length() checking that `len < INLINE_CAPACITY`,
                // which yields a slice within the memory layout of `Inner`.
                // Inline data is never uninitialized.
                unsafe {
                    let data_ptr = struct_ptr.offset(INLINE_DATA_OFFSET_BYTES);
                    slice::from_raw_parts(data_ptr, len)
                }
            }
        }
    }
}

/// This copies the existing data if there are other references to this buffer.
impl DerefMut for BytesBuf {
    fn deref_mut(&mut self) -> &mut [u8] {
        let (data, _) = self.data_and_uninitialized_tail();
        data
    }
}

impl AsRef<[u8]> for BytesBuf {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self
    }
}

impl AsMut<[u8]> for BytesBuf {
    #[inline]
    fn as_mut(&mut self) -> &mut [u8] {
        self
    }
}

impl<'a> From<&'a [u8]> for BytesBuf {
    #[inline]
    fn from(slice: &'a [u8]) -> Self {
        let mut buf = Self::new();
        buf.push_slice(slice);
        buf
    }
}

impl<'a, 'b> From<&'a &'b [u8]> for BytesBuf {
    #[inline]
    fn from(slice: &'a &'b [u8]) -> Self {
        let mut buf = Self::new();
        buf.push_slice(slice);
        buf
    }
}

impl fmt::Debug for BytesBuf {
    #[inline]
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("b\"")?;
        for &byte in &**self {
            if let b' '...b'~' = byte {
                formatter.write_char(byte as char)?
            } else {
                write!(formatter, "\\x{:02X}", byte)?
            }
        }
        formatter.write_char('"')
    }
}

impl hash::Hash for BytesBuf {
    #[inline]
    fn hash<H>(&self, hasher: &mut H) where H: hash::Hasher {
        <[u8]>::hash(self, hasher)
    }
}

impl Default for BytesBuf {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl Eq for BytesBuf {}

impl<T: AsRef<[u8]>> PartialEq<T> for BytesBuf {
    #[inline]
    fn eq(&self, other: &T) -> bool {
        <[u8]>::eq(self, other.as_ref())
    }
}

impl Ord for BytesBuf {
    #[inline]
    fn cmp(&self, other: &Self) -> ::std::cmp::Ordering {
        <[u8]>::cmp(self, &other)
    }
}

impl<T: AsRef<[u8]>> PartialOrd<T> for BytesBuf {
    #[inline]
    fn partial_cmp(&self, other: &T) -> Option<::std::cmp::Ordering> {
        <[u8]>::partial_cmp(self, other.as_ref())
    }
}

impl<'a> Extend<&'a [u8]> for BytesBuf {
    #[inline]
    fn extend<I>(&mut self, iter: I) where I: IntoIterator<Item=&'a [u8]> {
        for item in iter {
            self.push_slice(item)
        }
    }
}

impl<'a> FromIterator<&'a [u8]> for BytesBuf {
    #[inline]
    fn from_iter<I>(iter: I) -> Self where I: IntoIterator<Item=&'a [u8]> {
        let mut buf = Self::new();
        buf.extend(iter);
        buf
    }
}

impl<'a> Extend<&'a BytesBuf> for BytesBuf {
    #[inline]
    fn extend<I>(&mut self, iter: I) where I: IntoIterator<Item=&'a BytesBuf> {
        for item in iter {
            self.push_buf(item)
        }
    }
}

impl<'a> FromIterator<&'a BytesBuf> for BytesBuf {
    #[inline]
    fn from_iter<I>(iter: I) -> Self where I: IntoIterator<Item=&'a BytesBuf> {
        let mut buf = Self::new();
        buf.extend(iter);
        buf
    }
}

impl Extend<BytesBuf> for BytesBuf {
    #[inline]
    fn extend<I>(&mut self, iter: I) where I: IntoIterator<Item=BytesBuf> {
        for item in iter {
            self.push_buf(&item)
        }
    }
}

impl FromIterator<BytesBuf> for BytesBuf {
    #[inline]
    fn from_iter<I>(iter: I) -> Self where I: IntoIterator<Item=BytesBuf> {
        let mut buf = Self::new();
        buf.extend(iter);
        buf
    }
}

impl io::Write for BytesBuf {
    #[inline]
    fn write(&mut self, slice: &[u8]) -> io::Result<usize> {
        self.push_slice(slice);
        Ok(slice.len())
    }

    #[inline]
    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}