Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make all methods const that can be const on 1.70.0 #850

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ impl Color {
}

/// Create a vec4 of red, green, blue, and alpha components.
pub fn to_vec(&self) -> glam::Vec4 {
pub const fn to_vec(&self) -> glam::Vec4 {
glam::Vec4::new(self.r, self.g, self.b, self.a)
}

/// Create a color from a vec4 of red, green, blue, and alpha components.
pub fn from_vec(vec: glam::Vec4) -> Self {
pub const fn from_vec(vec: glam::Vec4) -> Self {
Self::new(vec.x, vec.y, vec.z, vec.w)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/experimental/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl AnimatedSprite {
}

/// Currently chosen animation
pub fn current_animation(&self) -> usize {
pub const fn current_animation(&self) -> usize {
self.current_animation
}

Expand Down
2 changes: 1 addition & 1 deletion src/experimental/camera/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Default for Camera {
}

impl Camera {
pub fn new(offset: Vec2, scale: f32) -> Self {
pub const fn new(offset: Vec2, scale: f32) -> Self {
Self {
offset,
scale,
Expand Down
6 changes: 3 additions & 3 deletions src/experimental/coroutines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ enum CoroutineState {
}

impl CoroutineState {
pub fn is_value(&self) -> bool {
pub const fn is_value(&self) -> bool {
matches!(self, CoroutineState::Value(_))
}

pub fn is_nothing(&self) -> bool {
pub const fn is_nothing(&self) -> bool {
matches!(self, CoroutineState::Nothing)
}

Expand Down Expand Up @@ -256,7 +256,7 @@ impl Future for TimerDelayFuture {
}
}

pub fn wait_seconds(time: f32) -> TimerDelayFuture {
pub const fn wait_seconds(time: f32) -> TimerDelayFuture {
TimerDelayFuture {
remaining_time: time,
}
Expand Down
10 changes: 5 additions & 5 deletions src/experimental/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<T: 'static> Clone for Handle<T> {
impl<T: 'static> Copy for Handle<T> {}

impl<T> Handle<T> {
pub fn null() -> Handle<T> {
pub const fn null() -> Handle<T> {
Handle {
id: None,
_marker: PhantomData,
Expand All @@ -75,7 +75,7 @@ impl<T> Handle<T> {
HandleUntyped(self.id.unwrap())
}

pub fn as_trait<T1: ?Sized>(&self) {}
pub const fn as_trait<T1: ?Sized>(&self) {}
}

pub(crate) struct Lens<T> {
Expand Down Expand Up @@ -127,7 +127,7 @@ pub struct RefMut<T: 'static> {
}

impl<T: 'static> RefMut<T> {
pub fn handle(&self) -> Handle<T> {
pub const fn handle(&self) -> Handle<T> {
Handle {
id: self.handle.id,
_marker: PhantomData,
Expand Down Expand Up @@ -190,7 +190,7 @@ pub struct RefMutAny<'a> {
}

impl<'a> RefMutAny<'a> {
pub fn handle<T>(&self) -> Handle<T> {
pub const fn handle<T>(&self) -> Handle<T> {
Handle {
id: Some(self.handle.0),
_marker: PhantomData,
Expand Down Expand Up @@ -700,6 +700,6 @@ pub(crate) fn in_fixed_update() -> bool {
unsafe { get_scene() }.in_fixed_update
}

pub(crate) fn fixed_frame_time() -> f32 {
pub(crate) const fn fixed_frame_time() -> f32 {
CONST_FPS as _
}
4 changes: 2 additions & 2 deletions src/experimental/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<T: scene::Node + 'static> StateMachine<T> {
}
}

pub fn state(&self) -> usize {
pub const fn state(&self) -> usize {
match self {
StateMachine::Ready(state_machine) => state_machine.state(),
StateMachine::InUse {
Expand Down Expand Up @@ -178,7 +178,7 @@ impl<T: 'static> StateMachineOwned<T> {
self.next_state = Some(state);
}

pub fn state(&self) -> usize {
pub const fn state(&self) -> usize {
self.current_state
}

Expand Down
6 changes: 3 additions & 3 deletions src/math/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ pub struct Circle {
}

impl Circle {
pub fn new(x: f32, y: f32, r: f32) -> Self {
pub const fn new(x: f32, y: f32, r: f32) -> Self {
Circle { x, y, r }
}

pub fn point(&self) -> Vec2 {
pub const fn point(&self) -> Vec2 {
vec2(self.x, self.y)
}

pub fn radius(&self) -> f32 {
pub const fn radius(&self) -> f32 {
self.r
}

Expand Down
12 changes: 6 additions & 6 deletions src/math/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ impl Rect {
/// * `y` - y-coordinate of the top-left corner.
/// * `w` - width of the `Rect`, going to the right.
/// * `h` - height of the `Rect`, going down.
pub fn new(x: f32, y: f32, w: f32, h: f32) -> Rect {
pub const fn new(x: f32, y: f32, w: f32, h: f32) -> Rect {
Rect { x, y, w, h }
}

/// Returns the top-left corner of the `Rect`.
pub fn point(&self) -> Vec2 {
pub const fn point(&self) -> Vec2 {
vec2(self.x, self.y)
}

/// Returns the size (width and height) of the `Rect`.
pub fn size(&self) -> Vec2 {
pub const fn size(&self) -> Vec2 {
vec2(self.w, self.h)
}

Expand All @@ -37,7 +37,7 @@ impl Rect {
}

/// Returns the left edge of the `Rect`
pub fn left(&self) -> f32 {
pub const fn left(&self) -> f32 {
self.x
}

Expand All @@ -47,7 +47,7 @@ impl Rect {
}

/// Returns the top edge of the `Rect`
pub fn top(&self) -> f32 {
pub const fn top(&self) -> f32 {
self.y
}

Expand Down Expand Up @@ -128,7 +128,7 @@ pub struct RectOffset {
}

impl RectOffset {
pub fn new(left: f32, right: f32, top: f32, bottom: f32) -> RectOffset {
pub const fn new(left: f32, right: f32, top: f32, bottom: f32) -> RectOffset {
RectOffset {
left,
right,
Expand Down
8 changes: 4 additions & 4 deletions src/quad_gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct DrawCall {
}

impl DrawCall {
fn new(
const fn new(
texture: Option<miniquad::TextureId>,
model: glam::Mat4,
draw_mode: DrawMode,
Expand Down Expand Up @@ -534,7 +534,7 @@ impl PipelinesStorage {
GlPipeline(id)
}

fn get(&self, draw_mode: DrawMode, depth_enabled: bool) -> GlPipeline {
const fn get(&self, draw_mode: DrawMode, depth_enabled: bool) -> GlPipeline {
match (draw_mode, depth_enabled) {
(DrawMode::Triangles, false) => Self::TRIANGLES_PIPELINE,
(DrawMode::Triangles, true) => Self::TRIANGLES_DEPTH_PIPELINE,
Expand Down Expand Up @@ -814,11 +814,11 @@ impl QuadGl {
crate::get_context().projection_matrix()
}

pub fn get_active_render_pass(&self) -> Option<RenderPass> {
pub const fn get_active_render_pass(&self) -> Option<RenderPass> {
self.state.render_pass
}

pub fn is_depth_test_enabled(&self) -> bool {
pub const fn is_depth_test_enabled(&self) -> bool {
self.state.depth_test_enable
}

Expand Down
2 changes: 1 addition & 1 deletion src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub struct Frame {
}

impl Frame {
fn new() -> Frame {
const fn new() -> Frame {
Frame {
full_frame_time: 0.0,
zones: vec![],
Expand Down
4 changes: 2 additions & 2 deletions src/text/atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ impl Atlas {
self.sprites.get(&key).cloned()
}

pub fn width(&self) -> u16 {
pub const fn width(&self) -> u16 {
self.image.width
}

pub fn height(&self) -> u16 {
pub const fn height(&self) -> u16 {
self.image.height
}

Expand Down
12 changes: 6 additions & 6 deletions src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl TexturesContext {
// fn remove(&mut self, texture: TextureSlotId) {
// self.textures.remove(texture);
// }
pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
self.textures.len()
}
pub fn garbage_collect(&mut self, ctx: &mut miniquad::Context) {
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Image {
/// # use macroquad::prelude::*;
/// let image = Image::empty();
/// ```
pub fn empty() -> Image {
pub const fn empty() -> Image {
Image {
width: 0,
height: 0,
Expand Down Expand Up @@ -157,12 +157,12 @@ impl Image {
}

/// Returns the width of this image.
pub fn width(&self) -> usize {
pub const fn width(&self) -> usize {
self.width as usize
}

/// Returns the height of this image.
pub fn height(&self) -> usize {
pub const fn height(&self) -> usize {
self.height as usize
}

Expand Down Expand Up @@ -611,7 +611,7 @@ impl Texture2D {
},
}
}
pub(crate) fn unmanaged(texture: miniquad::TextureId) -> Texture2D {
pub(crate) const fn unmanaged(texture: miniquad::TextureId) -> Texture2D {
Texture2D {
texture: TextureHandle::Unmanaged(texture),
}
Expand Down Expand Up @@ -672,7 +672,7 @@ impl Texture2D {

/// Creates a Texture2D from a miniquad
/// [Texture](https://docs.rs/miniquad/0.3.0-alpha/miniquad/graphics/struct.Texture.html)
pub fn from_miniquad_texture(texture: miniquad::TextureId) -> Texture2D {
pub const fn from_miniquad_texture(texture: miniquad::TextureId) -> Texture2D {
Texture2D {
texture: TextureHandle::Unmanaged(texture),
}
Expand Down
2 changes: 1 addition & 1 deletion src/texture/slotmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl TextureIdSlotMap {
}

/// Returns the number of elements in the slot map.
pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
self.num_elems as usize
}

Expand Down
12 changes: 6 additions & 6 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ impl Window {
);
}

pub fn top_level(&self) -> bool {
pub const fn top_level(&self) -> bool {
self.parent.is_none()
}

pub fn full_rect(&self) -> Rect {
pub const fn full_rect(&self) -> Rect {
Rect::new(self.position.x, self.position.y, self.size.x, self.size.y)
}

Expand All @@ -215,7 +215,7 @@ impl Window {
self.cursor.area.y = position.y + self.title_height + self.window_margin.top;
}

pub fn title_rect(&self) -> Rect {
pub const fn title_rect(&self) -> Rect {
Rect::new(
self.position.x,
self.position.y,
Expand Down Expand Up @@ -267,7 +267,7 @@ pub(crate) struct TabSelector {
}

impl TabSelector {
fn new() -> Self {
const fn new() -> Self {
TabSelector {
counter: 0,
wants: None,
Expand Down Expand Up @@ -970,7 +970,7 @@ impl Ui {
)
}

pub fn is_mouse_captured(&self) -> bool {
pub const fn is_mouse_captured(&self) -> bool {
self.input.cursor_grabbed
}

Expand Down Expand Up @@ -998,7 +998,7 @@ impl Ui {
self.active_window.map_or(false, |wnd| self.is_focused(wnd))
}

pub fn is_dragging(&self) -> bool {
pub const fn is_dragging(&self) -> bool {
self.dragging.is_some()
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct Cursor {
}

impl Cursor {
pub fn new(area: Rect, margin: f32) -> Cursor {
pub const fn new(area: Rect, margin: f32) -> Cursor {
Cursor {
margin,
x: margin,
Expand Down
6 changes: 3 additions & 3 deletions src/ui/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ pub struct Input {
}

impl Input {
pub fn is_mouse_down(&self) -> bool {
pub const fn is_mouse_down(&self) -> bool {
self.is_mouse_down && self.cursor_grabbed == false && self.window_active
}

pub fn click_down(&self) -> bool {
pub const fn click_down(&self) -> bool {
self.click_down && self.cursor_grabbed == false && self.window_active
}

pub fn click_up(&self) -> bool {
pub const fn click_up(&self) -> bool {
self.click_up && self.cursor_grabbed == false && self.window_active
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/render/mesh_rasterizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct DrawList {
}

impl DrawList {
pub fn new() -> DrawList {
pub const fn new() -> DrawList {
DrawList {
vertices: vec![],
indices: vec![],
Expand Down
Loading
Loading