pub trait WriteBytesExt: Write {
Show 20 methods
// Provided methods
fn write_u8(&mut self, n: u8) -> Result<(), Self::Error> { ... }
fn write_i8(&mut self, n: i8) -> Result<(), Self::Error> { ... }
fn write_u16<T: ByteOrder>(&mut self, n: u16) -> Result<(), Self::Error> { ... }
fn write_i16<T: ByteOrder>(&mut self, n: i16) -> Result<(), Self::Error> { ... }
fn write_u24<T: ByteOrder>(&mut self, n: u32) -> Result<(), Self::Error> { ... }
fn write_i24<T: ByteOrder>(&mut self, n: i32) -> Result<(), Self::Error> { ... }
fn write_u32<T: ByteOrder>(&mut self, n: u32) -> Result<(), Self::Error> { ... }
fn write_i32<T: ByteOrder>(&mut self, n: i32) -> Result<(), Self::Error> { ... }
fn write_u48<T: ByteOrder>(&mut self, n: u64) -> Result<(), Self::Error> { ... }
fn write_i48<T: ByteOrder>(&mut self, n: i64) -> Result<(), Self::Error> { ... }
fn write_u64<T: ByteOrder>(&mut self, n: u64) -> Result<(), Self::Error> { ... }
fn write_i64<T: ByteOrder>(&mut self, n: i64) -> Result<(), Self::Error> { ... }
fn write_u128<T: ByteOrder>(&mut self, n: u128) -> Result<(), Self::Error> { ... }
fn write_i128<T: ByteOrder>(&mut self, n: i128) -> Result<(), Self::Error> { ... }
fn write_uint<T: ByteOrder>(
&mut self,
n: u64,
nbytes: usize,
) -> Result<(), Self::Error> { ... }
fn write_int<T: ByteOrder>(
&mut self,
n: i64,
nbytes: usize,
) -> Result<(), Self::Error> { ... }
fn write_uint128<T: ByteOrder>(
&mut self,
n: u128,
nbytes: usize,
) -> Result<(), Self::Error> { ... }
fn write_int128<T: ByteOrder>(
&mut self,
n: i128,
nbytes: usize,
) -> Result<(), Self::Error> { ... }
fn write_f32<T: ByteOrder>(&mut self, n: f32) -> Result<(), Self::Error> { ... }
fn write_f64<T: ByteOrder>(&mut self, n: f64) -> Result<(), Self::Error> { ... }
}Expand description
Extends Write with methods for writing numbers. (For embedded_io.)
Most of the methods defined here have an unconstrained type parameter that must be explicitly
instantiated. Typically, it is instantiated with either the BigEndian or LittleEndian
types defined in this crate.
§Examples
Write unsigned 16 bit big-endian integers to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 4];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_u16::<BigEndian>(517).unwrap();
wtr.write_u16::<BigEndian>(768).unwrap();
assert_eq!(buff, [2, 5, 3, 0]);Provided Methods§
Sourcefn write_u8(&mut self, n: u8) -> Result<(), Self::Error>
fn write_u8(&mut self, n: u8) -> Result<(), Self::Error>
Writes an unsigned 8 bit integer to the underlying writer.
Note that since this writes a single byte, no byte order conversions are used. It is included for completeness.
§Errors
This method returns the same errors as Write::write_all.
§Examples
Write unsigned 8 bit integers to a Write:
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 2];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_u8(2).unwrap();
wtr.write_u8(5).unwrap();
assert_eq!(buff, [0x02, 0x05]);Sourcefn write_i8(&mut self, n: i8) -> Result<(), Self::Error>
fn write_i8(&mut self, n: i8) -> Result<(), Self::Error>
Writes a signed 8 bit integer to the underlying writer.
Note that since this writes a single byte, no byte order conversions are used. It is included for completeness.
§Errors
This method returns the same errors as Write::write_all.
§Examples
Write signed 8 bit integers to a Write:
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 2];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_i8(2).unwrap();
wtr.write_i8(-5).unwrap();
assert_eq!(buff, [0x02, 0xFB]);Sourcefn write_u16<T: ByteOrder>(&mut self, n: u16) -> Result<(), Self::Error>
fn write_u16<T: ByteOrder>(&mut self, n: u16) -> Result<(), Self::Error>
Writes an unsigned 16 bit integer to the underlying writer.
§Errors
This method returns the same errors as Write::write_all.
§Examples
Write unsigned 16 bit big-endian integers to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 4];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_u16::<BigEndian>(517).unwrap();
wtr.write_u16::<BigEndian>(768).unwrap();
assert_eq!(buff, [0x02, 0x05, 0x03, 0x00]);Sourcefn write_i16<T: ByteOrder>(&mut self, n: i16) -> Result<(), Self::Error>
fn write_i16<T: ByteOrder>(&mut self, n: i16) -> Result<(), Self::Error>
Writes a signed 16 bit integer to the underlying writer.
§Errors
This method returns the same errors as Write::write_all.
§Examples
Write signed 16 bit big-endian integers to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 4];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_i16::<BigEndian>(193).unwrap();
wtr.write_i16::<BigEndian>(-132).unwrap();
assert_eq!(buff, [0x00, 0xC1, 0xFF, 0x7C]);Sourcefn write_u24<T: ByteOrder>(&mut self, n: u32) -> Result<(), Self::Error>
fn write_u24<T: ByteOrder>(&mut self, n: u32) -> Result<(), Self::Error>
Writes an unsigned 24 bit integer to the underlying writer.
§Errors
This method returns the same errors as Write::write_all.
§Examples
Write unsigned 24 bit big-endian integers to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 6];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_u24::<BigEndian>(267).unwrap();
wtr.write_u24::<BigEndian>(120111).unwrap();
assert_eq!(buff, [0x00, 0x01, 0x0B, 0x01, 0xD5, 0x2F]);Sourcefn write_i24<T: ByteOrder>(&mut self, n: i32) -> Result<(), Self::Error>
fn write_i24<T: ByteOrder>(&mut self, n: i32) -> Result<(), Self::Error>
Writes a signed 24 bit integer to the underlying writer.
§Errors
This method returns the same errors as Write::write_all.
§Examples
Write signed 24 bit big-endian integers to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 6];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_i24::<BigEndian>(-34253).unwrap();
wtr.write_i24::<BigEndian>(120111).unwrap();
assert_eq!(buff, [0xFF, 0x7A, 0x33, 0x01, 0xD5, 0x2F]);Sourcefn write_u32<T: ByteOrder>(&mut self, n: u32) -> Result<(), Self::Error>
fn write_u32<T: ByteOrder>(&mut self, n: u32) -> Result<(), Self::Error>
Writes an unsigned 32 bit integer to the underlying writer.
§Errors
This method returns the same errors as Write::write_all.
§Examples
Write unsigned 32 bit big-endian integers to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 8];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_u32::<BigEndian>(267).unwrap();
wtr.write_u32::<BigEndian>(1205419366).unwrap();
assert_eq!(buff, [0x00, 0x00, 0x01, 0x0B, 0x47, 0xD9, 0x3D, 0x66]);Sourcefn write_i32<T: ByteOrder>(&mut self, n: i32) -> Result<(), Self::Error>
fn write_i32<T: ByteOrder>(&mut self, n: i32) -> Result<(), Self::Error>
Writes a signed 32 bit integer to the underlying writer.
§Errors
This method returns the same errors as Write::write_all.
§Examples
Write signed 32 bit big-endian integers to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 8];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_i32::<BigEndian>(-34253).unwrap();
wtr.write_i32::<BigEndian>(1205419366).unwrap();
assert_eq!(buff, [0xFF, 0xFF, 0x7A, 0x33, 0x47, 0xD9, 0x3D, 0x66]);Sourcefn write_u48<T: ByteOrder>(&mut self, n: u64) -> Result<(), Self::Error>
fn write_u48<T: ByteOrder>(&mut self, n: u64) -> Result<(), Self::Error>
Writes an unsigned 48 bit integer to the underlying writer.
§Errors
This method returns the same errors as Write::write_all.
§Examples
Write unsigned 48 bit big-endian integers to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 12];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_u48::<BigEndian>(52360336390828).unwrap();
wtr.write_u48::<BigEndian>(541).unwrap();
assert_eq!(buff, [0x2F, 0x9F, 0x17, 0x40, 0x3A, 0xAC, 0x00, 0x00, 0x00, 0x00, 0x02, 0x1D]);Sourcefn write_i48<T: ByteOrder>(&mut self, n: i64) -> Result<(), Self::Error>
fn write_i48<T: ByteOrder>(&mut self, n: i64) -> Result<(), Self::Error>
Writes a signed 48 bit integer to the underlying writer.
§Errors
This method returns the same errors as Write::write_all.
§Examples
Write signed 48 bit big-endian integers to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 12];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_i48::<BigEndian>(-108363435763825).unwrap();
wtr.write_i48::<BigEndian>(77).unwrap();
assert_eq!(buff, [0x9D, 0x71, 0xAB, 0xE7, 0x97, 0x8F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4D]);Sourcefn write_u64<T: ByteOrder>(&mut self, n: u64) -> Result<(), Self::Error>
fn write_u64<T: ByteOrder>(&mut self, n: u64) -> Result<(), Self::Error>
Writes an unsigned 64 bit integer to the underlying writer.
§Errors
This method returns the same errors as Write::write_all.
§Examples
Write unsigned 64 bit big-endian integers to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 16];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_u64::<BigEndian>(918733457491587).unwrap();
wtr.write_u64::<BigEndian>(143).unwrap();
assert_eq!(buff, [
0x00, 0x03, 0x43, 0x95, 0x4D, 0x60, 0x86, 0x83,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8F,
]);Sourcefn write_i64<T: ByteOrder>(&mut self, n: i64) -> Result<(), Self::Error>
fn write_i64<T: ByteOrder>(&mut self, n: i64) -> Result<(), Self::Error>
Writes a signed 64 bit integer to the underlying writer.
§Errors
This method returns the same errors as Write::write_all.
§Examples
Write signed 64 bit big-endian integers to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 16];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_i64::<BigEndian>(i64::MIN).unwrap();
wtr.write_i64::<BigEndian>(i64::MAX).unwrap();
assert_eq!(buff, [
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
]);Sourcefn write_u128<T: ByteOrder>(&mut self, n: u128) -> Result<(), Self::Error>
fn write_u128<T: ByteOrder>(&mut self, n: u128) -> Result<(), Self::Error>
Writes an unsigned 128 bit integer to the underlying writer.
Sourcefn write_i128<T: ByteOrder>(&mut self, n: i128) -> Result<(), Self::Error>
fn write_i128<T: ByteOrder>(&mut self, n: i128) -> Result<(), Self::Error>
Writes a signed 128 bit integer to the underlying writer.
Sourcefn write_uint<T: ByteOrder>(
&mut self,
n: u64,
nbytes: usize,
) -> Result<(), Self::Error>
fn write_uint<T: ByteOrder>( &mut self, n: u64, nbytes: usize, ) -> Result<(), Self::Error>
Writes an unsigned n-bytes integer to the underlying writer.
§Errors
This method returns the same errors as Write::write_all.
§Panics
If the given integer is not representable in the given number of bytes, this method panics.
If nbytes > 8, this method panics.
§Examples
Write unsigned 40 bit big-endian integers to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 10];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_uint::<BigEndian>(312550384361, 5).unwrap();
wtr.write_uint::<BigEndian>(43, 5).unwrap();
assert_eq!(buff, [0x48, 0xC5, 0x74, 0x62, 0xE9, 0x00, 0x00, 0x00, 0x00, 0x2B]);Sourcefn write_int<T: ByteOrder>(
&mut self,
n: i64,
nbytes: usize,
) -> Result<(), Self::Error>
fn write_int<T: ByteOrder>( &mut self, n: i64, nbytes: usize, ) -> Result<(), Self::Error>
Writes a signed n-bytes integer to the underlying writer.
§Errors
This method returns the same errors as Write::write_all.
§Panics
If the given integer is not representable in the given number of bytes, this method panics.
If nbytes > 8, this method panics.
§Examples
Write signed 56 bit big-endian integers to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 14];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_int::<BigEndian>(-3548172039376767, 7).unwrap();
wtr.write_int::<BigEndian>(43, 7).unwrap();
assert_eq!(buff, [
0xF3, 0x64, 0xF4, 0xD1, 0xFd, 0xB0, 0x81,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2B,
]);Sourcefn write_uint128<T: ByteOrder>(
&mut self,
n: u128,
nbytes: usize,
) -> Result<(), Self::Error>
fn write_uint128<T: ByteOrder>( &mut self, n: u128, nbytes: usize, ) -> Result<(), Self::Error>
Writes an unsigned n-bytes integer to the underlying writer.
If the given integer is not representable in the given number of bytes, this method panics.
If nbytes > 16, this method panics.
Sourcefn write_int128<T: ByteOrder>(
&mut self,
n: i128,
nbytes: usize,
) -> Result<(), Self::Error>
fn write_int128<T: ByteOrder>( &mut self, n: i128, nbytes: usize, ) -> Result<(), Self::Error>
Writes a signed n-bytes integer to the underlying writer.
If the given integer is not representable in the given number of bytes, this method panics.
If nbytes > 16, this method panics.
Sourcefn write_f32<T: ByteOrder>(&mut self, n: f32) -> Result<(), Self::Error>
fn write_f32<T: ByteOrder>(&mut self, n: f32) -> Result<(), Self::Error>
Writes a IEEE754 single-precision (4 bytes) floating point number to the underlying writer.
§Errors
This method returns the same errors as Write::write_all.
§Examples
Write a big-endian single-precision floating point number to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 4];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_f32::<BigEndian>(core::f32::consts::PI).unwrap();
assert_eq!(buff, [0x40, 0x49, 0x0F, 0xDB]);Sourcefn write_f64<T: ByteOrder>(&mut self, n: f64) -> Result<(), Self::Error>
fn write_f64<T: ByteOrder>(&mut self, n: f64) -> Result<(), Self::Error>
Writes a IEEE754 double-precision (8 bytes) floating point number to the underlying writer.
§Errors
This method returns the same errors as Write::write_all.
§Examples
Write a big-endian double-precision floating point number to a Write:
use byteorder::BigEndian;
use embedded_io::Write;
use embedded_io_byteorder::WriteBytesExt;
let mut buff = [0; 8];
let mut wtr: &mut [u8] = &mut buff;
wtr.write_f64::<BigEndian>(core::f64::consts::PI).unwrap();
assert_eq!(buff, [0x40, 0x09, 0x21, 0xFB, 0x54, 0x44, 0x2D, 0x18]);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
impl<W: Write + ?Sized> WriteBytesExt for W
All types that implement Write get methods defined in WriteBytesExt for free.