From 042aa9023bcefbfdff13a99df6f4b04e141b0a35 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 5 Jul 2018 21:13:01 +0400 Subject: [PATCH 1/3] Fix `cargo doc` error on nightly caused by broken link to footnote (#218) --- src/bytes.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/bytes.rs b/src/bytes.rs index 2135ec0b1..48880d78a 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -95,11 +95,12 @@ use std::iter::{FromIterator, Iterator}; /// # Inline bytes /// /// As an optimization, when the slice referenced by a `Bytes` or `BytesMut` -/// handle is small enough [1], `Bytes` will avoid the allocation by inlining -/// the slice directly in the handle. In this case, a clone is no longer -/// "shallow" and the data will be copied. +/// handle is small enough [^1], `with_capacity` will avoid the allocation by +/// inlining the slice directly in the handle. In this case, a clone is no +/// longer "shallow" and the data will be copied. Converting from a `Vec` will +/// never use inlining. /// -/// [1] Small enough: 31 bytes on 64 bit systems, 15 on 32 bit systems. +/// [^1]: Small enough: 31 bytes on 64 bit systems, 15 on 32 bit systems. /// pub struct Bytes { inner: Inner, From 052648c3f5bb8875de721e3fa367016801e3f2ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20=C3=81vila=20de=20Esp=C3=ADndola?= Date: Thu, 12 Jul 2018 19:03:47 -0700 Subject: [PATCH 2/3] Implement IntoBuf for mut slices. (#214) With this if foo is a mutable slice, it is possible to do foo.into_buf().put_u32_le(42); Before this patch into_buf would create a Cursor<&'a [u8]> and it would not be possible to write into it. --- src/buf/into_buf.rs | 8 ++++++++ tests/test_bytes.rs | 9 ++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/buf/into_buf.rs b/src/buf/into_buf.rs index 1071908a2..4c3b42072 100644 --- a/src/buf/into_buf.rs +++ b/src/buf/into_buf.rs @@ -63,6 +63,14 @@ impl<'a> IntoBuf for &'a [u8] { } } +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]>; diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs index 03da9dd30..c0cba6b76 100644 --- a/tests/test_bytes.rs +++ b/tests/test_bytes.rs @@ -1,6 +1,6 @@ extern crate bytes; -use bytes::{Bytes, BytesMut, BufMut}; +use bytes::{Bytes, BytesMut, BufMut, IntoBuf}; const LONG: &'static [u8] = b"mary had a little lamb, little lamb, little lamb"; const SHORT: &'static [u8] = b"hello world"; @@ -303,6 +303,13 @@ fn fns_defined_for_bytes_mut() { assert_eq!(&v[..], bytes); } +#[test] +fn mut_into_buf() { + let mut v = vec![0, 0, 0, 0]; + let s = &mut v[..]; + s.into_buf().put_u32_le(42); +} + #[test] fn reserve_convert() { // Inline -> Vec From 890812af1b814c0f51430752dc825c723ba14e06 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Thu, 12 Jul 2018 19:05:35 -0700 Subject: [PATCH 3/3] inline Bytes::len and Bytes::is_empty (#211) --- src/bytes.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/bytes.rs b/src/bytes.rs index 48880d78a..89244dd40 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -465,6 +465,7 @@ impl Bytes { /// let b = Bytes::from(&b"hello"[..]); /// assert_eq!(b.len(), 5); /// ``` + #[inline] pub fn len(&self) -> usize { self.inner.len() } @@ -479,6 +480,7 @@ impl Bytes { /// let b = Bytes::new(); /// assert!(b.is_empty()); /// ``` + #[inline] pub fn is_empty(&self) -> bool { self.inner.is_empty() }