Skip to content

Commit

Permalink
Fix dead code warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbb committed Jul 14, 2024
1 parent 22053b0 commit 5fdac7a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
4 changes: 0 additions & 4 deletions src/async_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ pub async fn read_to_string(path: impl AsRef<Path>) -> Result<String, Error> {
///
/// The API mirrors the function in [`tokio::fs::write`] but is implemented via [`crate::fs::write`].
pub async fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<(), Error> {
// TODO: Should probably use scoped tasks, if they become available
// https://github.com/tokio-rs/tokio/issues/3162
let path = path.as_ref().to_owned();
let contents = contents.as_ref().to_owned();
tokio::task::spawn_blocking(move || crate::fs::write(path, contents)).await?
Expand All @@ -51,8 +49,6 @@ pub async fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result
///
/// The function is similar to [`write()`] but will append the content to the end of the file instead of truncating.
pub async fn append(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<(), Error> {
// TODO: Should probably use scoped tasks, if they become available
// https://github.com/tokio-rs/tokio/issues/3162
let path = path.as_ref().to_owned();
let contents = contents.as_ref().to_owned();
tokio::task::spawn_blocking(move || crate::fs::append(path, contents)).await?
Expand Down
23 changes: 7 additions & 16 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl From<Compression> for bzip2::Compression {
Compression::Fastest => bzip2::Compression::fast(),
Compression::Default => bzip2::Compression::default(),
Compression::Best => bzip2::Compression::best(),
Compression::Numeric(n) => bzip2::Compression::new(clamp(u32::from(n), 0, 9)),
Compression::Numeric(n) => bzip2::Compression::new(Ord::clamp(u32::from(n), 0, 9)),
}
}
}
Expand All @@ -308,39 +308,30 @@ impl From<Compression> for flate2::Compression {
Compression::Fastest => flate2::Compression::fast(),
Compression::Default => flate2::Compression::default(),
Compression::Best => flate2::Compression::best(),
Compression::Numeric(n) => flate2::Compression::new(clamp(u32::from(n), 0, 9)),
Compression::Numeric(n) => flate2::Compression::new(Ord::clamp(u32::from(n), 0, 9)),
}
}
}

/// Implementation detail to convert a [`Compression`] into a `u32` in the range `0-9` (inclusive).
///
/// [`Compression`]: ./enum.Compression.html
#[cfg(feature = "file-xz")]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
struct XzCompression(u32);

#[cfg(feature = "file-xz")]
impl From<Compression> for XzCompression {
fn from(compression: Compression) -> Self {
match compression {
Compression::Fastest => XzCompression(0),
Compression::Default => XzCompression(6),
Compression::Best => XzCompression(9),
Compression::Numeric(n) => XzCompression(clamp(u32::from(n), 0, 9)),
Compression::Numeric(n) => XzCompression(Ord::clamp(u32::from(n), 0, 9)),
}
}
}

// TODO consider using num for this
// https://docs.rs/num/0.1.40/num/fn.clamp.html
fn clamp<T: PartialOrd>(input: T, min: T, max: T) -> T {
if input < min {
min
} else if input > max {
max
} else {
input
}
}

/// Builder to control how the writeable file will be opened.
#[derive(Debug)]
pub struct WriteBuilder {
Expand Down Expand Up @@ -434,7 +425,7 @@ impl WriteBuilder {
#[cfg(feature = "file-xz")]
Xz => {
let level: XzCompression = self.compression_level.into();
let threads = clamp(self.threads, 1, u8::MAX);
let threads = Ord::clamp(self.threads, 1, u8::MAX);
if threads == 1 {
Ok(Box::new(XzEncoder::new(bufwrite, level.0)))
} else {
Expand Down

0 comments on commit 5fdac7a

Please sign in to comment.