BurntSushi / byteorder

Rust library for reading/writing numbers in big-endian and little-endian.
The Unlicense
984 stars 144 forks source link

Feature request: encode/decode from fixed size arrays #210

Closed antonilol closed 3 months ago

antonilol commented 3 months ago

It would be useful to have functions that convert between [u8; N] and number primitive types, where N is the size in bytes of that number type.

Public api example:

pub trait ByteOrder: // ...
{
    // ...

    fn encode_u16(n: u16) -> [u8; 2];
    fn encode_i64(n: i64) -> [u8; 8];
    fn decode_f32(buf: [u8; 4]) -> f32;
    // etc
}

These can be directly implemented with from_le_bytes or from_be_bytes from the standard library.

The names of the functions can be different than I suggest here, decode_f32(buf: [u8; 4]) -> f32 is very close to fn read_f32(buf: &[u8]) -> f32, the only real difference is that the size of the input buffer is fixed.

antonilol commented 3 months ago

just found out this is a duplicate of #163 and #96 (closed because rust was in a too early stage?)