Skip to content

Commit b1501bf

Browse files
authored
refactor!: make encode and decode traits more symmetric (#112)
1 parent b19b329 commit b1501bf

4 files changed

Lines changed: 58 additions & 39 deletions

File tree

fuzz/Cargo.lock

Lines changed: 41 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fuzz/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ cargo-fuzz = true
1111
[dependencies]
1212
libfuzzer-sys = "0.4"
1313
arithmetic-coding = { path = ".." }
14-
bitstream-io = "1.2.0"
14+
bitstream-io = "4.9.0"
1515
fenwick-model = { path = "../fenwick-model" }
1616

1717
# Prevent this from interfering with workspaces

fuzz/fuzz_targets/round_trip.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ where
1717
M: Model,
1818
{
1919
let mut bitwriter = BitWriter::endian(Vec::new(), BigEndian);
20-
let mut encoder = Encoder::<M>::new(model);
20+
let encoder = Encoder::new(model, &mut bitwriter);
2121

22-
encoder.encode_all(input, &mut bitwriter).expect("failed to encode data!");
22+
encoder.encode_all(input).expect("failed to encode data!");
23+
2324
bitwriter.byte_align().expect("failed to byte-align the stream");
2425

2526
bitwriter.into_writer()
@@ -30,7 +31,7 @@ where
3031
M: Model,
3132
{
3233
let bitreader = BitReader::endian(buffer, BigEndian);
33-
let mut decoder = Decoder::new(model, bitreader).unwrap();
34+
let mut decoder = Decoder::new(model, bitreader);
3435

3536
decoder.decode_all().map(Result::unwrap).collect()
3637
}

src/encoder.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ use crate::{
1616
/// An arithmetic decoder converts a stream of symbols into a stream of bits,
1717
/// using a predictive [`Model`].
1818
#[derive(Debug)]
19-
pub struct Encoder<'a, M, W>
19+
pub struct Encoder<M, W>
2020
where
2121
M: Model,
2222
W: BitWrite,
2323
{
2424
model: M,
25-
state: State<'a, M::B, W>,
25+
state: State<M::B, W>,
2626
}
2727

28-
impl<'a, M, W> Encoder<'a, M, W>
28+
impl<M, W> Encoder<M, W>
2929
where
3030
M: Model,
3131
W: BitWrite,
@@ -48,7 +48,7 @@ where
4848
///
4949
/// If these constraints cannot be satisfied this method will panic in debug
5050
/// builds
51-
pub fn new(model: M, bitwriter: &'a mut W) -> Self {
51+
pub fn new(model: M, bitwriter: W) -> Self {
5252
let frequency_bits = model.max_denominator().log2() + 1;
5353
let precision = M::B::BITS - frequency_bits;
5454
Self::with_precision(model, bitwriter, precision)
@@ -67,7 +67,7 @@ where
6767
///
6868
/// If these constraints cannot be satisfied this method will panic in debug
6969
/// builds
70-
pub fn with_precision(model: M, bitwriter: &'a mut W, precision: u32) -> Self {
70+
pub fn with_precision(model: M, bitwriter: W, precision: u32) -> Self {
7171
let state = State::new(precision, bitwriter);
7272
Self::with_state(state, model)
7373
}
@@ -76,7 +76,7 @@ where
7676
///
7777
/// This is useful for manually chaining a shared buffer through multiple
7878
/// encoders.
79-
pub fn with_state(state: State<'a, M::B, W>, model: M) -> Self {
79+
pub fn with_state(state: State<M::B, W>, model: M) -> Self {
8080
#[cfg(debug_assertions)]
8181
assert_precision_sufficient::<M>(model.max_denominator(), state.state.precision);
8282
Self { model, state }
@@ -143,15 +143,15 @@ where
143143
}
144144

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

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

162162
/// A convenience struct which stores the internal state of an [`Encoder`].
163163
#[derive(Debug)]
164-
pub struct State<'a, B, W>
164+
pub struct State<B, W>
165165
where
166166
B: BitStore,
167167
W: BitWrite,
168168
{
169169
#[allow(clippy::struct_field_names)]
170170
state: common::State<B>,
171171
pending: u32,
172-
output: &'a mut W,
172+
output: W,
173173
}
174174

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

0 commit comments

Comments
 (0)