From 04ef0edfaa3220ac3aff3c90b2d5950c4222d5a4 Mon Sep 17 00:00:00 2001 From: Andrew Duffy Date: Wed, 14 Aug 2024 16:40:16 -0400 Subject: [PATCH] make clippy and cargofmt happy --- Cargo.toml | 16 +++++----------- examples/round_trip.rs | 4 ++-- src/lib.rs | 43 ++++++++++++++++++------------------------ src/lossy_pht.rs | 9 ++++----- tests/correctness.rs | 2 ++ 5 files changed, 31 insertions(+), 43 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e3161e8..31f9e7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" [lints.rust] warnings = "deny" -# missing_docs = "deny" +missing_docs = "deny" [lints.clippy] all = { level = "deny", priority = -1 } @@ -35,13 +35,7 @@ test = false name = "compress" harness = false -# [profile.dev] -# lto = "off" - -# [profile.release] -# opt-level = 3 -# lto = "off" - -# [profile.bench] -# opt-level = 3 -# lto = "thin" +[[test]] +name = "correctness" +test = true +bench = false diff --git a/examples/round_trip.rs b/examples/round_trip.rs index 1924065..0f3fab7 100644 --- a/examples/round_trip.rs +++ b/examples/round_trip.rs @@ -1,6 +1,6 @@ -use core::str; +//! Simple example where we show round-tripping a string through the static symbol table. -/// Simple example of compression. +use core::str; fn main() { // Train on a sample. diff --git a/src/lib.rs b/src/lib.rs index f17e8a5..41b9d08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,10 +22,7 @@ macro_rules! assert_sizeof { }; } -use std::{ - fmt::{Debug, Formatter}, - u64, -}; +use std::fmt::{Debug, Formatter}; pub use builder::*; use lossy_pht::LossyPHT; @@ -71,7 +68,6 @@ impl Symbol { /// /// Each symbol has the capacity to hold up to 8 bytes of data, but the symbols /// can contain fewer bytes, padded with 0x00. - #[inline(never)] pub fn len(&self) -> usize { let numeric = unsafe { self.num }; // For little-endian platforms, this counts the number of *trailing* zeros @@ -88,7 +84,7 @@ impl Symbol { } #[inline] - pub fn as_u64(&self) -> u64 { + fn as_u64(&self) -> u64 { // SAFETY: the bytes can always be viewed as a u64 unsafe { self.num } } @@ -164,7 +160,7 @@ impl Debug for Symbol { /// /// Bits 12-15 store the length of the symbol (values ranging from 0-8). #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] -pub struct CodeMeta(u16); +struct CodeMeta(u16); /// Code used to indicate bytes that are not in the symbol table. /// @@ -179,21 +175,22 @@ pub const ESCAPE_CODE: u8 = 255; /// When truncated to u8 this is code 255, which is equivalent to [`ESCAPE_CODE`]. pub const MAX_CODE: u16 = 511; +#[allow(clippy::len_without_is_empty)] impl CodeMeta { - pub const EMPTY: Self = CodeMeta(MAX_CODE); + const EMPTY: Self = CodeMeta(MAX_CODE); - pub fn new(code: u8, escape: bool, len: u16) -> Self { + fn new(code: u8, escape: bool, len: u16) -> Self { let value = (len << 12) | ((escape as u16) << 8) | (code as u16); Self(value) } /// Create a new code representing an escape byte. - pub fn new_escaped(byte: u8) -> Self { + fn new_escaped(byte: u8) -> Self { Self::new(byte, true, 1) } /// Create a new code from a [`Symbol`]. - pub fn new_symbol(code: u8, symbol: Symbol) -> Self { + fn new_symbol(code: u8, symbol: Symbol) -> Self { assert_ne!(code, ESCAPE_CODE, "ESCAPE_CODE cannot be used for symbol"); Self::new(code, false, symbol.len() as u16) @@ -203,39 +200,35 @@ impl CodeMeta { /// /// # Panics /// Panic if the value is ≥ the defined `CODE_MAX`. - pub fn from_u16(code: u16) -> Self { + fn from_u16(code: u16) -> Self { assert!((code >> 12) <= 8, "len must be <= 8"); - assert!( - (code & 0b111_111_111) <= MAX_CODE, - "code value higher than MAX_CODE" - ); Self(code) } /// Returns true if the code is for an escape byte. #[inline] - pub fn is_escape(&self) -> bool { + fn is_escape(&self) -> bool { self.0 <= 255 } #[inline] - pub fn code(&self) -> u8 { + fn code(&self) -> u8 { self.0 as u8 } #[inline] - pub fn extended_code(&self) -> u16 { + fn extended_code(&self) -> u16 { self.0 & 0b111_111_111 } #[inline] - pub fn len(&self) -> u16 { + fn len(&self) -> u16 { self.0 >> 12 } #[inline] - pub fn as_u16(&self) -> u16 { + fn as_u16(&self) -> u16 { self.0 } } @@ -406,7 +399,7 @@ impl SymbolTable { out_ptr.write_unaligned(code.code()); } - return (code.len() as usize, 1); + (code.len() as usize, 1) } /// Use the symbol table to compress the plaintext into a sequence of codes and escapes. @@ -529,7 +522,7 @@ fn mask_prefix(word: u64, prefix_bytes: usize) -> u64 { let mask = if prefix_bytes == 0 { 0 } else { - u64::MAX >> 8 * (8 - prefix_bytes) + u64::MAX >> (8 * (8 - prefix_bytes)) }; word & mask @@ -544,11 +537,11 @@ fn advance_8byte_word(word: u64, bytes: usize) -> u64 { if bytes == 8 { 0 } else { - word >> 8 * bytes + word >> (8 * bytes) } } -pub fn advance_8byte_word_bits(word: u64, bits: usize) -> u64 { +fn advance_8byte_word_bits(word: u64, bits: usize) -> u64 { // shift the word off the right-end, because little endian means the first // char is stored in the LSB. // diff --git a/src/lossy_pht.rs b/src/lossy_pht.rs index e6097da..ba87e0e 100644 --- a/src/lossy_pht.rs +++ b/src/lossy_pht.rs @@ -1,6 +1,5 @@ use std::fmt::Debug; use std::fmt::Formatter; -use std::u16; use crate::CodeMeta; use crate::Symbol; @@ -68,7 +67,7 @@ impl PackedMeta { /// Always <= 64 #[inline] pub(crate) fn ignored_bits(&self) -> u16 { - (self.0 >> 9) as u16 + self.0 >> 9 } /// Get the code value. @@ -175,13 +174,13 @@ impl LossyPHT { let slot = self.hash(prefix_3bytes) as usize & (HASH_TABLE_SIZE - 1); let entry = &mut self.slots[slot]; - if !entry.is_unused() { - return false; + if entry.is_unused() { + false } else { entry.symbol = symbol; entry.code = CodeMeta::new_symbol(code, symbol); entry.ignored_bits = (64 - 8 * symbol.len()) as u16; - return true; + true } } diff --git a/tests/correctness.rs b/tests/correctness.rs index fabef5b..31bde28 100644 --- a/tests/correctness.rs +++ b/tests/correctness.rs @@ -1,3 +1,5 @@ +#![cfg(test)] + static PREAMBLE: &str = r#" When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the