summaryrefslogtreecommitdiffstats
path: root/third_party/rust/bytes-0.4.9/src/buf
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/bytes-0.4.9/src/buf')
-rw-r--r--third_party/rust/bytes-0.4.9/src/buf/buf.rs1153
-rw-r--r--third_party/rust/bytes-0.4.9/src/buf/buf_mut.rs1166
-rw-r--r--third_party/rust/bytes-0.4.9/src/buf/chain.rs226
-rw-r--r--third_party/rust/bytes-0.4.9/src/buf/from_buf.rs117
-rw-r--r--third_party/rust/bytes-0.4.9/src/buf/into_buf.rs146
-rw-r--r--third_party/rust/bytes-0.4.9/src/buf/iter.rs116
-rw-r--r--third_party/rust/bytes-0.4.9/src/buf/mod.rs37
-rw-r--r--third_party/rust/bytes-0.4.9/src/buf/reader.rs88
-rw-r--r--third_party/rust/bytes-0.4.9/src/buf/take.rs155
-rw-r--r--third_party/rust/bytes-0.4.9/src/buf/writer.rs88
10 files changed, 3292 insertions, 0 deletions
diff --git a/third_party/rust/bytes-0.4.9/src/buf/buf.rs b/third_party/rust/bytes-0.4.9/src/buf/buf.rs
new file mode 100644
index 0000000000..b72c8d91cb
--- /dev/null
+++ b/third_party/rust/bytes-0.4.9/src/buf/buf.rs
@@ -0,0 +1,1153 @@
+use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain};
+use byteorder::{BigEndian, ByteOrder, LittleEndian};
+use iovec::IoVec;
+
+use std::{cmp, io, ptr};
+
+macro_rules! buf_get_impl {
+ ($this:ident, $size:expr, $conv:path) => ({
+ // try to convert directly from the bytes
+ let ret = {
+ // this Option<ret> trick is to avoid keeping a borrow on self
+ // when advance() is called (mut borrow) and to call bytes() only once
+ if let Some(src) = $this.bytes().get(..($size)) {
+ Some($conv(src))
+ } else {
+ None
+ }
+ };
+ if let Some(ret) = ret {
+ // if the direct convertion was possible, advance and return
+ $this.advance($size);
+ return ret;
+ } else {
+ // if not we copy the bytes in a temp buffer then convert
+ let mut buf = [0; ($size)];
+ $this.copy_to_slice(&mut buf); // (do the advance)
+ return $conv(&buf);
+ }
+ });
+ ($this:ident, $buf_size:expr, $conv:path, $len_to_read:expr) => ({
+ // The same trick as above does not improve the best case speed.
+ // It seems to be linked to the way the method is optimised by the compiler
+ let mut buf = [0; ($buf_size)];
+ $this.copy_to_slice(&mut buf[..($len_to_read)]);
+ return $conv(&buf[..($len_to_read)], $len_to_read);
+ });
+}
+
+/// Read bytes from a buffer.
+///
+/// A buffer stores bytes in memory such that read operations are infallible.
+/// The underlying storage may or may not be in contiguous memory. A `Buf` value
+/// is a cursor into the buffer. Reading from `Buf` advances the cursor
+/// position. It can be thought of as an efficient `Iterator` for collections of
+/// bytes.
+///
+/// The simplest `Buf` is a `Cursor` wrapping a `[u8]`.
+///
+/// ```
+/// use bytes::Buf;
+/// use std::io::Cursor;
+///
+/// let mut buf = Cursor::new(b"hello world");
+///
+/// assert_eq!(b'h', buf.get_u8());
+/// assert_eq!(b'e', buf.get_u8());
+/// assert_eq!(b'l', buf.get_u8());
+///
+/// let mut rest = [0; 8];
+/// buf.copy_to_slice(&mut rest);
+///
+/// assert_eq!(&rest[..], b"lo world");
+/// ```
+pub trait Buf {
+ /// Returns the number of bytes between the current position and the end of
+ /// the buffer.
+ ///
+ /// This value is greater than or equal to the length of the slice returned
+ /// by `bytes`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"hello world");
+ ///
+ /// assert_eq!(buf.remaining(), 11);
+ ///
+ /// buf.get_u8();
+ ///
+ /// assert_eq!(buf.remaining(), 10);
+ /// ```
+ ///
+ /// # Implementer notes
+ ///
+ /// Implementations of `remaining` should ensure that the return value does
+ /// not change unless a call is made to `advance` or any other function that
+ /// is documented to change the `Buf`'s current position.
+ fn remaining(&self) -> usize;
+
+ /// Returns a slice starting at the current position and of length between 0
+ /// and `Buf::remaining()`.
+ ///
+ /// This is a lower level function. Most operations are done with other
+ /// functions.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"hello world");
+ ///
+ /// assert_eq!(buf.bytes(), b"hello world");
+ ///
+ /// buf.advance(6);
+ ///
+ /// assert_eq!(buf.bytes(), b"world");
+ /// ```
+ ///
+ /// # Implementer notes
+ ///
+ /// This function should never panic. Once the end of the buffer is reached,
+ /// i.e., `Buf::remaining` returns 0, calls to `bytes` should return an
+ /// empty slice.
+ fn bytes(&self) -> &[u8];
+
+ /// Fills `dst` with potentially multiple slices starting at `self`'s
+ /// current position.
+ ///
+ /// If the `Buf` is backed by disjoint slices of bytes, `bytes_vec` enables
+ /// fetching more than one slice at once. `dst` is a slice of `IoVec`
+ /// references, enabling the slice to be directly used with [`writev`]
+ /// without any further conversion. The sum of the lengths of all the
+ /// buffers in `dst` will be less than or equal to `Buf::remaining()`.
+ ///
+ /// The entries in `dst` will be overwritten, but the data **contained** by
+ /// the slices **will not** be modified. If `bytes_vec` does not fill every
+ /// entry in `dst`, then `dst` is guaranteed to contain all remaining slices
+ /// in `self.
+ ///
+ /// This is a lower level function. Most operations are done with other
+ /// functions.
+ ///
+ /// # Implementer notes
+ ///
+ /// This function should never panic. Once the end of the buffer is reached,
+ /// i.e., `Buf::remaining` returns 0, calls to `bytes_vec` must return 0
+ /// without mutating `dst`.
+ ///
+ /// Implementations should also take care to properly handle being called
+ /// with `dst` being a zero length slice.
+ ///
+ /// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html
+ fn bytes_vec<'a>(&'a self, dst: &mut [&'a IoVec]) -> usize {
+ if dst.is_empty() {
+ return 0;
+ }
+
+ if self.has_remaining() {
+ dst[0] = self.bytes().into();
+ 1
+ } else {
+ 0
+ }
+ }
+
+ /// Advance the internal cursor of the Buf
+ ///
+ /// The next call to `bytes` will return a slice starting `cnt` bytes
+ /// further into the underlying buffer.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"hello world");
+ ///
+ /// assert_eq!(buf.bytes(), b"hello world");
+ ///
+ /// buf.advance(6);
+ ///
+ /// assert_eq!(buf.bytes(), b"world");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function **may** panic if `cnt > self.remaining()`.
+ ///
+ /// # Implementer notes
+ ///
+ /// It is recommended for implementations of `advance` to panic if `cnt >
+ /// self.remaining()`. If the implementation does not panic, the call must
+ /// behave as if `cnt == self.remaining()`.
+ ///
+ /// A call with `cnt == 0` should never panic and be a no-op.
+ fn advance(&mut self, cnt: usize);
+
+ /// Returns true if there are any more bytes to consume
+ ///
+ /// This is equivalent to `self.remaining() != 0`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"a");
+ ///
+ /// assert!(buf.has_remaining());
+ ///
+ /// buf.get_u8();
+ ///
+ /// assert!(!buf.has_remaining());
+ /// ```
+ fn has_remaining(&self) -> bool {
+ self.remaining() > 0
+ }
+
+ /// Copies bytes from `self` into `dst`.
+ ///
+ /// The cursor is advanced by the number of bytes copied. `self` must have
+ /// enough remaining bytes to fill `dst`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"hello world");
+ /// let mut dst = [0; 5];
+ ///
+ /// buf.copy_to_slice(&mut dst);
+ /// assert_eq!(b"hello", &dst);
+ /// assert_eq!(6, buf.remaining());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if `self.remaining() < dst.len()`
+ fn copy_to_slice(&mut self, dst: &mut [u8]) {
+ let mut off = 0;
+
+ assert!(self.remaining() >= dst.len());
+
+ while off < dst.len() {
+ let cnt;
+
+ unsafe {
+ let src = self.bytes();
+ cnt = cmp::min(src.len(), dst.len() - off);
+
+ ptr::copy_nonoverlapping(
+ src.as_ptr(), dst[off..].as_mut_ptr(), cnt);
+
+ off += src.len();
+ }
+
+ self.advance(cnt);
+ }
+ }
+
+ /// Gets an unsigned 8 bit integer from `self`.
+ ///
+ /// The current position is advanced by 1.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x08 hello");
+ /// assert_eq!(8, buf.get_u8());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is no more remaining data in `self`.
+ fn get_u8(&mut self) -> u8 {
+ assert!(self.remaining() >= 1);
+ let ret = self.bytes()[0];
+ self.advance(1);
+ ret
+ }
+
+ /// Gets a signed 8 bit integer from `self`.
+ ///
+ /// The current position is advanced by 1.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x08 hello");
+ /// assert_eq!(8, buf.get_i8());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is no more remaining data in `self`.
+ fn get_i8(&mut self) -> i8 {
+ assert!(self.remaining() >= 1);
+ let ret = self.bytes()[0] as i8;
+ self.advance(1);
+ ret
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use get_u16_be or get_u16_le")]
+ fn get_u16<T: ByteOrder>(&mut self) -> u16 where Self: Sized {
+ let mut buf = [0; 2];
+ self.copy_to_slice(&mut buf);
+ T::read_u16(&buf)
+ }
+
+ /// Gets an unsigned 16 bit integer from `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by 2.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x08\x09 hello");
+ /// assert_eq!(0x0809, buf.get_u16_be());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_u16_be(&mut self) -> u16 {
+ buf_get_impl!(self, 2, BigEndian::read_u16);
+ }
+
+ /// Gets an unsigned 16 bit integer from `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 2.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x09\x08 hello");
+ /// assert_eq!(0x0809, buf.get_u16_le());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_u16_le(&mut self) -> u16 {
+ buf_get_impl!(self, 2, LittleEndian::read_u16);
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use get_i16_be or get_i16_le")]
+ fn get_i16<T: ByteOrder>(&mut self) -> i16 where Self: Sized {
+ let mut buf = [0; 2];
+ self.copy_to_slice(&mut buf);
+ T::read_i16(&buf)
+ }
+
+ /// Gets a signed 16 bit integer from `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by 2.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x08\x09 hello");
+ /// assert_eq!(0x0809, buf.get_i16_be());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_i16_be(&mut self) -> i16 {
+ buf_get_impl!(self, 2, BigEndian::read_i16);
+ }
+
+ /// Gets a signed 16 bit integer from `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 2.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x09\x08 hello");
+ /// assert_eq!(0x0809, buf.get_i16_le());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_i16_le(&mut self) -> i16 {
+ buf_get_impl!(self, 2, LittleEndian::read_i16);
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use get_u32_be or get_u32_le")]
+ fn get_u32<T: ByteOrder>(&mut self) -> u32 where Self: Sized {
+ let mut buf = [0; 4];
+ self.copy_to_slice(&mut buf);
+ T::read_u32(&buf)
+ }
+
+ /// Gets an unsigned 32 bit integer from `self` in the big-endian byte order.
+ ///
+ /// The current position is advanced by 4.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x08\x09\xA0\xA1 hello");
+ /// assert_eq!(0x0809A0A1, buf.get_u32_be());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_u32_be(&mut self) -> u32 {
+ buf_get_impl!(self, 4, BigEndian::read_u32);
+ }
+
+ /// Gets an unsigned 32 bit integer from `self` in the little-endian byte order.
+ ///
+ /// The current position is advanced by 4.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\xA1\xA0\x09\x08 hello");
+ /// assert_eq!(0x0809A0A1, buf.get_u32_le());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_u32_le(&mut self) -> u32 {
+ buf_get_impl!(self, 4, LittleEndian::read_u32);
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use get_i32_be or get_i32_le")]
+ fn get_i32<T: ByteOrder>(&mut self) -> i32 where Self: Sized {
+ let mut buf = [0; 4];
+ self.copy_to_slice(&mut buf);
+ T::read_i32(&buf)
+ }
+
+ /// Gets a signed 32 bit integer from `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by 4.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x08\x09\xA0\xA1 hello");
+ /// assert_eq!(0x0809A0A1, buf.get_i32_be());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_i32_be(&mut self) -> i32 {
+ buf_get_impl!(self, 4, BigEndian::read_i32);
+ }
+
+ /// Gets a signed 32 bit integer from `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 4.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\xA1\xA0\x09\x08 hello");
+ /// assert_eq!(0x0809A0A1, buf.get_i32_le());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_i32_le(&mut self) -> i32 {
+ buf_get_impl!(self, 4, LittleEndian::read_i32);
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use get_u64_be or get_u64_le")]
+ fn get_u64<T: ByteOrder>(&mut self) -> u64 where Self: Sized {
+ let mut buf = [0; 8];
+ self.copy_to_slice(&mut buf);
+ T::read_u64(&buf)
+ }
+
+ /// Gets an unsigned 64 bit integer from `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by 8.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08 hello");
+ /// assert_eq!(0x0102030405060708, buf.get_u64_be());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_u64_be(&mut self) -> u64 {
+ buf_get_impl!(self, 8, BigEndian::read_u64);
+ }
+
+ /// Gets an unsigned 64 bit integer from `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 8.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x08\x07\x06\x05\x04\x03\x02\x01 hello");
+ /// assert_eq!(0x0102030405060708, buf.get_u64_le());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_u64_le(&mut self) -> u64 {
+ buf_get_impl!(self, 8, LittleEndian::read_u64);
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use get_i64_be or get_i64_le")]
+ fn get_i64<T: ByteOrder>(&mut self) -> i64 where Self: Sized {
+ let mut buf = [0; 8];
+ self.copy_to_slice(&mut buf);
+ T::read_i64(&buf)
+ }
+
+ /// Gets a signed 64 bit integer from `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by 8.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08 hello");
+ /// assert_eq!(0x0102030405060708, buf.get_i64_be());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_i64_be(&mut self) -> i64 {
+ buf_get_impl!(self, 8, BigEndian::read_i64);
+ }
+
+ /// Gets a signed 64 bit integer from `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 8.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x08\x07\x06\x05\x04\x03\x02\x01 hello");
+ /// assert_eq!(0x0102030405060708, buf.get_i64_le());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_i64_le(&mut self) -> i64 {
+ buf_get_impl!(self, 8, LittleEndian::read_i64);
+ }
+
+ /// Gets an unsigned 128 bit integer from `self` in big-endian byte order.
+ ///
+ /// **NOTE:** This method requires the `i128` feature.
+ /// The current position is advanced by 16.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello");
+ /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_be());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ #[cfg(feature = "i128")]
+ fn get_u128_be(&mut self) -> u128 {
+ buf_get_impl!(self, 16, BigEndian::read_u128);
+ }
+
+ /// Gets an unsigned 128 bit integer from `self` in little-endian byte order.
+ ///
+ /// **NOTE:** This method requires the `i128` feature.
+ /// The current position is advanced by 16.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello");
+ /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_le());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ #[cfg(feature = "i128")]
+ fn get_u128_le(&mut self) -> u128 {
+ buf_get_impl!(self, 16, LittleEndian::read_u128);
+ }
+
+ /// Gets a signed 128 bit integer from `self` in big-endian byte order.
+ ///
+ /// **NOTE:** This method requires the `i128` feature.
+ /// The current position is advanced by 16.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello");
+ /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_be());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ #[cfg(feature = "i128")]
+ fn get_i128_be(&mut self) -> i128 {
+ buf_get_impl!(self, 16, BigEndian::read_i128);
+ }
+
+ /// Gets a signed 128 bit integer from `self` in little-endian byte order.
+ ///
+ /// **NOTE:** This method requires the `i128` feature.
+ /// The current position is advanced by 16.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello");
+ /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_le());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ #[cfg(feature = "i128")]
+ fn get_i128_le(&mut self) -> i128 {
+ buf_get_impl!(self, 16, LittleEndian::read_i128);
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use get_uint_be or get_uint_le")]
+ fn get_uint<T: ByteOrder>(&mut self, nbytes: usize) -> u64 where Self: Sized {
+ let mut buf = [0; 8];
+ self.copy_to_slice(&mut buf[..nbytes]);
+ T::read_uint(&buf[..nbytes], nbytes)
+ }
+
+ /// Gets an unsigned n-byte integer from `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by `nbytes`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Buf, BigEndian};
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x01\x02\x03 hello");
+ /// assert_eq!(0x010203, buf.get_uint_be(3));
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_uint_be(&mut self, nbytes: usize) -> u64 {
+ buf_get_impl!(self, 8, BigEndian::read_uint, nbytes);
+ }
+
+ /// Gets an unsigned n-byte integer from `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by `nbytes`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x03\x02\x01 hello");
+ /// assert_eq!(0x010203, buf.get_uint_le(3));
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_uint_le(&mut self, nbytes: usize) -> u64 {
+ buf_get_impl!(self, 8, LittleEndian::read_uint, nbytes);
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use get_int_be or get_int_le")]
+ fn get_int<T: ByteOrder>(&mut self, nbytes: usize) -> i64 where Self: Sized {
+ let mut buf = [0; 8];
+ self.copy_to_slice(&mut buf[..nbytes]);
+ T::read_int(&buf[..nbytes], nbytes)
+ }
+
+ /// Gets a signed n-byte integer from `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by `nbytes`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x01\x02\x03 hello");
+ /// assert_eq!(0x010203, buf.get_int_be(3));
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_int_be(&mut self, nbytes: usize) -> i64 {
+ buf_get_impl!(self, 8, BigEndian::read_int, nbytes);
+ }
+
+ /// Gets a signed n-byte integer from `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by `nbytes`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x03\x02\x01 hello");
+ /// assert_eq!(0x010203, buf.get_int_le(3));
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_int_le(&mut self, nbytes: usize) -> i64 {
+ buf_get_impl!(self, 8, LittleEndian::read_int, nbytes);
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use get_f32_be or get_f32_le")]
+ fn get_f32<T: ByteOrder>(&mut self) -> f32 where Self: Sized {
+ let mut buf = [0; 4];
+ self.copy_to_slice(&mut buf);
+ T::read_f32(&buf)
+ }
+
+ /// Gets an IEEE754 single-precision (4 bytes) floating point number from
+ /// `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by 4.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x3F\x99\x99\x9A hello");
+ /// assert_eq!(1.2f32, buf.get_f32_be());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_f32_be(&mut self) -> f32 {
+ buf_get_impl!(self, 4, BigEndian::read_f32);
+ }
+
+ /// Gets an IEEE754 single-precision (4 bytes) floating point number from
+ /// `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 4.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x9A\x99\x99\x3F hello");
+ /// assert_eq!(1.2f32, buf.get_f32_le());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_f32_le(&mut self) -> f32 {
+ buf_get_impl!(self, 4, LittleEndian::read_f32);
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use get_f64_be or get_f64_le")]
+ fn get_f64<T: ByteOrder>(&mut self) -> f64 where Self: Sized {
+ let mut buf = [0; 8];
+ self.copy_to_slice(&mut buf);
+ T::read_f64(&buf)
+ }
+
+ /// Gets an IEEE754 double-precision (8 bytes) floating point number from
+ /// `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by 8.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello");
+ /// assert_eq!(1.2f64, buf.get_f64_be());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_f64_be(&mut self) -> f64 {
+ buf_get_impl!(self, 8, BigEndian::read_f64);
+ }
+
+ /// Gets an IEEE754 double-precision (8 bytes) floating point number from
+ /// `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 8.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello");
+ /// assert_eq!(1.2f64, buf.get_f64_le());
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining data in `self`.
+ fn get_f64_le(&mut self) -> f64 {
+ buf_get_impl!(self, 8, LittleEndian::read_f64);
+ }
+
+ /// Transforms a `Buf` into a concrete buffer.
+ ///
+ /// `collect()` can operate on any value that implements `Buf`, and turn it
+ /// into the relevent concrete buffer type.
+ ///
+ /// # Examples
+ ///
+ /// Collecting a buffer and loading the contents into a `Vec<u8>`.
+ ///
+ /// ```
+ /// use bytes::{Buf, Bytes, IntoBuf};
+ ///
+ /// let buf = Bytes::from(&b"hello world"[..]).into_buf();
+ /// let vec: Vec<u8> = buf.collect();
+ ///
+ /// assert_eq!(vec, &b"hello world"[..]);
+ /// ```
+ fn collect<B>(self) -> B
+ where Self: Sized,
+ B: FromBuf,
+ {
+ B::from_buf(self)
+ }
+
+ /// Creates an adaptor which will read at most `limit` bytes from `self`.
+ ///
+ /// This function returns a new instance of `Buf` which will read at most
+ /// `limit` bytes.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Buf, BufMut};
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new("hello world").take(5);
+ /// let mut dst = vec![];
+ ///
+ /// dst.put(&mut buf);
+ /// assert_eq!(dst, b"hello");
+ ///
+ /// let mut buf = buf.into_inner();
+ /// dst.clear();
+ /// dst.put(&mut buf);
+ /// assert_eq!(dst, b" world");
+ /// ```
+ fn take(self, limit: usize) -> Take<Self>
+ where Self: Sized
+ {
+ super::take::new(self, limit)
+ }
+
+ /// Creates an adaptor which will chain this buffer with another.
+ ///
+ /// The returned `Buf` instance will first consume all bytes from `self`.
+ /// Afterwards the output is equivalent to the output of next.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Bytes, Buf, IntoBuf};
+ /// use bytes::buf::Chain;
+ ///
+ /// let buf = Bytes::from(&b"hello "[..]).into_buf()
+ /// .chain(Bytes::from(&b"world"[..]));
+ ///
+ /// let full: Bytes = buf.collect();
+ /// assert_eq!(full[..], b"hello world"[..]);
+ /// ```
+ fn chain<U>(self, next: U) -> Chain<Self, U::Buf>
+ where U: IntoBuf,
+ Self: Sized,
+ {
+ Chain::new(self, next.into_buf())
+ }
+
+ /// Creates a "by reference" adaptor for this instance of `Buf`.
+ ///
+ /// The returned adaptor also implements `Buf` and will simply borrow `self`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Buf, BufMut};
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new("hello world");
+ /// let mut dst = vec![];
+ ///
+ /// {
+ /// let mut reference = buf.by_ref();
+ /// dst.put(&mut reference.take(5));
+ /// assert_eq!(dst, b"hello");
+ /// } // drop our &mut reference so we can use `buf` again
+ ///
+ /// dst.clear();
+ /// dst.put(&mut buf);
+ /// assert_eq!(dst, b" world");
+ /// ```
+ fn by_ref(&mut self) -> &mut Self where Self: Sized {
+ self
+ }
+
+ /// Creates an adaptor which implements the `Read` trait for `self`.
+ ///
+ /// This function returns a new value which implements `Read` by adapting
+ /// the `Read` trait functions to the `Buf` trait functions. Given that
+ /// `Buf` operations are infallible, none of the `Read` functions will
+ /// return with `Err`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Buf, IntoBuf, Bytes};
+ /// use std::io::Read;
+ ///
+ /// let buf = Bytes::from("hello world").into_buf();
+ ///
+ /// let mut reader = buf.reader();
+ /// let mut dst = [0; 1024];
+ ///
+ /// let num = reader.read(&mut dst).unwrap();
+ ///
+ /// assert_eq!(11, num);
+ /// assert_eq!(&dst[..11], b"hello world");
+ /// ```
+ fn reader(self) -> Reader<Self> where Self: Sized {
+ super::reader::new(self)
+ }
+
+ /// Returns an iterator over the bytes contained by the buffer.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Buf, IntoBuf, Bytes};
+ ///
+ /// let buf = Bytes::from(&b"abc"[..]).into_buf();
+ /// let mut iter = buf.iter();
+ ///
+ /// assert_eq!(iter.next(), Some(b'a'));
+ /// assert_eq!(iter.next(), Some(b'b'));
+ /// assert_eq!(iter.next(), Some(b'c'));
+ /// assert_eq!(iter.next(), None);
+ /// ```
+ fn iter(self) -> Iter<Self> where Self: Sized {
+ super::iter::new(self)
+ }
+}
+
+impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
+ fn remaining(&self) -> usize {
+ (**self).remaining()
+ }
+
+ fn bytes(&self) -> &[u8] {
+ (**self).bytes()
+ }
+
+ fn bytes_vec<'b>(&'b self, dst: &mut [&'b IoVec]) -> usize {
+ (**self).bytes_vec(dst)
+ }
+
+ fn advance(&mut self, cnt: usize) {
+ (**self).advance(cnt)
+ }
+}
+
+impl<T: Buf + ?Sized> Buf for Box<T> {
+ fn remaining(&self) -> usize {
+ (**self).remaining()
+ }
+
+ fn bytes(&self) -> &[u8] {
+ (**self).bytes()
+ }
+
+ fn bytes_vec<'b>(&'b self, dst: &mut [&'b IoVec]) -> usize {
+ (**self).bytes_vec(dst)
+ }
+
+ fn advance(&mut self, cnt: usize) {
+ (**self).advance(cnt)
+ }
+}
+
+impl<T: AsRef<[u8]>> Buf for io::Cursor<T> {
+ fn remaining(&self) -> usize {
+ let len = self.get_ref().as_ref().len();
+ let pos = self.position();
+
+ if pos >= len as u64 {
+ return 0;
+ }
+
+ len - pos as usize
+ }
+
+ fn bytes(&self) -> &[u8] {
+ let len = self.get_ref().as_ref().len();
+ let pos = self.position() as usize;
+
+ if pos >= len {
+ return Default::default();
+ }
+
+ &(self.get_ref().as_ref())[pos..]
+ }
+
+ fn advance(&mut self, cnt: usize) {
+ let pos = (self.position() as usize)
+ .checked_add(cnt).expect("overflow");
+
+ assert!(pos <= self.get_ref().as_ref().len());
+
+ self.set_position(pos as u64);
+ }
+}
+
+impl Buf for Option<[u8; 1]> {
+ fn remaining(&self) -> usize {
+ if self.is_some() {
+ 1
+ } else {
+ 0
+ }
+ }
+
+ fn bytes(&self) -> &[u8] {
+ self.as_ref().map(AsRef::as_ref)
+ .unwrap_or(Default::default())
+ }
+
+ fn advance(&mut self, cnt: usize) {
+ if cnt == 0 {
+ return;
+ }
+
+ if self.is_none() {
+ panic!("overflow");
+ } else {
+ assert_eq!(1, cnt);
+ *self = None;
+ }
+ }
+}
+
+// The existance of this function makes the compiler catch if the Buf
+// trait is "object-safe" or not.
+fn _assert_trait_object(_b: &Buf) {}
diff --git a/third_party/rust/bytes-0.4.9/src/buf/buf_mut.rs b/third_party/rust/bytes-0.4.9/src/buf/buf_mut.rs
new file mode 100644
index 0000000000..71dbda9afe
--- /dev/null
+++ b/third_party/rust/bytes-0.4.9/src/buf/buf_mut.rs
@@ -0,0 +1,1166 @@
+use super::{IntoBuf, Writer};
+use byteorder::{LittleEndian, ByteOrder, BigEndian};
+use iovec::IoVec;
+
+use std::{cmp, io, ptr, usize};
+
+/// A trait for values that provide sequential write access to bytes.
+///
+/// Write bytes to a buffer
+///
+/// A buffer stores bytes in memory such that write operations are infallible.
+/// The underlying storage may or may not be in contiguous memory. A `BufMut`
+/// value is a cursor into the buffer. Writing to `BufMut` advances the cursor
+/// position.
+///
+/// The simplest `BufMut` is a `Vec<u8>`.
+///
+/// ```
+/// use bytes::BufMut;
+///
+/// let mut buf = vec![];
+///
+/// buf.put("hello world");
+///
+/// assert_eq!(buf, b"hello world");
+/// ```
+pub trait BufMut {
+ /// Returns the number of bytes that can be written from the current
+ /// position until the end of the buffer is reached.
+ ///
+ /// This value is greater than or equal to the length of the slice returned
+ /// by `bytes_mut`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ /// use std::io::Cursor;
+ ///
+ /// let mut dst = [0; 10];
+ /// let mut buf = Cursor::new(&mut dst[..]);
+ ///
+ /// assert_eq!(10, buf.remaining_mut());
+ /// buf.put("hello");
+ ///
+ /// assert_eq!(5, buf.remaining_mut());
+ /// ```
+ ///
+ /// # Implementer notes
+ ///
+ /// Implementations of `remaining_mut` should ensure that the return value
+ /// does not change unless a call is made to `advance_mut` or any other
+ /// function that is documented to change the `BufMut`'s current position.
+ fn remaining_mut(&self) -> usize;
+
+ /// Advance the internal cursor of the BufMut
+ ///
+ /// The next call to `bytes_mut` will return a slice starting `cnt` bytes
+ /// further into the underlying buffer.
+ ///
+ /// This function is unsafe because there is no guarantee that the bytes
+ /// being advanced past have been initialized.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = Vec::with_capacity(16);
+ ///
+ /// unsafe {
+ /// buf.bytes_mut()[0] = b'h';
+ /// buf.bytes_mut()[1] = b'e';
+ ///
+ /// buf.advance_mut(2);
+ ///
+ /// buf.bytes_mut()[0] = b'l';
+ /// buf.bytes_mut()[1..3].copy_from_slice(b"lo");
+ ///
+ /// buf.advance_mut(3);
+ /// }
+ ///
+ /// assert_eq!(5, buf.len());
+ /// assert_eq!(buf, b"hello");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function **may** panic if `cnt > self.remaining_mut()`.
+ ///
+ /// # Implementer notes
+ ///
+ /// It is recommended for implementations of `advance_mut` to panic if
+ /// `cnt > self.remaining_mut()`. If the implementation does not panic,
+ /// the call must behave as if `cnt == self.remaining_mut()`.
+ ///
+ /// A call with `cnt == 0` should never panic and be a no-op.
+ unsafe fn advance_mut(&mut self, cnt: usize);
+
+ /// Returns true if there is space in `self` for more bytes.
+ ///
+ /// This is equivalent to `self.remaining_mut() != 0`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ /// use std::io::Cursor;
+ ///
+ /// let mut dst = [0; 5];
+ /// let mut buf = Cursor::new(&mut dst);
+ ///
+ /// assert!(buf.has_remaining_mut());
+ ///
+ /// buf.put("hello");
+ ///
+ /// assert!(!buf.has_remaining_mut());
+ /// ```
+ fn has_remaining_mut(&self) -> bool {
+ self.remaining_mut() > 0
+ }
+
+ /// Returns a mutable slice starting at the current BufMut position and of
+ /// length between 0 and `BufMut::remaining_mut()`.
+ ///
+ /// This is a lower level function. Most operations are done with other
+ /// functions.
+ ///
+ /// The returned byte slice may represent uninitialized memory.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = Vec::with_capacity(16);
+ ///
+ /// unsafe {
+ /// buf.bytes_mut()[0] = b'h';
+ /// buf.bytes_mut()[1] = b'e';
+ ///
+ /// buf.advance_mut(2);
+ ///
+ /// buf.bytes_mut()[0] = b'l';
+ /// buf.bytes_mut()[1..3].copy_from_slice(b"lo");
+ ///
+ /// buf.advance_mut(3);
+ /// }
+ ///
+ /// assert_eq!(5, buf.len());
+ /// assert_eq!(buf, b"hello");
+ /// ```
+ ///
+ /// # Implementer notes
+ ///
+ /// This function should never panic. `bytes_mut` should return an empty
+ /// slice **if and only if** `remaining_mut` returns 0. In other words,
+ /// `bytes_mut` returning an empty slice implies that `remaining_mut` will
+ /// return 0 and `remaining_mut` returning 0 implies that `bytes_mut` will
+ /// return an empty slice.
+ unsafe fn bytes_mut(&mut self) -> &mut [u8];
+
+ /// Fills `dst` with potentially multiple mutable slices starting at `self`'s
+ /// current position.
+ ///
+ /// If the `BufMut` is backed by disjoint slices of bytes, `bytes_vec_mut`
+ /// enables fetching more than one slice at once. `dst` is a slice of
+ /// mutable `IoVec` references, enabling the slice to be directly used with
+ /// [`readv`] without any further conversion. The sum of the lengths of all
+ /// the buffers in `dst` will be less than or equal to
+ /// `Buf::remaining_mut()`.
+ ///
+ /// The entries in `dst` will be overwritten, but the data **contained** by
+ /// the slices **will not** be modified. If `bytes_vec_mut` does not fill every
+ /// entry in `dst`, then `dst` is guaranteed to contain all remaining slices
+ /// in `self.
+ ///
+ /// This is a lower level function. Most operations are done with other
+ /// functions.
+ ///
+ /// # Implementer notes
+ ///
+ /// This function should never panic. Once the end of the buffer is reached,
+ /// i.e., `BufMut::remaining_mut` returns 0, calls to `bytes_vec_mut` must
+ /// return 0 without mutating `dst`.
+ ///
+ /// Implementations should also take care to properly handle being called
+ /// with `dst` being a zero length slice.
+ ///
+ /// [`readv`]: http://man7.org/linux/man-pages/man2/readv.2.html
+ unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [&'a mut IoVec]) -> usize {
+ if dst.is_empty() {
+ return 0;
+ }
+
+ if self.has_remaining_mut() {
+ dst[0] = self.bytes_mut().into();
+ 1
+ } else {
+ 0
+ }
+ }
+
+ /// Transfer bytes into `self` from `src` and advance the cursor by the
+ /// number of bytes written.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ ///
+ /// buf.put(b'h');
+ /// buf.put(&b"ello"[..]);
+ /// buf.put(" world");
+ ///
+ /// assert_eq!(buf, b"hello world");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// Panics if `self` does not have enough capacity to contain `src`.
+ fn put<T: IntoBuf>(&mut self, src: T) where Self: Sized {
+ use super::Buf;
+
+ let mut src = src.into_buf();
+
+ assert!(self.remaining_mut() >= src.remaining());
+
+ while src.has_remaining() {
+ let l;
+
+ unsafe {
+ let s = src.bytes();
+ let d = self.bytes_mut();
+ l = cmp::min(s.len(), d.len());
+
+ ptr::copy_nonoverlapping(
+ s.as_ptr(),
+ d.as_mut_ptr(),
+ l);
+ }
+
+ src.advance(l);
+ unsafe { self.advance_mut(l); }
+ }
+ }
+
+ /// Transfer bytes into `self` from `src` and advance the cursor by the
+ /// number of bytes written.
+ ///
+ /// `self` must have enough remaining capacity to contain all of `src`.
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ /// use std::io::Cursor;
+ ///
+ /// let mut dst = [0; 6];
+ ///
+ /// {
+ /// let mut buf = Cursor::new(&mut dst);
+ /// buf.put_slice(b"hello");
+ ///
+ /// assert_eq!(1, buf.remaining_mut());
+ /// }
+ ///
+ /// assert_eq!(b"hello\0", &dst);
+ /// ```
+ fn put_slice(&mut self, src: &[u8]) {
+ let mut off = 0;
+
+ assert!(self.remaining_mut() >= src.len(), "buffer overflow");
+
+ while off < src.len() {
+ let cnt;
+
+ unsafe {
+ let dst = self.bytes_mut();
+ cnt = cmp::min(dst.len(), src.len() - off);
+
+ ptr::copy_nonoverlapping(
+ src[off..].as_ptr(),
+ dst.as_mut_ptr(),
+ cnt);
+
+ off += cnt;
+
+ }
+
+ unsafe { self.advance_mut(cnt); }
+ }
+ }
+
+ /// Writes an unsigned 8 bit integer to `self`.
+ ///
+ /// The current position is advanced by 1.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_u8(0x01);
+ /// assert_eq!(buf, b"\x01");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_u8(&mut self, n: u8) {
+ let src = [n];
+ self.put_slice(&src);
+ }
+
+ /// Writes a signed 8 bit integer to `self`.
+ ///
+ /// The current position is advanced by 1.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_i8(0x01);
+ /// assert_eq!(buf, b"\x01");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_i8(&mut self, n: i8) {
+ let src = [n as u8];
+ self.put_slice(&src)
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use put_u16_be or put_u16_le")]
+ fn put_u16<T: ByteOrder>(&mut self, n: u16) where Self: Sized {
+ let mut buf = [0; 2];
+ T::write_u16(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes an unsigned 16 bit integer to `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by 2.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_u16_be(0x0809);
+ /// assert_eq!(buf, b"\x08\x09");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_u16_be(&mut self, n: u16) {
+ let mut buf = [0; 2];
+ BigEndian::write_u16(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes an unsigned 16 bit integer to `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 2.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_u16_le(0x0809);
+ /// assert_eq!(buf, b"\x09\x08");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_u16_le(&mut self, n: u16) {
+ let mut buf = [0; 2];
+ LittleEndian::write_u16(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use put_i16_be or put_i16_le")]
+ fn put_i16<T: ByteOrder>(&mut self, n: i16) where Self: Sized {
+ let mut buf = [0; 2];
+ T::write_i16(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes a signed 16 bit integer to `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by 2.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_i16_be(0x0809);
+ /// assert_eq!(buf, b"\x08\x09");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_i16_be(&mut self, n: i16) {
+ let mut buf = [0; 2];
+ BigEndian::write_i16(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes a signed 16 bit integer to `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 2.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_i16_le(0x0809);
+ /// assert_eq!(buf, b"\x09\x08");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_i16_le(&mut self, n: i16) {
+ let mut buf = [0; 2];
+ LittleEndian::write_i16(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use put_u32_be or put_u32_le")]
+ fn put_u32<T: ByteOrder>(&mut self, n: u32) where Self: Sized {
+ let mut buf = [0; 4];
+ T::write_u32(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes an unsigned 32 bit integer to `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by 4.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_u32_be(0x0809A0A1);
+ /// assert_eq!(buf, b"\x08\x09\xA0\xA1");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_u32_be(&mut self, n: u32) {
+ let mut buf = [0; 4];
+ BigEndian::write_u32(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes an unsigned 32 bit integer to `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 4.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_u32_le(0x0809A0A1);
+ /// assert_eq!(buf, b"\xA1\xA0\x09\x08");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_u32_le(&mut self, n: u32) {
+ let mut buf = [0; 4];
+ LittleEndian::write_u32(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use put_i32_be or put_i32_le")]
+ fn put_i32<T: ByteOrder>(&mut self, n: i32) where Self: Sized {
+ let mut buf = [0; 4];
+ T::write_i32(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes a signed 32 bit integer to `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by 4.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_i32_be(0x0809A0A1);
+ /// assert_eq!(buf, b"\x08\x09\xA0\xA1");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_i32_be(&mut self, n: i32) {
+ let mut buf = [0; 4];
+ BigEndian::write_i32(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes a signed 32 bit integer to `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 4.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_i32_le(0x0809A0A1);
+ /// assert_eq!(buf, b"\xA1\xA0\x09\x08");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_i32_le(&mut self, n: i32) {
+ let mut buf = [0; 4];
+ LittleEndian::write_i32(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use put_u64_be or put_u64_le")]
+ fn put_u64<T: ByteOrder>(&mut self, n: u64) where Self: Sized {
+ let mut buf = [0; 8];
+ T::write_u64(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes an unsigned 64 bit integer to `self` in the big-endian byte order.
+ ///
+ /// The current position is advanced by 8.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_u64_be(0x0102030405060708);
+ /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_u64_be(&mut self, n: u64) {
+ let mut buf = [0; 8];
+ BigEndian::write_u64(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes an unsigned 64 bit integer to `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 8.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_u64_le(0x0102030405060708);
+ /// assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_u64_le(&mut self, n: u64) {
+ let mut buf = [0; 8];
+ LittleEndian::write_u64(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use put_i64_be or put_i64_le")]
+ fn put_i64<T: ByteOrder>(&mut self, n: i64) where Self: Sized {
+ let mut buf = [0; 8];
+ T::write_i64(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes a signed 64 bit integer to `self` in the big-endian byte order.
+ ///
+ /// The current position is advanced by 8.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_i64_be(0x0102030405060708);
+ /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_i64_be(&mut self, n: i64) {
+ let mut buf = [0; 8];
+ BigEndian::write_i64(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes a signed 64 bit integer to `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 8.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_i64_le(0x0102030405060708);
+ /// assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_i64_le(&mut self, n: i64) {
+ let mut buf = [0; 8];
+ LittleEndian::write_i64(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes an unsigned 128 bit integer to `self` in the big-endian byte order.
+ ///
+ /// **NOTE:** This method requires the `i128` feature.
+ /// The current position is advanced by 16.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_u128_be(0x01020304050607080910111213141516);
+ /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ #[cfg(feature = "i128")]
+ fn put_u128_be(&mut self, n: u128) {
+ let mut buf = [0; 16];
+ BigEndian::write_u128(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes an unsigned 128 bit integer to `self` in little-endian byte order.
+ ///
+ /// **NOTE:** This method requires the `i128` feature.
+ /// The current position is advanced by 16.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_u128_le(0x01020304050607080910111213141516);
+ /// assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ #[cfg(feature = "i128")]
+ fn put_u128_le(&mut self, n: u128) {
+ let mut buf = [0; 16];
+ LittleEndian::write_u128(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes a signed 128 bit integer to `self` in the big-endian byte order.
+ ///
+ /// **NOTE:** This method requires the `i128` feature.
+ /// The current position is advanced by 16.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_i128_be(0x01020304050607080910111213141516);
+ /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ #[cfg(feature = "i128")]
+ fn put_i128_be(&mut self, n: i128) {
+ let mut buf = [0; 16];
+ BigEndian::write_i128(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes a signed 128 bit integer to `self` in little-endian byte order.
+ ///
+ /// **NOTE:** This method requires the `i128` feature.
+ /// The current position is advanced by 16.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_i128_le(0x01020304050607080910111213141516);
+ /// assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ #[cfg(feature = "i128")]
+ fn put_i128_le(&mut self, n: i128) {
+ let mut buf = [0; 16];
+ LittleEndian::write_i128(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use put_uint_be or put_uint_le")]
+ fn put_uint<T: ByteOrder>(&mut self, n: u64, nbytes: usize) where Self: Sized {
+ let mut buf = [0; 8];
+ T::write_uint(&mut buf, n, nbytes);
+ self.put_slice(&buf[0..nbytes])
+ }
+
+ /// Writes an unsigned n-byte integer to `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by `nbytes`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_uint_be(0x010203, 3);
+ /// assert_eq!(buf, b"\x01\x02\x03");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_uint_be(&mut self, n: u64, nbytes: usize) {
+ let mut buf = [0; 8];
+ BigEndian::write_uint(&mut buf, n, nbytes);
+ self.put_slice(&buf[0..nbytes])
+ }
+
+ /// Writes an unsigned n-byte integer to `self` in the little-endian byte order.
+ ///
+ /// The current position is advanced by `nbytes`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_uint_le(0x010203, 3);
+ /// assert_eq!(buf, b"\x03\x02\x01");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_uint_le(&mut self, n: u64, nbytes: usize) {
+ let mut buf = [0; 8];
+ LittleEndian::write_uint(&mut buf, n, nbytes);
+ self.put_slice(&buf[0..nbytes])
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use put_int_be or put_int_le")]
+ fn put_int<T: ByteOrder>(&mut self, n: i64, nbytes: usize) where Self: Sized {
+ let mut buf = [0; 8];
+ T::write_int(&mut buf, n, nbytes);
+ self.put_slice(&buf[0..nbytes])
+ }
+
+ /// Writes a signed n-byte integer to `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by `nbytes`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_int_be(0x010203, 3);
+ /// assert_eq!(buf, b"\x01\x02\x03");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_int_be(&mut self, n: i64, nbytes: usize) {
+ let mut buf = [0; 8];
+ BigEndian::write_int(&mut buf, n, nbytes);
+ self.put_slice(&buf[0..nbytes])
+ }
+
+ /// Writes a signed n-byte integer to `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by `nbytes`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_int_le(0x010203, 3);
+ /// assert_eq!(buf, b"\x03\x02\x01");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_int_le(&mut self, n: i64, nbytes: usize) {
+ let mut buf = [0; 8];
+ LittleEndian::write_int(&mut buf, n, nbytes);
+ self.put_slice(&buf[0..nbytes])
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use put_f32_be or put_f32_le")]
+ fn put_f32<T: ByteOrder>(&mut self, n: f32) where Self: Sized {
+ let mut buf = [0; 4];
+ T::write_f32(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes an IEEE754 single-precision (4 bytes) floating point number to
+ /// `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by 4.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_f32_be(1.2f32);
+ /// assert_eq!(buf, b"\x3F\x99\x99\x9A");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_f32_be(&mut self, n: f32) {
+ let mut buf = [0; 4];
+ BigEndian::write_f32(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes an IEEE754 single-precision (4 bytes) floating point number to
+ /// `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 4.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_f32_le(1.2f32);
+ /// assert_eq!(buf, b"\x9A\x99\x99\x3F");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_f32_le(&mut self, n: f32) {
+ let mut buf = [0; 4];
+ LittleEndian::write_f32(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ #[doc(hidden)]
+ #[deprecated(note="use put_f64_be or put_f64_le")]
+ fn put_f64<T: ByteOrder>(&mut self, n: f64) where Self: Sized {
+ let mut buf = [0; 8];
+ T::write_f64(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes an IEEE754 double-precision (8 bytes) floating point number to
+ /// `self` in big-endian byte order.
+ ///
+ /// The current position is advanced by 8.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_f64_be(1.2f64);
+ /// assert_eq!(buf, b"\x3F\xF3\x33\x33\x33\x33\x33\x33");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_f64_be(&mut self, n: f64) {
+ let mut buf = [0; 8];
+ BigEndian::write_f64(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Writes an IEEE754 double-precision (8 bytes) floating point number to
+ /// `self` in little-endian byte order.
+ ///
+ /// The current position is advanced by 8.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![];
+ /// buf.put_f64_le(1.2f64);
+ /// assert_eq!(buf, b"\x33\x33\x33\x33\x33\x33\xF3\x3F");
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if there is not enough remaining capacity in
+ /// `self`.
+ fn put_f64_le(&mut self, n: f64) {
+ let mut buf = [0; 8];
+ LittleEndian::write_f64(&mut buf, n);
+ self.put_slice(&buf)
+ }
+
+ /// Creates a "by reference" adaptor for this instance of `BufMut`.
+ ///
+ /// The returned adapter also implements `BufMut` and will simply borrow
+ /// `self`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ /// use std::io;
+ ///
+ /// let mut buf = vec![];
+ ///
+ /// {
+ /// let mut reference = buf.by_ref();
+ ///
+ /// // Adapt reference to `std::io::Write`.
+ /// let mut writer = reference.writer();
+ ///
+ /// // Use the buffer as a writter
+ /// io::Write::write(&mut writer, &b"hello world"[..]).unwrap();
+ /// } // drop our &mut reference so that we can use `buf` again
+ ///
+ /// assert_eq!(buf, &b"hello world"[..]);
+ /// ```
+ fn by_ref(&mut self) -> &mut Self where Self: Sized {
+ self
+ }
+
+ /// Creates an adaptor which implements the `Write` trait for `self`.
+ ///
+ /// This function returns a new value which implements `Write` by adapting
+ /// the `Write` trait functions to the `BufMut` trait functions. Given that
+ /// `BufMut` operations are infallible, none of the `Write` functions will
+ /// return with `Err`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BufMut;
+ /// use std::io::Write;
+ ///
+ /// let mut buf = vec![].writer();
+ ///
+ /// let num = buf.write(&b"hello world"[..]).unwrap();
+ /// assert_eq!(11, num);
+ ///
+ /// let buf = buf.into_inner();
+ ///
+ /// assert_eq!(*buf, b"hello world"[..]);
+ /// ```
+ fn writer(self) -> Writer<Self> where Self: Sized {
+ super::writer::new(self)
+ }
+}
+
+impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
+ fn remaining_mut(&self) -> usize {
+ (**self).remaining_mut()
+ }
+
+ unsafe fn bytes_mut(&mut self) -> &mut [u8] {
+ (**self).bytes_mut()
+ }
+
+ unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [&'b mut IoVec]) -> usize {
+ (**self).bytes_vec_mut(dst)
+ }
+
+ unsafe fn advance_mut(&mut self, cnt: usize) {
+ (**self).advance_mut(cnt)
+ }
+}
+
+impl<T: BufMut + ?Sized> BufMut for Box<T> {
+ fn remaining_mut(&self) -> usize {
+ (**self).remaining_mut()
+ }
+
+ unsafe fn bytes_mut(&mut self) -> &mut [u8] {
+ (**self).bytes_mut()
+ }
+
+ unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [&'b mut IoVec]) -> usize {
+ (**self).bytes_vec_mut(dst)
+ }
+
+ unsafe fn advance_mut(&mut self, cnt: usize) {
+ (**self).advance_mut(cnt)
+ }
+}
+
+impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
+ fn remaining_mut(&self) -> usize {
+ use Buf;
+ self.remaining()
+ }
+
+ /// Advance the internal cursor of the BufMut
+ unsafe fn advance_mut(&mut self, cnt: usize) {
+ use Buf;
+ self.advance(cnt);
+ }
+
+ /// Returns a mutable slice starting at the current BufMut position and of
+ /// length between 0 and `BufMut::remaining()`.
+ ///
+ /// The returned byte slice may represent uninitialized memory.
+ unsafe fn bytes_mut(&mut self) -> &mut [u8] {
+ let len = self.get_ref().as_ref().len();
+ let pos = self.position() as usize;
+
+ if pos >= len {
+ return Default::default();
+ }
+
+ &mut (self.get_mut().as_mut())[pos..]
+ }
+}
+
+impl BufMut for Vec<u8> {
+ #[inline]
+ fn remaining_mut(&self) -> usize {
+ usize::MAX - self.len()
+ }
+
+ #[inline]
+ unsafe fn advance_mut(&mut self, cnt: usize) {
+ let len = self.len();
+ let remaining = self.capacity() - len;
+ if cnt > remaining {
+ // Reserve additional capacity, and ensure that the total length
+ // will not overflow usize.
+ self.reserve(cnt);
+ }
+
+ self.set_len(len + cnt);
+ }
+
+ #[inline]
+ unsafe fn bytes_mut(&mut self) -> &mut [u8] {
+ use std::slice;
+
+ if self.capacity() == self.len() {
+ self.reserve(64); // Grow the vec
+ }
+
+ let cap = self.capacity();
+ let len = self.len();
+
+ let ptr = self.as_mut_ptr();
+ &mut slice::from_raw_parts_mut(ptr, cap)[len..]
+ }
+}
+
+// The existance of this function makes the compiler catch if the BufMut
+// trait is "object-safe" or not.
+fn _assert_trait_object(_b: &BufMut) {}
diff --git a/third_party/rust/bytes-0.4.9/src/buf/chain.rs b/third_party/rust/bytes-0.4.9/src/buf/chain.rs
new file mode 100644
index 0000000000..7dd44ab021
--- /dev/null
+++ b/third_party/rust/bytes-0.4.9/src/buf/chain.rs
@@ -0,0 +1,226 @@
+use {Buf, BufMut};
+use iovec::IoVec;
+
+/// A `Chain` sequences two buffers.
+///
+/// `Chain` is an adapter that links two underlying buffers and provides a
+/// continous view across both buffers. It is able to sequence either immutable
+/// buffers ([`Buf`] values) or mutable buffers ([`BufMut`] values).
+///
+/// This struct is generally created by calling [`Buf::chain`]. Please see that
+/// function's documentation for more detail.
+///
+/// # Examples
+///
+/// ```
+/// use bytes::{Bytes, Buf, IntoBuf};
+/// use bytes::buf::Chain;
+///
+/// let buf = Bytes::from(&b"hello "[..]).into_buf()
+/// .chain(Bytes::from(&b"world"[..]));
+///
+/// let full: Bytes = buf.collect();
+/// assert_eq!(full[..], b"hello world"[..]);
+/// ```
+///
+/// [`Buf::chain`]: trait.Buf.html#method.chain
+/// [`Buf`]: trait.Buf.html
+/// [`BufMut`]: trait.BufMut.html
+#[derive(Debug)]
+pub struct Chain<T, U> {
+ a: T,
+ b: U,
+}
+
+impl<T, U> Chain<T, U> {
+ /// Creates a new `Chain` sequencing the provided values.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::BytesMut;
+ /// use bytes::buf::Chain;
+ ///
+ /// let buf = Chain::new(
+ /// BytesMut::with_capacity(1024),
+ /// BytesMut::with_capacity(1024));
+ ///
+ /// // Use the chained buffer
+ /// ```
+ pub fn new(a: T, b: U) -> Chain<T, U> {
+ Chain {
+ a: a,
+ b: b,
+ }
+ }
+
+ /// Gets a reference to the first underlying `Buf`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Bytes, Buf, IntoBuf};
+ ///
+ /// let buf = Bytes::from(&b"hello"[..]).into_buf()
+ /// .chain(Bytes::from(&b"world"[..]));
+ ///
+ /// assert_eq!(buf.first_ref().get_ref()[..], b"hello"[..]);
+ /// ```
+ pub fn first_ref(&self) -> &T {
+ &self.a
+ }
+
+ /// Gets a mutable reference to the first underlying `Buf`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Bytes, Buf, IntoBuf};
+ ///
+ /// let mut buf = Bytes::from(&b"hello "[..]).into_buf()
+ /// .chain(Bytes::from(&b"world"[..]));
+ ///
+ /// buf.first_mut().set_position(1);
+ ///
+ /// let full: Bytes = buf.collect();
+ /// assert_eq!(full[..], b"ello world"[..]);
+ /// ```
+ pub fn first_mut(&mut self) -> &mut T {
+ &mut self.a
+ }
+
+ /// Gets a reference to the last underlying `Buf`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Bytes, Buf, IntoBuf};
+ ///
+ /// let buf = Bytes::from(&b"hello"[..]).into_buf()
+ /// .chain(Bytes::from(&b"world"[..]));
+ ///
+ /// assert_eq!(buf.last_ref().get_ref()[..], b"world"[..]);
+ /// ```
+ pub fn last_ref(&self) -> &U {
+ &self.b
+ }
+
+ /// Gets a mutable reference to the last underlying `Buf`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Bytes, Buf, IntoBuf};
+ ///
+ /// let mut buf = Bytes::from(&b"hello "[..]).into_buf()
+ /// .chain(Bytes::from(&b"world"[..]));
+ ///
+ /// buf.last_mut().set_position(1);
+ ///
+ /// let full: Bytes = buf.collect();
+ /// assert_eq!(full[..], b"hello orld"[..]);
+ /// ```
+ pub fn last_mut(&mut self) -> &mut U {
+ &mut self.b
+ }
+
+ /// Consumes this `Chain`, returning the underlying values.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Bytes, Buf, IntoBuf};
+ ///
+ /// let buf = Bytes::from(&b"hello"[..]).into_buf()
+ /// .chain(Bytes::from(&b"world"[..]));
+ ///
+ /// let (first, last) = buf.into_inner();
+ /// assert_eq!(first.get_ref()[..], b"hello"[..]);
+ /// assert_eq!(last.get_ref()[..], b"world"[..]);
+ /// ```
+ pub fn into_inner(self) -> (T, U) {
+ (self.a, self.b)
+ }
+}
+
+impl<T, U> Buf for Chain<T, U>
+ where T: Buf,
+ U: Buf,
+{
+ fn remaining(&self) -> usize {
+ self.a.remaining() + self.b.remaining()
+ }
+
+ fn bytes(&self) -> &[u8] {
+ if self.a.has_remaining() {
+ self.a.bytes()
+ } else {
+ self.b.bytes()
+ }
+ }
+
+ fn advance(&mut self, mut cnt: usize) {
+ let a_rem = self.a.remaining();
+
+ if a_rem != 0 {
+ if a_rem >= cnt {
+ self.a.advance(cnt);
+ return;
+ }
+
+ // Consume what is left of a
+ self.a.advance(a_rem);
+
+ cnt -= a_rem;
+ }
+
+ self.b.advance(cnt);
+ }
+
+ fn bytes_vec<'a>(&'a self, dst: &mut [&'a IoVec]) -> usize {
+ let mut n = self.a.bytes_vec(dst);
+ n += self.b.bytes_vec(&mut dst[n..]);
+ n
+ }
+}
+
+impl<T, U> BufMut for Chain<T, U>
+ where T: BufMut,
+ U: BufMut,
+{
+ fn remaining_mut(&self) -> usize {
+ self.a.remaining_mut() + self.b.remaining_mut()
+ }
+
+ unsafe fn bytes_mut(&mut self) -> &mut [u8] {
+ if self.a.has_remaining_mut() {
+ self.a.bytes_mut()
+ } else {
+ self.b.bytes_mut()
+ }
+ }
+
+ unsafe fn advance_mut(&mut self, mut cnt: usize) {
+ let a_rem = self.a.remaining_mut();
+
+ if a_rem != 0 {
+ if a_rem >= cnt {
+ self.a.advance_mut(cnt);
+ return;
+ }
+
+ // Consume what is left of a
+ self.a.advance_mut(a_rem);
+
+ cnt -= a_rem;
+ }
+
+ self.b.advance_mut(cnt);
+ }
+
+ unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [&'a mut IoVec]) -> usize {
+ let mut n = self.a.bytes_vec_mut(dst);
+ n += self.b.bytes_vec_mut(&mut dst[n..]);
+ n
+ }
+}
diff --git a/third_party/rust/bytes-0.4.9/src/buf/from_buf.rs b/third_party/rust/bytes-0.4.9/src/buf/from_buf.rs
new file mode 100644
index 0000000000..55f5cef31b
--- /dev/null
+++ b/third_party/rust/bytes-0.4.9/src/buf/from_buf.rs
@@ -0,0 +1,117 @@
+use {Buf, BufMut, IntoBuf, Bytes, BytesMut};
+
+/// Conversion from a [`Buf`]
+///
+/// Implementing `FromBuf` for a type defines how it is created from a buffer.
+/// This is common for types which represent byte storage of some kind.
+///
+/// [`FromBuf::from_buf`] is rarely called explicitly, and it is instead used
+/// through [`Buf::collect`]. See [`Buf::collect`] documentation for more examples.
+///
+/// See also [`IntoBuf`].
+///
+/// # Examples
+///
+/// Basic usage:
+///
+/// ```
+/// use bytes::{Bytes, IntoBuf};
+/// use bytes::buf::FromBuf;
+///
+/// let buf = Bytes::from(&b"hello world"[..]).into_buf();
+/// let vec = Vec::from_buf(buf);
+///
+/// assert_eq!(vec, &b"hello world"[..]);
+/// ```
+///
+/// Using [`Buf::collect`] to implicitly use `FromBuf`:
+///
+/// ```
+/// use bytes::{Buf, Bytes, IntoBuf};
+///
+/// let buf = Bytes::from(&b"hello world"[..]).into_buf();
+/// let vec: Vec<u8> = buf.collect();
+///
+/// assert_eq!(vec, &b"hello world"[..]);
+/// ```
+///
+/// Implementing `FromBuf` for your type:
+///
+/// ```
+/// use bytes::{BufMut, Bytes};
+/// use bytes::buf::{IntoBuf, FromBuf};
+///
+/// // A sample buffer, that's just a wrapper over Vec<u8>
+/// struct MyBuffer(Vec<u8>);
+///
+/// impl FromBuf for MyBuffer {
+/// fn from_buf<B>(buf: B) -> Self where B: IntoBuf {
+/// let mut v = Vec::new();
+/// v.put(buf.into_buf());
+/// MyBuffer(v)
+/// }
+/// }
+///
+/// // Now we can make a new buf
+/// let buf = Bytes::from(&b"hello world"[..]);
+///
+/// // And make a MyBuffer out of it
+/// let my_buf = MyBuffer::from_buf(buf);
+///
+/// assert_eq!(my_buf.0, &b"hello world"[..]);
+/// ```
+///
+/// [`Buf`]: trait.Buf.html
+/// [`FromBuf::from_buf`]: #method.from_buf
+/// [`Buf::collect`]: trait.Buf.html#method.collect
+/// [`IntoBuf`]: trait.IntoBuf.html
+pub trait FromBuf {
+ /// Creates a value from a buffer.
+ ///
+ /// See the [type-level documentation](#) for more details.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// use bytes::{Bytes, IntoBuf};
+ /// use bytes::buf::FromBuf;
+ ///
+ /// let buf = Bytes::from(&b"hello world"[..]).into_buf();
+ /// let vec = Vec::from_buf(buf);
+ ///
+ /// assert_eq!(vec, &b"hello world"[..]);
+ /// ```
+ fn from_buf<T>(buf: T) -> Self where T: IntoBuf;
+}
+
+impl FromBuf for Vec<u8> {
+ fn from_buf<T>(buf: T) -> Self
+ where T: IntoBuf
+ {
+ let buf = buf.into_buf();
+ let mut ret = Vec::with_capacity(buf.remaining());
+ ret.put(buf);
+ ret
+ }
+}
+
+impl FromBuf for Bytes {
+ fn from_buf<T>(buf: T) -> Self
+ where T: IntoBuf
+ {
+ BytesMut::from_buf(buf).freeze()
+ }
+}
+
+impl FromBuf for BytesMut {
+ fn from_buf<T>(buf: T) -> Self
+ where T: IntoBuf
+ {
+ let buf = buf.into_buf();
+ let mut ret = BytesMut::with_capacity(buf.remaining());
+ ret.put(buf);
+ ret
+ }
+}
diff --git a/third_party/rust/bytes-0.4.9/src/buf/into_buf.rs b/third_party/rust/bytes-0.4.9/src/buf/into_buf.rs
new file mode 100644
index 0000000000..4c3b420728
--- /dev/null
+++ b/third_party/rust/bytes-0.4.9/src/buf/into_buf.rs
@@ -0,0 +1,146 @@
+use super::{Buf};
+
+use std::io;
+
+/// Conversion into a `Buf`
+///
+/// An `IntoBuf` implementation defines how to convert a value into a `Buf`.
+/// This is common for types that represent byte storage of some kind. `IntoBuf`
+/// may be implemented directly for types or on references for those types.
+///
+/// # Examples
+///
+/// ```
+/// use bytes::{Buf, IntoBuf, BigEndian};
+///
+/// let bytes = b"\x00\x01hello world";
+/// let mut buf = bytes.into_buf();
+///
+/// assert_eq!(1, buf.get_u16::<BigEndian>());
+///
+/// let mut rest = [0; 11];
+/// buf.copy_to_slice(&mut rest);
+///
+/// assert_eq!(b"hello world", &rest);
+/// ```
+pub trait IntoBuf {
+ /// The `Buf` type that `self` is being converted into
+ type Buf: Buf;
+
+ /// Creates a `Buf` from a value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Buf, IntoBuf, BigEndian};
+ ///
+ /// let bytes = b"\x00\x01hello world";
+ /// let mut buf = bytes.into_buf();
+ ///
+ /// assert_eq!(1, buf.get_u16::<BigEndian>());
+ ///
+ /// let mut rest = [0; 11];
+ /// buf.copy_to_slice(&mut rest);
+ ///
+ /// assert_eq!(b"hello world", &rest);
+ /// ```
+ fn into_buf(self) -> Self::Buf;
+}
+
+impl<T: Buf> IntoBuf for T {
+ type Buf = Self;
+
+ fn into_buf(self) -> Self {
+ self
+ }
+}
+
+impl<'a> IntoBuf for &'a [u8] {
+ type Buf = io::Cursor<&'a [u8]>;
+
+ fn into_buf(self) -> Self::Buf {
+ io::Cursor::new(self)
+ }
+}
+
+impl<'a> IntoBuf for &'a mut [u8] {
+ type Buf = io::Cursor<&'a mut [u8]>;
+
+ fn into_buf(self) -> Self::Buf {
+ io::Cursor::new(self)
+ }
+}
+
+impl<'a> IntoBuf for &'a str {
+ type Buf = io::Cursor<&'a [u8]>;
+
+ fn into_buf(self) -> Self::Buf {
+ self.as_bytes().into_buf()
+ }
+}
+
+impl IntoBuf for Vec<u8> {
+ type Buf = io::Cursor<Vec<u8>>;
+
+ fn into_buf(self) -> Self::Buf {
+ io::Cursor::new(self)
+ }
+}
+
+impl<'a> IntoBuf for &'a Vec<u8> {
+ type Buf = io::Cursor<&'a [u8]>;
+
+ fn into_buf(self) -> Self::Buf {
+ io::Cursor::new(&self[..])
+ }
+}
+
+// Kind of annoying... but this impl is required to allow passing `&'static
+// [u8]` where for<'a> &'a T: IntoBuf is required.
+impl<'a> IntoBuf for &'a &'static [u8] {
+ type Buf = io::Cursor<&'static [u8]>;
+
+ fn into_buf(self) -> Self::Buf {
+ io::Cursor::new(self)
+ }
+}
+
+impl<'a> IntoBuf for &'a &'static str {
+ type Buf = io::Cursor<&'static [u8]>;
+
+ fn into_buf(self) -> Self::Buf {
+ self.as_bytes().into_buf()
+ }
+}
+
+impl IntoBuf for String {
+ type Buf = io::Cursor<Vec<u8>>;
+
+ fn into_buf(self) -> Self::Buf {
+ self.into_bytes().into_buf()
+ }
+}
+
+impl<'a> IntoBuf for &'a String {
+ type Buf = io::Cursor<&'a [u8]>;
+
+ fn into_buf(self) -> Self::Buf {
+ self.as_bytes().into_buf()
+ }
+}
+
+impl IntoBuf for u8 {
+ type Buf = Option<[u8; 1]>;
+
+ fn into_buf(self) -> Self::Buf {
+ Some([self])
+ }
+}
+
+impl IntoBuf for i8 {
+ type Buf = Option<[u8; 1]>;
+
+ fn into_buf(self) -> Self::Buf {
+ Some([self as u8; 1])
+ }
+}
diff --git a/third_party/rust/bytes-0.4.9/src/buf/iter.rs b/third_party/rust/bytes-0.4.9/src/buf/iter.rs
new file mode 100644
index 0000000000..9345c05b63
--- /dev/null
+++ b/third_party/rust/bytes-0.4.9/src/buf/iter.rs
@@ -0,0 +1,116 @@
+use Buf;
+
+/// Iterator over the bytes contained by the buffer.
+///
+/// This struct is created by the [`iter`] method on [`Buf`].
+///
+/// # Examples
+///
+/// Basic usage:
+///
+/// ```
+/// use bytes::{Buf, IntoBuf, Bytes};
+///
+/// let buf = Bytes::from(&b"abc"[..]).into_buf();
+/// let mut iter = buf.iter();
+///
+/// assert_eq!(iter.next(), Some(b'a'));
+/// assert_eq!(iter.next(), Some(b'b'));
+/// assert_eq!(iter.next(), Some(b'c'));
+/// assert_eq!(iter.next(), None);
+/// ```
+///
+/// [`iter`]: trait.Buf.html#method.iter
+/// [`Buf`]: trait.Buf.html
+#[derive(Debug)]
+pub struct Iter<T> {
+ inner: T,
+}
+
+impl<T> Iter<T> {
+ /// Consumes this `Iter`, returning the underlying value.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::{Buf, IntoBuf, Bytes};
+ ///
+ /// let buf = Bytes::from(&b"abc"[..]).into_buf();
+ /// let mut iter = buf.iter();
+ ///
+ /// assert_eq!(iter.next(), Some(b'a'));
+ ///
+ /// let buf = iter.into_inner();
+ /// assert_eq!(2, buf.remaining());
+ /// ```
+ pub fn into_inner(self) -> T {
+ self.inner
+ }
+
+ /// Gets a reference to the underlying `Buf`.
+ ///
+ /// It is inadvisable to directly read from the underlying `Buf`.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::{Buf, IntoBuf, Bytes};
+ ///
+ /// let buf = Bytes::from(&b"abc"[..]).into_buf();
+ /// let mut iter = buf.iter();
+ ///
+ /// assert_eq!(iter.next(), Some(b'a'));
+ ///
+ /// assert_eq!(2, iter.get_ref().remaining());
+ /// ```
+ pub fn get_ref(&self) -> &T {
+ &self.inner
+ }
+
+ /// Gets a mutable reference to the underlying `Buf`.
+ ///
+ /// It is inadvisable to directly read from the underlying `Buf`.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::{Buf, IntoBuf, BytesMut};
+ ///
+ /// let buf = BytesMut::from(&b"abc"[..]).into_buf();
+ /// let mut iter = buf.iter();
+ ///
+ /// assert_eq!(iter.next(), Some(b'a'));
+ ///
+ /// iter.get_mut().set_position(0);
+ ///
+ /// assert_eq!(iter.next(), Some(b'a'));
+ /// ```
+ pub fn get_mut(&mut self) -> &mut T {
+ &mut self.inner
+ }
+}
+
+pub fn new<T>(inner: T) -> Iter<T> {
+ Iter { inner: inner }
+}
+
+impl<T: Buf> Iterator for Iter<T> {
+ type Item = u8;
+
+ fn next(&mut self) -> Option<u8> {
+ if !self.inner.has_remaining() {
+ return None;
+ }
+
+ let b = self.inner.bytes()[0];
+ self.inner.advance(1);
+ Some(b)
+ }
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ let rem = self.inner.remaining();
+ (rem, Some(rem))
+ }
+}
+
+impl<T: Buf> ExactSizeIterator for Iter<T> { }
diff --git a/third_party/rust/bytes-0.4.9/src/buf/mod.rs b/third_party/rust/bytes-0.4.9/src/buf/mod.rs
new file mode 100644
index 0000000000..1f74e0ab40
--- /dev/null
+++ b/third_party/rust/bytes-0.4.9/src/buf/mod.rs
@@ -0,0 +1,37 @@
+//! Utilities for working with buffers.
+//!
+//! A buffer is any structure that contains a sequence of bytes. The bytes may
+//! or may not be stored in contiguous memory. This module contains traits used
+//! to abstract over buffers as well as utilities for working with buffer types.
+//!
+//! # `Buf`, `BufMut`
+//!
+//! These are the two foundational traits for abstractly working with buffers.
+//! They can be thought as iterators for byte structures. They offer additional
+//! performance over `Iterator` by providing an API optimized for byte slices.
+//!
+//! See [`Buf`] and [`BufMut`] for more details.
+//!
+//! [rope]: https://en.wikipedia.org/wiki/Rope_(data_structure)
+//! [`Buf`]: trait.Buf.html
+//! [`BufMut`]: trait.BufMut.html
+
+mod buf;
+mod buf_mut;
+mod from_buf;
+mod chain;
+mod into_buf;
+mod iter;
+mod reader;
+mod take;
+mod writer;
+
+pub use self::buf::Buf;
+pub use self::buf_mut::BufMut;
+pub use self::from_buf::FromBuf;
+pub use self::chain::Chain;
+pub use self::into_buf::IntoBuf;
+pub use self::iter::Iter;
+pub use self::reader::Reader;
+pub use self::take::Take;
+pub use self::writer::Writer;
diff --git a/third_party/rust/bytes-0.4.9/src/buf/reader.rs b/third_party/rust/bytes-0.4.9/src/buf/reader.rs
new file mode 100644
index 0000000000..59f9c33049
--- /dev/null
+++ b/third_party/rust/bytes-0.4.9/src/buf/reader.rs
@@ -0,0 +1,88 @@
+use {Buf};
+
+use std::{cmp, io};
+
+/// A `Buf` adapter which implements `io::Read` for the inner value.
+///
+/// This struct is generally created by calling `reader()` on `Buf`. See
+/// documentation of [`reader()`](trait.Buf.html#method.reader) for more
+/// details.
+#[derive(Debug)]
+pub struct Reader<B> {
+ buf: B,
+}
+
+pub fn new<B>(buf: B) -> Reader<B> {
+ Reader { buf: buf }
+}
+
+impl<B: Buf> Reader<B> {
+ /// Gets a reference to the underlying `Buf`.
+ ///
+ /// It is inadvisable to directly read from the underlying `Buf`.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::Buf;
+ /// use std::io::{self, Cursor};
+ ///
+ /// let mut buf = Cursor::new(b"hello world").reader();
+ ///
+ /// assert_eq!(0, buf.get_ref().position());
+ /// ```
+ pub fn get_ref(&self) -> &B {
+ &self.buf
+ }
+
+ /// Gets a mutable reference to the underlying `Buf`.
+ ///
+ /// It is inadvisable to directly read from the underlying `Buf`.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::Buf;
+ /// use std::io::{self, Cursor};
+ ///
+ /// let mut buf = Cursor::new(b"hello world").reader();
+ /// let mut dst = vec![];
+ ///
+ /// buf.get_mut().set_position(2);
+ /// io::copy(&mut buf, &mut dst).unwrap();
+ ///
+ /// assert_eq!(*dst, b"llo world"[..]);
+ /// ```
+ pub fn get_mut(&mut self) -> &mut B {
+ &mut self.buf
+ }
+
+ /// Consumes this `Reader`, returning the underlying value.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::Buf;
+ /// use std::io::{self, Cursor};
+ ///
+ /// let mut buf = Cursor::new(b"hello world").reader();
+ /// let mut dst = vec![];
+ ///
+ /// io::copy(&mut buf, &mut dst).unwrap();
+ ///
+ /// let buf = buf.into_inner();
+ /// assert_eq!(0, buf.remaining());
+ /// ```
+ pub fn into_inner(self) -> B {
+ self.buf
+ }
+}
+
+impl<B: Buf + Sized> io::Read for Reader<B> {
+ fn read(&mut self, dst: &mut [u8]) -> io::Result<usize> {
+ let len = cmp::min(self.buf.remaining(), dst.len());
+
+ Buf::copy_to_slice(&mut self.buf, &mut dst[0..len]);
+ Ok(len)
+ }
+}
diff --git a/third_party/rust/bytes-0.4.9/src/buf/take.rs b/third_party/rust/bytes-0.4.9/src/buf/take.rs
new file mode 100644
index 0000000000..a0c8ed479e
--- /dev/null
+++ b/third_party/rust/bytes-0.4.9/src/buf/take.rs
@@ -0,0 +1,155 @@
+use {Buf};
+
+use std::cmp;
+
+/// A `Buf` adapter which limits the bytes read from an underlying buffer.
+///
+/// This struct is generally created by calling `take()` on `Buf`. See
+/// documentation of [`take()`](trait.Buf.html#method.take) for more details.
+#[derive(Debug)]
+pub struct Take<T> {
+ inner: T,
+ limit: usize,
+}
+
+pub fn new<T>(inner: T, limit: usize) -> Take<T> {
+ Take {
+ inner: inner,
+ limit: limit,
+ }
+}
+
+impl<T> Take<T> {
+ /// Consumes this `Take`, returning the underlying value.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::{Buf, BufMut};
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"hello world").take(2);
+ /// let mut dst = vec![];
+ ///
+ /// dst.put(&mut buf);
+ /// assert_eq!(*dst, b"he"[..]);
+ ///
+ /// let mut buf = buf.into_inner();
+ ///
+ /// dst.clear();
+ /// dst.put(&mut buf);
+ /// assert_eq!(*dst, b"llo world"[..]);
+ /// ```
+ pub fn into_inner(self) -> T {
+ self.inner
+ }
+
+ /// Gets a reference to the underlying `Buf`.
+ ///
+ /// It is inadvisable to directly read from the underlying `Buf`.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::{Buf, BufMut};
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"hello world").take(2);
+ ///
+ /// assert_eq!(0, buf.get_ref().position());
+ /// ```
+ pub fn get_ref(&self) -> &T {
+ &self.inner
+ }
+
+ /// Gets a mutable reference to the underlying `Buf`.
+ ///
+ /// It is inadvisable to directly read from the underlying `Buf`.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::{Buf, BufMut};
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"hello world").take(2);
+ /// let mut dst = vec![];
+ ///
+ /// buf.get_mut().set_position(2);
+ ///
+ /// dst.put(&mut buf);
+ /// assert_eq!(*dst, b"ll"[..]);
+ /// ```
+ pub fn get_mut(&mut self) -> &mut T {
+ &mut self.inner
+ }
+
+ /// Returns the maximum number of bytes that can be read.
+ ///
+ /// # Note
+ ///
+ /// If the inner `Buf` has fewer bytes than indicated by this method then
+ /// that is the actual number of available bytes.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::Buf;
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"hello world").take(2);
+ ///
+ /// assert_eq!(2, buf.limit());
+ /// assert_eq!(b'h', buf.get_u8());
+ /// assert_eq!(1, buf.limit());
+ /// ```
+ pub fn limit(&self) -> usize {
+ self.limit
+ }
+
+ /// Sets the maximum number of bytes that can be read.
+ ///
+ /// # Note
+ ///
+ /// If the inner `Buf` has fewer bytes than `lim` then that is the actual
+ /// number of available bytes.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::{Buf, BufMut};
+ /// use std::io::Cursor;
+ ///
+ /// let mut buf = Cursor::new(b"hello world").take(2);
+ /// let mut dst = vec![];
+ ///
+ /// dst.put(&mut buf);
+ /// assert_eq!(*dst, b"he"[..]);
+ ///
+ /// dst.clear();
+ ///
+ /// buf.set_limit(3);
+ /// dst.put(&mut buf);
+ /// assert_eq!(*dst, b"llo"[..]);
+ /// ```
+ pub fn set_limit(&mut self, lim: usize) {
+ self.limit = lim
+ }
+}
+
+impl<T: Buf> Buf for Take<T> {
+ fn remaining(&self) -> usize {
+ cmp::min(self.inner.remaining(), self.limit)
+ }
+
+ fn bytes(&self) -> &[u8] {
+ let bytes = self.inner.bytes();
+ &bytes[..cmp::min(bytes.len(), self.limit)]
+ }
+
+ fn advance(&mut self, cnt: usize) {
+ assert!(cnt <= self.limit);
+ self.inner.advance(cnt);
+ self.limit -= cnt;
+ }
+}
diff --git a/third_party/rust/bytes-0.4.9/src/buf/writer.rs b/third_party/rust/bytes-0.4.9/src/buf/writer.rs
new file mode 100644
index 0000000000..38a739aa66
--- /dev/null
+++ b/third_party/rust/bytes-0.4.9/src/buf/writer.rs
@@ -0,0 +1,88 @@
+use BufMut;
+
+use std::{cmp, io};
+
+/// A `BufMut` adapter which implements `io::Write` for the inner value.
+///
+/// This struct is generally created by calling `writer()` on `BufMut`. See
+/// documentation of [`writer()`](trait.BufMut.html#method.writer) for more
+/// details.
+#[derive(Debug)]
+pub struct Writer<B> {
+ buf: B,
+}
+
+pub fn new<B>(buf: B) -> Writer<B> {
+ Writer { buf: buf }
+}
+
+impl<B: BufMut> Writer<B> {
+ /// Gets a reference to the underlying `BufMut`.
+ ///
+ /// It is inadvisable to directly write to the underlying `BufMut`.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = Vec::with_capacity(1024).writer();
+ ///
+ /// assert_eq!(1024, buf.get_ref().capacity());
+ /// ```
+ pub fn get_ref(&self) -> &B {
+ &self.buf
+ }
+
+ /// Gets a mutable reference to the underlying `BufMut`.
+ ///
+ /// It is inadvisable to directly write to the underlying `BufMut`.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::BufMut;
+ ///
+ /// let mut buf = vec![].writer();
+ ///
+ /// buf.get_mut().reserve(1024);
+ ///
+ /// assert_eq!(1024, buf.get_ref().capacity());
+ /// ```
+ pub fn get_mut(&mut self) -> &mut B {
+ &mut self.buf
+ }
+
+ /// Consumes this `Writer`, returning the underlying value.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::BufMut;
+ /// use std::io::{self, Cursor};
+ ///
+ /// let mut buf = vec![].writer();
+ /// let mut src = Cursor::new(b"hello world");
+ ///
+ /// io::copy(&mut src, &mut buf).unwrap();
+ ///
+ /// let buf = buf.into_inner();
+ /// assert_eq!(*buf, b"hello world"[..]);
+ /// ```
+ pub fn into_inner(self) -> B {
+ self.buf
+ }
+}
+
+impl<B: BufMut + Sized> io::Write for Writer<B> {
+ fn write(&mut self, src: &[u8]) -> io::Result<usize> {
+ let n = cmp::min(self.buf.remaining_mut(), src.len());
+
+ self.buf.put(&src[0..n]);
+ Ok(n)
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+}