anybytes 0.20.2

A small library abstracting over bytes owning types in an extensible way.
Documentation
use anybytes::{area::ByteArea, Bytes};
use tempfile::tempdir;

fn main() -> std::io::Result<()> {
    let mut area = ByteArea::new()?;
    let mut sections = area.sections();

    // Reserve two sections at once and mutate them independently.
    let mut raw = sections.reserve::<u8>(4)?;
    let mut nums = sections.reserve::<u32>(2)?;

    raw.as_mut_slice().copy_from_slice(b"test");
    nums.as_mut_slice().copy_from_slice(&[1, 2]);

    // Store handles so we can later extract the sections from the frozen area.
    let handle_raw = raw.handle();
    let handle_nums = nums.handle();

    // Freeze the sections into immutable `Bytes`.
    let frozen_raw: Bytes = raw.freeze()?;
    let frozen_nums: Bytes = nums.freeze()?;
    assert_eq!(frozen_raw.as_ref(), b"test");
    assert_eq!(frozen_nums.view::<[u32]>().unwrap().as_ref(), &[1, 2]);

    drop(sections);

    // Decide whether to keep the area in memory or persist it to disk.
    let memory_or_file = true;
    if memory_or_file {
        // Freeze the whole area into immutable `Bytes`.
        let all: Bytes = area.freeze()?;
        assert_eq!(handle_raw.bytes(&all).as_ref(), frozen_raw.as_ref());
        assert_eq!(handle_nums.view(&all).unwrap().as_ref(), &[1, 2]);
    } else {
        // Persist the temporary file.
        let dir = tempdir()?;
        let path = dir.path().join("area.bin");
        area.persist(&path)?;
        assert!(path.exists());
    }

    Ok(())
}