WriteAllExt

Trait WriteAllExt 

Source
pub trait WriteAllExt: Write {
    // Provided methods
    fn write_bool(&mut self, boolean: bool) -> Result<()> { ... }
    fn write_num_be<N, const SIZE: usize>(&mut self, num: N) -> Result<()>
       where N: ToBytes<Bytes = [u8; SIZE]> { ... }
    fn write_num_le<N, const SIZE: usize>(&mut self, num: N) -> Result<()>
       where N: ToBytes<Bytes = [u8; SIZE]> { ... }
    fn write_num_ne<N, const SIZE: usize>(&mut self, num: N) -> Result<()>
       where N: ToBytes<Bytes = [u8; SIZE]> { ... }
}

Provided Methods§

Source

fn write_bool(&mut self, boolean: bool) -> Result<()>

Write a bool as one byte.

For further semantics please refer to Read::read_exact.

§Examples
use rw_exact_ext::WriteAllExt;
use std::io::Cursor;

let mut bytes = vec![0xFF];

Cursor::new(&mut bytes).write_bool(true).unwrap();
assert_eq!(bytes, [0x01]);

Cursor::new(&mut bytes).write_bool(false).unwrap();
assert_eq!(bytes, [0x00]);
Source

fn write_num_be<N, const SIZE: usize>(&mut self, num: N) -> Result<()>
where N: ToBytes<Bytes = [u8; SIZE]>,

Write a number to bytes in big endian.

For further semantics please refer to Write::write_all.

§Examples
use rw_exact_ext::WriteAllExt;
use std::io::Cursor;

let mut bytes = vec![0u8; 4];

let unsigned: u32 = 1337;
Cursor::new(&mut bytes).write_num_be(unsigned).unwrap();
assert_eq!(bytes, vec![0x00, 0x00, 0x05, 0x39]);

let signed: i32 = -1337;
Cursor::new(&mut bytes).write_num_be(signed).unwrap();
assert_eq!(bytes, vec![0xFF, 0xFF, 0xFA, 0xC7]);

let float: f32 = 133.7;
Cursor::new(&mut bytes).write_num_be(float).unwrap();
assert_eq!(bytes, vec![0x43, 0x05, 0xB3, 0x33]);
Source

fn write_num_le<N, const SIZE: usize>(&mut self, num: N) -> Result<()>
where N: ToBytes<Bytes = [u8; SIZE]>,

Write a number to bytes in little endian.

For further semantics please refer to Write::write_all.

§Examples
use rw_exact_ext::WriteAllExt;
use std::io::Cursor;

let mut bytes = vec![0u8; 4];

let unsigned: u32 = 1337;
Cursor::new(&mut bytes).write_num_le(unsigned).unwrap();
assert_eq!(bytes, vec![0x39, 0x05, 0x00, 0x00]);

let signed: i32 = -1337;
Cursor::new(&mut bytes).write_num_le(signed).unwrap();
assert_eq!(bytes, vec![0xC7, 0xFA, 0xFF, 0xFF]);

let float: f32 = 133.7;
Cursor::new(&mut bytes).write_num_le(float).unwrap();
assert_eq!(bytes, vec![0x33, 0xB3, 0x05, 0x43]);
Source

fn write_num_ne<N, const SIZE: usize>(&mut self, num: N) -> Result<()>
where N: ToBytes<Bytes = [u8; SIZE]>,

Write a number to bytes in native endianness.

For further semantics please refer to Write::write_all.

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§

Source§

impl<T> WriteAllExt for T
where T: Write,