#endianness #binary-data

lebe

Tiny, dead simple, high performance endianness conversions with a generic API

3 releases

0.5.2 Aug 16, 2022
0.5.1 Feb 21, 2020
0.5.0 Feb 15, 2020

#580 in Rust patterns

Download history 156727/week @ 2024-07-22 166218/week @ 2024-07-29 151507/week @ 2024-08-05 183405/week @ 2024-08-12 164626/week @ 2024-08-19 154580/week @ 2024-08-26 150044/week @ 2024-09-02 148142/week @ 2024-09-09 142319/week @ 2024-09-16 157443/week @ 2024-09-23 163692/week @ 2024-09-30 151998/week @ 2024-10-07 164540/week @ 2024-10-14 157487/week @ 2024-10-21 167073/week @ 2024-10-28 162180/week @ 2024-11-04

663,566 downloads per month
Used in 50 crates (via exr)

BSD-3-Clause

27KB
273 lines

Rust Docs Crate Crate Lines of Code

LEBE

Tiny, dead simple, high performance endianness conversions with a generic API. This crate purposefully does not have a different method, like write_u16(my_value), for each primitive type. Instead, this uses generic type inference: write(my_u16).

Purpose

This crate has exactly two purposes:

  1. Simple conversion between slices of primitives and byte arrays without unsafe code
  2. Simple and fast conversion from one endianness to the other one

The byteorder crate uses Lines of Code for this.

This simplifies reading and writing binary data to files or network streams.

Usage

Write values.

    use lebe::io::WriteEndian;
    use std::io::Write;
    
    fn main(){
        let mut output_bytes: Vec<u8> = Vec::new();

        let numbers: &[i32] = &[ 32, 102, 420, 594 ];
        output_bytes.write_as_little_endian(numbers.len()).unwrap();
        output_bytes.write_as_little_endian(numbers).unwrap();
    }

Read numbers.

    use lebe::io::ReadEndian;
    use std::io::Read;
    
    fn main(){
        let mut input_bytes: &[u8] = &[ 3, 244 ];
        let number: u16 = input_bytes.read_from_little_endian().unwrap();
    }

Read slices.

    use lebe::io::ReadEndian;
    use std::io::Read;
    
    fn main(){
        let mut input_bytes: &[u8] = &[ 0, 2, 0, 3, 244, 1, 0, 3, 244, 1 ];
        
        let len: u16 = input_bytes.read_from_little_endian().unwrap();
        let mut numbers = vec![ 0.0; len as usize ];
        
        input_bytes.read_from_little_endian_into(numbers.as_mut_slice()).unwrap();
    }

Convert slices in-place.

    use lebe::Endian;
    
    fn main(){
        let mut numbers: &[i32] = &[ 32, 102, 420, 594 ];
        numbers.convert_current_to_little_endian();
    }

Why not use byteorder?

This crate supports batch-writing slices with native speed where the os has the matching endianness. Writing slices in byteorder must be done manually, and may be slower than expected. This crate does provide u8 and i8 slice operations for completeness. Also, the API of this crate looks simpler.

Why not use endianness?

This crate has no runtime costs, just as byteorder.

Why not use this crate?

The other crates probably have better documentation.

Fun Facts

LEBE is made up from 'le' for little endian and 'be' for big endian. If you say that word using english pronounciation, a german might think you said the german word for 'love'.

No runtime deps