Skip to content

Commit

Permalink
docs: fix compression level range from 0-9 to 0-10
Browse files Browse the repository at this point in the history
Also store the min, max, and default levels as constants.
While calling `Compression::new`, assert that level is in range.
  • Loading branch information
ByteBaker authored and sk314e committed Sep 23, 2024
1 parent f6b4c60 commit 9f48867
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,41 +187,46 @@ fn _assert_send_sync() {
pub struct Compression(u32);

impl Compression {
const MAX: u32 = 10;
const MIN: u32 = 0;
const DEFAULT: u32 = 6;
/// Creates a new description of the compression level with an explicitly
/// specified integer.
///
/// The integer here is typically on a scale of 0-9 where 0 means "no
/// compression" and 9 means "take as long as you'd like".
pub const fn new(level: u32) -> Compression {
Compression(level)
/// The integer here is typically on a scale of 0-10 where 0 means "no
/// compression" and 10 means "take as long as you'd like".
pub fn new(level: u32) -> Self {
// Ensure early that the level is within the valid range
debug_assert!(level <= Self::MAX);
Self(level)
}

/// No compression is to be performed, this may actually inflate data
/// slightly when encoding.
pub const fn none() -> Compression {
Compression(0)
pub const fn none() -> Self {
Self(Self::MIN)
}

/// Optimize for the best speed of encoding.
pub const fn fast() -> Compression {
Compression(1)
pub const fn fast() -> Self {
Self(1)
}

/// Optimize for the size of data being encoded.
pub const fn best() -> Compression {
Compression(9)
pub const fn best() -> Self {
Self(Self::MAX)
}

/// Returns an integer representing the compression level, typically on a
/// scale of 0-9
/// scale of 0-10
pub fn level(&self) -> u32 {
self.0
}
}

impl Default for Compression {
fn default() -> Compression {
Compression(6)
fn default() -> Self {
Self(Self::DEFAULT)
}
}

Expand Down

0 comments on commit 9f48867

Please sign in to comment.