Skip to content

Commit 7dd51d3

Browse files
committed
cargo fmt --all
1 parent 6ead171 commit 7dd51d3

File tree

7 files changed

+313
-306
lines changed

7 files changed

+313
-306
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "range-encoding"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
authors = ["David Teller <[email protected]>"]
55
description = "An implementation of range encoding (entropy coding), extracted from Opus."
66
readme = "README.md"

src/lib.rs

+20-22
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ pub mod opus {
44
mod imported_decode;
55
mod imported_encode;
66

7-
mod encode;
87
mod decode;
8+
mod encode;
99

10-
pub use self::encode::Writer;
1110
pub use self::decode::Reader;
11+
pub use self::encode::Writer;
1212
}
1313

1414
#[derive(Clone)]
@@ -21,10 +21,7 @@ pub struct Segment {
2121
}
2222
impl Segment {
2323
pub fn new(low: u32, next: u32) -> Segment {
24-
Segment {
25-
low,
26-
next,
27-
}
24+
Segment { low, next }
2825
}
2926
pub fn width(&self) -> u32 {
3027
self.next - self.low
@@ -53,8 +50,7 @@ impl CumulativeDistributionFrequency {
5350
start = next;
5451
}
5552
Self {
56-
segments: segments
57-
.into_boxed_slice(),
53+
segments: segments.into_boxed_slice(),
5854
width: start,
5955
}
6056
}
@@ -66,28 +62,30 @@ impl CumulativeDistributionFrequency {
6662

6763
/// Iterate through the widths of the symbols.
6864
pub fn widths<'a>(&'a self) -> impl Iterator<Item = u32> + 'a {
69-
self.segments.iter()
70-
.map(Segment::width)
65+
self.segments.iter().map(Segment::width)
7166
}
7267

7368
/// Find a value from its frequency.
7469
pub fn find(&self, probability: u32) -> Option<IndexedSegment> {
7570
if probability >= self.width {
76-
return None
71+
return None;
7772
}
78-
let index = self.segments.binary_search_by(|segment| {
79-
use std::cmp::Ordering;
80-
if segment.low > probability {
81-
return Ordering::Greater
82-
}
83-
if segment.next <= probability {
84-
return Ordering::Less
85-
}
86-
Ordering::Equal
87-
}).ok()?;
73+
let index = self
74+
.segments
75+
.binary_search_by(|segment| {
76+
use std::cmp::Ordering;
77+
if segment.low > probability {
78+
return Ordering::Greater;
79+
}
80+
if segment.next <= probability {
81+
return Ordering::Less;
82+
}
83+
Ordering::Equal
84+
})
85+
.ok()?;
8886
Some(IndexedSegment {
8987
index,
90-
segment: self.segments[index].clone()
88+
segment: self.segments[index].clone(),
9189
})
9290
}
9391

src/opus/decode.rs

+52-38
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
2-
use ::CumulativeDistributionFrequency;
1+
use CumulativeDistributionFrequency;
32

43
use opus::imported_decode;
54

65
use std;
76

8-
pub struct Reader<R> where R: std::io::Read {
7+
pub struct Reader<R>
8+
where
9+
R: std::io::Read,
10+
{
911
state: imported_decode::ec_dec<R>,
1012
}
1113

12-
impl<R> Reader<R> where R: std::io::Read {
13-
/*
14-
pub fn from_boxed_slice(mut source: Box<[u8]>) -> Self {
15-
let state = unsafe {
16-
let mut state : imported_decode::ec_dec = std::mem::uninitialized();
17-
imported_decode::ec_dec_init(&mut state, source.as_mut_ptr(), source.len() as u32);
18-
state
19-
};
20-
Reader {
21-
source,
22-
state
14+
impl<R> Reader<R>
15+
where
16+
R: std::io::Read,
17+
{
18+
/*
19+
pub fn from_boxed_slice(mut source: Box<[u8]>) -> Self {
20+
let state = unsafe {
21+
let mut state : imported_decode::ec_dec = std::mem::uninitialized();
22+
imported_decode::ec_dec_init(&mut state, source.as_mut_ptr(), source.len() as u32);
23+
state
24+
};
25+
Reader {
26+
source,
27+
state
28+
}
2329
}
24-
}
25-
*/
30+
*/
2631
pub fn new(input: R) -> Result<Self, std::io::Error> {
2732
let mut state = imported_decode::ec_dec {
2833
inp: input,
@@ -35,39 +40,48 @@ impl<R> Reader<R> where R: std::io::Read {
3540
val: 0,
3641
ext: 0,
3742
};
38-
unsafe { imported_decode::ec_dec_init(&mut state)?; }
39-
Ok(Reader {
40-
state
41-
})
43+
unsafe {
44+
imported_decode::ec_dec_init(&mut state)?;
45+
}
46+
Ok(Reader { state })
4247
}
4348

4449
/// Decode the next symbol in line.
45-
pub fn symbol(&mut self, icdf: &CumulativeDistributionFrequency) -> Result<u32, std::io::Error> {
50+
pub fn symbol(
51+
&mut self,
52+
icdf: &CumulativeDistributionFrequency,
53+
) -> Result<u32, std::io::Error> {
4654
let index = unsafe {
4755
let frequency = imported_decode::ec_decode(&mut self.state, icdf.width());
48-
let indexed= icdf.find(frequency)
49-
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid probability"))?;
50-
imported_decode::ec_dec_update(&mut self.state, indexed.segment.low, indexed.segment.next, icdf.width())?;
56+
let indexed = icdf.find(frequency).ok_or_else(|| {
57+
std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid probability")
58+
})?;
59+
imported_decode::ec_dec_update(
60+
&mut self.state,
61+
indexed.segment.low,
62+
indexed.segment.next,
63+
icdf.width(),
64+
)?;
5165
indexed.index
5266
};
5367
Ok(index as u32)
5468
}
5569

56-
/*
57-
// FIXME: I actually don't understand `bits()` well enough
58-
// to publish it. /// Encode a sequence of raw bits, without any frequency information.
59-
pub fn bits(&mut self, size: usize) -> Result<u16, std::io::Error> {
60-
let result = unsafe {
61-
let result = imported_decode::ec_dec_bits(&mut self.state,
62-
size as u32);
63-
self.check_status()?;
64-
result as u16
65-
};
66-
Ok(result)
67-
}
68-
*/
70+
/*
71+
// FIXME: I actually don't understand `bits()` well enough
72+
// to publish it. /// Encode a sequence of raw bits, without any frequency information.
73+
pub fn bits(&mut self, size: usize) -> Result<u16, std::io::Error> {
74+
let result = unsafe {
75+
let result = imported_decode::ec_dec_bits(&mut self.state,
76+
size as u32);
77+
self.check_status()?;
78+
result as u16
79+
};
80+
Ok(result)
81+
}
82+
*/
6983

7084
pub fn done(self) {
7185
// FIXME: Nothing to do?
7286
}
73-
}
87+
}

src/opus/encode.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ impl<W> Writer<W> where W: std::io::Write {
3030
/// Encode the next symbol in line.
3131
pub fn symbol(&mut self, index: usize, icdf: &CumulativeDistributionFrequency) -> Result<(), std::io::Error> {
3232
let width = icdf.width();
33-
let segment = icdf.at_index(index)
34-
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid symbol"))?;
33+
let segment = icdf.at_index(index).ok_or_else(|| {
34+
std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid symbol")
35+
})?;
3536
unsafe {
3637
imported_encode::ec_encode(&mut self.state, segment.low, segment.next, width)?;
3738
};

0 commit comments

Comments
 (0)