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

[Merged by Bors] - Make transform builder methods const #3045

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 12 additions & 12 deletions crates/bevy_transform/src/components/global_transform.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Transform;
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_math::{Mat3, Mat4, Quat, Vec3};
use bevy_math::{const_vec3, Mat3, Mat4, Quat, Vec3};
use bevy_reflect::Reflect;
use std::ops::Mul;

Expand Down Expand Up @@ -44,8 +44,8 @@ pub struct GlobalTransform {
impl GlobalTransform {
#[doc(hidden)]
#[inline]
pub fn from_xyz(x: f32, y: f32, z: f32) -> Self {
Self::from_translation(Vec3::new(x, y, z))
pub const fn from_xyz(x: f32, y: f32, z: f32) -> Self {
Self::from_translation(const_vec3!([x, y, z]))
}

/// Creates a new identity [`GlobalTransform`], with no translation, rotation, and a scale of 1
Expand Down Expand Up @@ -73,28 +73,28 @@ impl GlobalTransform {

#[doc(hidden)]
#[inline]
pub fn from_translation(translation: Vec3) -> Self {
pub const fn from_translation(translation: Vec3) -> Self {
GlobalTransform {
translation,
..Default::default()
..Self::identity()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my own learning, any reason to prefer Self::identity() over Default::default()? Is it because Default is a trait and won't work here with const?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly, default() can't be const

}
}

#[doc(hidden)]
#[inline]
pub fn from_rotation(rotation: Quat) -> Self {
pub const fn from_rotation(rotation: Quat) -> Self {
GlobalTransform {
rotation,
..Default::default()
..Self::identity()
}
}

#[doc(hidden)]
#[inline]
pub fn from_scale(scale: Vec3) -> Self {
pub const fn from_scale(scale: Vec3) -> Self {
GlobalTransform {
scale,
..Default::default()
..Self::identity()
}
}

Expand All @@ -107,21 +107,21 @@ impl GlobalTransform {

#[doc(hidden)]
#[inline]
pub fn with_translation(mut self, translation: Vec3) -> Self {
pub const fn with_translation(mut self, translation: Vec3) -> Self {
self.translation = translation;
self
}

#[doc(hidden)]
#[inline]
pub fn with_rotation(mut self, rotation: Quat) -> Self {
pub const fn with_rotation(mut self, rotation: Quat) -> Self {
self.rotation = rotation;
self
}

#[doc(hidden)]
#[inline]
pub fn with_scale(mut self, scale: Vec3) -> Self {
pub const fn with_scale(mut self, scale: Vec3) -> Self {
self.scale = scale;
self
}
Expand Down
24 changes: 12 additions & 12 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::GlobalTransform;
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_math::{Mat3, Mat4, Quat, Vec3};
use bevy_math::{const_vec3, Mat3, Mat4, Quat, Vec3};
use bevy_reflect::Reflect;
use std::ops::Mul;

Expand Down Expand Up @@ -50,8 +50,8 @@ impl Transform {
/// is used for z-ordering elements: higher `z`-value will be in front of lower
/// `z`-value.
#[inline]
pub fn from_xyz(x: f32, y: f32, z: f32) -> Self {
Self::from_translation(Vec3::new(x, y, z))
pub const fn from_xyz(x: f32, y: f32, z: f32) -> Self {
Self::from_translation(const_vec3!([x, y, z]))
}

/// Creates a new identity [`Transform`], with no translation, rotation, and a scale of 1 on
Expand Down Expand Up @@ -81,30 +81,30 @@ impl Transform {
/// Creates a new [`Transform`], with `translation`. Rotation will be 0 and scale 1 on
/// all axes.
#[inline]
pub fn from_translation(translation: Vec3) -> Self {
pub const fn from_translation(translation: Vec3) -> Self {
Transform {
translation,
..Default::default()
..Self::identity()
}
}

/// Creates a new [`Transform`], with `rotation`. Translation will be 0 and scale 1 on
/// all axes.
#[inline]
pub fn from_rotation(rotation: Quat) -> Self {
pub const fn from_rotation(rotation: Quat) -> Self {
Transform {
rotation,
..Default::default()
..Self::identity()
}
}

/// Creates a new [`Transform`], with `scale`. Translation will be 0 and rotation 0 on
/// all axes.
#[inline]
pub fn from_scale(scale: Vec3) -> Self {
pub const fn from_scale(scale: Vec3) -> Self {
Transform {
scale,
..Default::default()
..Self::identity()
}
}

Expand All @@ -119,21 +119,21 @@ impl Transform {

/// Returns this [`Transform`] with a new translation.
#[inline]
pub fn with_translation(mut self, translation: Vec3) -> Self {
pub const fn with_translation(mut self, translation: Vec3) -> Self {
self.translation = translation;
self
}

/// Returns this [`Transform`] with a new rotation.
#[inline]
pub fn with_rotation(mut self, rotation: Quat) -> Self {
pub const fn with_rotation(mut self, rotation: Quat) -> Self {
self.rotation = rotation;
self
}

/// Returns this [`Transform`] with a new scale.
#[inline]
pub fn with_scale(mut self, scale: Vec3) -> Self {
pub const fn with_scale(mut self, scale: Vec3) -> Self {
self.scale = scale;
self
}
Expand Down