Make functions const when possible

This commit is contained in:
Chris Hennick 2023-06-21 12:27:27 -07:00
parent e8a8dede55
commit a688086d27
No known key found for this signature in database
GPG key ID: 25653935CC8B6C74
5 changed files with 14 additions and 14 deletions

View file

@ -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
}

View file

@ -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(),

View file

@ -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
}

View file

@ -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<T>(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
}

View file

@ -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<u8>) {
}
// 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)
}