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
// Adapted from [`bytes`](https://github.com/tokio-rs/bytes)

#[cfg(feature = "bytes-buf")]
use bytes::{Bytes, BytesMut};

/// Minimal Buffer trait
pub trait Buffer {
    /// Into immutable type
    type Freeze;

    /// Returns new `Buffer` with capacity
    fn with_capacity(capacity: usize) -> Self
    where
        Self: Sized;

    /// Appends given bytes to this `Buffer`.
    ///
    /// # Panics
    /// Can panic if current length plus `src` length overflows usize
    fn extend_from_slice(&mut self, src: &[u8]);

    /// Reserves capacity for at least `additional` more bytes to be inserted
    /// into the given `Buffer`.
    ///
    /// # Panics
    /// Can panic if current capacity plus `additional` overflows usize
    fn reserve(&mut self, additional: usize);

    /// Converts `self` into a Freeze type
    fn freeze(self) -> Self::Freeze;

    /// Advance the internal cursor of the `Buffer`
    ///
    /// # Safety
    /// Can't advance more than capacity of the `Buffer`
    ///
    /// # Panics
    /// Can panic if length plus `cnt` is bigger than capacity
    unsafe fn advance(&mut self, cnt: usize);

    /// Return unsafe ptr to current `Buffer` position
    ///
    /// # Safety
    /// If buffer is full, can return invalid pointer
    unsafe fn buf_ptr(&mut self) -> *mut u8;
}

#[cfg(feature = "bytes-buf")]
impl Buffer for BytesMut {
    type Freeze = Bytes;

    #[inline]
    fn with_capacity(capacity: usize) -> Self
    where
        Self: Sized,
    {
        BytesMut::with_capacity(capacity)
    }

    #[inline]
    fn extend_from_slice(&mut self, src: &[u8]) {
        self.reserve(src.len());
        unsafe {
            debug_assert!(self.capacity() - self.len() >= src.len());
            std::ptr::copy_nonoverlapping(src.as_ptr(), self.buf_ptr(), src.len());
            self.advance(src.len())
        }
    }

    #[inline(always)]
    fn reserve(&mut self, additional: usize) {
        self.reserve(additional);
    }

    #[inline(always)]
    fn freeze(self) -> Self::Freeze {
        self.freeze()
    }

    #[inline]
    unsafe fn advance(&mut self, cnt: usize) {
        let new_len = self.len() + cnt;
        debug_assert!(
            new_len <= self.capacity(),
            "new_len = {}; capacity = {}",
            new_len,
            self.capacity()
        );
        self.set_len(new_len);
    }

    #[inline]
    unsafe fn buf_ptr(&mut self) -> *mut u8 {
        self.as_mut_ptr().add(self.len())
    }
}

#[cfg(all(test, feature = "bytes-buf"))]
mod test {
    use super::*;

    use bytes::BytesMut;

    #[test]
    fn test() {
        let e = b"Hello world!";
        let mut buf: BytesMut = Buffer::with_capacity(0);
        Buffer::extend_from_slice(&mut buf, e);
        assert_eq!(e, &Buffer::freeze(buf)[..])
    }
}