Skip to content

Commit 6e5ad8d

Browse files
committed
Split swizzle functionality into own file
Rename trait ToVector -> SwizzleToVector and make it doc-private
1 parent 0c9052a commit 6e5ad8d

File tree

11 files changed

+166
-170
lines changed

11 files changed

+166
-170
lines changed

godot-core/src/builtin/mod.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,27 @@ pub(crate) use crate::gen::central::VariantDispatch;
4646
pub mod __prelude_reexport {
4747
use super::*;
4848

49-
pub use aabb::Aabb;
49+
pub use aabb::*;
5050
pub use array_inner::{Array, VariantArray};
51-
pub use basis::Basis;
52-
pub use callable::{Callable, RustCallable};
53-
pub use color::{Color, ColorChannelOrder};
54-
pub use color_hsv::ColorHsv;
51+
pub use basis::*;
52+
pub use callable::*;
53+
pub use color::*;
54+
pub use color_hsv::*;
5555
pub use dictionary_inner::Dictionary;
5656
pub use packed_array::*;
57-
pub use plane::Plane;
58-
pub use projection::{Projection, ProjectionEye, ProjectionPlane};
59-
pub use quaternion::Quaternion;
57+
pub use plane::*;
58+
pub use projection::*;
59+
pub use quaternion::*;
6060
pub use real_inner::*;
61-
pub use rect2::Rect2;
62-
pub use rect2i::Rect2i;
63-
pub use rid::Rid;
64-
pub use signal::Signal;
61+
pub use rect2::*;
62+
pub use rect2i::*;
63+
pub use rid::*;
64+
pub use signal::*;
6565
pub use string::{GString, NodePath, StringName};
66-
pub use transform2d::Transform2D;
67-
pub use transform3d::Transform3D;
68-
pub use variant::Variant;
69-
pub use vectors::{
70-
swizzle, ToVector, Vector2, Vector2Axis, Vector2i, Vector3, Vector3Axis, Vector3i,
71-
Vector4, Vector4Axis, Vector4i,
72-
};
66+
pub use transform2d::*;
67+
pub use transform3d::*;
68+
pub use variant::*;
69+
pub use vectors::*;
7370

7471
pub use super::{EulerOrder, Side, VariantOperator, VariantType};
7572
pub use crate::{array, dict, real, reals, varray};

godot-core/src/builtin/vectors/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8+
// Must be first.
89
mod vector_macros;
910

1011
mod vector2;
@@ -14,6 +15,7 @@ mod vector3i;
1415
mod vector4;
1516
mod vector4i;
1617
mod vector_axis;
18+
mod vector_swizzle;
1719

1820
pub use vector2::*;
1921
pub use vector2i::*;
@@ -22,5 +24,6 @@ pub use vector3i::*;
2224
pub use vector4::*;
2325
pub use vector4i::*;
2426
pub use vector_axis::*;
27+
pub use vector_swizzle::*;
2528

2629
pub use crate::swizzle;

godot-core/src/builtin/vectors/vector2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl_common_vector_fns!(Vector2, real);
245245
impl_float_vector_glam_fns!(Vector2, real);
246246
impl_float_vector_component_fns!(Vector2, real, (x, y));
247247
impl_vector_operators!(Vector2, real, (x, y));
248-
impl_from_tuple_for_vector2x!(Vector2, real);
248+
impl_swizzle_trait_for_vector2x!(Vector2, real);
249249

250250
// SAFETY:
251251
// This type is represented as `Self` in Godot, so `*mut Self` is sound.

godot-core/src/builtin/vectors/vector2i.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl_common_vector_fns!(Vector2i, i32);
126126
impl_integer_vector_glam_fns!(Vector2i, real);
127127
impl_integer_vector_component_fns!(Vector2i, real, (x, y));
128128
impl_vector_operators!(Vector2i, i32, (x, y));
129-
impl_from_tuple_for_vector2x!(Vector2i, i32);
129+
impl_swizzle_trait_for_vector2x!(Vector2i, i32);
130130

131131
// SAFETY:
132132
// This type is represented as `Self` in Godot, so `*mut Self` is sound.

godot-core/src/builtin/vectors/vector3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ impl_common_vector_fns!(Vector3, real);
276276
impl_float_vector_glam_fns!(Vector3, real);
277277
impl_float_vector_component_fns!(Vector3, real, (x, y, z));
278278
impl_vector_operators!(Vector3, real, (x, y, z));
279-
impl_from_tuple_for_vector3x!(Vector3, real);
279+
impl_swizzle_trait_for_vector3x!(Vector3, real);
280280

281281
// SAFETY:
282282
// This type is represented as `Self` in Godot, so `*mut Self` is sound.

godot-core/src/builtin/vectors/vector3i.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl_common_vector_fns!(Vector3i, i32);
157157
impl_integer_vector_glam_fns!(Vector3i, real);
158158
impl_integer_vector_component_fns!(Vector3i, real, (x, y, z));
159159
impl_vector_operators!(Vector3i, i32, (x, y, z));
160-
impl_from_tuple_for_vector3x!(Vector3i, i32);
160+
impl_swizzle_trait_for_vector3x!(Vector3i, i32);
161161

162162
// SAFETY:
163163
// This type is represented as `Self` in Godot, so `*mut Self` is sound.

godot-core/src/builtin/vectors/vector4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl_vector_operators!(Vector4, real, (x, y, z, w));
4444
impl_common_vector_fns!(Vector4, real);
4545
impl_float_vector_glam_fns!(Vector4, real);
4646
impl_float_vector_component_fns!(Vector4, real, (x, y, z, w));
47-
impl_from_tuple_for_vector4x!(Vector4, real);
47+
impl_swizzle_trait_for_vector4x!(Vector4, real);
4848

4949
impl Vector4 {
5050
/// Returns a `Vector4` with the given components.

godot-core/src/builtin/vectors/vector4i.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl_vector_operators!(Vector4i, i32, (x, y, z, w));
4343
impl_integer_vector_glam_fns!(Vector4i, real);
4444
impl_integer_vector_component_fns!(Vector4i, real, (x, y, z, w));
4545
impl_common_vector_fns!(Vector4i, i32);
46-
impl_from_tuple_for_vector4x!(Vector4i, i32);
46+
impl_swizzle_trait_for_vector4x!(Vector4i, i32);
4747

4848
impl Vector4i {
4949
/// Returns a `Vector4i` with the given components.

godot-core/src/builtin/vectors/vector_axis.rs

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,6 @@ use crate::builtin::meta::{ConvertError, FromGodot, FromGodotError, GodotConvert
99
use crate::builtin::{real, Vector2, Vector2i, Vector3, Vector3i, Vector4, Vector4i};
1010
use crate::obj::EngineEnum;
1111

12-
/// Access vector components in different order.
13-
///
14-
/// Allows to rearrange components, as well as to create higher- or lower-order vectors.
15-
///
16-
/// This macro supports all vector types (2D, 3D, 4D; both integer and float). The resulting vector
17-
/// type is deduced from the number and types of components.
18-
///
19-
/// To repeat a single component, check out the `splat` method on specific vector types.
20-
///
21-
/// # Examples
22-
///
23-
/// Reorder or duplicate fields:
24-
/// ```
25-
/// use godot::prelude::*;
26-
///
27-
/// let vec3 = Vector3i::new(1, 2, 3);
28-
/// let xzx = swizzle!(vec3 => x, z, x); // Vector3i
29-
///
30-
/// assert_eq!(xzx, Vector3i::new(1, 3, 1));
31-
/// ```
32-
///
33-
/// Create lower-order vector:
34-
/// ```
35-
/// # use godot::prelude::*;
36-
/// let vec4 = Vector4::new(1.0, 2.0, 3.0, 4.0);
37-
/// let yw = swizzle!(vec4 => y, w); // Vector2
38-
///
39-
/// assert_eq!(yw, Vector2::new(2.0, 4.0));
40-
/// ```
41-
///
42-
/// Create higher-order vector:
43-
/// ```
44-
/// # use godot::prelude::*;
45-
/// let vec3 = Vector3i::new(1, 2, 3);
46-
/// let xyyz = swizzle!(vec3 => x, y, y, z); // Vector4i
47-
///
48-
/// assert_eq!(xyyz, Vector4i::new(1, 2, 2, 3));
49-
/// ```
50-
#[macro_export]
51-
macro_rules! swizzle {
52-
($vec:expr => $a:ident, $b:ident) => {{
53-
let expr = $vec;
54-
$crate::builtin::ToVector::to_vector((expr.$a, expr.$b))
55-
}};
56-
($vec:expr => $a:ident, $b:ident, $c:ident) => {{
57-
let expr = $vec;
58-
$crate::builtin::ToVector::to_vector((expr.$a, expr.$b, expr.$c))
59-
}};
60-
($vec:expr => $a:ident, $b:ident, $c:ident, $d:ident) => {{
61-
let expr = $vec;
62-
$crate::builtin::ToVector::to_vector((expr.$a, expr.$b, expr.$c, expr.$d))
63-
}};
64-
}
65-
66-
// ----------------------------------------------------------------------------------------------------------------------------------------------
67-
68-
/// Trait that allows conversion from tuples to vectors.
69-
///
70-
/// Is implemented instead of `From`/`Into` because it provides type inference.
71-
pub trait ToVector: Sized {
72-
type Output;
73-
fn to_vector(self) -> Self::Output;
74-
}
75-
7612
macro_rules! impl_vector_axis_enum {
7713
($Vector:ident, $AxisEnum:ident, ($($axis:ident),+)) => {
7814
#[doc = concat!("Enumerates the axes in a [`", stringify!($Vector), "`].")]
@@ -135,63 +71,3 @@ impl_vector_index!(Vector3i, i32, (x, y, z), Vector3Axis, (X, Y, Z));
13571

13672
impl_vector_index!(Vector4, real, (x, y, z, w), Vector4Axis, (X, Y, Z, W));
13773
impl_vector_index!(Vector4i, i32, (x, y, z, w), Vector4Axis, (X, Y, Z, W));
138-
139-
// ----------------------------------------------------------------------------------------------------------------------------------------------
140-
141-
#[cfg(test)]
142-
mod test {
143-
use crate::assert_eq_approx;
144-
use crate::builtin::*;
145-
146-
#[test]
147-
fn test_vector_swizzle() {
148-
// * VectorN swizzle
149-
let vector2 = Vector2::new(1.0, 2.0);
150-
let vector3 = Vector3::new(1.0, 2.0, 3.0);
151-
let vector4 = Vector4::new(1.0, 2.0, 3.0, 4.0);
152-
153-
// VectorN to Vector2
154-
let vec2swiz2: Vector2 = swizzle!(vector2 => y, x);
155-
let vec3swiz2: Vector2 = swizzle!(vector3 => y, x);
156-
let vec4swiz2: Vector2 = swizzle!(vector4 => y, x);
157-
assert_eq_approx!(vec2swiz2, Vector2::new(2.0, 1.0));
158-
assert_eq_approx!(vec3swiz2, Vector2::new(2.0, 1.0));
159-
assert_eq_approx!(vec4swiz2, Vector2::new(2.0, 1.0));
160-
161-
// VectorN to Vector3
162-
let vec2swiz3: Vector3 = swizzle!(vector2 => y, x, x);
163-
let vec3swiz3: Vector3 = swizzle!(vector3 => y, x, z);
164-
let vec4swiz3: Vector3 = swizzle!(vector4 => y, x, z);
165-
assert_eq_approx!(vec2swiz3, Vector3::new(2.0, 1.0, 1.0),);
166-
assert_eq_approx!(vec3swiz3, Vector3::new(2.0, 1.0, 3.0),);
167-
assert_eq_approx!(vec4swiz3, Vector3::new(2.0, 1.0, 3.0),);
168-
169-
// VectorN to Vector4
170-
let vec2swiz4: Vector4 = swizzle!(vector2 => y, x, x, y);
171-
let vec3swiz4: Vector4 = swizzle!(vector3 => y, x, z, y);
172-
let vec4swiz4: Vector4 = swizzle!(vector4 => y, x, z, w);
173-
assert_eq_approx!(vec2swiz4, Vector4::new(2.0, 1.0, 1.0, 2.0),);
174-
assert_eq_approx!(vec3swiz4, Vector4::new(2.0, 1.0, 3.0, 2.0),);
175-
assert_eq_approx!(vec4swiz4, Vector4::new(2.0, 1.0, 3.0, 4.0),);
176-
177-
// * VectorNi swizzle
178-
let vector2i = Vector2i::new(1, 2);
179-
let vector3i = Vector3i::new(1, 2, 3);
180-
let vector4i = Vector4i::new(1, 2, 3, 4);
181-
182-
// VectorNi to Vector2i
183-
assert_eq!(Vector2i::new(2, 1), swizzle!(vector2i => y, x));
184-
assert_eq!(swizzle!(vector3i => y, x), Vector2i::new(2, 1));
185-
assert_eq!(swizzle!(vector4i => y, x), Vector2i::new(2, 1));
186-
187-
// VectorNi to Vector3i
188-
assert_eq!(swizzle!(vector2i => y, x, x), Vector3i::new(2, 1, 1));
189-
assert_eq!(swizzle!(vector3i => y, x, z), Vector3i::new(2, 1, 3));
190-
assert_eq!(swizzle!(vector4i => y, x, z), Vector3i::new(2, 1, 3));
191-
192-
// VectorNi to Vector4i
193-
assert_eq!(swizzle!(vector2i => y, x, x, y), Vector4i::new(2, 1, 1, 2));
194-
assert_eq!(swizzle!(vector3i => y, x, z, y), Vector4i::new(2, 1, 3, 2));
195-
assert_eq!(swizzle!(vector4i => y, x, z, w), Vector4i::new(2, 1, 3, 4));
196-
}
197-
}

godot-core/src/builtin/vectors/vector_macros.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -476,42 +476,33 @@ macro_rules! impl_integer_vector_component_fns {
476476
};
477477
}
478478

479-
macro_rules! impl_from_tuple_for_vector2x {
480-
(
481-
$Vector:ty,
482-
$Scalar:ty
483-
) => {
484-
impl $crate::builtin::ToVector for ($Scalar, $Scalar) {
479+
macro_rules! impl_swizzle_trait_for_vector2x {
480+
($Vector:ty, $Scalar:ty) => {
481+
impl $crate::builtin::SwizzleToVector for ($Scalar, $Scalar) {
485482
type Output = $Vector;
486-
fn to_vector(self) -> $Vector {
483+
fn swizzle_to_vector(self) -> $Vector {
487484
<$Vector>::new(self.0, self.1)
488485
}
489486
}
490487
};
491488
}
492489

493-
macro_rules! impl_from_tuple_for_vector3x {
494-
(
495-
$Vector:ty,
496-
$Scalar:ty
497-
) => {
498-
impl $crate::builtin::ToVector for ($Scalar, $Scalar, $Scalar) {
490+
macro_rules! impl_swizzle_trait_for_vector3x {
491+
($Vector:ty, $Scalar:ty) => {
492+
impl $crate::builtin::SwizzleToVector for ($Scalar, $Scalar, $Scalar) {
499493
type Output = $Vector;
500-
fn to_vector(self) -> $Vector {
494+
fn swizzle_to_vector(self) -> $Vector {
501495
<$Vector>::new(self.0, self.1, self.2)
502496
}
503497
}
504498
};
505499
}
506500

507-
macro_rules! impl_from_tuple_for_vector4x {
508-
(
509-
$Vector:ty,
510-
$Scalar:ty
511-
) => {
512-
impl $crate::builtin::ToVector for ($Scalar, $Scalar, $Scalar, $Scalar) {
501+
macro_rules! impl_swizzle_trait_for_vector4x {
502+
($Vector:ty, $Scalar:ty) => {
503+
impl $crate::builtin::SwizzleToVector for ($Scalar, $Scalar, $Scalar, $Scalar) {
513504
type Output = $Vector;
514-
fn to_vector(self) -> $Vector {
505+
fn swizzle_to_vector(self) -> $Vector {
515506
<$Vector>::new(self.0, self.1, self.2, self.3)
516507
}
517508
}

0 commit comments

Comments
 (0)