From 3cb826fe8b1ae9f5f8e24db586a922b7fffc1d01 Mon Sep 17 00:00:00 2001 From: Chris Hennick Date: Wed, 7 Jun 2023 22:15:06 -0700 Subject: [PATCH] Make functions `const` where possible --- src/aes.rs | 2 +- src/compression.rs | 4 ++-- src/read.rs | 4 ++-- src/read/stream.rs | 4 ++-- src/types.rs | 32 ++++++++++++++++---------------- src/write.rs | 22 +++++++++++----------- src/zipcrypto.rs | 2 +- 7 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/aes.rs b/src/aes.rs index c42a05ca..55ea8bf3 100644 --- a/src/aes.rs +++ b/src/aes.rs @@ -45,7 +45,7 @@ pub struct AesReader { } impl AesReader { - pub fn new(reader: R, aes_mode: AesMode, compressed_size: u64) -> AesReader { + pub const fn new(reader: R, aes_mode: AesMode, compressed_size: u64) -> AesReader { let data_length = compressed_size - (PWD_VERIFY_LENGTH + AUTH_CODE_LENGTH + aes_mode.salt_length()) as u64; diff --git a/src/compression.rs b/src/compression.rs index 9abd34af..fddc54c2 100644 --- a/src/compression.rs +++ b/src/compression.rs @@ -100,7 +100,7 @@ impl CompressionMethod { since = "0.5.7", note = "use a constant to construct a compression method" )] - pub fn from_u16(val: u16) -> CompressionMethod { + pub const fn from_u16(val: u16) -> CompressionMethod { #[allow(deprecated)] match val { 0 => CompressionMethod::Stored, @@ -128,7 +128,7 @@ impl CompressionMethod { since = "0.5.7", note = "to match on other compression methods, use a constant" )] - pub fn to_u16(self) -> u16 { + pub const fn to_u16(self) -> u16 { #[allow(deprecated)] match self { CompressionMethod::Stored => 0, diff --git a/src/read.rs b/src/read.rs index 5c8d818d..2c7809ce 100644 --- a/src/read.rs +++ b/src/read.rs @@ -107,7 +107,7 @@ impl<'a> CryptoReader<'a> { } /// Returns `true` if the data is encrypted using AE2. - pub fn is_ae2_encrypted(&self) -> bool { + pub const fn is_ae2_encrypted(&self) -> bool { #[cfg(feature = "aes-crypto")] return matches!( self, @@ -692,7 +692,7 @@ impl ZipArchive { } } -fn unsupported_zip_error(detail: &'static str) -> ZipResult { +const fn unsupported_zip_error(detail: &'static str) -> ZipResult { Err(ZipError::UnsupportedArchive(detail)) } diff --git a/src/read/stream.rs b/src/read/stream.rs index 1075c423..97e7019a 100644 --- a/src/read/stream.rs +++ b/src/read/stream.rs @@ -15,7 +15,7 @@ pub struct ZipStreamReader(R); impl ZipStreamReader { /// Create a new ZipStreamReader - pub fn new(reader: R) -> Self { + pub const fn new(reader: R) -> Self { Self(reader) } } @@ -204,7 +204,7 @@ impl ZipStreamFileMetadata { } /// Get unix mode for the file - pub fn unix_mode(&self) -> Option { + pub const fn unix_mode(&self) -> Option { self.0.unix_mode() } } diff --git a/src/types.rs b/src/types.rs index d9ced849..78896903 100644 --- a/src/types.rs +++ b/src/types.rs @@ -63,7 +63,7 @@ pub enum System { } impl System { - pub fn from_u8(system: u8) -> System { + pub const fn from_u8(system: u8) -> System { use self::System::*; match system { @@ -161,7 +161,7 @@ impl Default for DateTime { impl DateTime { /// Converts an msdos (u16, u16) pair to a DateTime object - pub fn from_msdos(datepart: u16, timepart: u16) -> DateTime { + pub const fn from_msdos(datepart: u16, timepart: u16) -> DateTime { let seconds = (timepart & 0b0000000000011111) << 1; let minutes = (timepart & 0b0000011111100000) >> 5; let hours = (timepart & 0b1111100000000000) >> 11; @@ -239,12 +239,12 @@ impl DateTime { } /// Gets the time portion of this datetime in the msdos representation - pub fn timepart(&self) -> u16 { + pub const fn timepart(&self) -> u16 { ((self.second as u16) >> 1) | ((self.minute as u16) << 5) | ((self.hour as u16) << 11) } /// Gets the date portion of this datetime in the msdos representation - pub fn datepart(&self) -> u16 { + pub const fn datepart(&self) -> u16 { (self.day as u16) | ((self.month as u16) << 5) | ((self.year - 1980) << 9) } @@ -258,7 +258,7 @@ impl DateTime { } /// Get the year. There is no epoch, i.e. 2018 will be returned as 2018. - pub fn year(&self) -> u16 { + pub const fn year(&self) -> u16 { self.year } @@ -267,7 +267,7 @@ impl DateTime { /// # Warning /// /// When read from a zip file, this may not be a reasonable value - pub fn month(&self) -> u8 { + pub const fn month(&self) -> u8 { self.month } @@ -276,7 +276,7 @@ impl DateTime { /// # Warning /// /// When read from a zip file, this may not be a reasonable value - pub fn day(&self) -> u8 { + pub const fn day(&self) -> u8 { self.day } @@ -285,7 +285,7 @@ impl DateTime { /// # Warning /// /// When read from a zip file, this may not be a reasonable value - pub fn hour(&self) -> u8 { + pub const fn hour(&self) -> u8 { self.hour } @@ -294,7 +294,7 @@ impl DateTime { /// # Warning /// /// When read from a zip file, this may not be a reasonable value - pub fn minute(&self) -> u8 { + pub const fn minute(&self) -> u8 { self.minute } @@ -303,7 +303,7 @@ impl DateTime { /// # Warning /// /// When read from a zip file, this may not be a reasonable value - pub fn second(&self) -> u8 { + pub const fn second(&self) -> u8 { self.second } } @@ -338,7 +338,7 @@ pub const DEFAULT_VERSION: u8 = 46; pub struct AtomicU64(atomic::AtomicU64); impl AtomicU64 { - pub fn new(v: u64) -> Self { + pub const fn new(v: u64) -> Self { Self(atomic::AtomicU64::new(v)) } @@ -456,7 +456,7 @@ impl ZipFileData { } /// Get unix mode for the file - pub(crate) fn unix_mode(&self) -> Option { + pub(crate) const fn unix_mode(&self) -> Option { if self.external_attributes == 0 { return None; } @@ -480,13 +480,13 @@ impl ZipFileData { } } - pub fn zip64_extension(&self) -> bool { + pub const fn zip64_extension(&self) -> bool { self.uncompressed_size > 0xFFFFFFFF || self.compressed_size > 0xFFFFFFFF || self.header_start > 0xFFFFFFFF } - pub fn version_needed(&self) -> u16 { + pub const fn version_needed(&self) -> u16 { // higher versions matched first match (self.zip64_extension(), self.compression_method) { #[cfg(feature = "bzip2")] @@ -517,11 +517,11 @@ pub enum AesMode { #[cfg(feature = "aes-crypto")] impl AesMode { - pub fn salt_length(&self) -> usize { + pub const fn salt_length(&self) -> usize { self.key_length() / 2 } - pub fn key_length(&self) -> usize { + pub const fn key_length(&self) -> usize { match self { Self::Aes128 => 16, Self::Aes192 => 24, diff --git a/src/write.rs b/src/write.rs index 67200c9f..f3545cf6 100644 --- a/src/write.rs +++ b/src/write.rs @@ -236,7 +236,7 @@ impl FileOptions { /// is enabled, `CompressionMethod::Zlib` is the default. If all else fails, /// `CompressionMethod::Stored` becomes the default and files are written uncompressed. #[must_use] - pub fn compression_method(mut self, method: CompressionMethod) -> FileOptions { + pub const fn compression_method(mut self, method: CompressionMethod) -> FileOptions { self.compression_method = method; self } @@ -252,7 +252,7 @@ impl FileOptions { /// * `Zstd`: -7 - 22, with zero being mapped to default level. Default is 3 /// * others: only `None` is allowed #[must_use] - pub fn compression_level(mut self, level: Option) -> FileOptions { + pub const fn compression_level(mut self, level: Option) -> FileOptions { self.compression_level = level; self } @@ -262,7 +262,7 @@ impl FileOptions { /// The default is the current timestamp if the 'time' feature is enabled, and 1980-01-01 /// otherwise #[must_use] - pub fn last_modified_time(mut self, mod_time: DateTime) -> FileOptions { + pub const fn last_modified_time(mut self, mod_time: DateTime) -> FileOptions { self.last_modified_time = mod_time; self } @@ -277,7 +277,7 @@ impl FileOptions { /// higher file mode bits. So it cannot be used to denote an entry as a directory, /// symlink, or other special file type. #[must_use] - pub fn unix_permissions(mut self, mode: u32) -> FileOptions { + pub const fn unix_permissions(mut self, mode: u32) -> FileOptions { self.permissions = Some(mode & 0o777); self } @@ -288,7 +288,7 @@ impl FileOptions { /// aborted. If set to `true`, readers will require ZIP64 support and if the file does not /// exceed the limit, 20 B are wasted. The default is `false`. #[must_use] - pub fn large_file(mut self, large: bool) -> FileOptions { + pub const fn large_file(mut self, large: bool) -> FileOptions { self.large_file = large; self } @@ -346,7 +346,7 @@ impl FileOptions { /// Sets the alignment to the given number of bytes. #[must_use] - pub fn with_alignment(mut self, alignment: u16) -> FileOptions { + pub const fn with_alignment(mut self, alignment: u16) -> FileOptions { self.alignment = alignment; self } @@ -357,13 +357,13 @@ impl FileOptions { /// are larger than about 32 KiB. #[must_use] #[cfg(feature = "deflate-zopfli")] - pub fn with_zopfli_buffer(mut self, size: Option) -> FileOptions { + pub const fn with_zopfli_buffer(mut self, size: Option) -> FileOptions { self.zopfli_buffer_size = size; self } /// Returns the compression level currently set. - pub fn get_compression_level(&self) -> Option { + pub const fn get_compression_level(&self) -> Option { self.compression_level } } @@ -581,7 +581,7 @@ impl ZipWriter { } /// Returns true if a file is currently open for writing. - pub fn is_writing_file(&self) -> bool { + pub const fn is_writing_file(&self) -> bool { self.writing_to_file && !self.inner.is_closed() } @@ -610,7 +610,7 @@ impl ZipWriter { /// /// This returns the raw bytes of the comment. The comment /// is typically expected to be encoded in UTF-8 - pub fn get_raw_comment(&self) -> &Vec { + pub const fn get_raw_comment(&self) -> &Vec { &self.comment } @@ -1364,7 +1364,7 @@ impl GenericZipWriter { } } - fn is_closed(&self) -> bool { + const fn is_closed(&self) -> bool { matches!(*self, GenericZipWriter::Closed) } diff --git a/src/zipcrypto.rs b/src/zipcrypto.rs index 7826ac2d..43ef1048 100644 --- a/src/zipcrypto.rs +++ b/src/zipcrypto.rs @@ -36,7 +36,7 @@ impl Debug for ZipCryptoKeys { } impl ZipCryptoKeys { - fn new() -> ZipCryptoKeys { + const fn new() -> ZipCryptoKeys { ZipCryptoKeys { key_0: Wrapping(0x12345678), key_1: Wrapping(0x23456789),