Trait byteorder::ByteOrder
[−]
[src]
pub trait ByteOrder { 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_i16(buf: &[u8]) -> i16 { ... } 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_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) { ... } }
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
.
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
fn read_u16(buf: &[u8]) -> u16
Reads an unsigned 16 bit integer from buf
.
Panics when buf.len() < 2
.
fn read_u32(buf: &[u8]) -> u32
Reads an unsigned 32 bit integer from buf
.
Panics when buf.len() < 4
.
fn read_u64(buf: &[u8]) -> u64
Reads an unsigned 64 bit integer from buf
.
Panics when buf.len() < 8
.
fn read_uint(buf: &[u8], nbytes: usize) -> u64
Reads an unsigned n-bytes integer from buf
.
Panics when nbytes < 1
or nbytes > 8
or
buf.len() < nbytes
fn write_u16(buf: &mut [u8], n: u16)
Writes an unsigned 16 bit integer n
to buf
.
Panics when buf.len() < 2
.
fn write_u32(buf: &mut [u8], n: u32)
Writes an unsigned 32 bit integer n
to buf
.
Panics when buf.len() < 4
.
fn write_u64(buf: &mut [u8], n: u64)
Writes an unsigned 64 bit integer n
to buf
.
Panics when buf.len() < 8
.
fn write_uint(buf: &mut [u8], n: u64, nbytes: usize)
Writes an unsigned integer n
to buf
using only nbytes
.
If n
is not representable in nbytes
, or if nbytes
is > 8
, then
this method panics.
Provided Methods
fn read_i16(buf: &[u8]) -> i16
Reads a signed 16 bit integer from buf
.
Panics when buf.len() < 2
.
fn read_i32(buf: &[u8]) -> i32
Reads a signed 32 bit integer from buf
.
Panics when buf.len() < 4
.
fn read_i64(buf: &[u8]) -> i64
Reads a signed 64 bit integer from buf
.
Panics when buf.len() < 8
.
fn read_int(buf: &[u8], nbytes: usize) -> i64
Reads a signed n-bytes integer from buf
.
Panics when nbytes < 1
or nbytes > 8
or
buf.len() < nbytes
fn read_f32(buf: &[u8]) -> f32
Reads a IEEE754 single-precision (4 bytes) floating point number.
Panics when buf.len() < 4
.
fn read_f64(buf: &[u8]) -> f64
Reads a IEEE754 double-precision (8 bytes) floating point number.
Panics when buf.len() < 8
.
fn write_i16(buf: &mut [u8], n: i16)
Writes a signed 16 bit integer n
to buf
.
Panics when buf.len() < 2
.
fn write_i32(buf: &mut [u8], n: i32)
Writes a signed 32 bit integer n
to buf
.
Panics when buf.len() < 4
.
fn write_i64(buf: &mut [u8], n: i64)
Writes a signed 64 bit integer n
to buf
.
Panics when buf.len() < 8
.
fn write_int(buf: &mut [u8], n: i64, nbytes: usize)
Writes a signed integer n
to buf
using only nbytes
.
If n
is not representable in nbytes
, or if nbytes
is > 8
, then
this method panics.
fn write_f32(buf: &mut [u8], n: f32)
Writes a IEEE754 single-precision (4 bytes) floating point number.
Panics when buf.len() < 4
.
fn write_f64(buf: &mut [u8], n: f64)
Writes a IEEE754 double-precision (8 bytes) floating point number.
Panics when buf.len() < 8
.
Implementors
impl ByteOrder for BigEndian
impl ByteOrder for LittleEndian