Compare commits

...
11 Commits
Author SHA1 Message Date
Alice RyhlandGitHub 21ed332836 chore: prepare bytes v1.4.0 (#593) 2023-01-31 20:38:32 +01:00
brian m. carlsonandGitHub 05e9d5cab9 Avoid large reallocations when freezing BytesMut (#592)
When we freeze a BytesMut, we turn it into a Vec, and then convert that
to a Bytes.  Currently, this happen using Vec::into_boxed_slice, which
reallocates to a slice of the same length as the Vev if the length and
the capacity are not equal.  This can pose a performance problem if the
Vec is large or if this happens many times in a loop.

Instead, let's compare the length and capacity, and if they're the same,
continue to handle this using into_boxed_slice.  Otherwise, since we
have a type of vtable which can handle a separate capacity, the shared
vtable, let's turn our Vec into that kind of Bytes.  While this does not
avoid allocation altogether, it performs a fixed size allocation and
avoids any need to memcpy.
2023-01-31 20:04:22 +01:00
0xc0001a2040andGitHub f15bba3375 Document which functions require std (#591) 2023-01-31 11:42:28 +01:00
c93a94b974 Fix duplicate "the the" typos (#585)
Co-authored-by: Nicolae Mihalache <[email protected]>
2022-12-20 11:49:55 +01:00
Matthijs van OtterdijkandGitHub 050d65b2ce make IntoIter constructor public (#581) 2022-11-25 22:48:20 +01:00
Sean LynchandGitHub 9b227220df chore: prepare bytes v1.3.0 (#579) 2022-11-21 08:06:49 +01:00
Sean LynchandGitHub 8a8c41f5c2 Implement native-endian get and put functions for Buf and BufMut (#576)
Fixes #549
2022-11-14 10:33:36 +01:00
Sean LynchandGitHub 5c7b4317e0 Update nightly snapshot to the current one (#577) 2022-11-14 10:56:33 +09:00
Yotam OfekandGitHub 6e4b1f244b Rename and expose BytesMut::spare_capacity_mut (#572) 2022-10-04 12:23:07 +02:00
Adam ChalmersandGitHub a36f661354 docs: Bytes::new etc should return Self not Bytes (#568) 2022-08-24 13:30:21 +02:00
Alice RyhlandGitHub d1b5d4ceb1 Don't have important data in unused capacity when calling reserve (#563) 2022-08-12 12:22:43 +02:00
12 changed files with 846 additions and 26 deletions
+2 -2
View File
@@ -11,7 +11,7 @@ on:
env:
RUSTFLAGS: -Dwarnings
RUST_BACKTRACE: 1
nightly: nightly-2021-11-05
nightly: nightly-2022-11-12
defaults:
run:
@@ -157,7 +157,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Install Rust
run: rustup update stable && rustup default stable
run: rustup update $nightly && rustup default $nightly
- name: Build documentation
run: cargo doc --no-deps --all-features
env:
+30
View File
@@ -1,3 +1,33 @@
# 1.4.0 (January 31, 2023)
### Added
- Make `IntoIter` constructor public (#581)
### Fixed
- Avoid large reallocations when freezing `BytesMut` (#592)
### Documented
- Document which functions require `std` (#591)
- Fix duplicate "the the" typos (#585)
# 1.3.0 (November 20, 2022)
### Added
- Rename and expose `BytesMut::spare_capacity_mut` (#572)
- Implement native-endian get and put functions for `Buf` and `BufMut` (#576)
### Fixed
- Don't have important data in unused capacity when calling reserve (#563)
### Documented
- `Bytes::new` etc should return `Self` not `Bytes` (#568)
# 1.2.1 (July 30, 2022)
### Fixed
+1 -1
View File
@@ -4,7 +4,7 @@ name = "bytes"
# When releasing to crates.io:
# - Update CHANGELOG.md.
# - Create "v1.x.y" git tag.
version = "1.2.1"
version = "1.4.0"
license = "MIT"
authors = [
"Carl Lerche <[email protected]>",
+9
View File
@@ -36,6 +36,15 @@ Serde support is optional and disabled by default. To enable use the feature `se
bytes = { version = "1", features = ["serde"] }
```
## Building documentation
When building the `bytes` documentation the `docsrs` option should be used, otherwise
feature gates will not be shown. This requires a nightly toolchain:
```
RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc
```
## License
This project is licensed under the [MIT license](LICENSE).
+320
View File
@@ -160,6 +160,7 @@ pub trait Buf {
///
/// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
if dst.is_empty() {
return 0;
@@ -354,6 +355,29 @@ pub trait Buf {
buf_get_impl!(self, u16::from_le_bytes);
}
/// Gets an unsigned 16 bit integer from `self` in native-endian byte order.
///
/// The current position is advanced by 2.
///
/// # Examples
///
/// ```
/// use bytes::Buf;
///
/// let mut buf: &[u8] = match cfg!(target_endian = "big") {
/// true => b"\x08\x09 hello",
/// false => b"\x09\x08 hello",
/// };
/// assert_eq!(0x0809, buf.get_u16_ne());
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining data in `self`.
fn get_u16_ne(&mut self) -> u16 {
buf_get_impl!(self, u16::from_ne_bytes);
}
/// Gets a signed 16 bit integer from `self` in big-endian byte order.
///
/// The current position is advanced by 2.
@@ -394,6 +418,29 @@ pub trait Buf {
buf_get_impl!(self, i16::from_le_bytes);
}
/// Gets a signed 16 bit integer from `self` in native-endian byte order.
///
/// The current position is advanced by 2.
///
/// # Examples
///
/// ```
/// use bytes::Buf;
///
/// let mut buf: &[u8] = match cfg!(target_endian = "big") {
/// true => b"\x08\x09 hello",
/// false => b"\x09\x08 hello",
/// };
/// assert_eq!(0x0809, buf.get_i16_ne());
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining data in `self`.
fn get_i16_ne(&mut self) -> i16 {
buf_get_impl!(self, i16::from_ne_bytes);
}
/// Gets an unsigned 32 bit integer from `self` in the big-endian byte order.
///
/// The current position is advanced by 4.
@@ -434,6 +481,29 @@ pub trait Buf {
buf_get_impl!(self, u32::from_le_bytes);
}
/// Gets an unsigned 32 bit integer from `self` in native-endian byte order.
///
/// The current position is advanced by 4.
///
/// # Examples
///
/// ```
/// use bytes::Buf;
///
/// let mut buf: &[u8] = match cfg!(target_endian = "big") {
/// true => b"\x08\x09\xA0\xA1 hello",
/// false => b"\xA1\xA0\x09\x08 hello",
/// };
/// assert_eq!(0x0809A0A1, buf.get_u32_ne());
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining data in `self`.
fn get_u32_ne(&mut self) -> u32 {
buf_get_impl!(self, u32::from_ne_bytes);
}
/// Gets a signed 32 bit integer from `self` in big-endian byte order.
///
/// The current position is advanced by 4.
@@ -474,6 +544,29 @@ pub trait Buf {
buf_get_impl!(self, i32::from_le_bytes);
}
/// Gets a signed 32 bit integer from `self` in native-endian byte order.
///
/// The current position is advanced by 4.
///
/// # Examples
///
/// ```
/// use bytes::Buf;
///
/// let mut buf: &[u8] = match cfg!(target_endian = "big") {
/// true => b"\x08\x09\xA0\xA1 hello",
/// false => b"\xA1\xA0\x09\x08 hello",
/// };
/// assert_eq!(0x0809A0A1, buf.get_i32_ne());
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining data in `self`.
fn get_i32_ne(&mut self) -> i32 {
buf_get_impl!(self, i32::from_ne_bytes);
}
/// Gets an unsigned 64 bit integer from `self` in big-endian byte order.
///
/// The current position is advanced by 8.
@@ -514,6 +607,29 @@ pub trait Buf {
buf_get_impl!(self, u64::from_le_bytes);
}
/// Gets an unsigned 64 bit integer from `self` in native-endian byte order.
///
/// The current position is advanced by 8.
///
/// # Examples
///
/// ```
/// use bytes::Buf;
///
/// let mut buf: &[u8] = match cfg!(target_endian = "big") {
/// true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello",
/// false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello",
/// };
/// assert_eq!(0x0102030405060708, buf.get_u64_ne());
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining data in `self`.
fn get_u64_ne(&mut self) -> u64 {
buf_get_impl!(self, u64::from_ne_bytes);
}
/// Gets a signed 64 bit integer from `self` in big-endian byte order.
///
/// The current position is advanced by 8.
@@ -554,6 +670,29 @@ pub trait Buf {
buf_get_impl!(self, i64::from_le_bytes);
}
/// Gets a signed 64 bit integer from `self` in native-endian byte order.
///
/// The current position is advanced by 8.
///
/// # Examples
///
/// ```
/// use bytes::Buf;
///
/// let mut buf: &[u8] = match cfg!(target_endian = "big") {
/// true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello",
/// false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello",
/// };
/// assert_eq!(0x0102030405060708, buf.get_i64_ne());
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining data in `self`.
fn get_i64_ne(&mut self) -> i64 {
buf_get_impl!(self, i64::from_ne_bytes);
}
/// Gets an unsigned 128 bit integer from `self` in big-endian byte order.
///
/// The current position is advanced by 16.
@@ -594,6 +733,29 @@ pub trait Buf {
buf_get_impl!(self, u128::from_le_bytes);
}
/// Gets an unsigned 128 bit integer from `self` in native-endian byte order.
///
/// The current position is advanced by 16.
///
/// # Examples
///
/// ```
/// use bytes::Buf;
///
/// let mut buf: &[u8] = match cfg!(target_endian = "big") {
/// true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello",
/// false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello",
/// };
/// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_ne());
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining data in `self`.
fn get_u128_ne(&mut self) -> u128 {
buf_get_impl!(self, u128::from_ne_bytes);
}
/// Gets a signed 128 bit integer from `self` in big-endian byte order.
///
/// The current position is advanced by 16.
@@ -634,6 +796,29 @@ pub trait Buf {
buf_get_impl!(self, i128::from_le_bytes);
}
/// Gets a signed 128 bit integer from `self` in native-endian byte order.
///
/// The current position is advanced by 16.
///
/// # Examples
///
/// ```
/// use bytes::Buf;
///
/// let mut buf: &[u8] = match cfg!(target_endian = "big") {
/// true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello",
/// false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello",
/// };
/// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_ne());
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining data in `self`.
fn get_i128_ne(&mut self) -> i128 {
buf_get_impl!(self, i128::from_ne_bytes);
}
/// Gets an unsigned n-byte integer from `self` in big-endian byte order.
///
/// The current position is advanced by `nbytes`.
@@ -674,6 +859,33 @@ pub trait Buf {
buf_get_impl!(le => self, u64, nbytes);
}
/// Gets an unsigned n-byte integer from `self` in native-endian byte order.
///
/// The current position is advanced by `nbytes`.
///
/// # Examples
///
/// ```
/// use bytes::Buf;
///
/// let mut buf: &[u8] = match cfg!(target_endian = "big") {
/// true => b"\x01\x02\x03 hello",
/// false => b"\x03\x02\x01 hello",
/// };
/// assert_eq!(0x010203, buf.get_uint_ne(3));
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining data in `self`.
fn get_uint_ne(&mut self, nbytes: usize) -> u64 {
if cfg!(target_endian = "big") {
self.get_uint(nbytes)
} else {
self.get_uint_le(nbytes)
}
}
/// Gets a signed n-byte integer from `self` in big-endian byte order.
///
/// The current position is advanced by `nbytes`.
@@ -714,6 +926,33 @@ pub trait Buf {
buf_get_impl!(le => self, i64, nbytes);
}
/// Gets a signed n-byte integer from `self` in native-endian byte order.
///
/// The current position is advanced by `nbytes`.
///
/// # Examples
///
/// ```
/// use bytes::Buf;
///
/// let mut buf: &[u8] = match cfg!(target_endian = "big") {
/// true => b"\x01\x02\x03 hello",
/// false => b"\x03\x02\x01 hello",
/// };
/// assert_eq!(0x010203, buf.get_int_ne(3));
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining data in `self`.
fn get_int_ne(&mut self, nbytes: usize) -> i64 {
if cfg!(target_endian = "big") {
self.get_int(nbytes)
} else {
self.get_int_le(nbytes)
}
}
/// Gets an IEEE754 single-precision (4 bytes) floating point number from
/// `self` in big-endian byte order.
///
@@ -756,6 +995,30 @@ pub trait Buf {
f32::from_bits(Self::get_u32_le(self))
}
/// Gets an IEEE754 single-precision (4 bytes) floating point number from
/// `self` in native-endian byte order.
///
/// The current position is advanced by 4.
///
/// # Examples
///
/// ```
/// use bytes::Buf;
///
/// let mut buf: &[u8] = match cfg!(target_endian = "big") {
/// true => b"\x3F\x99\x99\x9A hello",
/// false => b"\x9A\x99\x99\x3F hello",
/// };
/// assert_eq!(1.2f32, buf.get_f32_ne());
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining data in `self`.
fn get_f32_ne(&mut self) -> f32 {
f32::from_bits(Self::get_u32_ne(self))
}
/// Gets an IEEE754 double-precision (8 bytes) floating point number from
/// `self` in big-endian byte order.
///
@@ -798,6 +1061,30 @@ pub trait Buf {
f64::from_bits(Self::get_u64_le(self))
}
/// Gets an IEEE754 double-precision (8 bytes) floating point number from
/// `self` in native-endian byte order.
///
/// The current position is advanced by 8.
///
/// # Examples
///
/// ```
/// use bytes::Buf;
///
/// let mut buf: &[u8] = match cfg!(target_endian = "big") {
/// true => b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello",
/// false => b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello",
/// };
/// assert_eq!(1.2f64, buf.get_f64_ne());
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining data in `self`.
fn get_f64_ne(&mut self) -> f64 {
f64::from_bits(Self::get_u64_ne(self))
}
/// Consumes `len` bytes inside self and returns new instance of `Bytes`
/// with this data.
///
@@ -897,6 +1184,7 @@ pub trait Buf {
/// assert_eq!(&dst[..11], &b"hello world"[..]);
/// ```
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn reader(self) -> Reader<Self>
where
Self: Sized,
@@ -948,6 +1236,10 @@ macro_rules! deref_forward_buf {
(**self).get_u16_le()
}
fn get_u16_ne(&mut self) -> u16 {
(**self).get_u16_ne()
}
fn get_i16(&mut self) -> i16 {
(**self).get_i16()
}
@@ -956,6 +1248,10 @@ macro_rules! deref_forward_buf {
(**self).get_i16_le()
}
fn get_i16_ne(&mut self) -> i16 {
(**self).get_i16_ne()
}
fn get_u32(&mut self) -> u32 {
(**self).get_u32()
}
@@ -964,6 +1260,10 @@ macro_rules! deref_forward_buf {
(**self).get_u32_le()
}
fn get_u32_ne(&mut self) -> u32 {
(**self).get_u32_ne()
}
fn get_i32(&mut self) -> i32 {
(**self).get_i32()
}
@@ -972,6 +1272,10 @@ macro_rules! deref_forward_buf {
(**self).get_i32_le()
}
fn get_i32_ne(&mut self) -> i32 {
(**self).get_i32_ne()
}
fn get_u64(&mut self) -> u64 {
(**self).get_u64()
}
@@ -980,6 +1284,10 @@ macro_rules! deref_forward_buf {
(**self).get_u64_le()
}
fn get_u64_ne(&mut self) -> u64 {
(**self).get_u64_ne()
}
fn get_i64(&mut self) -> i64 {
(**self).get_i64()
}
@@ -988,6 +1296,10 @@ macro_rules! deref_forward_buf {
(**self).get_i64_le()
}
fn get_i64_ne(&mut self) -> i64 {
(**self).get_i64_ne()
}
fn get_uint(&mut self, nbytes: usize) -> u64 {
(**self).get_uint(nbytes)
}
@@ -996,6 +1308,10 @@ macro_rules! deref_forward_buf {
(**self).get_uint_le(nbytes)
}
fn get_uint_ne(&mut self, nbytes: usize) -> u64 {
(**self).get_uint_ne(nbytes)
}
fn get_int(&mut self, nbytes: usize) -> i64 {
(**self).get_int(nbytes)
}
@@ -1004,6 +1320,10 @@ macro_rules! deref_forward_buf {
(**self).get_int_le(nbytes)
}
fn get_int_ne(&mut self, nbytes: usize) -> i64 {
(**self).get_int_ne(nbytes)
}
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
(**self).copy_to_bytes(len)
}
+347
View File
@@ -386,6 +386,32 @@ pub unsafe trait BufMut {
self.put_slice(&n.to_le_bytes())
}
/// Writes an unsigned 16 bit integer to `self` in native-endian byte order.
///
/// The current position is advanced by 2.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut buf = vec![];
/// buf.put_u16_ne(0x0809);
/// if cfg!(target_endian = "big") {
/// assert_eq!(buf, b"\x08\x09");
/// } else {
/// assert_eq!(buf, b"\x09\x08");
/// }
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining capacity in
/// `self`.
fn put_u16_ne(&mut self, n: u16) {
self.put_slice(&n.to_ne_bytes())
}
/// Writes a signed 16 bit integer to `self` in big-endian byte order.
///
/// The current position is advanced by 2.
@@ -430,6 +456,32 @@ pub unsafe trait BufMut {
self.put_slice(&n.to_le_bytes())
}
/// Writes a signed 16 bit integer to `self` in native-endian byte order.
///
/// The current position is advanced by 2.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut buf = vec![];
/// buf.put_i16_ne(0x0809);
/// if cfg!(target_endian = "big") {
/// assert_eq!(buf, b"\x08\x09");
/// } else {
/// assert_eq!(buf, b"\x09\x08");
/// }
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining capacity in
/// `self`.
fn put_i16_ne(&mut self, n: i16) {
self.put_slice(&n.to_ne_bytes())
}
/// Writes an unsigned 32 bit integer to `self` in big-endian byte order.
///
/// The current position is advanced by 4.
@@ -474,6 +526,32 @@ pub unsafe trait BufMut {
self.put_slice(&n.to_le_bytes())
}
/// Writes an unsigned 32 bit integer to `self` in native-endian byte order.
///
/// The current position is advanced by 4.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut buf = vec![];
/// buf.put_u32_ne(0x0809A0A1);
/// if cfg!(target_endian = "big") {
/// assert_eq!(buf, b"\x08\x09\xA0\xA1");
/// } else {
/// assert_eq!(buf, b"\xA1\xA0\x09\x08");
/// }
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining capacity in
/// `self`.
fn put_u32_ne(&mut self, n: u32) {
self.put_slice(&n.to_ne_bytes())
}
/// Writes a signed 32 bit integer to `self` in big-endian byte order.
///
/// The current position is advanced by 4.
@@ -518,6 +596,32 @@ pub unsafe trait BufMut {
self.put_slice(&n.to_le_bytes())
}
/// Writes a signed 32 bit integer to `self` in native-endian byte order.
///
/// The current position is advanced by 4.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut buf = vec![];
/// buf.put_i32_ne(0x0809A0A1);
/// if cfg!(target_endian = "big") {
/// assert_eq!(buf, b"\x08\x09\xA0\xA1");
/// } else {
/// assert_eq!(buf, b"\xA1\xA0\x09\x08");
/// }
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining capacity in
/// `self`.
fn put_i32_ne(&mut self, n: i32) {
self.put_slice(&n.to_ne_bytes())
}
/// Writes an unsigned 64 bit integer to `self` in the big-endian byte order.
///
/// The current position is advanced by 8.
@@ -562,6 +666,32 @@ pub unsafe trait BufMut {
self.put_slice(&n.to_le_bytes())
}
/// Writes an unsigned 64 bit integer to `self` in native-endian byte order.
///
/// The current position is advanced by 8.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut buf = vec![];
/// buf.put_u64_ne(0x0102030405060708);
/// if cfg!(target_endian = "big") {
/// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08");
/// } else {
/// 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_ne(&mut self, n: u64) {
self.put_slice(&n.to_ne_bytes())
}
/// Writes a signed 64 bit integer to `self` in the big-endian byte order.
///
/// The current position is advanced by 8.
@@ -606,6 +736,32 @@ pub unsafe trait BufMut {
self.put_slice(&n.to_le_bytes())
}
/// Writes a signed 64 bit integer to `self` in native-endian byte order.
///
/// The current position is advanced by 8.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut buf = vec![];
/// buf.put_i64_ne(0x0102030405060708);
/// if cfg!(target_endian = "big") {
/// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08");
/// } else {
/// 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_ne(&mut self, n: i64) {
self.put_slice(&n.to_ne_bytes())
}
/// Writes an unsigned 128 bit integer to `self` in the big-endian byte order.
///
/// The current position is advanced by 16.
@@ -650,6 +806,32 @@ pub unsafe trait BufMut {
self.put_slice(&n.to_le_bytes())
}
/// Writes an unsigned 128 bit integer to `self` in native-endian byte order.
///
/// The current position is advanced by 16.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut buf = vec![];
/// buf.put_u128_ne(0x01020304050607080910111213141516);
/// if cfg!(target_endian = "big") {
/// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16");
/// } else {
/// 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`.
fn put_u128_ne(&mut self, n: u128) {
self.put_slice(&n.to_ne_bytes())
}
/// Writes a signed 128 bit integer to `self` in the big-endian byte order.
///
/// The current position is advanced by 16.
@@ -694,6 +876,32 @@ pub unsafe trait BufMut {
self.put_slice(&n.to_le_bytes())
}
/// Writes a signed 128 bit integer to `self` in native-endian byte order.
///
/// The current position is advanced by 16.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut buf = vec![];
/// buf.put_i128_ne(0x01020304050607080910111213141516);
/// if cfg!(target_endian = "big") {
/// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16");
/// } else {
/// 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`.
fn put_i128_ne(&mut self, n: i128) {
self.put_slice(&n.to_ne_bytes())
}
/// Writes an unsigned n-byte integer to `self` in big-endian byte order.
///
/// The current position is advanced by `nbytes`.
@@ -738,6 +946,36 @@ pub unsafe trait BufMut {
self.put_slice(&n.to_le_bytes()[0..nbytes]);
}
/// Writes an unsigned n-byte integer to `self` in the native-endian byte order.
///
/// The current position is advanced by `nbytes`.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut buf = vec![];
/// buf.put_uint_ne(0x010203, 3);
/// if cfg!(target_endian = "big") {
/// assert_eq!(buf, b"\x01\x02\x03");
/// } else {
/// assert_eq!(buf, b"\x03\x02\x01");
/// }
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining capacity in
/// `self`.
fn put_uint_ne(&mut self, n: u64, nbytes: usize) {
if cfg!(target_endian = "big") {
self.put_uint(n, nbytes)
} else {
self.put_uint_le(n, nbytes)
}
}
/// Writes low `nbytes` of a signed integer to `self` in big-endian byte order.
///
/// The current position is advanced by `nbytes`.
@@ -782,6 +1020,36 @@ pub unsafe trait BufMut {
self.put_slice(&n.to_le_bytes()[0..nbytes]);
}
/// Writes low `nbytes` of a signed integer to `self` in native-endian byte order.
///
/// The current position is advanced by `nbytes`.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut buf = vec![];
/// buf.put_int_ne(0x010203, 3);
/// if cfg!(target_endian = "big") {
/// assert_eq!(buf, b"\x01\x02\x03");
/// } else {
/// assert_eq!(buf, b"\x03\x02\x01");
/// }
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining capacity in
/// `self` or if `nbytes` is greater than 8.
fn put_int_ne(&mut self, n: i64, nbytes: usize) {
if cfg!(target_endian = "big") {
self.put_int(n, nbytes)
} else {
self.put_int_le(n, nbytes)
}
}
/// Writes an IEEE754 single-precision (4 bytes) floating point number to
/// `self` in big-endian byte order.
///
@@ -828,6 +1096,33 @@ pub unsafe trait BufMut {
self.put_u32_le(n.to_bits());
}
/// Writes an IEEE754 single-precision (4 bytes) floating point number to
/// `self` in native-endian byte order.
///
/// The current position is advanced by 4.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut buf = vec![];
/// buf.put_f32_ne(1.2f32);
/// if cfg!(target_endian = "big") {
/// assert_eq!(buf, b"\x3F\x99\x99\x9A");
/// } else {
/// assert_eq!(buf, b"\x9A\x99\x99\x3F");
/// }
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining capacity in
/// `self`.
fn put_f32_ne(&mut self, n: f32) {
self.put_u32_ne(n.to_bits());
}
/// Writes an IEEE754 double-precision (8 bytes) floating point number to
/// `self` in big-endian byte order.
///
@@ -874,6 +1169,33 @@ pub unsafe trait BufMut {
self.put_u64_le(n.to_bits());
}
/// Writes an IEEE754 double-precision (8 bytes) floating point number to
/// `self` in native-endian byte order.
///
/// The current position is advanced by 8.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut buf = vec![];
/// buf.put_f64_ne(1.2f64);
/// if cfg!(target_endian = "big") {
/// assert_eq!(buf, b"\x3F\xF3\x33\x33\x33\x33\x33\x33");
/// } else {
/// 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_ne(&mut self, n: f64) {
self.put_u64_ne(n.to_bits());
}
/// Creates an adaptor which can write at most `limit` bytes to `self`.
///
/// # Examples
@@ -917,6 +1239,7 @@ pub unsafe trait BufMut {
/// assert_eq!(*buf, b"hello world"[..]);
/// ```
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn writer(self) -> Writer<Self>
where
Self: Sized,
@@ -986,6 +1309,10 @@ macro_rules! deref_forward_bufmut {
(**self).put_u16_le(n)
}
fn put_u16_ne(&mut self, n: u16) {
(**self).put_u16_ne(n)
}
fn put_i16(&mut self, n: i16) {
(**self).put_i16(n)
}
@@ -994,6 +1321,10 @@ macro_rules! deref_forward_bufmut {
(**self).put_i16_le(n)
}
fn put_i16_ne(&mut self, n: i16) {
(**self).put_i16_ne(n)
}
fn put_u32(&mut self, n: u32) {
(**self).put_u32(n)
}
@@ -1002,6 +1333,10 @@ macro_rules! deref_forward_bufmut {
(**self).put_u32_le(n)
}
fn put_u32_ne(&mut self, n: u32) {
(**self).put_u32_ne(n)
}
fn put_i32(&mut self, n: i32) {
(**self).put_i32(n)
}
@@ -1010,6 +1345,10 @@ macro_rules! deref_forward_bufmut {
(**self).put_i32_le(n)
}
fn put_i32_ne(&mut self, n: i32) {
(**self).put_i32_ne(n)
}
fn put_u64(&mut self, n: u64) {
(**self).put_u64(n)
}
@@ -1018,6 +1357,10 @@ macro_rules! deref_forward_bufmut {
(**self).put_u64_le(n)
}
fn put_u64_ne(&mut self, n: u64) {
(**self).put_u64_ne(n)
}
fn put_i64(&mut self, n: i64) {
(**self).put_i64(n)
}
@@ -1025,6 +1368,10 @@ macro_rules! deref_forward_bufmut {
fn put_i64_le(&mut self, n: i64) {
(**self).put_i64_le(n)
}
fn put_i64_ne(&mut self, n: i64) {
(**self).put_i64_ne(n)
}
};
}
+1 -3
View File
@@ -2,8 +2,6 @@ use crate::Buf;
/// Iterator over the bytes contained by the buffer.
///
/// This struct is created by the [`iter`] method on [`Buf`].
///
/// # Examples
///
/// Basic usage:
@@ -43,7 +41,7 @@ impl<T> IntoIter<T> {
/// assert_eq!(iter.next(), Some(b'c'));
/// assert_eq!(iter.next(), None);
/// ```
pub(crate) fn new(inner: T) -> IntoIter<T> {
pub fn new(inner: T) -> IntoIter<T> {
IntoIter { inner }
}
+5 -1
View File
@@ -22,6 +22,10 @@ use core::ops::{
pub struct UninitSlice([MaybeUninit<u8>]);
impl UninitSlice {
pub(crate) fn from_slice(slice: &mut [MaybeUninit<u8>]) -> &mut UninitSlice {
unsafe { &mut *(slice as *mut [MaybeUninit<u8>] as *mut UninitSlice) }
}
/// Create a `&mut UninitSlice` from a pointer and a length.
///
/// # Safety
@@ -44,7 +48,7 @@ impl UninitSlice {
pub unsafe fn from_raw_parts_mut<'a>(ptr: *mut u8, len: usize) -> &'a mut UninitSlice {
let maybe_init: &mut [MaybeUninit<u8>] =
core::slice::from_raw_parts_mut(ptr as *mut _, len);
&mut *(maybe_init as *mut [MaybeUninit<u8>] as *mut UninitSlice)
Self::from_slice(maybe_init)
}
/// Write a single byte at the specified offset.
+40 -12
View File
@@ -32,7 +32,7 @@ use crate::Buf;
/// All `Bytes` implementations must fulfill the following requirements:
/// - They are cheaply cloneable and thereby shareable between an unlimited amount
/// of components, for example by modifying a reference count.
/// - Instances can be sliced to refer to a subset of the the original buffer.
/// - Instances can be sliced to refer to a subset of the original buffer.
///
/// ```
/// use bytes::Bytes;
@@ -71,7 +71,7 @@ use crate::Buf;
///
/// For `Bytes` implementations which point to a reference counted shared storage
/// (e.g. an `Arc<[u8]>`), sharing will be implemented by increasing the
/// the reference count.
/// reference count.
///
/// Due to this mechanism, multiple `Bytes` instances may point to the same
/// shared memory region.
@@ -131,7 +131,7 @@ impl Bytes {
/// ```
#[inline]
#[cfg(not(all(loom, test)))]
pub const fn new() -> Bytes {
pub const fn new() -> Self {
// Make it a named const to work around
// "unsizing casts are not allowed in const fn"
const EMPTY: &[u8] = &[];
@@ -139,7 +139,7 @@ impl Bytes {
}
#[cfg(all(loom, test))]
pub fn new() -> Bytes {
pub fn new() -> Self {
const EMPTY: &[u8] = &[];
Bytes::from_static(EMPTY)
}
@@ -159,7 +159,7 @@ impl Bytes {
/// ```
#[inline]
#[cfg(not(all(loom, test)))]
pub const fn from_static(bytes: &'static [u8]) -> Bytes {
pub const fn from_static(bytes: &'static [u8]) -> Self {
Bytes {
ptr: bytes.as_ptr(),
len: bytes.len(),
@@ -169,7 +169,7 @@ impl Bytes {
}
#[cfg(all(loom, test))]
pub fn from_static(bytes: &'static [u8]) -> Bytes {
pub fn from_static(bytes: &'static [u8]) -> Self {
Bytes {
ptr: bytes.as_ptr(),
len: bytes.len(),
@@ -235,7 +235,7 @@ impl Bytes {
///
/// Requires that `begin <= end` and `end <= self.len()`, otherwise slicing
/// will panic.
pub fn slice(&self, range: impl RangeBounds<usize>) -> Bytes {
pub fn slice(&self, range: impl RangeBounds<usize>) -> Self {
use core::ops::Bound;
let len = self.len();
@@ -302,7 +302,7 @@ impl Bytes {
///
/// Requires that the given `sub` slice is in fact contained within the
/// `Bytes` buffer; otherwise this function will panic.
pub fn slice_ref(&self, subset: &[u8]) -> Bytes {
pub fn slice_ref(&self, subset: &[u8]) -> Self {
// Empty slice and empty Bytes may have their pointers reset
// so explicitly allow empty slice to be a subslice of any slice.
if subset.is_empty() {
@@ -359,7 +359,7 @@ impl Bytes {
///
/// Panics if `at > len`.
#[must_use = "consider Bytes::truncate if you don't need the other half"]
pub fn split_off(&mut self, at: usize) -> Bytes {
pub fn split_off(&mut self, at: usize) -> Self {
assert!(
at <= self.len(),
"split_off out of bounds: {:?} <= {:?}",
@@ -408,7 +408,7 @@ impl Bytes {
///
/// Panics if `at > len`.
#[must_use = "consider Bytes::advance if you don't need the other half"]
pub fn split_to(&mut self, at: usize) -> Bytes {
pub fn split_to(&mut self, at: usize) -> Self {
assert!(
at <= self.len(),
"split_to out of bounds: {:?} <= {:?}",
@@ -807,8 +807,36 @@ impl From<&'static str> for Bytes {
impl From<Vec<u8>> for Bytes {
fn from(vec: Vec<u8>) -> Bytes {
let slice = vec.into_boxed_slice();
slice.into()
let mut vec = vec;
let ptr = vec.as_mut_ptr();
let len = vec.len();
let cap = vec.capacity();
// Avoid an extra allocation if possible.
if len == cap {
return Bytes::from(vec.into_boxed_slice());
}
let shared = Box::new(Shared {
buf: ptr,
cap,
ref_cnt: AtomicUsize::new(1),
});
mem::forget(vec);
let shared = Box::into_raw(shared);
// The pointer should be aligned, so this assert should
// always succeed.
debug_assert!(
0 == (shared as usize & KIND_MASK),
"internal: Box<Shared> should have an aligned pointer",
);
Bytes {
ptr,
len,
data: AtomicPtr::new(shared as _),
vtable: &SHARED_VTABLE,
}
}
}
+45 -7
View File
@@ -1,5 +1,5 @@
use core::iter::{FromIterator, Iterator};
use core::mem::{self, ManuallyDrop};
use core::mem::{self, ManuallyDrop, MaybeUninit};
use core::ops::{Deref, DerefMut};
use core::ptr::{self, NonNull};
use core::{cmp, fmt, hash, isize, slice, usize};
@@ -705,6 +705,15 @@ impl BytesMut {
new_cap = cmp::max(double, new_cap);
// No space - allocate more
//
// The length field of `Shared::vec` is not used by the `BytesMut`;
// instead we use the `len` field in the `BytesMut` itself. However,
// when calling `reserve`, it doesn't guarantee that data stored in
// the unused capacity of the vector is copied over to the new
// allocation, so we need to ensure that we don't have any data we
// care about in the unused capacity before calling `reserve`.
debug_assert!(off + len <= v.capacity());
v.set_len(off + len);
v.reserve(new_cap - v.len());
// Update the info
@@ -757,11 +766,11 @@ impl BytesMut {
self.reserve(cnt);
unsafe {
let dst = self.uninit_slice();
let dst = self.spare_capacity_mut();
// Reserved above
debug_assert!(dst.len() >= cnt);
ptr::copy_nonoverlapping(extend.as_ptr(), dst.as_mut_ptr(), cnt);
ptr::copy_nonoverlapping(extend.as_ptr(), dst.as_mut_ptr().cast(), cnt);
}
unsafe {
@@ -983,13 +992,42 @@ impl BytesMut {
self.data = invalid_ptr((pos << VEC_POS_OFFSET) | (prev & NOT_VEC_POS_MASK));
}
/// Returns the remaining spare capacity of the buffer as a slice of `MaybeUninit<u8>`.
///
/// The returned slice can be used to fill the buffer with data (e.g. by
/// reading from a file) before marking the data as initialized using the
/// [`set_len`] method.
///
/// [`set_len`]: BytesMut::set_len
///
/// # Examples
///
/// ```
/// use bytes::BytesMut;
///
/// // Allocate buffer big enough for 10 bytes.
/// let mut buf = BytesMut::with_capacity(10);
///
/// // Fill in the first 3 elements.
/// let uninit = buf.spare_capacity_mut();
/// uninit[0].write(0);
/// uninit[1].write(1);
/// uninit[2].write(2);
///
/// // Mark the first 3 bytes of the buffer as being initialized.
/// unsafe {
/// buf.set_len(3);
/// }
///
/// assert_eq!(&buf[..], &[0, 1, 2]);
/// ```
#[inline]
fn uninit_slice(&mut self) -> &mut UninitSlice {
pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<u8>] {
unsafe {
let ptr = self.ptr.as_ptr().add(self.len);
let len = self.cap - self.len;
UninitSlice::from_raw_parts_mut(ptr, len)
slice::from_raw_parts_mut(ptr.cast(), len)
}
}
}
@@ -1063,7 +1101,7 @@ unsafe impl BufMut for BytesMut {
if self.capacity() == self.len() {
self.reserve(64);
}
self.uninit_slice()
UninitSlice::from_slice(self.spare_capacity_mut())
}
// Specialize these methods so they can skip checking `remaining_mut`
@@ -1088,7 +1126,7 @@ unsafe impl BufMut for BytesMut {
fn put_bytes(&mut self, val: u8, cnt: usize) {
self.reserve(cnt);
unsafe {
let dst = self.uninit_slice();
let dst = self.spare_capacity_mut();
// Reserved above
debug_assert!(dst.len() >= cnt);
+1
View File
@@ -4,6 +4,7 @@
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
))]
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
//! Provides abstractions for working with bytes.
//!
+45
View File
@@ -1163,3 +1163,48 @@ fn test_bytes_into_vec_promotable_even() {
assert_eq!(Vec::from(b2), vec[20..]);
assert_eq!(Vec::from(b1), vec[..20]);
}
#[test]
fn test_bytes_vec_conversion() {
let mut vec = Vec::with_capacity(10);
vec.extend(b"abcdefg");
let b = Bytes::from(vec);
let v = Vec::from(b);
assert_eq!(v.len(), 7);
assert_eq!(v.capacity(), 10);
let mut b = Bytes::from(v);
b.advance(1);
let v = Vec::from(b);
assert_eq!(v.len(), 6);
assert_eq!(v.capacity(), 10);
assert_eq!(v.as_slice(), b"bcdefg");
}
#[test]
fn test_bytes_mut_conversion() {
let mut b1 = BytesMut::with_capacity(10);
b1.extend(b"abcdefg");
let b2 = Bytes::from(b1);
let v = Vec::from(b2);
assert_eq!(v.len(), 7);
assert_eq!(v.capacity(), 10);
let mut b = Bytes::from(v);
b.advance(1);
let v = Vec::from(b);
assert_eq!(v.len(), 6);
assert_eq!(v.capacity(), 10);
assert_eq!(v.as_slice(), b"bcdefg");
}
#[test]
fn test_bytes_capacity_len() {
for cap in 0..100 {
for len in 0..=cap {
let mut v = Vec::with_capacity(cap);
v.resize(len, 0);
let _ = Bytes::from(v);
}
}
}