Skip to main content

Crate ntex_bytes

Crate ntex_bytes 

Source
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

  • simd enables SIMD-accelerated UTF-8 validation.
  • overuse enables diagnostic logging for unusually large page stacks.

Re-exports§

pub use crate::buf::Buf;
pub use crate::buf::BufMut;

Modules§

buf
Utilities for working with buffers.

Structs§

BytePage
A contiguous chunk stored by BytePages.
BytePages
A growable sequence of byte pages.
ByteString
An immutable UTF-8 string backed by Bytes.
Bytes
A reference counted contiguous slice of memory.
BytesMut
A unique reference to a contiguous slice of memory.
StorageVTable
Functions that give Bytes access to externally owned data.

Enums§

BytePageSize
Capacity category used when allocating crate::BytePage storage.

Traits§

StorageExt
Types that can be used as external storage for Bytes, see Bytes::from_ext.
StorageExtStr
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 size on the current thread.
set_pages_cacheDeprecated
Sets the maximum number of cached page allocations for every page size on the current thread.