Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vector{2,3}>` and `Mul<Point{2,3}>` 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

Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"]
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<f32>` is supported, but not `Vector4<MyFloatUnit>`).
- `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

Expand Down
3 changes: 2 additions & 1 deletion src/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
all(not(target_arch = "wasm32"), feature = "wasmtime"),
component(record)
)]
#[cfg_attr(feature = "facet", derive(facet_derive::Facet))]

Check warning on line 27 in src/angle.rs

View check run for this annotation

Codecov / codecov/patch

src/angle.rs#L27

Added line #L27 was not covered by tests
pub struct Angle<U: Scalar = f32> {
/// Angle in radians.
pub radians: U,
Expand Down Expand Up @@ -152,7 +153,7 @@

impl<T> Unit for Angle<T>
where
T: Scalar + AngleConsts,
T: Scalar + AngleConsts + crate::traits::marker::Facet<'static>,
{
type Scalar = T;
}
Expand Down
2 changes: 2 additions & 0 deletions src/box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
all(not(target_arch = "wasm32"), feature = "wasmtime"),
component(record)
)]
#[cfg_attr(feature = "facet", derive(facet_derive::Facet))]

Check warning on line 24 in src/box.rs

View check run for this annotation

Codecov / codecov/patch

src/box.rs#L24

Added line #L24 was not covered by tests
#[repr(C)]
pub struct Box2<U: Unit = f32> {
/// Lower bound of the box.
Expand All @@ -47,6 +48,7 @@
all(not(target_arch = "wasm32"), feature = "wasmtime"),
component(record)
)]
#[cfg_attr(feature = "facet", derive(facet_derive::Facet))]

Check warning on line 51 in src/box.rs

View check run for this annotation

Codecov / codecov/patch

src/box.rs#L51

Added line #L51 was not covered by tests
#[repr(C)]
pub struct Box3<U: Unit = f32> {
/// Lower bound of the box.
Expand Down
3 changes: 3 additions & 0 deletions src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
/// Alignment: Always 16-byte aligned.
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(C)]
#[cfg_attr(feature = "facet", derive(facet_derive::Facet))]

Check warning on line 28 in src/matrix.rs

View check run for this annotation

Codecov / codecov/patch

src/matrix.rs#L28

Added line #L28 was not covered by tests
// #[cfg_attr(feature = "wasmtime", derive(wasmtime::component::ComponentType))]
// #[cfg_attr(feature = "wasmtime", component(record))]
pub struct Matrix2<T: Scalar>(Vector4<T>);
Expand Down Expand Up @@ -56,6 +57,7 @@
all(not(target_arch = "wasm32"), feature = "wasmtime"),
component(record)
)]
#[cfg_attr(feature = "facet", derive(facet_derive::Facet))]

Check warning on line 60 in src/matrix.rs

View check run for this annotation

Codecov / codecov/patch

src/matrix.rs#L60

Added line #L60 was not covered by tests
pub struct Matrix3<U: Scalar> {
#[cfg_attr(
all(not(target_arch = "wasm32"), feature = "wasmtime"),
Expand Down Expand Up @@ -101,6 +103,7 @@
all(not(target_arch = "wasm32"), feature = "wasmtime"),
component(record)
)]
#[cfg_attr(feature = "facet", derive(facet_derive::Facet))]

Check warning on line 106 in src/matrix.rs

View check run for this annotation

Codecov / codecov/patch

src/matrix.rs#L106

Added line #L106 was not covered by tests
pub struct Matrix4<U: Scalar> {
#[cfg_attr(
all(not(target_arch = "wasm32"), feature = "wasmtime"),
Expand Down
2 changes: 2 additions & 0 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
component(record)
)]
#[repr(C)]
#[cfg_attr(feature = "facet", derive(facet_derive::Facet))]

Check warning on line 37 in src/point.rs

View check run for this annotation

Codecov / codecov/patch

src/point.rs#L37

Added line #L37 was not covered by tests
pub struct Point2<U: Unit = f32> {
/// X coordinate
pub x: U::Scalar,
Expand Down Expand Up @@ -66,6 +67,7 @@
all(not(target_arch = "wasm32"), feature = "wasmtime"),
component(record)
)]
#[cfg_attr(feature = "facet", derive(facet_derive::Facet))]

Check warning on line 70 in src/point.rs

View check run for this annotation

Codecov / codecov/patch

src/point.rs#L70

Added line #L70 was not covered by tests
#[repr(C)]
pub struct Point3<U: Unit = f32> {
/// X coordinate
Expand Down
1 change: 1 addition & 0 deletions src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
all(not(target_arch = "wasm32"), feature = "wasmtime"),
component(record)
)]
#[cfg_attr(feature = "facet", derive(facet_derive::Facet))]

Check warning on line 25 in src/rect.rs

View check run for this annotation

Codecov / codecov/patch

src/rect.rs#L25

Added line #L25 was not covered by tests
#[repr(C)]
pub struct Rect<U: Unit = f32> {
/// Lower bound of the rect.
Expand Down
2 changes: 2 additions & 0 deletions src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
all(not(target_arch = "wasm32"), feature = "wasmtime"),
component(record)
)]
#[cfg_attr(feature = "facet", derive(facet_derive::Facet))]

Check warning on line 24 in src/size.rs

View check run for this annotation

Codecov / codecov/patch

src/size.rs#L24

Added line #L24 was not covered by tests
#[repr(C)]
pub struct Size2<U: Unit = f32> {
/// Width
Expand Down Expand Up @@ -51,6 +52,7 @@
component(record)
)]
#[repr(C)]
#[cfg_attr(feature = "facet", derive(facet_derive::Facet))]

Check warning on line 55 in src/size.rs

View check run for this annotation

Codecov / codecov/patch

src/size.rs#L55

Added line #L55 was not covered by tests
pub struct Size3<U: Unit = f32> {
/// Width
pub width: U::Scalar,
Expand Down
10 changes: 10 additions & 0 deletions src/traits/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,13 @@ impl<T> WasmComponentType for T where
}
#[cfg(any(target_arch = "wasm32", not(feature = "wasmtime")))]
impl<T> 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 {}
2 changes: 2 additions & 0 deletions src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
/// 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))]

Check warning on line 24 in src/transform.rs

View check run for this annotation

Codecov / codecov/patch

src/transform.rs#L24

Added line #L24 was not covered by tests
pub struct Transform2<Src: Unit, Dst: Unit> {
/// Underlying matrix.
pub matrix: Matrix3<Src::Scalar>,
Expand All @@ -32,6 +33,7 @@
/// 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))]

Check warning on line 36 in src/transform.rs

View check run for this annotation

Codecov / codecov/patch

src/transform.rs#L36

Added line #L36 was not covered by tests
pub struct Transform3<Src: Unit, Dst: Unit> {
/// Underlying matrix.
pub matrix: Matrix4<Src::Scalar>,
Expand Down
2 changes: 1 addition & 1 deletion src/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 11 additions & 15 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
all(not(target_arch = "wasm32"), feature = "wasmtime"),
component(record)
)]
#[cfg_attr(feature = "facet", derive(facet_derive::Facet))]

Check warning on line 63 in src/vector.rs

View check run for this annotation

Codecov / codecov/patch

src/vector.rs#L63

Added line #L63 was not covered by tests
pub struct Vector2<U: Unit = f32> {
/// X coordinate
pub x: U::Scalar,
Expand Down Expand Up @@ -92,6 +93,7 @@
all(not(target_arch = "wasm32"), feature = "wasmtime"),
component(record)
)]
#[cfg_attr(feature = "facet", derive(facet_derive::Facet))]

Check warning on line 96 in src/vector.rs

View check run for this annotation

Codecov / codecov/patch

src/vector.rs#L96

Added line #L96 was not covered by tests
#[repr(C)]
pub struct Vector3<U: Unit = f32> {
/// X coordinate
Expand Down Expand Up @@ -136,6 +138,7 @@
all(not(target_arch = "wasm32"), feature = "wasmtime"),
component(record)
)]
#[cfg_attr(feature = "facet", derive(facet_derive::Facet))]

Check warning on line 141 in src/vector.rs

View check run for this annotation

Codecov / codecov/patch

src/vector.rs#L141

Added line #L141 was not covered by tests
pub struct Vector4<U: Unit = f32> {
/// X coordinate
pub x: U::Scalar,
Expand Down Expand Up @@ -412,6 +415,7 @@
#[cfg(test)]
mod tests {
use approx::{RelativeEq, UlpsEq, assert_abs_diff_eq};
use core::ptr;

use crate::{Angle, AngleConsts, vec2, vec3, vec4, vector};

Expand Down Expand Up @@ -757,6 +761,7 @@
}

#[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
Expand Down Expand Up @@ -1156,29 +1161,19 @@
let v3 = Vector3::<f32>::new(1.0, 2.0, 3.0);
let v4 = Vector4::<f32>::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<T: core::hash::Hash, H: core::hash::BuildHasher>(
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);

Expand All @@ -1196,9 +1191,10 @@

#[test]
fn hash_equality() {
use core::hash::BuildHasher;
let hasher = core::hash::BuildHasherDefault::<PoorHasher>::default();
let h1 = hash_one(&hasher, Vector2::<i32> { x: 123, y: 456 });
let h2 = hash_one(&hasher, glam::IVec2::new(123, 456));
let h1 = hasher.hash_one(Vector2::<i32> { x: 123, y: 456 });
let h2 = hasher.hash_one(glam::IVec2::new(123, 456));
assert_eq!(h1, h2);
}

Expand Down