Skip to content

Commit

Permalink
Make scalar quaternion components public instead of using Deref.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bitshifter committed Jun 21, 2022
1 parent 12aa60e commit 3746251
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
23 changes: 17 additions & 6 deletions codegen/templates/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ use core::arch::wasm32::*;
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
use core::ops::{Add, Deref, Div, Mul, MulAssign, Neg, Sub};
use core::ops::{
{% if not is_scalar %}
Deref, DerefMut,
{% endif %}
Add, Div, Mul, MulAssign, Neg, Sub
};

{% if is_sse2 %}
union UnionCast {
Expand Down Expand Up @@ -90,10 +95,10 @@ pub const fn {{ self_t | lower }}(x: {{ scalar_t }}, y: {{ scalar_t }}, z: {{ sc
#[cfg_attr(not(any(feature = "scalar-math", target_arch = "spirv")), repr(C, align(16)))]
{%- endif %}
pub struct {{ self_t }}{
x: {{ scalar_t }},
y: {{ scalar_t }},
z: {{ scalar_t }},
w: {{ scalar_t }},
pub x: {{ scalar_t }},
pub y: {{ scalar_t }},
pub z: {{ scalar_t }},
pub w: {{ scalar_t }},
}
{%- else %}
#[repr(transparent)]
Expand Down Expand Up @@ -1135,7 +1140,6 @@ impl From<{{ self_t }}> for {{ simd_t }} {
{% endif %}
}
}
{% endif %}

impl Deref for {{ self_t }} {
type Target = crate::deref::Vec4<{{ scalar_t }}>;
Expand All @@ -1145,3 +1149,10 @@ impl Deref for {{ self_t }} {
}
}

impl DerefMut for {{ self_t }} {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self as *mut Self).cast() }
}
}
{% endif %}
18 changes: 5 additions & 13 deletions src/f32/scalar/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use num_traits::Float;
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
use core::ops::{Add, Deref, Div, Mul, MulAssign, Neg, Sub};
use core::ops::{Add, Div, Mul, MulAssign, Neg, Sub};

/// Creates a quaternion from `x`, `y`, `z` and `w` values.
///
Expand All @@ -33,10 +33,10 @@ pub const fn quat(x: f32, y: f32, z: f32, w: f32) -> Quat {
repr(C, align(16))
)]
pub struct Quat {
x: f32,
y: f32,
z: f32,
w: f32,
pub x: f32,
pub y: f32,
pub z: f32,
pub w: f32,
}

impl Quat {
Expand Down Expand Up @@ -844,11 +844,3 @@ impl From<Quat> for [f32; 4] {
[q.x, q.y, q.z, q.w]
}
}

impl Deref for Quat {
type Target = crate::deref::Vec4<f32>;
#[inline]
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const Self).cast() }
}
}
9 changes: 8 additions & 1 deletion src/f32/sse2/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use core::arch::x86_64::*;
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
use core::ops::{Add, Deref, Div, Mul, MulAssign, Neg, Sub};
use core::ops::{Add, Deref, DerefMut, Div, Mul, MulAssign, Neg, Sub};

union UnionCast {
a: [f32; 4],
Expand Down Expand Up @@ -909,3 +909,10 @@ impl Deref for Quat {
unsafe { &*(self as *const Self).cast() }
}
}

impl DerefMut for Quat {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self as *mut Self).cast() }
}
}
9 changes: 8 additions & 1 deletion src/f32/wasm32/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use core::arch::wasm32::*;
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
use core::ops::{Add, Deref, Div, Mul, MulAssign, Neg, Sub};
use core::ops::{Add, Deref, DerefMut, Div, Mul, MulAssign, Neg, Sub};

/// Creates a quaternion from `x`, `y`, `z` and `w` values.
///
Expand Down Expand Up @@ -899,3 +899,10 @@ impl Deref for Quat {
unsafe { &*(self as *const Self).cast() }
}
}

impl DerefMut for Quat {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self as *mut Self).cast() }
}
}
18 changes: 5 additions & 13 deletions src/f64/dquat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use num_traits::Float;
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
use core::ops::{Add, Deref, Div, Mul, MulAssign, Neg, Sub};
use core::ops::{Add, Div, Mul, MulAssign, Neg, Sub};

/// Creates a quaternion from `x`, `y`, `z` and `w` values.
///
Expand All @@ -29,10 +29,10 @@ pub const fn dquat(x: f64, y: f64, z: f64, w: f64) -> DQuat {
/// operations are applied.
#[derive(Clone, Copy)]
pub struct DQuat {
x: f64,
y: f64,
z: f64,
w: f64,
pub x: f64,
pub y: f64,
pub z: f64,
pub w: f64,
}

impl DQuat {
Expand Down Expand Up @@ -826,11 +826,3 @@ impl From<DQuat> for [f64; 4] {
[q.x, q.y, q.z, q.w]
}
}

impl Deref for DQuat {
type Target = crate::deref::Vec4<f64>;
#[inline]
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const Self).cast() }
}
}

0 comments on commit 3746251

Please sign in to comment.