#endian #byteorder #no-std

无需 std 端序类型

为具有定义字节序的类型提供类型安全包装

4 个版本

0.2.0 2023年3月6日
0.1.2 2015年7月28日
0.1.1 2015年7月27日
0.1.0 2015年7月27日

#1687编码 中排名

Download history 176718/week @ 2024-04-22 151581/week @ 2024-04-29 143407/week @ 2024-05-06 163336/week @ 2024-05-13 154162/week @ 2024-05-20 149140/week @ 2024-05-27 149389/week @ 2024-06-03 156637/week @ 2024-06-10 146278/week @ 2024-06-17 143377/week @ 2024-06-24 134395/week @ 2024-07-01 151387/week @ 2024-07-08 137566/week @ 2024-07-15 149504/week @ 2024-07-22 140758/week @ 2024-07-29 144059/week @ 2024-08-05

580,787 每月下载量
469 crate 中使用(直接使用 2 个)

MIT 许可证

14KB
273 代码行

endian-type

crates.io Documentation License

为具有定义字节序的类型提供类型安全包装。

示例

use endian_type::{types, BigEndian, LittleEndian, NetworkOrder};

// The endianness reflects the type-safety of the declaration:
let foo = 0xbeef_u32;
let foo_be = BigEndian::from(foo);
assert_eq!(foo_be.to_bytes(), foo.to_be_bytes());
assert_eq!(foo_be.as_byte_slice(), &foo.to_be_bytes());

// One can convert back to the native representation using the `From`/`Into` traits:
assert_eq!(foo, foo_be.into());

// To operate on the wrapped types as if they are regular numbers, one has to
// be explicit and switch between the byte representations:
// Note: Internally, these are just [transmutations](https://doc.rust-lang.net.cn/core/mem/fn.transmute.html) and should not affect performance.
let foo = u128::MAX;
let foo_le = LittleEndian::from(foo);
assert_eq!(
    LittleEndian::<u128>::from_bytes(u128::from_ne_bytes(foo_le.to_bytes()).wrapping_add(1).to_ne_bytes()),
    LittleEndian::<u128>::from(0)
);

// We also have a couple of aliases to be used as helper.
//
// This will assert our `NetworkOrder` type is in accordance with the IETF RFC1700.
let foo = -0xdead_i32;
let foo_no = NetworkOrder::from(foo);
let foo_be = types::i32_be::from(foo);
assert_eq!(foo.to_be_bytes(), foo_no.to_bytes());
assert_eq!(foo.to_be_bytes(), foo_be.to_bytes());

无运行时依赖