From 9f488675ba47bb4ac80794e294dde63d88302a22 Mon Sep 17 00:00:00 2001 From: ByteBaker <42913098+ByteBaker@users.noreply.github.com> Date: Mon, 23 Sep 2024 09:11:48 +0530 Subject: [PATCH] docs: fix compression level range from 0-9 to 0-10 Also store the min, max, and default levels as constants. While calling `Compression::new`, assert that level is in range. --- src/lib.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d286ba2e..0bc95738 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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) } }