Trait bytes::ByteOrder [] [src]

pub trait ByteOrder: Sealed + Copy + PartialEq<Self> + Eq + Ord + PartialOrd<Self> + Clone + Default + Hash + Debug {
    fn read_u16(buf: &[u8]) -> u16;
fn read_u32(buf: &[u8]) -> u32;
fn read_u64(buf: &[u8]) -> u64;
fn read_uint(buf: &[u8], nbytes: usize) -> u64;
fn write_u16(buf: &mut [u8], n: u16);
fn write_u32(buf: &mut [u8], n: u32);
fn write_u64(buf: &mut [u8], n: u64);
fn write_uint(buf: &mut [u8], n: u64, nbytes: usize);
fn read_u16_into(src: &[u8], dst: &mut [u16]);
fn read_u32_into(src: &[u8], dst: &mut [u32]);
fn read_u64_into(src: &[u8], dst: &mut [u64]);
fn write_u16_into(src: &[u16], dst: &mut [u8]);
fn write_u32_into(src: &[u32], dst: &mut [u8]);
fn write_u64_into(src: &[u64], dst: &mut [u8]);
fn from_slice_u16(numbers: &mut [u16]);
fn from_slice_u32(numbers: &mut [u32]);
fn from_slice_u64(numbers: &mut [u64]);
fn from_slice_f32(numbers: &mut [f32]);
fn from_slice_f64(numbers: &mut [f64]); fn read_u24(buf: &[u8]) -> u32 { ... }
fn write_u24(buf: &mut [u8], n: u32) { ... }
fn read_i16(buf: &[u8]) -> i16 { ... }
fn read_i24(buf: &[u8]) -> i32 { ... }
fn read_i32(buf: &[u8]) -> i32 { ... }
fn read_i64(buf: &[u8]) -> i64 { ... }
fn read_int(buf: &[u8], nbytes: usize) -> i64 { ... }
fn read_f32(buf: &[u8]) -> f32 { ... }
fn read_f64(buf: &[u8]) -> f64 { ... }
fn write_i16(buf: &mut [u8], n: i16) { ... }
fn write_i24(buf: &mut [u8], n: i32) { ... }
fn write_i32(buf: &mut [u8], n: i32) { ... }
fn write_i64(buf: &mut [u8], n: i64) { ... }
fn write_int(buf: &mut [u8], n: i64, nbytes: usize) { ... }
fn write_f32(buf: &mut [u8], n: f32) { ... }
fn write_f64(buf: &mut [u8], n: f64) { ... }
fn read_i16_into(src: &[u8], dst: &mut [i16]) { ... }
fn read_i32_into(src: &[u8], dst: &mut [i32]) { ... }
fn read_i64_into(src: &[u8], dst: &mut [i64]) { ... }
fn read_f32_into_unchecked(src: &[u8], dst: &mut [f32]) { ... }
fn read_f64_into_unchecked(src: &[u8], dst: &mut [f64]) { ... }
fn write_i16_into(src: &[i16], dst: &mut [u8]) { ... }
fn write_i32_into(src: &[i32], dst: &mut [u8]) { ... }
fn write_i64_into(src: &[i64], dst: &mut [u8]) { ... }
fn write_f32_into(src: &[f32], dst: &mut [u8]) { ... }
fn write_f64_into(src: &[f64], dst: &mut [u8]) { ... }
fn from_slice_i16(src: &mut [i16]) { ... }
fn from_slice_i32(src: &mut [i32]) { ... }
fn from_slice_i64(src: &mut [i64]) { ... } }

ByteOrder describes types that can serialize integers as bytes.

Note that Self does not appear anywhere in this trait's definition! Therefore, in order to use it, you'll need to use syntax like T::read_u16(&[0, 1]) where T implements ByteOrder.

This crate provides two types that implement ByteOrder: BigEndian and LittleEndian. This trait is sealed and cannot be implemented for callers to avoid breaking backwards compatibility when adding new derived traits.

Examples

Write and read u32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 4];
LittleEndian::write_u32(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u32(&buf));

Write and read i16 numbers in big endian order:

use byteorder::{ByteOrder, BigEndian};

let mut buf = [0; 2];
BigEndian::write_i16(&mut buf, -50_000);
assert_eq!(-50_000, BigEndian::read_i16(&buf));

Required Methods

Reads an unsigned 16 bit integer from buf.

Panics

Panics when buf.len() < 2.

Reads an unsigned 32 bit integer from buf.

Panics

Panics when buf.len() < 4.

Examples

Write and read u32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 4];
LittleEndian::write_u32(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u32(&buf));

Reads an unsigned 64 bit integer from buf.

Panics

Panics when buf.len() < 8.

Examples

Write and read u64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 8];
LittleEndian::write_u64(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u64(&buf));

Reads an unsigned n-bytes integer from buf.

Panics

Panics when nbytes < 1 or nbytes > 8 or buf.len() < nbytes

Examples

Write and read an n-byte number in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_uint(&mut buf, 1_000_000, 3);
assert_eq!(1_000_000, LittleEndian::read_uint(&buf, 3));

Writes an unsigned 16 bit integer n to buf.

Panics

Panics when buf.len() < 2.

Examples

Write and read u16 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 2];
LittleEndian::write_u16(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u16(&buf));

Writes an unsigned 32 bit integer n to buf.

Panics

Panics when buf.len() < 4.

Examples

Write and read u32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 4];
LittleEndian::write_u32(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u32(&buf));

Writes an unsigned 64 bit integer n to buf.

Panics

Panics when buf.len() < 8.

Examples

Write and read u64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 8];
LittleEndian::write_u64(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u64(&buf));

Writes an unsigned integer n to buf using only nbytes.

Panics

If n is not representable in nbytes, or if nbytes is > 8, then this method panics.

Examples

Write and read an n-byte number in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_uint(&mut buf, 1_000_000, 3);
assert_eq!(1_000_000, LittleEndian::read_uint(&buf, 3));

Reads unsigned 16 bit integers from src into dst.

Panics

Panics when src.len() != 2*dst.len().

Examples

Write and read u16 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 8];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_u16_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_u16_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

Reads unsigned 32 bit integers from src into dst.

Panics

Panics when src.len() != 4*dst.len().

Examples

Write and read u32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 16];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_u32_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_u32_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

Reads unsigned 64 bit integers from src into dst.

Panics

Panics when src.len() != 8*dst.len().

Examples

Write and read u64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 32];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_u64_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_u64_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

Writes unsigned 16 bit integers from src into dst.

Panics

Panics when dst.len() != 2*src.len().

Examples

Write and read u16 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 8];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_u16_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_u16_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

Writes unsigned 32 bit integers from src into dst.

Panics

Panics when dst.len() != 4*src.len().

Examples

Write and read u32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 16];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_u32_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_u32_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

Writes unsigned 64 bit integers from src into dst.

Panics

Panics when dst.len() != 8*src.len().

Examples

Write and read u64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 32];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_u64_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_u64_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

Converts the given slice of unsigned 16 bit integers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Examples

Convert the host platform's endianness to big-endian:

use byteorder::{ByteOrder, BigEndian};

let mut numbers = [5, 65000];
BigEndian::from_slice_u16(&mut numbers);
assert_eq!(numbers, [5u16.to_be(), 65000u16.to_be()]);

Converts the given slice of unsigned 32 bit integers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Examples

Convert the host platform's endianness to big-endian:

use byteorder::{ByteOrder, BigEndian};

let mut numbers = [5, 65000];
BigEndian::from_slice_u32(&mut numbers);
assert_eq!(numbers, [5u32.to_be(), 65000u32.to_be()]);

Converts the given slice of unsigned 64 bit integers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Examples

Convert the host platform's endianness to big-endian:

use byteorder::{ByteOrder, BigEndian};

let mut numbers = [5, 65000];
BigEndian::from_slice_u64(&mut numbers);
assert_eq!(numbers, [5u64.to_be(), 65000u64.to_be()]);

Converts the given slice of IEEE754 single-precision (4 bytes) floating point numbers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Converts the given slice of IEEE754 double-precision (8 bytes) floating point numbers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Provided Methods

Reads an unsigned 24 bit integer from buf, stored in u32.

Panics

Panics when buf.len() < 3.

Examples

Write and read 24 bit u32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_u24(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u24(&buf));

Writes an unsigned 24 bit integer n to buf, stored in u32.

Panics

Panics when buf.len() < 3.

Examples

Write and read 24 bit u32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_u24(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u24(&buf));

Reads a signed 16 bit integer from buf.

Panics

Panics when buf.len() < 2.

Examples

Write and read i16 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 2];
LittleEndian::write_i16(&mut buf, -1_000);
assert_eq!(-1_000, LittleEndian::read_i16(&buf));

Reads a signed 24 bit integer from buf, stored in i32.

Panics

Panics when buf.len() < 3.

Examples

Write and read 24 bit i32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_i24(&mut buf, -1_000_000);
assert_eq!(-1_000_000, LittleEndian::read_i24(&buf));

Reads a signed 32 bit integer from buf.

Panics

Panics when buf.len() < 4.

Examples

Write and read i32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 4];
LittleEndian::write_i32(&mut buf, -1_000_000);
assert_eq!(-1_000_000, LittleEndian::read_i32(&buf));

Reads a signed 64 bit integer from buf.

Panics

Panics when buf.len() < 8.

Examples

Write and read i64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 8];
LittleEndian::write_i64(&mut buf, -1_000_000_000);
assert_eq!(-1_000_000_000, LittleEndian::read_i64(&buf));

Reads a signed n-bytes integer from buf.

Panics

Panics when nbytes < 1 or nbytes > 8 or buf.len() < nbytes

Examples

Write and read n-length signed numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_int(&mut buf, -1_000, 3);
assert_eq!(-1_000, LittleEndian::read_int(&buf, 3));

Reads a IEEE754 single-precision (4 bytes) floating point number.

Panics

Panics when buf.len() < 4.

Examples

Write and read f32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let e = 2.71828;
let mut buf = [0; 4];
LittleEndian::write_f32(&mut buf, e);
assert_eq!(e, LittleEndian::read_f32(&buf));

Reads a IEEE754 double-precision (8 bytes) floating point number.

Panics

Panics when buf.len() < 8.

Examples

Write and read f64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let phi = 1.6180339887;
let mut buf = [0; 8];
LittleEndian::write_f64(&mut buf, phi);
assert_eq!(phi, LittleEndian::read_f64(&buf));

Writes a signed 16 bit integer n to buf.

Panics

Panics when buf.len() < 2.

Examples

Write and read i16 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 2];
LittleEndian::write_i16(&mut buf, -1_000);
assert_eq!(-1_000, LittleEndian::read_i16(&buf));

Writes a signed 24 bit integer n to buf, stored in i32.

Panics

Panics when buf.len() < 3.

Examples

Write and read 24 bit i32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_i24(&mut buf, -1_000_000);
assert_eq!(-1_000_000, LittleEndian::read_i24(&buf));

Writes a signed 32 bit integer n to buf.

Panics

Panics when buf.len() < 4.

Examples

Write and read i32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 4];
LittleEndian::write_i32(&mut buf, -1_000_000);
assert_eq!(-1_000_000, LittleEndian::read_i32(&buf));

Writes a signed 64 bit integer n to buf.

Panics

Panics when buf.len() < 8.

Examples

Write and read i64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 8];
LittleEndian::write_i64(&mut buf, -1_000_000_000);
assert_eq!(-1_000_000_000, LittleEndian::read_i64(&buf));

Writes a signed integer n to buf using only nbytes.

Panics

If n is not representable in nbytes, or if nbytes is > 8, then this method panics.

Examples

Write and read an n-byte number in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_int(&mut buf, -1_000, 3);
assert_eq!(-1_000, LittleEndian::read_int(&buf, 3));

Writes a IEEE754 single-precision (4 bytes) floating point number.

Panics

Panics when buf.len() < 4.

Examples

Write and read f32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let e = 2.71828;
let mut buf = [0; 4];
LittleEndian::write_f32(&mut buf, e);
assert_eq!(e, LittleEndian::read_f32(&buf));

Writes a IEEE754 double-precision (8 bytes) floating point number.

Panics

Panics when buf.len() < 8.

Examples

Write and read f64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let phi = 1.6180339887;
let mut buf = [0; 8];
LittleEndian::write_f64(&mut buf, phi);
assert_eq!(phi, LittleEndian::read_f64(&buf));

Reads signed 16 bit integers from src to dst.

Panics

Panics when buf.len() != 2*dst.len().

Examples

Write and read i16 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 8];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_i16_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_i16_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

Reads signed 32 bit integers from src into dst.

Panics

Panics when src.len() != 4*dst.len().

Examples

Write and read i32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 16];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_i32_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_i32_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

Reads signed 64 bit integers from src into dst.

Panics

Panics when src.len() != 8*dst.len().

Examples

Write and read i64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 32];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_i64_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_i64_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

Reads IEEE754 single-precision (4 bytes) floating point numbers from src into dst.

Panics

Panics when src.len() != 4*dst.len().

Examples

Write and read f32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 16];
let numbers_given = [1.0, 2.0, 31.312e311, -11.32e91];
LittleEndian::write_f32_into(&numbers_given, &mut bytes);

let mut numbers_got = [0.0; 4];
LittleEndian::read_f32_into_unchecked(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

Reads IEEE754 single-precision (4 bytes) floating point numbers from src into dst.

Panics

Panics when src.len() != 8*dst.len().

Examples

Write and read f64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 32];
let numbers_given = [1.0, 2.0, 31.312e311, -11.32e91];
LittleEndian::write_f64_into(&numbers_given, &mut bytes);

let mut numbers_got = [0.0; 4];
LittleEndian::read_f64_into_unchecked(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

Writes signed 16 bit integers from src into dst.

Panics

Panics when buf.len() != 2*src.len().

Examples

Write and read i16 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 8];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_i16_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_i16_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

Writes signed 32 bit integers from src into dst.

Panics

Panics when dst.len() != 4*src.len().

Examples

Write and read i32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 16];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_i32_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_i32_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

Writes signed 64 bit integers from src into dst.

Panics

Panics when dst.len() != 8*src.len().

Examples

Write and read i64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 32];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_i64_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_i64_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

Writes IEEE754 single-precision (4 bytes) floating point numbers from src into dst.

Panics

Panics when src.len() != 4*dst.len().

Examples

Write and read f32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 16];
let numbers_given = [1.0, 2.0, 31.312e311, -11.32e91];
LittleEndian::write_f32_into(&numbers_given, &mut bytes);

let mut numbers_got = [0.0; 4];
unsafe {
    LittleEndian::read_f32_into_unchecked(&bytes, &mut numbers_got);
}
assert_eq!(numbers_given, numbers_got);

Writes IEEE754 double-precision (8 bytes) floating point numbers from src into dst.

Panics

Panics when src.len() != 8*dst.len().

Examples

Write and read f64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 32];
let numbers_given = [1.0, 2.0, 31.312e311, -11.32e91];
LittleEndian::write_f64_into(&numbers_given, &mut bytes);

let mut numbers_got = [0.0; 4];
unsafe {
    LittleEndian::read_f64_into_unchecked(&bytes, &mut numbers_got);
}
assert_eq!(numbers_given, numbers_got);

Converts the given slice of signed 16 bit integers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Examples

Convert the host platform's endianness to big-endian:

use byteorder::{ByteOrder, BigEndian};

let mut numbers = [5, 65000];
BigEndian::from_slice_i16(&mut numbers);
assert_eq!(numbers, [5i16.to_be(), 65000i16.to_be()]);

Converts the given slice of signed 32 bit integers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Examples

Convert the host platform's endianness to big-endian:

use byteorder::{ByteOrder, BigEndian};

let mut numbers = [5, 65000];
BigEndian::from_slice_i32(&mut numbers);
assert_eq!(numbers, [5i32.to_be(), 65000i32.to_be()]);

Converts the given slice of signed 64 bit integers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Examples

Convert the host platform's endianness to big-endian:

use byteorder::{ByteOrder, BigEndian};

let mut numbers = [5, 65000];
BigEndian::from_slice_i64(&mut numbers);
assert_eq!(numbers, [5i64.to_be(), 65000i64.to_be()]);

Implementors