Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 41 additions & 23 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cargo-fuzz = true
[dependencies]
libfuzzer-sys = "0.4"
arithmetic-coding = { path = ".." }
bitstream-io = "1.2.0"
bitstream-io = "4.9.0"
fenwick-model = { path = "../fenwick-model" }

# Prevent this from interfering with workspaces
Expand Down
7 changes: 4 additions & 3 deletions fuzz/fuzz_targets/round_trip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ where
M: Model,
{
let mut bitwriter = BitWriter::endian(Vec::new(), BigEndian);
let mut encoder = Encoder::<M>::new(model);
let encoder = Encoder::new(model, &mut bitwriter);

encoder.encode_all(input, &mut bitwriter).expect("failed to encode data!");
encoder.encode_all(input).expect("failed to encode data!");

bitwriter.byte_align().expect("failed to byte-align the stream");

bitwriter.into_writer()
Expand All @@ -30,7 +31,7 @@ where
M: Model,
{
let bitreader = BitReader::endian(buffer, BigEndian);
let mut decoder = Decoder::new(model, bitreader).unwrap();
let mut decoder = Decoder::new(model, bitreader);

decoder.decode_all().map(Result::unwrap).collect()
}
24 changes: 12 additions & 12 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ use crate::{
/// An arithmetic decoder converts a stream of symbols into a stream of bits,
/// using a predictive [`Model`].
#[derive(Debug)]
pub struct Encoder<'a, M, W>
pub struct Encoder<M, W>
where
M: Model,
W: BitWrite,
{
model: M,
state: State<'a, M::B, W>,
state: State<M::B, W>,
}

impl<'a, M, W> Encoder<'a, M, W>
impl<M, W> Encoder<M, W>
where
M: Model,
W: BitWrite,
Expand All @@ -48,7 +48,7 @@ where
///
/// If these constraints cannot be satisfied this method will panic in debug
/// builds
pub fn new(model: M, bitwriter: &'a mut W) -> Self {
pub fn new(model: M, bitwriter: W) -> Self {
let frequency_bits = model.max_denominator().log2() + 1;
let precision = M::B::BITS - frequency_bits;
Self::with_precision(model, bitwriter, precision)
Expand All @@ -67,7 +67,7 @@ where
///
/// If these constraints cannot be satisfied this method will panic in debug
/// builds
pub fn with_precision(model: M, bitwriter: &'a mut W, precision: u32) -> Self {
pub fn with_precision(model: M, bitwriter: W, precision: u32) -> Self {
let state = State::new(precision, bitwriter);
Self::with_state(state, model)
}
Expand All @@ -76,7 +76,7 @@ where
///
/// This is useful for manually chaining a shared buffer through multiple
/// encoders.
pub fn with_state(state: State<'a, M::B, W>, model: M) -> Self {
pub fn with_state(state: State<M::B, W>, model: M) -> Self {
#[cfg(debug_assertions)]
assert_precision_sufficient::<M>(model.max_denominator(), state.state.precision);
Self { model, state }
Expand Down Expand Up @@ -143,15 +143,15 @@ where
}

/// Return the internal model and state of the encoder.
pub fn into_inner(self) -> (M, State<'a, M::B, W>) {
pub fn into_inner(self) -> (M, State<M::B, W>) {
(self.model, self.state)
}

/// Reuse the internal state of the Encoder with a new model.
///
/// Allows for chaining multiple sequences of symbols into a single stream
/// of bits
pub fn chain<X>(self, model: X) -> Encoder<'a, X, W>
pub fn chain<X>(self, model: X) -> Encoder<X, W>
where
X: Model<B = M::B>,
{
Expand All @@ -161,18 +161,18 @@ where

/// A convenience struct which stores the internal state of an [`Encoder`].
#[derive(Debug)]
pub struct State<'a, B, W>
pub struct State<B, W>
where
B: BitStore,
W: BitWrite,
{
#[allow(clippy::struct_field_names)]
state: common::State<B>,
pending: u32,
output: &'a mut W,
output: W,
}

impl<'a, B, W> State<'a, B, W>
impl<B, W> State<B, W>
where
B: BitStore,
W: BitWrite,
Expand All @@ -181,7 +181,7 @@ where
///
/// Normally this would be done automatically using the [`Encoder::new`]
/// method.
pub fn new(precision: u32, output: &'a mut W) -> Self {
pub fn new(precision: u32, output: W) -> Self {
let state = common::State::new(precision);
let pending = 0;

Expand Down