Expand description
Provides abstractions for working with bytes.
The crate provides immutable Bytes and mutable BytesMut buffers,
UTF-8 ByteString values, paged buffers through BytePages, and the
Buf and BufMut traits.
§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 common pattern is to write into a BytesMut and extract immutable
Bytes views:
use ntex_bytes::{BytesMut, BufMut};
let mut buf = BytesMut::with_capacity(1024);
buf.put(&b"hello world"[..]);
buf.put_u16(1234);
let a = buf.take();
assert_eq!(a, b"hello world\x04\xD2"[..]);
buf.put(&b"goodbye world"[..]);
let b = buf.take();
assert_eq!(b, b"goodbye world"[..]);
assert_eq!(buf.capacity(), 998);In this example, a single 1,024-byte allocation is reused. The a and b
handles retain immutable views into that allocation, while buf continues
using its remaining capacity.
See Bytes and BytesMut for details about sharing, splitting, and
allocation behavior.
§Interoperability
Bytes and BytesMut implement the Buf trait of the
bytes crate, and BytesMut also implements its
BufMut trait. Bytes and ByteString implement
serde’s Serialize and Deserialize.
§Crate features
simdenables SIMD-accelerated UTF-8 validation.overuseenables diagnostic logging for unusually large page stacks.
Re-exports§
Modules§
- buf
- Utilities for working with buffers.
Structs§
- Byte
Page - A contiguous chunk stored by
BytePages. - Byte
Pages - A growable sequence of byte pages.
- Byte
String - An immutable UTF-8 string backed by
Bytes. - Bytes
- A reference counted contiguous slice of memory.
- Bytes
Mut - A unique reference to a contiguous slice of memory.
- StorageV
Table - Functions that give
Bytesaccess to externally owned data.
Enums§
- Byte
Page Size - Capacity category used when allocating
crate::BytePagestorage.
Traits§
- Storage
Ext - Types that can be used as external storage for
Bytes, seeBytes::from_ext. - Storage
ExtStr - External storage that holds valid UTF-8, see
ByteString::from_ext.
Functions§
- set_
page_ cache_ size - Sets the maximum number of cached page allocations of page size
sizeon the current thread. - set_
pages_ cache Deprecated - Sets the maximum number of cached page allocations for every page size on the current thread.