Skip to content

Commit 3746251

Browse files
committed
Make scalar quaternion components public instead of using Deref.
Deref was being used to allow read only access to quaternion components, however this doesn't work on `spirv` targets so just making them public instead.
1 parent 12aa60e commit 3746251

File tree

5 files changed

+43
-34
lines changed

5 files changed

+43
-34
lines changed

codegen/templates/quat.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ use core::arch::wasm32::*;
5757
#[cfg(not(target_arch = "spirv"))]
5858
use core::fmt;
5959
use core::iter::{Product, Sum};
60-
use core::ops::{Add, Deref, Div, Mul, MulAssign, Neg, Sub};
60+
use core::ops::{
61+
{% if not is_scalar %}
62+
Deref, DerefMut,
63+
{% endif %}
64+
Add, Div, Mul, MulAssign, Neg, Sub
65+
};
6166

6267
{% if is_sse2 %}
6368
union UnionCast {
@@ -90,10 +95,10 @@ pub const fn {{ self_t | lower }}(x: {{ scalar_t }}, y: {{ scalar_t }}, z: {{ sc
9095
#[cfg_attr(not(any(feature = "scalar-math", target_arch = "spirv")), repr(C, align(16)))]
9196
{%- endif %}
9297
pub struct {{ self_t }}{
93-
x: {{ scalar_t }},
94-
y: {{ scalar_t }},
95-
z: {{ scalar_t }},
96-
w: {{ scalar_t }},
98+
pub x: {{ scalar_t }},
99+
pub y: {{ scalar_t }},
100+
pub z: {{ scalar_t }},
101+
pub w: {{ scalar_t }},
97102
}
98103
{%- else %}
99104
#[repr(transparent)]
@@ -1135,7 +1140,6 @@ impl From<{{ self_t }}> for {{ simd_t }} {
11351140
{% endif %}
11361141
}
11371142
}
1138-
{% endif %}
11391143

11401144
impl Deref for {{ self_t }} {
11411145
type Target = crate::deref::Vec4<{{ scalar_t }}>;
@@ -1145,3 +1149,10 @@ impl Deref for {{ self_t }} {
11451149
}
11461150
}
11471151

1152+
impl DerefMut for {{ self_t }} {
1153+
#[inline]
1154+
fn deref_mut(&mut self) -> &mut Self::Target {
1155+
unsafe { &mut *(self as *mut Self).cast() }
1156+
}
1157+
}
1158+
{% endif %}

src/f32/scalar/quat.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use num_traits::Float;
1111
#[cfg(not(target_arch = "spirv"))]
1212
use core::fmt;
1313
use core::iter::{Product, Sum};
14-
use core::ops::{Add, Deref, Div, Mul, MulAssign, Neg, Sub};
14+
use core::ops::{Add, Div, Mul, MulAssign, Neg, Sub};
1515

1616
/// Creates a quaternion from `x`, `y`, `z` and `w` values.
1717
///
@@ -33,10 +33,10 @@ pub const fn quat(x: f32, y: f32, z: f32, w: f32) -> Quat {
3333
repr(C, align(16))
3434
)]
3535
pub struct Quat {
36-
x: f32,
37-
y: f32,
38-
z: f32,
39-
w: f32,
36+
pub x: f32,
37+
pub y: f32,
38+
pub z: f32,
39+
pub w: f32,
4040
}
4141

4242
impl Quat {
@@ -844,11 +844,3 @@ impl From<Quat> for [f32; 4] {
844844
[q.x, q.y, q.z, q.w]
845845
}
846846
}
847-
848-
impl Deref for Quat {
849-
type Target = crate::deref::Vec4<f32>;
850-
#[inline]
851-
fn deref(&self) -> &Self::Target {
852-
unsafe { &*(self as *const Self).cast() }
853-
}
854-
}

src/f32/sse2/quat.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use core::arch::x86_64::*;
1616
#[cfg(not(target_arch = "spirv"))]
1717
use core::fmt;
1818
use core::iter::{Product, Sum};
19-
use core::ops::{Add, Deref, Div, Mul, MulAssign, Neg, Sub};
19+
use core::ops::{Add, Deref, DerefMut, Div, Mul, MulAssign, Neg, Sub};
2020

2121
union UnionCast {
2222
a: [f32; 4],
@@ -909,3 +909,10 @@ impl Deref for Quat {
909909
unsafe { &*(self as *const Self).cast() }
910910
}
911911
}
912+
913+
impl DerefMut for Quat {
914+
#[inline]
915+
fn deref_mut(&mut self) -> &mut Self::Target {
916+
unsafe { &mut *(self as *mut Self).cast() }
917+
}
918+
}

src/f32/wasm32/quat.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use core::arch::wasm32::*;
1313
#[cfg(not(target_arch = "spirv"))]
1414
use core::fmt;
1515
use core::iter::{Product, Sum};
16-
use core::ops::{Add, Deref, Div, Mul, MulAssign, Neg, Sub};
16+
use core::ops::{Add, Deref, DerefMut, Div, Mul, MulAssign, Neg, Sub};
1717

1818
/// Creates a quaternion from `x`, `y`, `z` and `w` values.
1919
///
@@ -899,3 +899,10 @@ impl Deref for Quat {
899899
unsafe { &*(self as *const Self).cast() }
900900
}
901901
}
902+
903+
impl DerefMut for Quat {
904+
#[inline]
905+
fn deref_mut(&mut self) -> &mut Self::Target {
906+
unsafe { &mut *(self as *mut Self).cast() }
907+
}
908+
}

src/f64/dquat.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use num_traits::Float;
1111
#[cfg(not(target_arch = "spirv"))]
1212
use core::fmt;
1313
use core::iter::{Product, Sum};
14-
use core::ops::{Add, Deref, Div, Mul, MulAssign, Neg, Sub};
14+
use core::ops::{Add, Div, Mul, MulAssign, Neg, Sub};
1515

1616
/// Creates a quaternion from `x`, `y`, `z` and `w` values.
1717
///
@@ -29,10 +29,10 @@ pub const fn dquat(x: f64, y: f64, z: f64, w: f64) -> DQuat {
2929
/// operations are applied.
3030
#[derive(Clone, Copy)]
3131
pub struct DQuat {
32-
x: f64,
33-
y: f64,
34-
z: f64,
35-
w: f64,
32+
pub x: f64,
33+
pub y: f64,
34+
pub z: f64,
35+
pub w: f64,
3636
}
3737

3838
impl DQuat {
@@ -826,11 +826,3 @@ impl From<DQuat> for [f64; 4] {
826826
[q.x, q.y, q.z, q.w]
827827
}
828828
}
829-
830-
impl Deref for DQuat {
831-
type Target = crate::deref::Vec4<f64>;
832-
#[inline]
833-
fn deref(&self) -> &Self::Target {
834-
unsafe { &*(self as *const Self).cast() }
835-
}
836-
}

0 commit comments

Comments
 (0)