binary-layout 4.0.2

The binary-layout library allows type-safe, inplace, zero-copy access to structured binary data. You define a custom data layout and give it a slice of binary data, and it will allow you to read and write the fields defined in the layout from the binary data without having to copy any of the data. It's similar to transmuting to/from a `#[repr(packed)]` struct, but much safer.
Documentation
use binary_layout::{prelude::*, Data};

mod common;
use common::data_region;

#[test]
fn given_immutableview_when_extractingimmutableref() {
    binary_layout!(layout, LittleEndian, {
        field: u8,
        tail: [u8],
    });

    let storage = data_region(1024, 0);
    let extracted = {
        let view = layout::View::new(&storage);
        view.into_tail()
        // here, the view dies but the extracted reference lives on
    };

    assert_eq!(&data_region(1024, 0)[1..], &*extracted);
}

#[test]
fn given_immutableview_with_reftovec_when_extractingimmutableref() {
    binary_layout!(layout, LittleEndian, {
        field: u8,
        tail: [u8],
    });

    let storage = data_region(1024, 0);
    let extracted: Data<&Vec<u8>> = {
        let view: layout::View<&Vec<u8>> = layout::View::new(&storage);
        view.into_tail()
        // here, the view dies but the extracted reference lives on
    };

    assert_eq!(&data_region(1024, 0)[1..], &*extracted);
}

#[test]
fn given_mutableview_when_extractingimmutableref() {
    binary_layout!(layout, LittleEndian, {
        field: u8,
        tail: [u8],
    });

    let mut storage = data_region(1024, 0);
    let extracted: Data<&mut [u8]> = {
        let view: layout::View<&mut [u8]> = layout::View::new(&mut storage);
        view.into_tail()
    };

    assert_eq!(&data_region(1024, 0)[1..], &*extracted);
}

#[test]
fn given_mutableview_with_reftovec_when_extractingimmutableref() {
    binary_layout!(layout, LittleEndian, {
        field: u8,
        tail: [u8],
    });

    let mut storage = data_region(1024, 0);
    let extracted: Data<&mut Vec<u8>> = {
        let view: layout::View<&mut Vec<u8>> = layout::View::new(&mut storage);
        view.into_tail()
    };

    assert_eq!(&data_region(1024, 0)[1..], &*extracted);
}

#[test]
fn given_mutableview_when_extractingmutableref() {
    binary_layout!(layout, LittleEndian, {
        field: u8,
        tail: [u8],
    });

    let mut storage = data_region(1024, 0);
    let extracted: Data<&mut [u8]> = {
        let view: layout::View<&mut [u8]> = layout::View::new(&mut storage);
        view.into_tail()
    };

    assert_eq!(&data_region(1024, 0)[1..], &*extracted);
}

#[test]
fn given_mutableview_with_reftovec_when_extractingmutableref() {
    binary_layout!(layout, LittleEndian, {
        field: u8,
        tail: [u8],
    });

    let mut storage = data_region(1024, 0);
    let extracted: Data<&mut Vec<u8>> = {
        let view: layout::View<&mut Vec<u8>> = layout::View::new(&mut storage);
        view.into_tail()
    };

    assert_eq!(&data_region(1024, 0)[1..], &*extracted);
}