bitstream_io::write

Trait ByteWrite

Source
pub trait ByteWrite {
    // Required methods
    fn write<V>(&mut self, value: V) -> Result<()>
       where V: Primitive;
    fn write_as<F, V>(&mut self, value: V) -> Result<()>
       where F: Endianness,
             V: Primitive;
    fn write_bytes(&mut self, buf: &[u8]) -> Result<()>;
    fn writer_ref(&mut self) -> &mut dyn Write;

    // Provided methods
    fn build<T: ToByteStream>(&mut self, build: &T) -> Result<(), T::Error> { ... }
    fn build_with<'a, T: ToByteStreamWith<'a>>(
        &mut self,
        build: &T,
        context: &T::Context,
    ) -> Result<(), T::Error> { ... }
}
Expand description

A trait for anything that can write aligned values to an output stream

Required Methods§

Source

fn write<V>(&mut self, value: V) -> Result<()>
where V: Primitive,

Writes whole numeric value to stream

§Errors

Passes along any I/O error from the underlying stream.

§Examples
use std::io::Write;
use bitstream_io::{BigEndian, ByteWriter, ByteWrite};
let mut writer = ByteWriter::endian(Vec::new(), BigEndian);
writer.write(0b0000000011111111u16).unwrap();
assert_eq!(writer.into_writer(), [0b00000000, 0b11111111]);
use std::io::Write;
use bitstream_io::{LittleEndian, ByteWriter, ByteWrite};
let mut writer = ByteWriter::endian(Vec::new(), LittleEndian);
writer.write(0b0000000011111111u16).unwrap();
assert_eq!(writer.into_writer(), [0b11111111, 0b00000000]);
Source

fn write_as<F, V>(&mut self, value: V) -> Result<()>
where F: Endianness, V: Primitive,

Writes whole numeric value to stream in a potentially different endianness

§Errors

Passes along any I/O error from the underlying stream.

§Examples
use std::io::Write;
use bitstream_io::{BigEndian, ByteWriter, ByteWrite, LittleEndian};
let mut writer = ByteWriter::endian(Vec::new(), BigEndian);
writer.write_as::<LittleEndian, u16>(0b0000000011111111).unwrap();
assert_eq!(writer.into_writer(), [0b11111111, 0b00000000]);
use std::io::Write;
use bitstream_io::{BigEndian, ByteWriter, ByteWrite, LittleEndian};
let mut writer = ByteWriter::endian(Vec::new(), LittleEndian);
writer.write_as::<BigEndian, u16>(0b0000000011111111).unwrap();
assert_eq!(writer.into_writer(), [0b00000000, 0b11111111]);
Source

fn write_bytes(&mut self, buf: &[u8]) -> Result<()>

Writes the entirety of a byte buffer to the stream.

§Errors

Passes along any I/O error from the underlying stream.

Source

fn writer_ref(&mut self) -> &mut dyn Write

Returns mutable reference to underlying writer

Provided Methods§

Source

fn build<T: ToByteStream>(&mut self, build: &T) -> Result<(), T::Error>

Builds and writes complex type

Source

fn build_with<'a, T: ToByteStreamWith<'a>>( &mut self, build: &T, context: &T::Context, ) -> Result<(), T::Error>

Builds and writes complex type with context

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§