//! Provides abstractions for working with bytes. //! //! The `bytes` crate provides an efficient byte buffer structure //! ([`Bytes`]) and traits for working with buffer //! implementations ([`Buf`], [`BufMut`]). //! //! # `Bytes` //! //! `Bytes` is an efficient container for storing and operating on contiguous //! slices of memory. It is intended for use primarily in networking code, but //! could have applications elsewhere as well. //! //! `Bytes` values facilitate zero-copy network programming by allowing multiple //! `Bytes` objects to point to the same underlying memory. This is managed by //! using a reference count to track when the memory is no longer needed and can //! be freed. //! //! A `Bytes` handle can be created directly from an existing byte store (such as `&[u8]` //! or `Vec<u8>`), but usually a `BytesMut` is used first and written to. For //! example: //! //! ```rust //! use bytes::{BytesMut, BufMut}; //! //! let mut buf = BytesMut::with_capacity(1024); //! buf.put(&b"hello world"[..]); //! buf.put_u16(1234); //! //! let a = buf.split(); //! assert_eq!(a, b"hello world\x04\xD2"[..]); //! //! buf.put(&b"goodbye world"[..]); //! //! let b = buf.split(); //! assert_eq!(b, b"goodbye world"[..]); //! //! assert_eq!(buf.capacity(), 998); //! ``` //! //! In the above example, only a single buffer of 1024 is allocated. The handles //! `a` and `b` will share the underlying buffer and maintain indices tracking //! the view into the buffer represented by the handle. //! //! See the [struct docs](`Bytes`) for more details. //! //! # `Buf`, `BufMut` //! //! These two traits provide read and write access to buffers. The underlying //! storage may or may not be in contiguous memory. For example, `Bytes` is a //! buffer that guarantees contiguous memory, but a [rope] stores the bytes in //! disjoint chunks. `Buf` and `BufMut` maintain cursors tracking the current //! position in the underlying byte storage. When bytes are read or written, the //! cursor is advanced. //! //! [rope]: https://en.wikipedia.org/wiki/Rope_(data_structure) //! //! ## Relation with `Read` and `Write` //! //! At first glance, it may seem that `Buf` and `BufMut` overlap in //! functionality with [`std::io::Read`] and [`std::io::Write`]. However, they //! serve different purposes. A buffer is the value that is provided as an //! argument to `Read::read` and `Write::write`. `Read` and `Write` may then //! perform a syscall, which has the potential of failing. Operations on `Buf` //! and `BufMut` are infallible.
externcrate alloc;
#[cfg(feature = "std")] externcrate std;
pubmod buf; pubusecrate::buf::{Buf, BufMut};
mod bytes; mod bytes_mut; mod fmt; mod loom; pubusecrate::bytes::Bytes; pubusecrate::bytes_mut::BytesMut;
// Optional Serde support #[cfg(feature = "serde")] mod serde;
/// Error type for the `try_get_` methods of [`Buf`]. /// Indicates that there were not enough remaining /// bytes in the buffer while attempting /// to get a value from a [`Buf`] with one /// of the `try_get_` methods. #[derive(Debug, PartialEq, Eq)] pubstruct TryGetError { /// The number of bytes necessary to get the value pub requested: usize,
/// The number of bytes available in the buffer pub available: usize,
}
impl core::fmt::Display for TryGetError { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
write!(
f, "Not enough bytes remaining in buffer to read value (requested {} but only {} available)", self.requested, self.available
)
}
}
#[cfg(feature = "std")] impl std::error::Error for TryGetError {}
/// Panic with a nice error message. #[cold] fn panic_advance(error_info: &TryGetError) -> ! {
panic!( "advance out of bounds: the len is {} but advancing by {}",
error_info.available, error_info.requested
);
}
#[cold] fn panic_does_not_fit(size: usize, nbytes: usize) -> ! {
panic!( "size too large: the integer type can fit {} bytes, but nbytes is {}",
size, nbytes
);
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-08-25)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.