Make transform builder methods const (#3045)

# Objective

- Make transform builder methods `const`

## Solution

- I made them `const`
This commit is contained in:
François 2022-02-04 01:46:35 +00:00
parent 7d712406fe
commit 37a7be56db
2 changed files with 24 additions and 24 deletions

View File

@ -1,6 +1,6 @@
use super::Transform; use super::Transform;
use bevy_ecs::{component::Component, reflect::ReflectComponent}; 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 bevy_reflect::Reflect;
use std::ops::Mul; use std::ops::Mul;
@ -47,8 +47,8 @@ pub struct GlobalTransform {
impl GlobalTransform { impl GlobalTransform {
#[doc(hidden)] #[doc(hidden)]
#[inline] #[inline]
pub fn from_xyz(x: f32, y: f32, z: f32) -> Self { pub const fn from_xyz(x: f32, y: f32, z: f32) -> Self {
Self::from_translation(Vec3::new(x, y, z)) Self::from_translation(const_vec3!([x, y, z]))
} }
/// Creates a new identity [`GlobalTransform`], with no translation, rotation, and a scale of 1 /// Creates a new identity [`GlobalTransform`], with no translation, rotation, and a scale of 1
@ -76,28 +76,28 @@ impl GlobalTransform {
#[doc(hidden)] #[doc(hidden)]
#[inline] #[inline]
pub fn from_translation(translation: Vec3) -> Self { pub const fn from_translation(translation: Vec3) -> Self {
GlobalTransform { GlobalTransform {
translation, translation,
..Default::default() ..Self::identity()
} }
} }
#[doc(hidden)] #[doc(hidden)]
#[inline] #[inline]
pub fn from_rotation(rotation: Quat) -> Self { pub const fn from_rotation(rotation: Quat) -> Self {
GlobalTransform { GlobalTransform {
rotation, rotation,
..Default::default() ..Self::identity()
} }
} }
#[doc(hidden)] #[doc(hidden)]
#[inline] #[inline]
pub fn from_scale(scale: Vec3) -> Self { pub const fn from_scale(scale: Vec3) -> Self {
GlobalTransform { GlobalTransform {
scale, scale,
..Default::default() ..Self::identity()
} }
} }
@ -110,21 +110,21 @@ impl GlobalTransform {
#[doc(hidden)] #[doc(hidden)]
#[inline] #[inline]
pub fn with_translation(mut self, translation: Vec3) -> Self { pub const fn with_translation(mut self, translation: Vec3) -> Self {
self.translation = translation; self.translation = translation;
self self
} }
#[doc(hidden)] #[doc(hidden)]
#[inline] #[inline]
pub fn with_rotation(mut self, rotation: Quat) -> Self { pub const fn with_rotation(mut self, rotation: Quat) -> Self {
self.rotation = rotation; self.rotation = rotation;
self self
} }
#[doc(hidden)] #[doc(hidden)]
#[inline] #[inline]
pub fn with_scale(mut self, scale: Vec3) -> Self { pub const fn with_scale(mut self, scale: Vec3) -> Self {
self.scale = scale; self.scale = scale;
self self
} }

View File

@ -1,6 +1,6 @@
use super::GlobalTransform; use super::GlobalTransform;
use bevy_ecs::{component::Component, reflect::ReflectComponent}; 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 bevy_reflect::Reflect;
use std::ops::Mul; use std::ops::Mul;
@ -50,8 +50,8 @@ impl Transform {
/// is used for z-ordering elements: higher `z`-value will be in front of lower /// is used for z-ordering elements: higher `z`-value will be in front of lower
/// `z`-value. /// `z`-value.
#[inline] #[inline]
pub fn from_xyz(x: f32, y: f32, z: f32) -> Self { pub const fn from_xyz(x: f32, y: f32, z: f32) -> Self {
Self::from_translation(Vec3::new(x, y, z)) Self::from_translation(const_vec3!([x, y, z]))
} }
/// Creates a new identity [`Transform`], with no translation, rotation, and a scale of 1 on /// Creates a new identity [`Transform`], with no translation, rotation, and a scale of 1 on
@ -81,30 +81,30 @@ impl Transform {
/// Creates a new [`Transform`], with `translation`. Rotation will be 0 and scale 1 on /// Creates a new [`Transform`], with `translation`. Rotation will be 0 and scale 1 on
/// all axes. /// all axes.
#[inline] #[inline]
pub fn from_translation(translation: Vec3) -> Self { pub const fn from_translation(translation: Vec3) -> Self {
Transform { Transform {
translation, translation,
..Default::default() ..Self::identity()
} }
} }
/// Creates a new [`Transform`], with `rotation`. Translation will be 0 and scale 1 on /// Creates a new [`Transform`], with `rotation`. Translation will be 0 and scale 1 on
/// all axes. /// all axes.
#[inline] #[inline]
pub fn from_rotation(rotation: Quat) -> Self { pub const fn from_rotation(rotation: Quat) -> Self {
Transform { Transform {
rotation, rotation,
..Default::default() ..Self::identity()
} }
} }
/// Creates a new [`Transform`], with `scale`. Translation will be 0 and rotation 0 on /// Creates a new [`Transform`], with `scale`. Translation will be 0 and rotation 0 on
/// all axes. /// all axes.
#[inline] #[inline]
pub fn from_scale(scale: Vec3) -> Self { pub const fn from_scale(scale: Vec3) -> Self {
Transform { Transform {
scale, scale,
..Default::default() ..Self::identity()
} }
} }
@ -119,21 +119,21 @@ impl Transform {
/// Returns this [`Transform`] with a new translation. /// Returns this [`Transform`] with a new translation.
#[inline] #[inline]
pub fn with_translation(mut self, translation: Vec3) -> Self { pub const fn with_translation(mut self, translation: Vec3) -> Self {
self.translation = translation; self.translation = translation;
self self
} }
/// Returns this [`Transform`] with a new rotation. /// Returns this [`Transform`] with a new rotation.
#[inline] #[inline]
pub fn with_rotation(mut self, rotation: Quat) -> Self { pub const fn with_rotation(mut self, rotation: Quat) -> Self {
self.rotation = rotation; self.rotation = rotation;
self self
} }
/// Returns this [`Transform`] with a new scale. /// Returns this [`Transform`] with a new scale.
#[inline] #[inline]
pub fn with_scale(mut self, scale: Vec3) -> Self { pub const fn with_scale(mut self, scale: Vec3) -> Self {
self.scale = scale; self.scale = scale;
self self
} }