ExpGolombDecoder

Struct ExpGolombDecoder 

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

An Exponential-Golomb parser.

Implementations§

Source§

impl<'a> ExpGolombDecoder<'a>

Source

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

Create a new ExpGolombDecoder.

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 data = [0b01000000];
let mut reader = ExpGolombDecoder::new(&data, 0).unwrap();
assert_eq!(reader.next_unsigned(), Some(1));

Start at the second bit:

// Same as above but `010` is shifted one place to the right
let data = [0b00100000];
let mut reader = ExpGolombDecoder::new(&data, 1).unwrap();
assert_eq!(reader.next_unsigned(), Some(1));
Source

pub fn next_bit(&mut self) -> Option<u8>

Read the next bit (i.e, as a flag). Returns None if the end of the bitstream is reached.

§Examples
use exp_golomb::ExpGolombDecoder;

let data = [0b01010101];
let mut reader = ExpGolombDecoder::new(&data, 4).unwrap();

assert_eq!(reader.next_bit(), Some(0));
assert_eq!(reader.next_bit(), Some(1));
assert_eq!(reader.next_bit(), Some(0));
assert_eq!(reader.next_bit(), Some(1));
assert_eq!(reader.next_bit(), None);
assert_eq!(reader.next_bit(), None);
Source

pub fn next_unsigned(&mut self) -> Option<u64>

Read the next Exp-Golomb value as an unsigned integer. Returns None if the end of the bitstream is reached before parsing is completed or if the coded value is exceeds the limits of a u64.

§Examples
// 010               - 1
// 00110             - 5
// 00000000111111111 - 510
// 00101             - 4
// 01                - missing 1 more bit
let data = [0b01000110, 0b00000000, 0b11111111, 0b10010101];

let mut reader = ExpGolombDecoder::new(&data, 0).unwrap();
assert_eq!(reader.next_unsigned(), Some(1));
assert_eq!(reader.next_unsigned(), Some(5));
assert_eq!(reader.next_unsigned(), Some(510));
assert_eq!(reader.next_unsigned(), Some(4));
assert_eq!(reader.next_unsigned(), None);
assert_eq!(reader.next_unsigned(), None);

The coded value is limited to 64 bits. Trying to parse larger values would return None.

let data = [
    0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
    0b00000000, 0b00000001, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
    0b11111111, 0b11111111, 0b11111111,
];
let mut reader = ExpGolombDecoder::new(&data, 7).unwrap();
assert_eq!(reader.next_unsigned(), Some(u64::MAX));

// Attempt to parse a 65-bit number
let mut reader = ExpGolombDecoder::new(&data, 6).unwrap();
assert_eq!(reader.next_unsigned(), None);
Source

pub fn next_signed(&mut self) -> Option<i64>

Read the next Exp-Golomb value, interpreting it as a signed integer. Returns None if the end of the bitstream is reached before parsing is completed or if the coded value is exceeds the limits of a i64.

§Examples
// Concatenated Wikipedia example:
// https://en.wikipedia.org/wiki/Exponential-Golomb_coding#Extension_to_negative_numbers
let data = [0b10100110, 0b01000010, 0b10011000, 0b11100010, 0b00000100, 0b10000000];

let mut reader = ExpGolombDecoder::new(&data, 0).unwrap();
assert_eq!(reader.next_signed(), Some(0));
assert_eq!(reader.next_signed(), Some(1));
assert_eq!(reader.next_signed(), Some(-1));
assert_eq!(reader.next_signed(), Some(2));
assert_eq!(reader.next_signed(), Some(-2));
assert_eq!(reader.next_signed(), Some(3));
assert_eq!(reader.next_signed(), Some(-3));
assert_eq!(reader.next_signed(), Some(4));
assert_eq!(reader.next_signed(), Some(-4));
assert_eq!(reader.next_signed(), None);
assert_eq!(reader.next_signed(), None);

The coded value is limited to 64 bits. Trying to parse larger values would return None.

let data = [
    0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
    0b00000000, 0b00000001, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111,
    0b11111111, 0b11111111, 0b11111111,
];
let mut reader = ExpGolombDecoder::new(&data, 7).unwrap();
assert_eq!(reader.next_signed(), Some(i64::MIN));

// Attempt to parse a 65-bit number
let mut reader = ExpGolombDecoder::new(&data, 6).unwrap();
assert_eq!(reader.next_signed(), None);
Source

pub fn skip_next(&mut self)

Skip the next Exp-Golomb encoded value. Any parsing error at the end of the bitstream is ignored.

§Examples
let data = [0b01001001, 0b00110000];
let mut reader = ExpGolombDecoder::new(&data, 0).unwrap();
reader.skip_next();
reader.skip_next();
reader.skip_next();
assert_eq!(reader.next_unsigned(), Some(2));
reader.skip_next();
assert_eq!(reader.next_unsigned(), None);
reader.skip_next();
assert_eq!(reader.next_unsigned(), None);

Auto Trait Implementations§

§

impl<'a> Freeze for ExpGolombDecoder<'a>

§

impl<'a> RefUnwindSafe for ExpGolombDecoder<'a>

§

impl<'a> Send for ExpGolombDecoder<'a>

§

impl<'a> Sync for ExpGolombDecoder<'a>

§

impl<'a> Unpin for ExpGolombDecoder<'a>

§

impl<'a> UnwindSafe for ExpGolombDecoder<'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.