diff --git a/src/atomicmin.rs b/src/atomicmin.rs index 69a379f2..2df7028c 100644 --- a/src/atomicmin.rs +++ b/src/atomicmin.rs @@ -23,7 +23,7 @@ impl AtomicMin { } /// Unset value is usize_max - pub fn as_atomic_usize(&self) -> &AtomicUsize { + pub const fn as_atomic_usize(&self) -> &AtomicUsize { &self.val } diff --git a/src/colors.rs b/src/colors.rs index d81b1346..238bf262 100644 --- a/src/colors.rs +++ b/src/colors.rs @@ -45,7 +45,7 @@ impl Display for ColorType { impl ColorType { /// Get the code used by the PNG specification to denote this color type #[inline] - pub fn png_header_code(&self) -> u8 { + pub const fn png_header_code(&self) -> u8 { match self { ColorType::Grayscale { .. } => 0, ColorType::RGB { .. } => 2, @@ -56,7 +56,7 @@ impl ColorType { } #[inline] - pub(crate) fn channels_per_pixel(&self) -> u8 { + pub(crate) const fn channels_per_pixel(&self) -> u8 { match self { ColorType::Grayscale { .. } | ColorType::Indexed { .. } => 1, ColorType::GrayscaleAlpha => 2, @@ -66,12 +66,12 @@ impl ColorType { } #[inline] - pub(crate) fn is_rgb(&self) -> bool { + pub(crate) const fn is_rgb(&self) -> bool { matches!(self, ColorType::RGB { .. } | ColorType::RGBA) } #[inline] - pub(crate) fn is_gray(&self) -> bool { + pub(crate) const fn is_gray(&self) -> bool { matches!( self, ColorType::Grayscale { .. } | ColorType::GrayscaleAlpha @@ -79,12 +79,12 @@ impl ColorType { } #[inline] - pub(crate) fn has_alpha(&self) -> bool { + pub(crate) const fn has_alpha(&self) -> bool { matches!(self, ColorType::GrayscaleAlpha | ColorType::RGBA) } #[inline] - pub(crate) fn has_trns(&self) -> bool { + pub(crate) const fn has_trns(&self) -> bool { match self { ColorType::Grayscale { transparent_shade } => transparent_shade.is_some(), ColorType::RGB { transparent_color } => transparent_color.is_some(), diff --git a/src/headers.rs b/src/headers.rs index 06671437..779b185a 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -27,7 +27,7 @@ impl IhdrData { /// Bits per pixel #[must_use] #[inline] - pub fn bpp(&self) -> usize { + pub const fn bpp(&self) -> usize { self.bit_depth as usize * self.color_type.channels_per_pixel() as usize } @@ -38,7 +38,7 @@ impl IhdrData { let h = self.height as usize; let bpp = self.bpp(); - fn bitmap_size(bpp: usize, w: usize, h: usize) -> usize { + const fn bitmap_size(bpp: usize, w: usize, h: usize) -> usize { ((w * bpp + 7) / 8) * h } diff --git a/src/lib.rs b/src/lib.rs index 28e5a4fb..c941fd66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -249,7 +249,7 @@ impl Options { self } - fn apply_preset_2(self) -> Self { + const fn apply_preset_2(self) -> Self { self } @@ -904,7 +904,7 @@ fn postprocess_chunks(png: &mut PngData, opts: &Options, orig_ihdr: &IhdrData } /// Check if an image was already optimized prior to oxipng's operations -fn is_fully_optimized(original_size: usize, optimized_size: usize, opts: &Options) -> bool { +const fn is_fully_optimized(original_size: usize, optimized_size: usize, opts: &Options) -> bool { original_size <= optimized_size && !opts.force } diff --git a/src/png/mod.rs b/src/png/mod.rs index 49f7a89d..56c8b83f 100644 --- a/src/png/mod.rs +++ b/src/png/mod.rs @@ -241,13 +241,13 @@ impl PngImage { /// Return the number of channels in the image, based on color type #[inline] - pub fn channels_per_pixel(&self) -> usize { + pub const fn channels_per_pixel(&self) -> usize { self.ihdr.color_type.channels_per_pixel() as usize } /// Return the number of bytes per channel in the image #[inline] - pub fn bytes_per_channel(&self) -> usize { + pub const fn bytes_per_channel(&self) -> usize { match self.ihdr.bit_depth { BitDepth::Sixteen => 2, // Depths lower than 8 will round up to 1 byte @@ -472,7 +472,7 @@ fn write_png_block(key: &[u8], chunk: &[u8], output: &mut Vec) { } // Integer approximation for i * log2(i) - much faster than float calculations -fn ilog2i(i: u32) -> u32 { +const fn ilog2i(i: u32) -> u32 { let log = 32 - i.leading_zeros() - 1; i * log + ((i - (1 << log)) << 1) }