ExpGolombEncoder

Struct ExpGolombEncoder 

Source
pub struct ExpGolombEncoder<'a> { /* private fields */ }
Expand description

An Exponential-Golomb writer.

Implementations§

Source§

impl<'a> ExpGolombEncoder<'a>

Source

pub fn new(buf: &'a mut [u8], start: u32) -> Option<ExpGolombEncoder<'a>>

Create a new ExpGolombEncoder.

start denotes the starting position in the first byte of buf and goes from 0 (first) to 7 (last). This function returns None if the buffer is empty or if start is not within [0, 7].

§Examples
let mut buf = [0u8; 1];
// Write starting at the second bit
let mut writer = ExpGolombEncoder::new(&mut buf, 1).unwrap();
writer.put_unsigned(2).unwrap();
writer.close();
assert_eq!(buf[0], 0b00110000);
Source

pub fn put_unsigned(&mut self, value: u64) -> Option<()>

Encode a u64 into the buffer. Returns None if the buffer is full.

§Examples
let mut buf = [0u8; 6];
let mut writer = ExpGolombEncoder::new(&mut buf, 0).unwrap();
for i in 0..=8 {
    writer.put_unsigned(i).unwrap();
}
writer.close();
 
assert_eq!(
    buf,
    [0b10100110, 0b01000010, 0b10011000, 0b11100010, 0b00000100, 0b10000000]
);

This function guards against out of bounds indexing by returning None:

let mut buf = [0u8; 1];
let mut writer = ExpGolombEncoder::new(&mut buf, 0).unwrap();
assert!(writer.put_unsigned(1).is_some());
assert!(writer.put_unsigned(1).is_some());
assert!(writer.put_unsigned(1).is_none());
Source

pub fn put_bit(&mut self, value: bool) -> Option<()>

Write a single bit to the buffer. Returns None if the buffer is full.

§Examples
let mut buf = [0u8; 1];
let mut writer = ExpGolombEncoder::new(&mut buf, 6).unwrap();
writer.put_bit(true).unwrap();
writer.put_bit(false).unwrap();
assert!(writer.put_bit(true).is_none());
assert!(writer.put_bit(true).is_none());
writer.close();
assert_eq!(buf[0], 0b00000010);
Source

pub fn close(self) -> (usize, u32)

Consumes the ExpGolombEncoder, returning the bit position one past the last written bit.

§Examples
let mut buf = [0u8; 1];
let mut writer = ExpGolombEncoder::new(&mut buf, 2).unwrap();
writer.put_unsigned(0).unwrap();
assert_eq!(writer.close(), (0, 3));

Auto Trait Implementations§

§

impl<'a> Freeze for ExpGolombEncoder<'a>

§

impl<'a> RefUnwindSafe for ExpGolombEncoder<'a>

§

impl<'a> Send for ExpGolombEncoder<'a>

§

impl<'a> Sync for ExpGolombEncoder<'a>

§

impl<'a> Unpin for ExpGolombEncoder<'a>

§

impl<'a> !UnwindSafe for ExpGolombEncoder<'a>

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.