From 83513102beb915f9ae1ff9b714bf386618907b2d Mon Sep 17 00:00:00 2001
From: Tom Fryers
Date: Fri, 7 Nov 2025 13:19:56 +0000
Subject: [PATCH] Make some functions const
---
src/conf.rs | 2 +-
src/graphics.rs | 36 ++++++++++---------
src/graphics/gl.rs | 12 +++----
src/native/linux_wayland.rs | 4 +--
src/native/linux_wayland/decorations.rs | 5 ++-
src/native/linux_wayland/extensions/cursor.rs | 2 +-
src/native/linux_wayland/keycodes.rs | 2 +-
src/native/linux_wayland/libwayland_client.rs | 2 +-
src/native/linux_x11/clipboard.rs | 2 +-
src/native/linux_x11/keycodes.rs | 2 +-
10 files changed, 38 insertions(+), 31 deletions(-)
diff --git a/src/conf.rs b/src/conf.rs
index a7a3d4070..6808b8543 100644
--- a/src/conf.rs
+++ b/src/conf.rs
@@ -246,7 +246,7 @@ pub struct Icon {
}
impl Icon {
- pub fn miniquad_logo() -> Icon {
+ pub const fn miniquad_logo() -> Icon {
Icon {
small: crate::default_icon::SMALL,
medium: crate::default_icon::MEDIUM,
diff --git a/src/graphics.rs b/src/graphics.rs
index dbdfffc65..fe30fb147 100644
--- a/src/graphics.rs
+++ b/src/graphics.rs
@@ -42,7 +42,7 @@ pub enum UniformType {
impl UniformType {
/// Byte size for a given UniformType
- pub fn size(&self) -> usize {
+ pub const fn size(&self) -> usize {
match self {
UniformType::Float1 => 4,
UniformType::Float2 => 8,
@@ -134,7 +134,7 @@ impl VertexFormat {
/// Number of components in this VertexFormat
/// it is called size in OpenGl, but do not confuse this with bytes size
/// basically, its an N from FloatN/IntN
- pub fn components(&self) -> i32 {
+ pub const fn components(&self) -> i32 {
match self {
VertexFormat::Float1 => 1,
VertexFormat::Float2 => 2,
@@ -157,7 +157,7 @@ impl VertexFormat {
}
/// Size in bytes
- pub fn size_bytes(&self) -> i32 {
+ pub const fn size_bytes(&self) -> i32 {
match self {
VertexFormat::Float1 => 1 * 4,
VertexFormat::Float2 => 2 * 4,
@@ -179,7 +179,7 @@ impl VertexFormat {
}
}
- fn type_(&self) -> GLuint {
+ const fn type_(&self) -> GLuint {
match self {
VertexFormat::Float1 => GL_FLOAT,
VertexFormat::Float2 => GL_FLOAT,
@@ -328,7 +328,7 @@ pub enum TextureFormat {
}
impl TextureFormat {
/// Returns the size in bytes of texture with `dimensions`.
- pub fn size(self, width: u32, height: u32) -> u32 {
+ pub const fn size(self, width: u32, height: u32) -> u32 {
let square = width * height;
match self {
TextureFormat::RGB8 => 3 * square,
@@ -436,7 +436,7 @@ impl TextureId {
/// Wrap raw platform texture into a TextureId acceptable for miniquad
/// Without allocating any miniquad memory and without letting miniquad
/// manage the texture.
- pub fn from_raw_id(raw_id: RawId) -> TextureId {
+ pub const fn from_raw_id(raw_id: RawId) -> TextureId {
TextureId(TextureIdInner::Raw(raw_id))
}
}
@@ -465,7 +465,7 @@ pub struct BlendState {
}
impl BlendState {
- pub fn new(equation: Equation, sfactor: BlendFactor, dfactor: BlendFactor) -> BlendState {
+ pub const fn new(equation: Equation, sfactor: BlendFactor, dfactor: BlendFactor) -> BlendState {
BlendState {
equation,
sfactor,
@@ -547,7 +547,7 @@ pub enum PassAction {
}
impl PassAction {
- pub fn clear_color(r: f32, g: f32, b: f32, a: f32) -> PassAction {
+ pub const fn clear_color(r: f32, g: f32, b: f32, a: f32) -> PassAction {
PassAction::Clear {
color: Some((r, g, b, a)),
depth: Some(1.),
@@ -789,14 +789,14 @@ pub enum BufferUsage {
Stream,
}
-fn gl_buffer_target(buffer_type: &BufferType) -> GLenum {
+const fn gl_buffer_target(buffer_type: &BufferType) -> GLenum {
match buffer_type {
BufferType::VertexBuffer => GL_ARRAY_BUFFER,
BufferType::IndexBuffer => GL_ELEMENT_ARRAY_BUFFER,
}
}
-fn gl_usage(usage: &BufferUsage) -> GLenum {
+const fn gl_usage(usage: &BufferUsage) -> GLenum {
match usage {
BufferUsage::Immutable => GL_STATIC_DRAW,
BufferUsage::Dynamic => GL_DYNAMIC_DRAW,
@@ -879,7 +879,7 @@ impl Default for ElapsedQuery {
}
impl ElapsedQuery {
- pub fn new() -> ElapsedQuery {
+ pub const fn new() -> ElapsedQuery {
ElapsedQuery { gl_query: 0 }
}
@@ -919,7 +919,7 @@ impl ElapsedQuery {
/// available for retrieval.
///
/// Use [`ElapsedQuery::is_supported()`] to check if functionality is available and the method can be called.
- pub fn get_result(&self) -> u64 {
+ pub const fn get_result(&self) -> u64 {
// let mut time: GLuint64 = 0;
// assert!(self.gl_query != 0);
// unsafe { glGetQueryObjectui64v(self.gl_query, GL_QUERY_RESULT, &mut time) };
@@ -940,7 +940,7 @@ impl ElapsedQuery {
/// command submission.
///
/// Use [`ElapsedQuery::is_supported()`] to check if functionality is available and the method can be called.
- pub fn is_available(&self) -> bool {
+ pub const fn is_available(&self) -> bool {
// let mut available: GLint = 0;
// // begin_query was not called yet
@@ -996,7 +996,7 @@ impl<'a> BufferSource<'a> {
/// types are not supported.
///
/// For vertex buffers it is OK to use `empty::(byte_size);`
- pub fn empty(size: usize) -> BufferSource<'a> {
+ pub const fn empty(size: usize) -> BufferSource<'a> {
let element_size = std::mem::size_of::();
BufferSource::Empty {
size: size * std::mem::size_of::(),
@@ -1014,7 +1014,11 @@ impl<'a> BufferSource<'a> {
})
}
- pub unsafe fn pointer(ptr: *const u8, size: usize, element_size: usize) -> BufferSource<'a> {
+ pub const unsafe fn pointer(
+ ptr: *const u8,
+ size: usize,
+ element_size: usize,
+ ) -> BufferSource<'a> {
BufferSource::Slice(Arg {
ptr: ptr as _,
size,
@@ -1087,7 +1091,7 @@ pub struct ContextInfo {
}
impl ContextInfo {
- pub fn has_integer_attributes(&self) -> bool {
+ pub const fn has_integer_attributes(&self) -> bool {
match self.backend {
Backend::Metal => true,
Backend::OpenGl => {
diff --git a/src/graphics/gl.rs b/src/graphics/gl.rs
index 9c8c98440..d27451ac1 100644
--- a/src/graphics/gl.rs
+++ b/src/graphics/gl.rs
@@ -51,13 +51,13 @@ enum TextureOrRenderbuffer {
Renderbuffer(GLuint),
}
impl TextureOrRenderbuffer {
- fn texture(&self) -> Option {
+ const fn texture(&self) -> Option {
match self {
TextureOrRenderbuffer::Texture(id) => Some(*id),
_ => None,
}
}
- fn renderbuffer(&self) -> Option {
+ const fn renderbuffer(&self) -> Option {
match self {
TextureOrRenderbuffer::Renderbuffer(id) => Some(*id),
_ => None,
@@ -72,7 +72,7 @@ struct Texture {
}
impl TextureFormat {
- fn sized_internal_format(&self) -> GLenum {
+ const fn sized_internal_format(&self) -> GLenum {
match self {
TextureFormat::RGB8 => GL_RGB8,
TextureFormat::RGBA8 => GL_RGBA8,
@@ -446,11 +446,11 @@ impl Texture {
}
#[inline]
- fn size(&self, width: u32, height: u32) -> usize {
+ const fn size(&self, width: u32, height: u32) -> usize {
self.params.format.size(width, height) as usize
}
- fn gl_filter(filter: FilterMode, mipmap_filter: MipmapFilterMode) -> GLenum {
+ const fn gl_filter(filter: FilterMode, mipmap_filter: MipmapFilterMode) -> GLenum {
match filter {
FilterMode::Nearest => match mipmap_filter {
MipmapFilterMode::None => GL_NEAREST,
@@ -574,7 +574,7 @@ impl GlContext {
}
}
- pub fn features(&self) -> &Features {
+ pub const fn features(&self) -> &Features {
&self.info.features
}
}
diff --git a/src/native/linux_wayland.rs b/src/native/linux_wayland.rs
index 3b8ff0b46..9b468d870 100644
--- a/src/native/linux_wayland.rs
+++ b/src/native/linux_wayland.rs
@@ -227,7 +227,7 @@ struct KeyboardContext {
timerfd: core::ffi::c_int,
}
-fn new_itimerspec() -> libc::itimerspec {
+const fn new_itimerspec() -> libc::itimerspec {
libc::itimerspec {
it_interval: libc::timespec {
tv_sec: 0,
@@ -329,7 +329,7 @@ struct PointerContext {
relative_pointer: *mut extensions::cursor::zwp_relative_pointer_v1,
}
impl PointerContext {
- fn new() -> Self {
+ const fn new() -> Self {
Self {
pointer: std::ptr::null_mut(),
enter_serial: None,
diff --git a/src/native/linux_wayland/decorations.rs b/src/native/linux_wayland/decorations.rs
index a6680ca68..c3d5d8c87 100644
--- a/src/native/linux_wayland/decorations.rs
+++ b/src/native/linux_wayland/decorations.rs
@@ -287,7 +287,10 @@ unsafe extern "C" fn libdecor_frame_handle_close(_frame: *mut libdecor_frame, _d
crate::native_display().try_lock().unwrap().quit_requested = true;
}
-unsafe extern "C" fn libdecor_frame_handle_commit(_frame: *mut libdecor_frame, _data: *mut c_void) {
+const unsafe extern "C" fn libdecor_frame_handle_commit(
+ _frame: *mut libdecor_frame,
+ _data: *mut c_void,
+) {
}
unsafe extern "C" fn libdecor_handle_error(
_context: *mut libdecor,
diff --git a/src/native/linux_wayland/extensions/cursor.rs b/src/native/linux_wayland/extensions/cursor.rs
index d547ae7f9..0cc8b0051 100644
--- a/src/native/linux_wayland/extensions/cursor.rs
+++ b/src/native/linux_wayland/extensions/cursor.rs
@@ -108,7 +108,7 @@ crate::wl_listener!(
),
);
-pub fn translate_cursor(icon: crate::CursorIcon) -> core::ffi::c_uint {
+pub const fn translate_cursor(icon: crate::CursorIcon) -> core::ffi::c_uint {
// https://wayland.app/protocols/cursor-shape-v1#wp_cursor_shape_device_v1:enum:shape
match icon {
crate::CursorIcon::Default => 1,
diff --git a/src/native/linux_wayland/keycodes.rs b/src/native/linux_wayland/keycodes.rs
index 02d4ab5d6..9b208a3cb 100644
--- a/src/native/linux_wayland/keycodes.rs
+++ b/src/native/linux_wayland/keycodes.rs
@@ -16,7 +16,7 @@
use crate::event::KeyCode;
use crate::native::linux_wayland::libxkbcommon::xkb_keysym_t;
-pub fn translate_keysym(keysym: xkb_keysym_t) -> KeyCode {
+pub const fn translate_keysym(keysym: xkb_keysym_t) -> KeyCode {
// See xkbcommon/xkbcommon-keysyms.h
match keysym {
65307 => KeyCode::Escape,
diff --git a/src/native/linux_wayland/libwayland_client.rs b/src/native/linux_wayland/libwayland_client.rs
index c7d0ea49c..70a5a483f 100644
--- a/src/native/linux_wayland/libwayland_client.rs
+++ b/src/native/linux_wayland/libwayland_client.rs
@@ -372,7 +372,7 @@ macro_rules! wl_listener {
/// Implementation for the dummy event handlers
mod $name_dummy {
use super::*;
- $(pub unsafe extern "C" fn $event(_: *mut core::ffi::c_void, _: *mut $name, $(_: $arg_ty),*) {})*
+ $(pub const unsafe extern "C" fn $event(_: *mut core::ffi::c_void, _: *mut $name, $(_: $arg_ty),*) {})*
}
impl $name_listener {
/// Create a listener with dummy event handlers
diff --git a/src/native/linux_x11/clipboard.rs b/src/native/linux_x11/clipboard.rs
index b53a91ed0..5632e015f 100644
--- a/src/native/linux_x11/clipboard.rs
+++ b/src/native/linux_x11/clipboard.rs
@@ -218,7 +218,7 @@ unsafe impl Send for X11Clipboard {}
unsafe impl Sync for X11Clipboard {}
impl X11Clipboard {
- pub fn new(libx11: LibX11, display: *mut Display, window: Window) -> X11Clipboard {
+ pub const fn new(libx11: LibX11, display: *mut Display, window: Window) -> X11Clipboard {
X11Clipboard {
libx11,
display,
diff --git a/src/native/linux_x11/keycodes.rs b/src/native/linux_x11/keycodes.rs
index f48b98597..70b154a12 100644
--- a/src/native/linux_x11/keycodes.rs
+++ b/src/native/linux_x11/keycodes.rs
@@ -156,7 +156,7 @@ pub unsafe fn translate_mod(x11_mods: i32) -> KeyMods {
mods
}
-pub unsafe fn translate_mouse_button(button: i32) -> MouseButton {
+pub const unsafe fn translate_mouse_button(button: i32) -> MouseButton {
match button {
1 => MouseButton::Left,
2 => MouseButton::Middle,