Skip to content

Commit 7dc76a1

Browse files
committed
Follow-up changes to swizzling and tuple conversion
Changes: * Add type inference to swizzle!() * Module structure + macro publicly accessible * Instead of From<Tuple>, use ToVector trait * Documentation
1 parent 4d27611 commit 7dc76a1

File tree

7 files changed

+281
-212
lines changed

7 files changed

+281
-212
lines changed

godot-core/src/builtin/mod.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,7 @@ pub use string::*;
5454
pub use transform2d::*;
5555
pub use transform3d::*;
5656
pub use variant::*;
57-
pub use vectors::vector2::*;
58-
pub use vectors::vector2i::*;
59-
pub use vectors::vector3::*;
60-
pub use vectors::vector3i::*;
61-
pub use vectors::vector4::*;
62-
pub use vectors::vector4i::*;
63-
pub use vectors::vector_utils::*;
57+
pub use vectors::*;
6458

6559
/// Meta-information about variant types, properties and class names.
6660
pub mod meta;
@@ -195,7 +189,10 @@ mod real_mod {
195189
}
196190
}
197191

198-
pub use std::f32::consts;
192+
/// Re-export of [`std::f32::consts`] or [`std::f64::consts`], depending on precision config.
193+
pub mod real_consts {
194+
pub use std::f32::consts::*;
195+
}
199196

200197
/// A 2-dimensional vector from [`glam`]. Using a floating-point format compatible with [`real`].
201198
pub type RVec2 = glam::Vec2;
@@ -262,7 +259,10 @@ mod real_mod {
262259
}
263260
}
264261

265-
pub use std::f64::consts;
262+
/// Re-export of [`std::f32::consts`] or [`std::f64::consts`], depending on precision config.
263+
pub mod real_consts {
264+
pub use std::f64::consts::*;
265+
}
266266

267267
/// A 2-dimensional vector from [`glam`]. Using a floating-point format compatible with [`real`].
268268
pub type RVec2 = glam::DVec2;
@@ -292,26 +292,29 @@ mod real_mod {
292292

293293
pub use crate::real;
294294
pub(crate) use real_mod::*;
295-
pub use real_mod::{consts as real_consts, real};
295+
296+
pub use real_mod::{real, real_consts};
296297

297298
pub(crate) use glam::{IVec2, IVec3, IVec4};
298299

299-
/// A macro to coerce float-literals into the real type. Mainly used where
300-
/// you'd normally use a suffix to specity the type, such as `115.0f32`.
300+
/// A macro to coerce float-literals into the [`real`] type.
301+
///
302+
/// Mainly used where you'd normally use a suffix to specify the type, such as `115.0f32`.
303+
///
304+
/// # Examples
301305
///
302-
/// ### Examples
303-
/// Rust will not know how to infer the type of this call to `to_radians`:
306+
/// Rust is not able to infer the `self` type of this call to `to_radians`:
304307
/// ```compile_fail
305-
/// use godot_core::builtin::real;
308+
/// use godot::builtin::real;
306309
///
307310
/// let radians: real = 115.0.to_radians();
308311
/// ```
309-
/// But we can't add a suffix to the literal, since it may be either `f32` or
312+
/// But we cannot add a suffix to the literal, since it may be either `f32` or
310313
/// `f64` depending on the context. So instead we use our macro:
311314
/// ```
312-
/// use godot_core::builtin::real;
315+
/// use godot::builtin::real;
313316
///
314-
/// let radians: real = godot_core::real!(115.0).to_radians();
317+
/// let radians: real = real!(115.0).to_radians();
315318
/// ```
316319
#[macro_export]
317320
macro_rules! real {

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

Lines changed: 17 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,92 +6,20 @@
66

77
mod vector_macros;
88

9-
pub mod vector2;
10-
pub mod vector2i;
11-
pub mod vector3;
12-
pub mod vector3i;
13-
pub mod vector4;
14-
pub mod vector4i;
15-
16-
pub mod vector_utils;
17-
18-
#[cfg(test)]
19-
mod test {
20-
use crate::assert_eq_approx;
21-
use crate::builtin::*;
22-
#[test]
23-
fn test_vector_swizzle() {
24-
// * VectorN swizzle
25-
let vector2 = Vector2::new(1.0, 2.0);
26-
let vector3 = Vector3::new(1.0, 2.0, 3.0);
27-
let vector4 = Vector4::new(1.0, 2.0, 3.0, 4.0);
28-
29-
// VectorN to Vector2
30-
31-
let vc2swiz2: Vector2 = swizzle!(vector2 => y, x);
32-
let vc3swiz2: Vector2 = swizzle!(vector3 => y, x);
33-
let vc4swiz2: Vector2 = swizzle!(vector4 => y, x);
34-
assert_eq_approx!(Vector2::new(2.0, 1.0), vc2swiz2, Vector2::is_equal_approx);
35-
assert_eq_approx!(Vector2::new(2.0, 1.0), vc3swiz2, Vector2::is_equal_approx);
36-
assert_eq_approx!(Vector2::new(2.0, 1.0), vc4swiz2, Vector2::is_equal_approx);
37-
38-
// VectorN to Vector3
39-
40-
let vc2swiz3: Vector3 = swizzle!(vector2 => y, x, x);
41-
let vc3swiz3: Vector3 = swizzle!(vector3 => y, x, z);
42-
let vc4swiz3: Vector3 = swizzle!(vector4 => y, x, z);
43-
assert_eq_approx!(
44-
Vector3::new(2.0, 1.0, 1.0),
45-
vc2swiz3,
46-
Vector3::is_equal_approx
47-
);
48-
assert_eq_approx!(
49-
Vector3::new(2.0, 1.0, 3.0),
50-
vc3swiz3,
51-
Vector3::is_equal_approx
52-
);
53-
assert_eq_approx!(
54-
Vector3::new(2.0, 1.0, 3.0),
55-
vc4swiz3,
56-
Vector3::is_equal_approx
57-
);
58-
59-
// VectorN to Vector4
60-
61-
let vc2swiz4: Vector4 = swizzle!(vector2 => y, x, x, y);
62-
let vc3swiz4: Vector4 = swizzle!(vector3 => y, x, z, y);
63-
let vc4swiz4: Vector4 = swizzle!(vector4 => y, x, z, w);
64-
assert_eq_approx!(
65-
Vector4::new(2.0, 1.0, 1.0, 2.0),
66-
vc2swiz4,
67-
Vector4::is_equal_approx
68-
);
69-
assert_eq_approx!(
70-
Vector4::new(2.0, 1.0, 3.0, 2.0),
71-
vc3swiz4,
72-
Vector4::is_equal_approx
73-
);
74-
assert_eq_approx!(
75-
Vector4::new(2.0, 1.0, 3.0, 4.0),
76-
vc4swiz4,
77-
Vector4::is_equal_approx
78-
);
79-
80-
// * VectorNi swizzle
81-
let vector2i = Vector2i::new(1, 2);
82-
let vector3i = Vector3i::new(1, 2, 3);
83-
let vector4i = Vector4i::new(1, 2, 3, 4);
84-
// VectorNi to Vector2i
85-
assert_eq!(Vector2i::new(2, 1), swizzle!(vector2i => y, x));
86-
assert_eq!(Vector2i::new(2, 1), swizzle!(vector3i => y, x));
87-
assert_eq!(Vector2i::new(2, 1), swizzle!(vector4i => y, x));
88-
// VectorNi to Vector3i
89-
assert_eq!(Vector3i::new(2, 1, 1), swizzle!(vector2i => y, x, x));
90-
assert_eq!(Vector3i::new(2, 1, 3), swizzle!(vector3i => y, x, z));
91-
assert_eq!(Vector3i::new(2, 1, 3), swizzle!(vector4i => y, x, z));
92-
// VectorNi to Vector4i
93-
assert_eq!(Vector4i::new(2, 1, 1, 2), swizzle!(vector2i => y, x, x, y));
94-
assert_eq!(Vector4i::new(2, 1, 3, 2), swizzle!(vector3i => y, x, z, y));
95-
assert_eq!(Vector4i::new(2, 1, 3, 4), swizzle!(vector4i => y, x, z, w));
96-
}
97-
}
9+
mod vector2;
10+
mod vector2i;
11+
mod vector3;
12+
mod vector3i;
13+
mod vector4;
14+
mod vector4i;
15+
mod vector_axis;
16+
17+
pub use vector2::*;
18+
pub use vector2i::*;
19+
pub use vector3::*;
20+
pub use vector3i::*;
21+
pub use vector4::*;
22+
pub use vector4i::*;
23+
pub use vector_axis::*;
24+
25+
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
@@ -15,7 +15,7 @@ use crate::builtin::{inner, Vector2i};
1515
use super::super::glam_helpers::GlamConv;
1616
use super::super::glam_helpers::GlamType;
1717
use super::super::{real, RAffine2, RVec2};
18-
use super::vector_utils::*;
18+
use super::vector_axis::*;
1919

2020
/// Vector used for 2D math using floating point coordinates.
2121
///

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::builtin::Vector3i;
1616
use super::super::glam_helpers::GlamConv;
1717
use super::super::glam_helpers::GlamType;
1818
use super::super::{real, Basis, RVec3};
19-
use super::vector_utils::*;
19+
use super::vector_axis::*;
2020

2121
/// Vector used for 3D math using floating point coordinates.
2222
///

0 commit comments

Comments
 (0)