5 releases

0.2.2 Jul 26, 2021
0.2.1 Feb 28, 2021
0.2.0 Aug 17, 2020
0.1.1 Jun 13, 2020
0.1.0 Jun 12, 2020

#84 in #endian

Download history 22/week @ 2024-07-22 76/week @ 2024-07-29 53/week @ 2024-08-05 58/week @ 2024-08-12 95/week @ 2024-08-19 116/week @ 2024-08-26 176/week @ 2024-09-02 32/week @ 2024-09-09 53/week @ 2024-09-16 59/week @ 2024-09-23 39/week @ 2024-09-30 59/week @ 2024-10-07 57/week @ 2024-10-14 67/week @ 2024-10-21 18/week @ 2024-10-28 77/week @ 2024-11-04

221 downloads per month
Used in endiannezz

MIT license

12KB
279 lines

endiannezz

Build Status Latest Version Documentation

Zero dependencies library for I/O endianness on high-level

Installing

[dependencies]
endiannezz = "0.6"

Using #[derive(Io)]

use endiannezz::Io;
use std::io::Result;

#[derive(Io)]
#[endian(big)]
struct ParseMe {
    works: bool,
    data: u32,
    #[endian(little)]
    extra: i16,
}

fn main() -> Result<()> {
    let s1 = ParseMe {
        works: true,
        data: 10,
        extra: 20,
    };

    //writing struct as bytes into vec
    let mut vec = Vec::new();
    s1.write(&mut vec)?;

    let mut slice = vec.as_slice();
    #[rustfmt::skip]
    assert_eq!(slice, &[
        1, //bool as byte
        0, 0, 0, 10, //u32 in big-endian (because big-endian is set on top place struct as default)
        20, 0, //i16 in little-endian (overriding default)
    ]);

    //reading struct from bytes
    let _s2 = ParseMe::read(&mut slice)?;

    Ok(())
}

Simple example

use endiannezz::ext::{EndianReader, EndianWriter};
use endiannezz::{BigEndian, LittleEndian, NativeEndian};
use std::io::Result;

fn main() -> Result<()> {
    let mut vec = Vec::new();

    vec.try_write::<LittleEndian, i32>(1)?;
    vec.try_write::<BigEndian, _>(2)?;
    vec.try_write::<NativeEndian, _>(3_u16)?;

    let mut slice = vec.as_slice();

    slice.try_read::<LittleEndian, i32>()?;
    let _num32: i32 = slice.try_read::<BigEndian, _>()?;
    let _num16: u16 = slice.try_read::<NativeEndian, _>()?;

    Ok(())
}

Dependencies

~1.5MB
~37K SLoC