Crate tarrasque

source ·
Expand description

A library for zero-allocation parsing of binary formats.

Example

use tarrasque::{Endianness, ExtractError, Stream, extract};

extract! {
    /// A 2D point.
    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    pub struct Point[4](endianness: Endianness) {
        /// The x-coordinate of this point.
        pub x: u16 = [endianness],
        /// The y-coordinate of this point.
        pub y: u16 = [endianness],
    }
}

fn main() {
    // A stream of bytes.
    let mut stream = Stream(&[1, 2, 3, 4, 5, 6, 7, 8]);

    // Extract a point containing two big-endian `u16`s from the stream.
    let point = stream.extract::<Point, _>(Endianness::Big);
    assert_eq!(point, Ok(Point { x: 258, y: 772 }));

    // Extract a point containing two little-endian `u16`s from the stream.
    let point = stream.extract::<Point, _>(Endianness::Little);
    assert_eq!(point, Ok(Point { x: 1541, y: 2055 }));

    // Attempt to extract a point from the empty stream.
    let point = stream.extract::<Point, _>(Endianness::Big);
    assert_eq!(point, Err(ExtractError::Insufficient(2)));
}

Macros

Generates a struct and an implementation of Extract (and optionally Span) for that struct.

Structs

A stream of bytes from which values can be extracted.
A Stream wrapper that unwraps extracted values.
A slice of bytes containing values that span a fixed number of bytes.
An iterator over the values in a View.

Enums

A byte order.
An error encountered while extracting a value from a stream of bytes.

Traits

A type that can be extracted from a stream of bytes.
A type that spans a fixed number of bytes.

Functions

Returns the first four bytes in the supplied slice as a big-endian f32.
Returns the first eight bytes in the supplied slice as a big-endian f64.
Returns the first two bytes in the supplied slice as a big-endian i16.
Returns the first four bytes in the supplied slice as a big-endian i32.
Returns the first eight bytes in the supplied slice as a big-endian i64.
Returns the first two bytes in the supplied slice as a big-endian u16.
Returns the first four bytes in the supplied slice as a big-endian u32.
Returns the first eight bytes in the supplied slice as a big-endian u64.
Returns the first four bytes in the supplied slice as a little-endian f32.
Returns the first eight bytes in the supplied slice as a little-endian f64.
Returns the first two bytes in the supplied slice as a little-endian i16.
Returns the first four bytes in the supplied slice as a little-endian i32.
Returns the first eight bytes in the supplied slice as a little-endian i64.
Returns the first two bytes in the supplied slice as a little-endian u16.
Returns the first four bytes in the supplied slice as a little-endian u32.
Returns the first eight bytes in the supplied slice as a little-endian u64.

Type Definitions

The result of extracting a value from a stream of bytes.