Skip to content

Commit

Permalink
make clippy and cargofmt happy
Browse files Browse the repository at this point in the history
  • Loading branch information
a10y committed Aug 14, 2024
1 parent 4a45e91 commit 04ef0ed
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 43 deletions.
16 changes: 5 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"

[lints.rust]
warnings = "deny"
# missing_docs = "deny"
missing_docs = "deny"

[lints.clippy]
all = { level = "deny", priority = -1 }
Expand All @@ -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
4 changes: 2 additions & 2 deletions examples/round_trip.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
43 changes: 18 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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 }
}
Expand Down Expand Up @@ -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.
///
Expand All @@ -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)
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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.
//
Expand Down
9 changes: 4 additions & 5 deletions src/lossy_pht.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fmt::Debug;
use std::fmt::Formatter;
use std::u16;

use crate::CodeMeta;
use crate::Symbol;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/correctness.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 04ef0ed

Please sign in to comment.