This Library is still pre-0.1.0 the API is therefore in heavy flux, and everything should be considered alpha!
A small library for conveniently working with immutables bytes from different sources, providing zero-copy slicing and cloning.
Access itself is extremely cheap via no-op conversion to a &[u8].
The storage mechanism backing the bytes can be extended
and is implemented for a variety of sources already,
including other byte handling crates Bytes, mmap-ed files,
Strings and Zerocopy types.
See INVENTORY.md for notes on possible cleanup and future functionality.
Bytes decouples data access from lifetime management through two traits:
ByteSource and ByteOwner. A ByteSource
can yield a slice of its bytes and then convert itself into a ByteOwner that
keeps the underlying storage alive. This separation lets callers obtain a
borrow of the bytes, drop any locks or external guards, and still retain the
data by storing the owner behind an Arc. No runtime indirection is required
when constructing a Bytes, and custom storage types integrate by
implementing ByteSource.
use anybytes::Bytes;
fn main() {
// create `Bytes` from a vector
let bytes = Bytes::from(vec![1u8, 2, 3, 4]);
// take a zero-copy slice
let slice = bytes.slice(1..3);
// convert it to a typed View
let view = slice.view::<[u8]>().unwrap();
assert_eq!(&*view, &[2, 3]);
}The full example is available in examples/quick_start.rs.
Bytes can directly wrap memory-mapped files or other large buffers. Combined
with the view module this enables simple parsing of structured
data without copying:
use anybytes::Bytes;
use zerocopy::{FromBytes, Immutable, KnownLayout};
#[derive(FromBytes, Immutable, KnownLayout)]
#[repr(C)]
struct Header { magic: u32, count: u32 }
fn read_header(map: memmap2::Mmap) -> anybytes::view::View<Header> {
Bytes::from(map).view().unwrap()
}By default the crate enables the mmap and zerocopy features.
Other optional features provide additional integrations:
bytes– support for thebytescrate sobytes::Bytescan act as aByteSource.ownedbytes– adds compatibility withownedbytesand implements itsStableDereftrait.mmap– enables memory-mapped file handling via thememmap2crate.zerocopy– exposes theviewmodule for typed zero-copy access and allows usingzerocopytypes as sources.pyo3– builds thepybytesmodule to provide Python bindings forBytes.winnow– implements theStreamtraits forBytesand offers parsers (view,view_elems(count)) that return typedViews.
Enabling the pyo3 feature requires the Python development headers and libraries
(for example libpython3.x). Running cargo test --all-features therefore
needs these libraries installed; otherwise disable the feature during testing.
examples/quick_start.rs– the quick start shown aboveexamples/pybytes.rs– demonstrates thepyo3feature usingPyBytesexamples/from_python.rs– wrap a Pythonbytesobject intoBytes
| Crate | Active | Extensible | mmap support | Zerocopy Integration | Pyo3 Integration | kani verified |
|---|---|---|---|---|---|---|
| anybytes | ✅ | ✅ | ✅ | ✅ | ✅ | 🚧 |
| bytes | ✅ | ✅ | ✅1 | ❌ | ❌ | ❌ |
| ownedbytes | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ |
| minibytes | ✅2 | ✅ | ✅ | ❌ | ❌ | ❌ |
Run ./scripts/preflight.sh from the repository root before committing. The
script formats the code and executes all tests, automatically installing required
tools if needed.
Kani proofs are executed separately with ./scripts/verify.sh, which should be
run on a dedicated system. The script will install the Kani verifier
automatically. Verification can take a long time and isn't needed for quick
development iterations.
Bytes– primary container type.ByteSource– trait for objects that can provide bytes.ByteOwner– keeps backing storage alive.viewmodule – typed zero-copy access to bytes.pybytesmodule – Python bindings.
This library started as a fork of the minibyte library in facebooks sapling scm.
Thanks to @kylebarron for his feedback and ideas on Pyo3 integration.
