#byte #endian #transmute #loss #aligned #owned

transmute-bytes

简单。安全。无损耗地从字节转换

2个版本

0.1.1 2022年7月2日
0.1.0 2022年7月2日

#2326 in 编码

MIT许可证

6KB
90

简单。安全。无损耗地从字节转换

用法

use transmute_bytes::transmute_bytes;

fn main() {
    // Your byte data
    let bytes = [1_u8, 2, 3, 4, 5, 6, 7, 8, 0, 1];
    // You will receive:
    // - `Cow::Borrow(slice)` if data is aligned to `u64`
    //    and  length is a multiple of the length of u64
    //    ----------------------------------------------
    // - `Cow::Owned(vec)` if data is not aligned to `u64`
    //    or length is not a multiple of the length of u64
    let cow = transmute_bytes::<u64>(&bytes);

    // It depends on your endian
    let le = cow.as_ref() == [578437695752307201, 256];
    let be = cow.as_ref() == [72623859790382856, 281474976710656];
    assert!(le || be);
}

无运行时依赖