#字节序 #大端序 #小端序 #二进制

endiannezz

适用于高级I/O字节序的零依赖库

17个版本

0.6.5 2021年7月26日
0.6.3 2021年2月28日
0.6.0 2020年11月28日
0.4.1 2020年6月13日

#1451编码

Download history 31/week @ 2024-03-11 18/week @ 2024-03-18 66/week @ 2024-04-01 54/week @ 2024-04-08 39/week @ 2024-04-15 35/week @ 2024-04-22 34/week @ 2024-04-29 36/week @ 2024-05-06 6/week @ 2024-05-13 21/week @ 2024-05-20 10/week @ 2024-05-27 17/week @ 2024-06-03 98/week @ 2024-06-10 72/week @ 2024-06-17 33/week @ 2024-06-24

221 每月下载量
theory 中使用

MIT 许可证

16KB
202

endiannezz

Build Status Latest Version Documentation

适用于高级I/O字节序的零依赖库

安装

[dependencies]
endiannezz = "0.6"

使用 #[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(())
}

简单示例

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(())
}

依赖

~215KB