Expand description
Efficient byte buffer management.
bytes
provides a robust and performant way to work with byte buffers
without unnecessary allocations.
The crate is built around two primary types:
Bytes
is an immutable, reference-counted byte buffer
that enables zero-copy cloning and slicing.
BytesMut
is its mutable counterpart that can be efficiently
converted to Bytes
when you’re done modifying it.
The Buf
and BufMut
traits provide a cursor-based API
for reading and writing bytes to buffers.
These traits are implemented by various types including
Bytes
, BytesMut
, and standard types like Vec<u8>
and &[u8]
.
This crate is particularly useful in network programming where efficient buffer management is critical for performance. It’s a foundational component of the Tokio ecosystem and is used extensively in async I/O operations.
§Examples
Creating and sharing byte buffers efficiently:
use bytes::{Bytes, BytesMut};
// Create a mutable buffer
let mut buf = BytesMut::with_capacity(1024);
buf.extend_from_slice(b"hello ");
buf.extend_from_slice(b"world");
// Convert to immutable Bytes (zero-copy)
let bytes: Bytes = buf.freeze();
// Clone is cheap (reference counted)
let clone = bytes.clone();
// Slicing is also zero-copy
let slice = bytes.slice(0..5);
assert_eq!(&slice[..], b"hello");
Using the Buf
trait for reading:
use bytes::Buf;
fn read_u32(buf: &mut impl Buf) -> u32 {
buf.get_u32()
}
let mut data = &b"\x00\x00\x00\x42rest"[..];
let value = read_u32(&mut data);
assert_eq!(value, 0x42);
assert_eq!(data, b"rest");
Modules§
- buf
- Utilities for working with buffers.
Structs§
- Bytes
- A cheaply cloneable and sliceable chunk of contiguous memory.
- Bytes
Mut - A unique reference to a contiguous slice of memory.
- TryGet
Error - Error type for the
try_get_
methods ofBuf
. Indicates that there were not enough remaining bytes in the buffer while attempting to get a value from aBuf
with one of thetry_get_
methods.