From 51f5d04ff612c99e8af6c01c1946840ec6d050aa Mon Sep 17 00:00:00 2001 From: Simon Ask Ulsnes Date: Sat, 3 May 2025 09:14:10 +0200 Subject: [PATCH 1/3] Some lints --- src/vector.rs | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/vector.rs b/src/vector.rs index c3cefd0..16d6e2d 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -412,6 +412,7 @@ impl core::fmt::Debug for Vector4 { #[cfg(test)] mod tests { use approx::{RelativeEq, UlpsEq, assert_abs_diff_eq}; + use core::ptr; use crate::{Angle, AngleConsts, vec2, vec3, vec4, vector}; @@ -757,6 +758,7 @@ mod tests { } #[test] + #[allow(clippy::many_single_char_names)] fn scaling_by_scalar() { // Test that vector types can be multiplied/divided by their // (unsplatted) scalar. This doesn't work in generic code, but it should @@ -1156,29 +1158,19 @@ mod tests { let v3 = Vector3::::new(1.0, 2.0, 3.0); let v4 = Vector4::::new(1.0, 2.0, 3.0, 4.0); - assert_eq!(v2.as_raw() as *const _, peel_ref(&v2) as *const _); + assert!(ptr::eq(v2.as_raw(), peel_ref(&v2))); assert_eq!(v2.to_raw(), glam::Vec2::new(1.0, 2.0)); assert_eq!(v2, Vector2::from_raw(glam::Vec2::new(1.0, 2.0))); - assert_eq!(v3.as_raw() as *const _, peel_ref(&v3) as *const _); + assert!(ptr::eq(v3.as_raw(), peel_ref(&v3))); assert_eq!(v3.to_raw(), glam::Vec3::new(1.0, 2.0, 3.0)); assert_eq!(v3, Vector3::from_raw(glam::Vec3::new(1.0, 2.0, 3.0))); - assert_eq!(v4.as_raw() as *const _, peel_ref(&v4) as *const _); + assert!(ptr::eq(v4.as_raw(), peel_ref(&v4))); assert_eq!(v4.to_raw(), glam::Vec4::new(1.0, 2.0, 3.0, 4.0)); assert_eq!(v4, Vector4::from_raw(glam::Vec4::new(1.0, 2.0, 3.0, 4.0))); } - fn hash_one( - build_hasher: &H, - value: T, - ) -> u64 { - use core::hash::Hasher; - let mut h = build_hasher.build_hasher(); - value.hash(&mut h); - h.finish() - } - #[derive(Default)] struct PoorHasher(u64); @@ -1196,9 +1188,10 @@ mod tests { #[test] fn hash_equality() { + use core::hash::BuildHasher; let hasher = core::hash::BuildHasherDefault::::default(); - let h1 = hash_one(&hasher, Vector2:: { x: 123, y: 456 }); - let h2 = hash_one(&hasher, glam::IVec2::new(123, 456)); + let h1 = hasher.hash_one(Vector2:: { x: 123, y: 456 }); + let h2 = hasher.hash_one(glam::IVec2::new(123, 456)); assert_eq!(h1, h2); } From 39a8c4b69b166dac1a93c260d48540091acf63ca Mon Sep 17 00:00:00 2001 From: Simon Ask Ulsnes Date: Sat, 3 May 2025 09:23:23 +0200 Subject: [PATCH 2/3] Experimental support for Facet --- CHANGELOG.md | 1 + Cargo.toml | 3 +++ README.md | 4 ++++ src/angle.rs | 3 ++- src/box.rs | 2 ++ src/matrix.rs | 3 +++ src/point.rs | 2 ++ src/rect.rs | 1 + src/size.rs | 2 ++ src/traits/marker.rs | 11 +++++++++++ src/transform.rs | 2 ++ src/unit.rs | 2 +- src/vector.rs | 3 +++ 13 files changed, 37 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dbbfc26..dcd034f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Implement scalar `Point` operations (#71). - Implement `Mul` and `Mul` for `Matrix{3,4}`. - Implemented all the methods that exist on `Box2` for `Box3` as well. +- Experimental support for [`facet`](https://docs.rs/facet/latest/facet/). ### Breaking changes diff --git a/Cargo.toml b/Cargo.toml index f190ea8..ac83315 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,8 @@ glam = { version = "0.30.0", default-features = false, features = [ bytemuck = { version = "^1.8", default-features = false } num-traits = { version = "^0.2.19", default-features = false } approx = "^0.5" +facet = { version = "0.18.4", optional = true, default-features = false } +facet-derive = { version = "0.18.4", optional = true } [dependencies.serde] version = "^1.0" @@ -62,6 +64,7 @@ std = ["glam/std", "num-traits/std"] scalar-math = ["glam/scalar-math"] serde = ["dep:serde", "glam/serde"] core-simd = ["glam/core-simd"] +facet = ["dep:facet", "dep:facet-derive"] # Enable conversions to `mint` types mint = ["dep:mint", "glam/mint"] diff --git a/README.md b/README.md index 5788c7b..e023e67 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,10 @@ let vector_raw: &glam::Vec4 = glamour::Transparent::peel_ref(&vector); compatible type declaration in a WIT world, but limitations apply: Due to the way the `wasmtime` derive macros work, only plain scalar units can be used (so `Vector4` is supported, but not `Vector4`). +- `facet`: **(Experimental)** Adds support for + [`facet`](https://docs.rs/facet/latest/facet/) to all types. + +[`wasmtime::component::bindgen!()`]: https://docs.rs/wasmtime/latest/wasmtime/component/macro.bindgen.html # Advantages diff --git a/src/angle.rs b/src/angle.rs index eb2367a..d34e693 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -24,6 +24,7 @@ use crate::{ all(not(target_arch = "wasm32"), feature = "wasmtime"), component(record) )] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] pub struct Angle { /// Angle in radians. pub radians: U, @@ -152,7 +153,7 @@ impl core::fmt::Debug for Angle { impl Unit for Angle where - T: Scalar + AngleConsts, + T: Scalar + AngleConsts + crate::traits::marker::Facet<'static>, { type Scalar = T; } diff --git a/src/box.rs b/src/box.rs index dc23daf..46a5174 100644 --- a/src/box.rs +++ b/src/box.rs @@ -21,6 +21,7 @@ use crate::{ all(not(target_arch = "wasm32"), feature = "wasmtime"), component(record) )] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] #[repr(C)] pub struct Box2 { /// Lower bound of the box. @@ -47,6 +48,7 @@ unsafe impl bytemuck::Zeroable for Box2 {} all(not(target_arch = "wasm32"), feature = "wasmtime"), component(record) )] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] #[repr(C)] pub struct Box3 { /// Lower bound of the box. diff --git a/src/matrix.rs b/src/matrix.rs index a6945a7..40f8168 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -25,6 +25,7 @@ use bytemuck::{Pod, Zeroable}; /// Alignment: Always 16-byte aligned. #[derive(Clone, Copy, PartialEq, Eq)] #[repr(C)] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] // #[cfg_attr(feature = "wasmtime", derive(wasmtime::component::ComponentType))] // #[cfg_attr(feature = "wasmtime", component(record))] pub struct Matrix2(Vector4); @@ -56,6 +57,7 @@ unsafe impl Transparent for Matrix2 { all(not(target_arch = "wasm32"), feature = "wasmtime"), component(record) )] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] pub struct Matrix3 { #[cfg_attr( all(not(target_arch = "wasm32"), feature = "wasmtime"), @@ -101,6 +103,7 @@ unsafe impl Transparent for Matrix3 { all(not(target_arch = "wasm32"), feature = "wasmtime"), component(record) )] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] pub struct Matrix4 { #[cfg_attr( all(not(target_arch = "wasm32"), feature = "wasmtime"), diff --git a/src/point.rs b/src/point.rs index d561f5d..0b5abf9 100644 --- a/src/point.rs +++ b/src/point.rs @@ -34,6 +34,7 @@ use core::ops::Mul; component(record) )] #[repr(C)] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] pub struct Point2 { /// X coordinate pub x: U::Scalar, @@ -66,6 +67,7 @@ unsafe impl Transparent for Point2 { all(not(target_arch = "wasm32"), feature = "wasmtime"), component(record) )] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] #[repr(C)] pub struct Point3 { /// X coordinate diff --git a/src/rect.rs b/src/rect.rs index 3930a5e..efc7b60 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -22,6 +22,7 @@ use crate::{ all(not(target_arch = "wasm32"), feature = "wasmtime"), component(record) )] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] #[repr(C)] pub struct Rect { /// Lower bound of the rect. diff --git a/src/size.rs b/src/size.rs index ff9aa42..f15bec7 100644 --- a/src/size.rs +++ b/src/size.rs @@ -21,6 +21,7 @@ use num_traits::{ConstOne, ConstZero}; all(not(target_arch = "wasm32"), feature = "wasmtime"), component(record) )] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] #[repr(C)] pub struct Size2 { /// Width @@ -51,6 +52,7 @@ unsafe impl Transparent for Size2 { component(record) )] #[repr(C)] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] pub struct Size3 { /// Width pub width: U::Scalar, diff --git a/src/traits/marker.rs b/src/traits/marker.rs index 1710cfe..74f7d5b 100644 --- a/src/traits/marker.rs +++ b/src/traits/marker.rs @@ -61,3 +61,14 @@ impl WasmComponentType for T where } #[cfg(any(target_arch = "wasm32", not(feature = "wasmtime")))] impl WasmComponentType for T {} + + +#[cfg(feature = "facet")] +#[doc(no_inline)] +pub use facet::Facet; + +/// Marker trait for when `#[cfg(feature = "facet")]` is not enabled. +#[cfg(not(feature = "facet"))] +pub trait Facet<'a> {} +#[cfg(not(feature = "facet"))] +impl<'a, T: 'a> Facet<'a> for T {} diff --git a/src/transform.rs b/src/transform.rs index 44f8dd7..5359ebd 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -21,6 +21,7 @@ use crate::{ /// This is a strongly typed wrapper around a [`Matrix3`], where that matrix /// describes how to map between units. #[repr(transparent)] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] pub struct Transform2 { /// Underlying matrix. pub matrix: Matrix3, @@ -32,6 +33,7 @@ pub struct Transform2 { /// This is a strongly typed wrapper around a [`Matrix4`], where that matrix /// describes how to map between units. #[repr(transparent)] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] pub struct Transform3 { /// Underlying matrix. pub matrix: Matrix4, diff --git a/src/unit.rs b/src/unit.rs index 8a391b9..f3ef723 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -32,7 +32,7 @@ use super::Scalar; pub trait Unit: 'static { /// One of the vector component types of glam: `f32`, `f64`, `i32`, or /// `u32`. - type Scalar: Scalar; + type Scalar: Scalar + crate::traits::marker::Facet<'static>; } /// Convenience trait implemented for all [`Unit`]s with an integer scalar type. diff --git a/src/vector.rs b/src/vector.rs index 16d6e2d..536da2d 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -60,6 +60,7 @@ pub trait Swizzle { all(not(target_arch = "wasm32"), feature = "wasmtime"), component(record) )] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] pub struct Vector2 { /// X coordinate pub x: U::Scalar, @@ -92,6 +93,7 @@ unsafe impl Transparent for Vector2 { all(not(target_arch = "wasm32"), feature = "wasmtime"), component(record) )] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] #[repr(C)] pub struct Vector3 { /// X coordinate @@ -136,6 +138,7 @@ unsafe impl Transparent for Vector3 { all(not(target_arch = "wasm32"), feature = "wasmtime"), component(record) )] +#[cfg_attr(feature = "facet", derive(facet_derive::Facet))] pub struct Vector4 { /// X coordinate pub x: U::Scalar, From 94ff51177a8d7ca3b861936a54edde8b99ffc83c Mon Sep 17 00:00:00 2001 From: Simon Ask Ulsnes Date: Sat, 3 May 2025 09:27:12 +0200 Subject: [PATCH 3/3] cargo fmt --- src/traits/marker.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/traits/marker.rs b/src/traits/marker.rs index 74f7d5b..228777a 100644 --- a/src/traits/marker.rs +++ b/src/traits/marker.rs @@ -62,7 +62,6 @@ impl WasmComponentType for T where #[cfg(any(target_arch = "wasm32", not(feature = "wasmtime")))] impl WasmComponentType for T {} - #[cfg(feature = "facet")] #[doc(no_inline)] pub use facet::Facet;