From 79911b95090f7bdb221935bfa551c2db4a92b05a Mon Sep 17 00:00:00 2001 From: plule <630159+plule@users.noreply.github.com> Date: Sat, 23 Mar 2024 14:38:06 +0100 Subject: [PATCH] Restore the array access of vectors and quaternions --- src/leap_vector.rs | 25 ++++++++++++++++++------- src/quaternion.rs | 26 +++++++++++++++++++------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/leap_vector.rs b/src/leap_vector.rs index 3755154..291d1e7 100644 --- a/src/leap_vector.rs +++ b/src/leap_vector.rs @@ -1,19 +1,22 @@ -use derive_deref::Deref; +use std::ops::Deref; -use leap_sys::{_LEAP_VECTOR__bindgen_ty_1__bindgen_ty_1, LEAP_VECTOR}; +use leap_sys::{_LEAP_VECTOR__bindgen_ty_1, _LEAP_VECTOR__bindgen_ty_1__bindgen_ty_1, LEAP_VECTOR}; #[doc = " A three element, floating-point vector."] #[doc = " @since 3.0.0"] -#[derive(Deref)] -pub struct LeapVector<'a>(pub(crate) &'a _LEAP_VECTOR__bindgen_ty_1__bindgen_ty_1); +pub struct LeapVector<'a>(pub(crate) &'a _LEAP_VECTOR__bindgen_ty_1); impl<'a> From<&'a LEAP_VECTOR> for LeapVector<'a> { fn from(vector: &'a LEAP_VECTOR) -> Self { - Self(unsafe { &vector.__bindgen_anon_1.__bindgen_anon_1 }) + Self(&vector.__bindgen_anon_1) } } impl<'a> LeapVector<'a> { + pub fn array(&self) -> [f32; 3] { + unsafe { self.0.v } + } + /// Convert to a [glam::Vec3] #[cfg(feature = "glam")] pub fn into_glam(&self) -> glam::Vec3 { @@ -22,11 +25,19 @@ impl<'a> LeapVector<'a> { /// Convert to a [nalgebra::Vector3] #[cfg(feature = "nalgebra")] - pub fn to_nalgebra(&self) -> nalgebra::Vector3 { + pub fn into_nalgebra(&self) -> nalgebra::Vector3 { nalgebra::Vector3::new(self.x, self.y, self.z) } } +impl<'a> Deref for LeapVector<'a> { + type Target = _LEAP_VECTOR__bindgen_ty_1__bindgen_ty_1; + + fn deref(&self) -> &Self::Target { + unsafe { &self.0.__bindgen_anon_1 } + } +} + #[cfg(feature = "glam")] impl From> for glam::Vec3 { fn from(v: LeapVector) -> glam::Vec3 { @@ -37,7 +48,7 @@ impl From> for glam::Vec3 { #[cfg(feature = "nalgebra")] impl From> for nalgebra::Vector3 { fn from(v: LeapVector) -> nalgebra::Vector3 { - v.to_nalgebra() + v.into_nalgebra() } } diff --git a/src/quaternion.rs b/src/quaternion.rs index 6fc5539..5f71281 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -1,17 +1,21 @@ -use derive_deref::Deref; -use leap_sys::{_LEAP_QUATERNION__bindgen_ty_1__bindgen_ty_1, LEAP_QUATERNION}; +use leap_sys::{ + _LEAP_QUATERNION__bindgen_ty_1, _LEAP_QUATERNION__bindgen_ty_1__bindgen_ty_1, LEAP_QUATERNION, +}; #[doc = " A four element, floating point quaternion. @since 3.1.2"] -#[derive(Deref)] -pub struct Quaternion<'a>(pub(crate) &'a _LEAP_QUATERNION__bindgen_ty_1__bindgen_ty_1); +pub struct Quaternion<'a>(pub(crate) &'a _LEAP_QUATERNION__bindgen_ty_1); impl<'a> From<&'a LEAP_QUATERNION> for Quaternion<'a> { fn from(quaternion: &'a LEAP_QUATERNION) -> Self { - Self(unsafe { &quaternion.__bindgen_anon_1.__bindgen_anon_1 }) + Self(&quaternion.__bindgen_anon_1) } } impl<'a> Quaternion<'a> { + pub fn array(&self) -> [f32; 4] { + unsafe { self.0.v } + } + /// Convert to a [glam::Quat] #[cfg(feature = "glam")] pub fn into_glam(&self) -> glam::Quat { @@ -20,13 +24,21 @@ impl<'a> Quaternion<'a> { /// Convert to a [nalgebra::UnitQuaternion] #[cfg(feature = "nalgebra")] - pub fn to_nalgebra(&self) -> nalgebra::UnitQuaternion { + pub fn into_nalgebra(&self) -> nalgebra::UnitQuaternion { nalgebra::UnitQuaternion::new_unchecked(nalgebra::Quaternion::new( self.w, self.x, self.y, self.z, )) } } +impl<'a> core::ops::Deref for Quaternion<'a> { + type Target = _LEAP_QUATERNION__bindgen_ty_1__bindgen_ty_1; + + fn deref(&self) -> &Self::Target { + unsafe { &self.0.__bindgen_anon_1 } + } +} + #[cfg(feature = "glam")] impl From> for glam::Quat { fn from(q: Quaternion) -> glam::Quat { @@ -37,6 +49,6 @@ impl From> for glam::Quat { #[cfg(feature = "nalgebra")] impl From> for nalgebra::UnitQuaternion { fn from(q: Quaternion) -> nalgebra::UnitQuaternion { - q.to_nalgebra() + q.into_nalgebra() } }