Trait ByteOrder
trait ByteOrder: Clone + Copy + Debug + Default + Eq + Hash + Ord + PartialEq + PartialOrd + private::Sealed
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 ;
let mut buf = ;
write_u32;
assert_eq!;
Write and read i16 numbers in big endian order:
use ;
let mut buf = ;
write_i16;
assert_eq!;
Required Methods
fn read_u16(buf: &[u8]) -> u16Reads an unsigned 16 bit integer from
buf.Panics
Panics when
buf.len() < 2.fn read_u32(buf: &[u8]) -> u32Reads an unsigned 32 bit integer from
buf.Panics
Panics when
buf.len() < 4.Examples
Write and read
u32numbers in little endian order:use ; let mut buf = ; write_u32; assert_eq!;fn read_u64(buf: &[u8]) -> u64Reads an unsigned 64 bit integer from
buf.Panics
Panics when
buf.len() < 8.Examples
Write and read
u64numbers in little endian order:use ; let mut buf = ; write_u64; assert_eq!;fn read_u128(buf: &[u8]) -> u128Reads an unsigned 128 bit integer from
buf.Panics
Panics when
buf.len() < 16.Examples
Write and read
u128numbers in little endian order:use ; let mut buf = ; write_u128; assert_eq!;fn read_uint(buf: &[u8], nbytes: usize) -> u64Reads an unsigned n-bytes integer from
buf.Panics
Panics when
nbytes < 1ornbytes > 8orbuf.len() < nbytesExamples
Write and read an n-byte number in little endian order:
use ; let mut buf = ; write_uint; assert_eq!;fn read_uint128(buf: &[u8], nbytes: usize) -> u128Reads an unsigned n-bytes integer from
buf.Panics
Panics when
nbytes < 1ornbytes > 16orbuf.len() < nbytesExamples
Write and read an n-byte number in little endian order:
use ; let mut buf = ; write_uint128; assert_eq!;fn write_u16(buf: &mut [u8], n: u16)Writes an unsigned 16 bit integer
ntobuf.Panics
Panics when
buf.len() < 2.Examples
Write and read
u16numbers in little endian order:use ; let mut buf = ; write_u16; assert_eq!;fn write_u32(buf: &mut [u8], n: u32)Writes an unsigned 32 bit integer
ntobuf.Panics
Panics when
buf.len() < 4.Examples
Write and read
u32numbers in little endian order:use ; let mut buf = ; write_u32; assert_eq!;fn write_u64(buf: &mut [u8], n: u64)Writes an unsigned 64 bit integer
ntobuf.Panics
Panics when
buf.len() < 8.Examples
Write and read
u64numbers in little endian order:use ; let mut buf = ; write_u64; assert_eq!;fn write_u128(buf: &mut [u8], n: u128)Writes an unsigned 128 bit integer
ntobuf.Panics
Panics when
buf.len() < 16.Examples
Write and read
u128numbers in little endian order:use ; let mut buf = ; write_u128; assert_eq!;fn write_uint(buf: &mut [u8], n: u64, nbytes: usize)Writes an unsigned integer
ntobufusing onlynbytes.Panics
If
nis not representable innbytes, or ifnbytesis> 8, then this method panics.Examples
Write and read an n-byte number in little endian order:
use ; let mut buf = ; write_uint; assert_eq!;fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize)Writes an unsigned integer
ntobufusing onlynbytes.Panics
If
nis not representable innbytes, or ifnbytesis> 16, then this method panics.Examples
Write and read an n-byte number in little endian order:
use ; let mut buf = ; write_uint128; assert_eq!;fn read_u16_into(src: &[u8], dst: &mut [u16])Reads unsigned 16 bit integers from
srcintodst.Panics
Panics when
src.len() != 2*dst.len().Examples
Write and read
u16numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_u16_into; let mut numbers_got = ; read_u16_into; assert_eq!;fn read_u32_into(src: &[u8], dst: &mut [u32])Reads unsigned 32 bit integers from
srcintodst.Panics
Panics when
src.len() != 4*dst.len().Examples
Write and read
u32numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_u32_into; let mut numbers_got = ; read_u32_into; assert_eq!;fn read_u64_into(src: &[u8], dst: &mut [u64])Reads unsigned 64 bit integers from
srcintodst.Panics
Panics when
src.len() != 8*dst.len().Examples
Write and read
u64numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_u64_into; let mut numbers_got = ; read_u64_into; assert_eq!;fn read_u128_into(src: &[u8], dst: &mut [u128])Reads unsigned 128 bit integers from
srcintodst.Panics
Panics when
src.len() != 16*dst.len().Examples
Write and read
u128numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_u128_into; let mut numbers_got = ; read_u128_into; assert_eq!;fn write_u16_into(src: &[u16], dst: &mut [u8])Writes unsigned 16 bit integers from
srcintodst.Panics
Panics when
dst.len() != 2*src.len().Examples
Write and read
u16numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_u16_into; let mut numbers_got = ; read_u16_into; assert_eq!;fn write_u32_into(src: &[u32], dst: &mut [u8])Writes unsigned 32 bit integers from
srcintodst.Panics
Panics when
dst.len() != 4*src.len().Examples
Write and read
u32numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_u32_into; let mut numbers_got = ; read_u32_into; assert_eq!;fn write_u64_into(src: &[u64], dst: &mut [u8])Writes unsigned 64 bit integers from
srcintodst.Panics
Panics when
dst.len() != 8*src.len().Examples
Write and read
u64numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_u64_into; let mut numbers_got = ; read_u64_into; assert_eq!;fn write_u128_into(src: &[u128], dst: &mut [u8])Writes unsigned 128 bit integers from
srcintodst.Panics
Panics when
dst.len() != 16*src.len().Examples
Write and read
u128numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_u128_into; let mut numbers_got = ; read_u128_into; assert_eq!;fn from_slice_u16(numbers: &mut [u16])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 ; let mut numbers = ; from_slice_u16; assert_eq!;fn from_slice_u32(numbers: &mut [u32])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 ; let mut numbers = ; from_slice_u32; assert_eq!;fn from_slice_u64(numbers: &mut [u64])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 ; let mut numbers = ; from_slice_u64; assert_eq!;fn from_slice_u128(numbers: &mut [u128])Converts the given slice of unsigned 128 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 ; let mut numbers = ; from_slice_u128; assert_eq!;fn from_slice_f32(numbers: &mut [f32])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.
fn from_slice_f64(numbers: &mut [f64])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
fn read_u24(buf: &[u8]) -> u32Reads an unsigned 24 bit integer from
buf, stored in u32.Panics
Panics when
buf.len() < 3.Examples
Write and read 24 bit
u32numbers in little endian order:use ; let mut buf = ; write_u24; assert_eq!;fn read_u48(buf: &[u8]) -> u64Reads an unsigned 48 bit integer from
buf, stored in u64.Panics
Panics when
buf.len() < 6.Examples
Write and read 48 bit
u64numbers in little endian order:use ; let mut buf = ; write_u48; assert_eq!;fn write_u24(buf: &mut [u8], n: u32)Writes an unsigned 24 bit integer
ntobuf, stored in u32.Panics
Panics when
buf.len() < 3.Examples
Write and read 24 bit
u32numbers in little endian order:use ; let mut buf = ; write_u24; assert_eq!;fn write_u48(buf: &mut [u8], n: u64)Writes an unsigned 48 bit integer
ntobuf, stored in u64.Panics
Panics when
buf.len() < 6.Examples
Write and read 48 bit
u64numbers in little endian order:use ; let mut buf = ; write_u48; assert_eq!;fn read_i16(buf: &[u8]) -> i16Reads a signed 16 bit integer from
buf.Panics
Panics when
buf.len() < 2.Examples
Write and read
i16numbers in little endian order:use ; let mut buf = ; write_i16; assert_eq!;fn read_i24(buf: &[u8]) -> i32Reads a signed 24 bit integer from
buf, stored in i32.Panics
Panics when
buf.len() < 3.Examples
Write and read 24 bit
i32numbers in little endian order:use ; let mut buf = ; write_i24; assert_eq!;fn read_i32(buf: &[u8]) -> i32Reads a signed 32 bit integer from
buf.Panics
Panics when
buf.len() < 4.Examples
Write and read
i32numbers in little endian order:use ; let mut buf = ; write_i32; assert_eq!;fn read_i48(buf: &[u8]) -> i64Reads a signed 48 bit integer from
buf, stored in i64.Panics
Panics when
buf.len() < 6.Examples
Write and read 48 bit
i64numbers in little endian order:use ; let mut buf = ; write_i48; assert_eq!;fn read_i64(buf: &[u8]) -> i64Reads a signed 64 bit integer from
buf.Panics
Panics when
buf.len() < 8.Examples
Write and read
i64numbers in little endian order:use ; let mut buf = ; write_i64; assert_eq!;fn read_i128(buf: &[u8]) -> i128Reads a signed 128 bit integer from
buf.Panics
Panics when
buf.len() < 16.Examples
Write and read
i128numbers in little endian order:use ; let mut buf = ; write_i128; assert_eq!;fn read_int(buf: &[u8], nbytes: usize) -> i64Reads a signed n-bytes integer from
buf.Panics
Panics when
nbytes < 1ornbytes > 8orbuf.len() < nbytesExamples
Write and read n-length signed numbers in little endian order:
use ; let mut buf = ; write_int; assert_eq!;fn read_int128(buf: &[u8], nbytes: usize) -> i128Reads a signed n-bytes integer from
buf.Panics
Panics when
nbytes < 1ornbytes > 16orbuf.len() < nbytesExamples
Write and read n-length signed numbers in little endian order:
use ; let mut buf = ; write_int128; assert_eq!;fn read_f32(buf: &[u8]) -> f32Reads a IEEE754 single-precision (4 bytes) floating point number.
Panics
Panics when
buf.len() < 4.Examples
Write and read
f32numbers in little endian order:use ; let e = 2.71828; let mut buf = ; write_f32; assert_eq!;fn read_f64(buf: &[u8]) -> f64Reads a IEEE754 double-precision (8 bytes) floating point number.
Panics
Panics when
buf.len() < 8.Examples
Write and read
f64numbers in little endian order:use ; let phi = 1.6180339887; let mut buf = ; write_f64; assert_eq!;fn write_i16(buf: &mut [u8], n: i16)Writes a signed 16 bit integer
ntobuf.Panics
Panics when
buf.len() < 2.Examples
Write and read
i16numbers in little endian order:use ; let mut buf = ; write_i16; assert_eq!;fn write_i24(buf: &mut [u8], n: i32)Writes a signed 24 bit integer
ntobuf, stored in i32.Panics
Panics when
buf.len() < 3.Examples
Write and read 24 bit
i32numbers in little endian order:use ; let mut buf = ; write_i24; assert_eq!;fn write_i32(buf: &mut [u8], n: i32)Writes a signed 32 bit integer
ntobuf.Panics
Panics when
buf.len() < 4.Examples
Write and read
i32numbers in little endian order:use ; let mut buf = ; write_i32; assert_eq!;fn write_i48(buf: &mut [u8], n: i64)Writes a signed 48 bit integer
ntobuf, stored in i64.Panics
Panics when
buf.len() < 6.Examples
Write and read 48 bit
i64numbers in little endian order:use ; let mut buf = ; write_i48; assert_eq!;fn write_i64(buf: &mut [u8], n: i64)Writes a signed 64 bit integer
ntobuf.Panics
Panics when
buf.len() < 8.Examples
Write and read
i64numbers in little endian order:use ; let mut buf = ; write_i64; assert_eq!;fn write_i128(buf: &mut [u8], n: i128)Writes a signed 128 bit integer
ntobuf.Panics
Panics when
buf.len() < 16.Examples
Write and read n-byte
i128numbers in little endian order:use ; let mut buf = ; write_i128; assert_eq!;fn write_int(buf: &mut [u8], n: i64, nbytes: usize)Writes a signed integer
ntobufusing onlynbytes.Panics
If
nis not representable innbytes, or ifnbytesis> 8, then this method panics.Examples
Write and read an n-byte number in little endian order:
use ; let mut buf = ; write_int; assert_eq!;fn write_int128(buf: &mut [u8], n: i128, nbytes: usize)Writes a signed integer
ntobufusing onlynbytes.Panics
If
nis not representable innbytes, or ifnbytesis> 16, then this method panics.Examples
Write and read n-length signed numbers in little endian order:
use ; let mut buf = ; write_int128; assert_eq!;fn write_f32(buf: &mut [u8], n: f32)Writes a IEEE754 single-precision (4 bytes) floating point number.
Panics
Panics when
buf.len() < 4.Examples
Write and read
f32numbers in little endian order:use ; let e = 2.71828; let mut buf = ; write_f32; assert_eq!;fn write_f64(buf: &mut [u8], n: f64)Writes a IEEE754 double-precision (8 bytes) floating point number.
Panics
Panics when
buf.len() < 8.Examples
Write and read
f64numbers in little endian order:use ; let phi = 1.6180339887; let mut buf = ; write_f64; assert_eq!;fn read_i16_into(src: &[u8], dst: &mut [i16])Reads signed 16 bit integers from
srctodst.Panics
Panics when
buf.len() != 2*dst.len().Examples
Write and read
i16numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_i16_into; let mut numbers_got = ; read_i16_into; assert_eq!;fn read_i32_into(src: &[u8], dst: &mut [i32])Reads signed 32 bit integers from
srcintodst.Panics
Panics when
src.len() != 4*dst.len().Examples
Write and read
i32numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_i32_into; let mut numbers_got = ; read_i32_into; assert_eq!;fn read_i64_into(src: &[u8], dst: &mut [i64])Reads signed 64 bit integers from
srcintodst.Panics
Panics when
src.len() != 8*dst.len().Examples
Write and read
i64numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_i64_into; let mut numbers_got = ; read_i64_into; assert_eq!;fn read_i128_into(src: &[u8], dst: &mut [i128])Reads signed 128 bit integers from
srcintodst.Panics
Panics when
src.len() != 16*dst.len().Examples
Write and read
i128numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_i128_into; let mut numbers_got = ; read_i128_into; assert_eq!;fn read_f32_into(src: &[u8], dst: &mut [f32])Reads IEEE754 single-precision (4 bytes) floating point numbers from
srcintodst.Panics
Panics when
src.len() != 4*dst.len().Examples
Write and read
f32numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_f32_into; let mut numbers_got = ; read_f32_into; assert_eq!;fn read_f32_into_unchecked(src: &[u8], dst: &mut [f32])DEPRECATED.
This method is deprecated. Use
read_f32_intoinstead. Reads IEEE754 single-precision (4 bytes) floating point numbers fromsrcintodst.Panics
Panics when
src.len() != 4*dst.len().Examples
Write and read
f32numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_f32_into; let mut numbers_got = ; read_f32_into_unchecked; assert_eq!;fn read_f64_into(src: &[u8], dst: &mut [f64])Reads IEEE754 single-precision (4 bytes) floating point numbers from
srcintodst.Panics
Panics when
src.len() != 8*dst.len().Examples
Write and read
f64numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_f64_into; let mut numbers_got = ; read_f64_into; assert_eq!;fn read_f64_into_unchecked(src: &[u8], dst: &mut [f64])DEPRECATED.
This method is deprecated. Use
read_f64_intoinstead.Reads IEEE754 single-precision (4 bytes) floating point numbers from
srcintodst.Panics
Panics when
src.len() != 8*dst.len().Examples
Write and read
f64numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_f64_into; let mut numbers_got = ; read_f64_into_unchecked; assert_eq!;fn write_i8_into(src: &[i8], dst: &mut [u8])Writes signed 8 bit integers from
srcintodst.Note that since each
i8is a single byte, no byte order conversions are used. This method is included because it provides a safe, simple way for the caller to write from a&[i8]buffer. (Without this method, the caller would have to either useunsafecode or convert each byte tou8individually.)Panics
Panics when
buf.len() != src.len().Examples
Write and read
i8numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_i8_into; let mut numbers_got = ; bytes.as_ref.read_i8_into; assert_eq!;fn write_i16_into(src: &[i16], dst: &mut [u8])Writes signed 16 bit integers from
srcintodst.Panics
Panics when
buf.len() != 2*src.len().Examples
Write and read
i16numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_i16_into; let mut numbers_got = ; read_i16_into; assert_eq!;fn write_i32_into(src: &[i32], dst: &mut [u8])Writes signed 32 bit integers from
srcintodst.Panics
Panics when
dst.len() != 4*src.len().Examples
Write and read
i32numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_i32_into; let mut numbers_got = ; read_i32_into; assert_eq!;fn write_i64_into(src: &[i64], dst: &mut [u8])Writes signed 64 bit integers from
srcintodst.Panics
Panics when
dst.len() != 8*src.len().Examples
Write and read
i64numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_i64_into; let mut numbers_got = ; read_i64_into; assert_eq!;fn write_i128_into(src: &[i128], dst: &mut [u8])Writes signed 128 bit integers from
srcintodst.Panics
Panics when
dst.len() != 16*src.len().Examples
Write and read
i128numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_i128_into; let mut numbers_got = ; read_i128_into; assert_eq!;fn write_f32_into(src: &[f32], dst: &mut [u8])Writes IEEE754 single-precision (4 bytes) floating point numbers from
srcintodst.Panics
Panics when
src.len() != 4*dst.len().Examples
Write and read
f32numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_f32_into; let mut numbers_got = ; read_f32_into; assert_eq!;fn write_f64_into(src: &[f64], dst: &mut [u8])Writes IEEE754 double-precision (8 bytes) floating point numbers from
srcintodst.Panics
Panics when
src.len() != 8*dst.len().Examples
Write and read
f64numbers in little endian order:use ; let mut bytes = ; let numbers_given = ; write_f64_into; let mut numbers_got = ; read_f64_into; assert_eq!;fn from_slice_i16(src: &mut [i16])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 ; let mut numbers = ; from_slice_i16; assert_eq!;fn from_slice_i32(src: &mut [i32])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 ; let mut numbers = ; from_slice_i32; assert_eq!;fn from_slice_i64(src: &mut [i64])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 ; let mut numbers = ; from_slice_i64; assert_eq!;fn from_slice_i128(src: &mut [i128])Converts the given slice of signed 128 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 ; let mut numbers = ; from_slice_i128; assert_eq!;
Implementors
impl ByteOrder for LittleEndianimpl ByteOrder for BigEndian