| 1 |
[](https://docs.rs/lebe) |
| 2 |
[](https://crates.io/crates/lebe) |
| 3 |
 |
| 4 |
|
| 5 |
|
| 6 |
# LEBE |
| 7 |
Tiny, dead simple, high performance endianness conversions with a generic API. |
| 8 |
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)`. |
| 9 |
|
| 10 |
# Purpose |
| 11 |
This crate has exactly two purposes: |
| 12 |
1. Simple conversion between slices of primitives and byte arrays without unsafe code |
| 13 |
2. Simple and fast conversion from one endianness to the other one |
| 14 |
|
| 15 |
The [byteorder crate](https://github.com/BurntSushi/byteorder) uses  for this. |
| 16 |
|
| 17 |
This simplifies reading and writing binary data to files or network streams. |
| 18 |
|
| 19 |
|
| 20 |
# Usage |
| 21 |
|
| 22 |
Write values. |
| 23 |
```rust |
| 24 |
use lebe::io::WriteEndian; |
| 25 |
use std::io::Write; |
| 26 |
|
| 27 |
fn main(){ |
| 28 |
let mut output_bytes: Vec<u8> = Vec::new(); |
| 29 |
|
| 30 |
let numbers: &[i32] = &[ 32, 102, 420, 594 ]; |
| 31 |
output_bytes.write_as_little_endian(numbers.len()).unwrap(); |
| 32 |
output_bytes.write_as_little_endian(numbers).unwrap(); |
| 33 |
} |
| 34 |
``` |
| 35 |
|
| 36 |
Read numbers. |
| 37 |
```rust |
| 38 |
use lebe::io::ReadEndian; |
| 39 |
use std::io::Read; |
| 40 |
|
| 41 |
fn main(){ |
| 42 |
let mut input_bytes: &[u8] = &[ 3, 244 ]; |
| 43 |
let number: u16 = input_bytes.read_from_little_endian().unwrap(); |
| 44 |
} |
| 45 |
``` |
| 46 |
|
| 47 |
Read slices. |
| 48 |
```rust |
| 49 |
use lebe::io::ReadEndian; |
| 50 |
use std::io::Read; |
| 51 |
|
| 52 |
fn main(){ |
| 53 |
let mut input_bytes: &[u8] = &[ 0, 2, 0, 3, 244, 1, 0, 3, 244, 1 ]; |
| 54 |
|
| 55 |
let len: u16 = input_bytes.read_from_little_endian().unwrap(); |
| 56 |
let mut numbers = vec![ 0.0; len as usize ]; |
| 57 |
|
| 58 |
input_bytes.read_from_little_endian_into(numbers.as_mut_slice()).unwrap(); |
| 59 |
} |
| 60 |
``` |
| 61 |
|
| 62 |
Convert slices in-place. |
| 63 |
```rust |
| 64 |
use lebe::Endian; |
| 65 |
|
| 66 |
fn main(){ |
| 67 |
let mut numbers: &[i32] = &[ 32, 102, 420, 594 ]; |
| 68 |
numbers.convert_current_to_little_endian(); |
| 69 |
} |
| 70 |
``` |
| 71 |
|
| 72 |
|
| 73 |
# Why not use [byteorder](https://crates.io/crates/byteorder)? |
| 74 |
This crate supports batch-writing slices with native speed |
| 75 |
where the os has the matching endianness. Writing slices in `byteorder` |
| 76 |
must be done manually, and may be slower than expected. |
| 77 |
This crate does provide u8 and i8 slice operations for completeness. |
| 78 |
Also, the API of this crate looks simpler. |
| 79 |
|
| 80 |
# Why not use [endianness](https://crates.io/crates/endianness)? |
| 81 |
This crate has no runtime costs, just as `byteorder`. |
| 82 |
|
| 83 |
# Why not use this crate? |
| 84 |
The other crates probably have better documentation. |
| 85 |
|
| 86 |
|
| 87 |
# Fun Facts |
| 88 |
LEBE is made up from 'le' for little endian and 'be' for big endian. |
| 89 |
If you say that word using english pronounciation, |
| 90 |
a german might think you said the german word for 'love'. |