From 7b33b22509ca1fcbdb72621017b3f98278d3ba1e Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Sat, 13 Sep 2025 00:04:34 +0200 Subject: [PATCH] Add `encase` trait implementations --- CHANGELOG.md | 1 + Cargo.toml | 2 ++ README.md | 1 + src/impl_encase.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 4 +++ 5 files changed, 83 insertions(+) create mode 100644 src/impl_encase.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index fbdb42b..279e603 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Upgrade to edition 2024 - Minor implementation updates - Minor documentation fixes +- Added `encase` feature, providing `encase` trait implementations for `ultraviolet` types. ## 0.10.0 diff --git a/Cargo.toml b/Cargo.toml index 5912b34..1614194 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ serde = { version = "1.0", features = [], optional = true } mint = { version = "0.5", optional = true } bytemuck = { version = "1.22", optional = true } num-traits = { version = "0.2", optional = true } +encase = { version = "0.12", optional = true } [features] default = ["int"] @@ -28,6 +29,7 @@ serde = ["dep:serde"] mint = ["dep:mint"] bytemuck = ["dep:bytemuck"] num-traits = ["dep:num-traits"] +encase = ["dep:encase"] [dev-dependencies] serde_test = "1.0" diff --git a/README.md b/README.md index 899f794..1322be1 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ Here's a list of the available features: * `mint` – Enable interoperation with other math crates through the `mint` interface. * `num-traits` – Enable [identity traits](https://docs.rs/num-traits/latest/num_traits/identities/index.html) for interoperation with other math crates. * `serde` – Enable `Serialize` and `Deserialize` implementations for many scalar types. +* `encase` – Enable `encase` trait implementations for `ultraviolet` types. ## Crate Features diff --git a/src/impl_encase.rs b/src/impl_encase.rs new file mode 100644 index 0000000..4d07ef1 --- /dev/null +++ b/src/impl_encase.rs @@ -0,0 +1,75 @@ +use crate::{IVec2, IVec3, IVec4, Mat2, Mat3, Mat4, UVec2, UVec3, UVec4, Vec2, Vec3, Vec4}; +use encase::{ + matrix::{AsMutMatrixParts, AsRefMatrixParts, impl_matrix}, + vector::{AsMutVectorParts, AsRefVectorParts, impl_vector}, +}; + +impl_vector!(2, Vec2, f32; using From); +impl_vector!(2, UVec2, u32; using From); +impl_vector!(2, IVec2, i32; using From); + +impl_vector!(3, Vec3, f32; using From); +impl_vector!(3, UVec3, u32; using From); +impl_vector!(3, IVec3, i32; using From); + +impl_vector!(4, Vec4, f32; using From); +impl_vector!(4, UVec4, u32; using From); +impl_vector!(4, IVec4, i32; using From); + +impl_matrix!(2, 2, Mat2, f32; using From); +impl_matrix!(3, 3, Mat3, f32; using From); +impl_matrix!(4, 4, Mat4, f32; using From); + +macro_rules! impl_vector_traits { + ($n:literal, $type:ty, $el_ty:ty) => { + impl AsRefVectorParts<$el_ty, $n> for $type { + fn as_ref_parts(&self) -> &[$el_ty; $n] { + self.as_slice().try_into().unwrap() + } + } + impl AsMutVectorParts<$el_ty, $n> for $type { + fn as_mut_parts(&mut self) -> &mut [$el_ty; $n] { + self.as_mut_slice().try_into().unwrap() + } + } + }; +} + +impl_vector_traits!(2, Vec2, f32); +impl_vector_traits!(2, UVec2, u32); +impl_vector_traits!(2, IVec2, i32); + +impl_vector_traits!(3, Vec3, f32); +impl_vector_traits!(3, UVec3, u32); +impl_vector_traits!(3, IVec3, i32); + +impl_vector_traits!(4, Vec4, f32); +impl_vector_traits!(4, UVec4, u32); +impl_vector_traits!(4, IVec4, i32); + +macro_rules! impl_matrix_traits { + ($c:literal, $r:literal, $type:ty, $el_ty:ty) => { + impl AsRefMatrixParts<$el_ty, $c, $r> for $type { + fn as_ref_parts(&self) -> &[[$el_ty; $r]; $c] { + unsafe { + ::core::mem::transmute::<&[$el_ty; $r * $c], &[[$el_ty; $r]; $c]>( + self.as_array(), + ) + } + } + } + impl AsMutMatrixParts<$el_ty, $c, $r> for $type { + fn as_mut_parts(&mut self) -> &mut [[$el_ty; $r]; $c] { + unsafe { + ::core::mem::transmute::<&mut [$el_ty; $r * $c], &mut [[$el_ty; $r]; $c]>( + self.as_mut_array(), + ) + } + } + } + }; +} + +impl_matrix_traits!(2, 2, Mat2, f32); +impl_matrix_traits!(3, 3, Mat3, f32); +impl_matrix_traits!(4, 4, Mat4, f32); diff --git a/src/lib.rs b/src/lib.rs index fb87c82..7fc8be7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,6 +43,7 @@ //! * `mint` – Enable interoperation with other math crates through the `mint` interface. //! * `num-traits` – Enable [identity traits](https://docs.rs/num-traits/latest/num_traits/identities/index.html) for interoperation with other math crates. //! * `serde` – Enable `Serialize` and `Deserialize` implementations for many scalar types. +//! * `encase` – Enable `encase` trait implementations for `ultraviolet` types. //! //! ## Crate Features //! @@ -110,6 +111,9 @@ mod impl_mint; #[cfg(feature = "bytemuck")] mod impl_bytemuck; +#[cfg(feature = "encase")] +mod impl_encase; + pub use bivec::*; #[cfg(feature = "int")] pub use conversion::*;