BigEndianWriter

Struct BigEndianWriter 

Source
pub struct BigEndianWriter<W: Write>(/* private fields */);
Expand description

Writes bits in the big-endian format

BigEndianWriter writes bits starting from the most significant bit (MSB). Individual bits are packed into bytes with the first bit written going to the MSB position, and multi-byte values are written with the most significant byte first.

§Examples

use bitter::{BitWriter, BigEndianWriter};

let mut buffer = Vec::new();
{
    let mut writer = BigEndianWriter::new(&mut buffer);

    // Write some mixed data
    writer.write_bit(true).unwrap();           // 1 bit
    writer.write_u8(0xFF).unwrap();           // 8 bits  
    writer.write_u16(0x1234).unwrap();        // 16 bits
    writer.write_bits(4, 0xA).unwrap();       // 4 more bits
    writer.flush().unwrap();
}

// Data is written in big-endian bit order
println!("Written {} bytes", buffer.len());

§Bit Ordering

In big-endian format:

  • Individual bits are written from MSB to LSB within each byte
  • Multi-byte values have their most significant byte written first
  • This matches the bit ordering used by BigEndianReader

Implementations§

Source§

impl<W: Write> BigEndianWriter<W>

Source

pub fn new(writer: W) -> Self

Create a big endian writer from the given Write implementation.

§Examples
use bitter::{BitWriter, BigEndianWriter};

let mut buffer = Vec::new();
{
    let mut writer = BigEndianWriter::new(&mut buffer);
    writer.write_u32(0x12345678).unwrap();
    writer.flush().unwrap();
}

// Big endian: most significant byte first
assert_eq!(buffer, &[0x12, 0x34, 0x56, 0x78]);
Source

pub fn into_inner(self) -> Result<W, IntoInnerError<BigEndianWriter<W>>>

Unwraps this BigEndianWriter, returning the underlying writer.

The buffer is flushed before returning the writer. If an error occurs while flushing, both the error and the writer are returned.

§Errors

An IntoInnerError will be returned if an error occurs while flushing the buffer. This allows recovery of the original writer.

§Examples
use bitter::{BitWriter, BigEndianWriter};

let mut buffer = Vec::new();
let mut writer = BigEndianWriter::new(&mut buffer);
writer.write_u8(42).unwrap();

let buffer = writer.into_inner().unwrap();
assert_eq!(buffer, &[42]);
Source

pub fn get_ref(&self) -> &W

Gets a reference to the underlying writer.

§Examples
use bitter::{BitWriter, BigEndianWriter};

let mut buffer = Vec::new();
let writer = BigEndianWriter::new(&mut buffer);

// Get a reference to the underlying buffer
let buffer_ref = writer.get_ref();
Source

pub fn get_mut(&mut self) -> &mut W

Gets a mutable reference to the underlying writer.

It is inadvisable to directly write to the underlying writer.

§Examples
use bitter::{BitWriter, BigEndianWriter};

let mut buffer = Vec::new();
let mut writer = BigEndianWriter::new(&mut buffer);

// Get a mutable reference to the underlying buffer
let buffer_ref = writer.get_mut();

Trait Implementations§

Source§

impl<W: Write> BitWriter for BigEndianWriter<W>

Source§

fn write_bit(&mut self, bit: bool) -> Result<()>

Write a single bit Read more
Source§

fn write_u8(&mut self, value: u8) -> Result<()>

Write 8 bits as a byte Read more
Source§

fn write_i8(&mut self, value: i8) -> Result<()>

Write 8 bits as a signed byte Read more
Source§

fn write_u16(&mut self, value: u16) -> Result<()>

Write 16 bits as an unsigned short Read more
Source§

fn write_i16(&mut self, value: i16) -> Result<()>

Write 16 bits as a signed short Read more
Source§

fn write_u32(&mut self, value: u32) -> Result<()>

Write 32 bits as an unsigned int Read more
Source§

fn write_i32(&mut self, value: i32) -> Result<()>

Write 32 bits as a signed int Read more
Source§

fn write_f32(&mut self, value: f32) -> Result<()>

Write 32 bits as a floating point Read more
Source§

fn write_u64(&mut self, value: u64) -> Result<()>

Write 64 bits as an unsigned long Read more
Source§

fn write_i64(&mut self, value: i64) -> Result<()>

Write 64 bits as a signed long Read more
Source§

fn write_f64(&mut self, value: f64) -> Result<()>

Write 64 bits as a floating point Read more
Source§

fn write_bits(&mut self, bits: u32, value: u64) -> Result<()>

Write an arbitrary number of bits (up to and including 64) as unsigned value Read more
Source§

fn write_signed_bits(&mut self, bits: u32, value: i64) -> Result<()>

Write an arbitrary number of bits (up to and including 64) as signed value Read more
Source§

fn unaligned_bits(&self) -> u32

Returns the number of bits written in the current partial byte Read more
Source§

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

Write the provided buffer as bytes Read more
Source§

fn flush(&mut self) -> Result<()>

Flush any buffered bits, padding with zeros if necessary Read more
Source§

impl<W: Debug + Write> Debug for BigEndianWriter<W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<W: Write> Write for BigEndianWriter<W>

Source§

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

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

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

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<W> Freeze for BigEndianWriter<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for BigEndianWriter<W>
where W: RefUnwindSafe,

§

impl<W> Send for BigEndianWriter<W>
where W: Send,

§

impl<W> Sync for BigEndianWriter<W>
where W: Sync,

§

impl<W> Unpin for BigEndianWriter<W>
where W: Unpin,

§

impl<W> UnwindSafe for BigEndianWriter<W>
where W: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.