Module bytes

Module bytes 

Source
Expand description

Core byte container types and traits. Core byte container types.

Bytes provides cheap, zero-copy access to immutable bytes from a variety of sources. Implement the ByteSource trait for your type to integrate it with the container. Each Bytes keeps its backing storage alive through a reference-counted ByteOwner, ensuring the data stays valid for as long as needed.

Bytes decouples data access from ownership so that callers can obtain a slice and then release any external locks. After reading the bytes from a ByteSource, convert it into its ByteOwner to keep the data alive without retaining the original guard. This is especially useful for Python integration where acquiring the raw pointer to a bytes object requires holding the GIL, but once the slice is acquired, only the owner needs to be kept alive.

§Weak references

A Bytes can be downgraded to a WeakBytes to hold a non-owning reference without keeping the underlying data alive:

use anybytes::Bytes;

let bytes = Bytes::from(vec![1u8, 2, 3]);
let weak = bytes.downgrade();
assert!(weak.upgrade().is_some());
drop(bytes);
assert!(weak.upgrade().is_none());

§Downcasting owners

When the backing type is known, Bytes::downcast_to_owner retrieves it again:

use anybytes::Bytes;
use std::sync::Arc;

let bytes = Bytes::from_source(vec![1u8, 2, 3, 4]);
let owner: Arc<Vec<u8>> = bytes
    .downcast_to_owner()
    .expect("Downcast of known type.");
assert_eq!(&*owner, &[1, 2, 3, 4]);

Structs§

Bytes
Immutable bytes with zero-copy slicing and cloning.
WeakBytes
Weak variant of Bytes that doesn’t retain the data unless a strong Bytes is referencing it.

Traits§

ByteOwner
A trait for types that keep the backing bytes of Bytes alive.
ByteSource
A type that can provide its bytes and yield an owner for them.