fix some warnings (unneeded parentheses, missing dyn keyword)
[nihav.git] / nihav-core / src / io / codebook.rs
index 9355deb846cdc987b06372c8219fc08eeb1c9c49..0ec88833abb081485acf1e3dd3dec05a603713b0 100644 (file)
@@ -16,7 +16,7 @@
 //!             ShortCodebookDesc { code: 0b01,   bits: 2 },
 //!             ShortCodebookDesc { code: 0b1,    bits: 1 });
 //! let mut cr = ShortCodebookDescReader::new(cb_desc);
-//! let cb = Codebook::new(&mut cr, CodebookMode::LSB).unwrap();
+//! let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
 //! ```
 //!
 //! Create a codebook using more flexible [`TableCodebookDescReader`] approach.
@@ -113,6 +113,7 @@ pub struct ShortCodebookDesc {
 ///
 /// [`ShortCodebookDescReader`]: ./struct.ShortCodebookDescReader.html
 /// [`TableCodebookDescReader`]: ./struct.TableCodebookDescReader.html
+#[allow(clippy::len_without_is_empty)]
 pub trait CodebookDescReader<S> {
     /// Returns the codeword length for the provided index.
     fn bits(&mut self, idx: usize) -> u8;
@@ -127,9 +128,9 @@ pub trait CodebookDescReader<S> {
 /// The codebook structure for code reading.
 #[allow(dead_code)]
 pub struct Codebook<S> {
-    table: Vec<u32>,
-    syms:  Vec<S>,
-    lut_bits: u8,
+    pub table: Vec<u32>,
+    pub syms:  Vec<S>,
+    pub lut_bits: u8,
 }
 
 /// Trait allowing bitreader to use codebook for decoding bit sequences.
@@ -140,7 +141,7 @@ pub trait CodebookReader<S> {
     fn read_cb(&mut self, cb: &Codebook<S>) -> CodebookResult<S>;
 }
 
-const TABLE_FILL_VALUE: u32 = 0x7F;
+pub const TABLE_FILL_VALUE: u32 = 0x7F;
 const MAX_LUT_BITS: u8 = 10;
 
 fn fill_lut_msb(table: &mut Vec<u32>, off: usize,
@@ -288,7 +289,7 @@ fn build_esc_lut(table: &mut Vec<u32>,
 impl<S: Copy> Codebook<S> {
 
     /// Constructs a new `Codebook` instance using provided codebook description and mode.
-    pub fn new(cb: &mut CodebookDescReader<S>, mode: CodebookMode) -> CodebookResult<Self> {
+    pub fn new(cb: &mut dyn CodebookDescReader<S>, mode: CodebookMode) -> CodebookResult<Self> {
         let mut maxbits = 0;
         let mut nnz = 0;
         let mut escape_list: EscapeCodes = HashMap::new();
@@ -370,10 +371,10 @@ impl<'a, S: Copy> CodebookReader<S> for BitReader<'a> {
             let bits = cb.table[lut_idx] & 0x7F;
             esc  = (cb.table[lut_idx] & 0x80) != 0;
             idx  = (cb.table[lut_idx] >> 8) as usize;
-            if (bits as isize) > self.left() {
+            let skip_bits = if esc { u32::from(lut_bits) } else { bits };
+            if (skip_bits as isize) > self.left() {
                 return Err(CodebookError::InvalidCode);
             }
-            let skip_bits = if esc { u32::from(lut_bits) } else { bits };
             self.skip(skip_bits as u32).unwrap();
             lut_bits = bits as u8;
         }