Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MallocSizeOf for euclid types #541

Merged
merged 5 commits into from
Feb 19, 2025
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
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ jobs:
strategy:
matrix:
features: ["", "--features serde", "--no-default-features --features libm"]
version: ["1.56.0", "stable", "beta", "nightly"]
version: ["1.63.0", "stable", "beta", "nightly"]
include:
- version: stable
features: --features mint
- version: stable
features: --features bytemuck
- version: stable
features: --features arbitrary
- version: stable
features: --features malloc_size_of
- version: nightly
features: --features unstable
- version: nightly
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "euclid"
version = "0.22.11"
version = "0.22.12"
authors = ["The Servo Project Developers"]
edition = "2021"
rust-version = "1.56.0"
rust-version = "1.63.0"
description = "Geometry primitives"
documentation = "https://docs.rs/euclid/"
repository = "https://github.com/servo/euclid"
Expand All @@ -20,6 +20,7 @@ libm = ["num-traits/libm"]
[dependencies]
num-traits = { version = "0.2.15", default-features = false }
serde = { version = "1.0", default-features = false, features = ["serde_derive"], optional = true }
malloc_size_of = { version = "0.1", default-features = false, optional = true }
mint = { version = "0.5.1", optional = true }
arbitrary = { version = "1", optional = true }
bytemuck = { version = "1.9", optional = true }
Expand Down
9 changes: 9 additions & 0 deletions src/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, Sub, S

#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};
#[cfg(feature = "malloc_size_of")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use num_traits::real::Real;
use num_traits::{Float, FloatConst, NumCast, One, Zero};
#[cfg(feature = "serde")]
Expand Down Expand Up @@ -52,6 +54,13 @@ where
}
}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf> MallocSizeOf for Angle<T> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.radians.size_of(ops)
}
}

impl<T> Angle<T> {
#[inline]
pub fn radians(radians: T) -> Self {
Expand Down
9 changes: 9 additions & 0 deletions src/box2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use crate::vector::{vec2, Vector2D};

#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};
#[cfg(feature = "malloc_size_of")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use num_traits::{Float, NumCast};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -151,6 +153,13 @@ impl<T, U> Box2D<T, U> {
}
}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, U> MallocSizeOf for Box2D<T, U> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.min.size_of(ops) + self.max.size_of(ops)
}
}

impl<T, U> Box2D<T, U>
where
T: PartialOrd,
Expand Down
9 changes: 9 additions & 0 deletions src/box3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use crate::vector::Vector3D;

#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};
#[cfg(feature = "malloc_size_of")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use num_traits::{Float, NumCast};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -90,6 +92,13 @@ unsafe impl<T: Zeroable, U> Zeroable for Box3D<T, U> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: Pod, U: 'static> Pod for Box3D<T, U> {}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, U> MallocSizeOf for Box3D<T, U> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.min.size_of(ops) + self.max.size_of(ops)
}
}

impl<T, U> Box3D<T, U> {
/// Constructor.
#[inline]
Expand Down
9 changes: 9 additions & 0 deletions src/homogen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use core::fmt;
use core::hash::Hash;
use core::marker::PhantomData;
use core::ops::Div;
#[cfg(feature = "malloc_size_of")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
#[cfg(feature = "serde")]
use serde;

Expand Down Expand Up @@ -103,6 +105,13 @@ unsafe impl<T: Zeroable, U> Zeroable for HomogeneousVector<T, U> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: Pod, U: 'static> Pod for HomogeneousVector<T, U> {}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, U> MallocSizeOf for HomogeneousVector<T, U> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.x.size_of(ops) + self.y.size_of(ops) + self.z.size_of(ops) + self.w.size_of(ops)
}
}

impl<T, U> Eq for HomogeneousVector<T, U> where T: Eq {}

impl<T, U> PartialEq for HomogeneousVector<T, U>
Expand Down
9 changes: 9 additions & 0 deletions src/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use core::iter::Sum;
use core::marker::PhantomData;
use core::ops::{Add, Div, Mul, Neg, Sub};
use core::ops::{AddAssign, DivAssign, MulAssign, SubAssign};
#[cfg(feature = "malloc_size_of")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use num_traits::{NumCast, Saturating};
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
Expand Down Expand Up @@ -91,6 +93,13 @@ unsafe impl<T: Zeroable, U> Zeroable for Length<T, U> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: Pod, U: 'static> Pod for Length<T, U> {}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, U> MallocSizeOf for Length<T, U> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.0.size_of(ops)
}
}

impl<T, U> Length<T, U> {
/// Associate a value with a unit of measure.
#[inline]
Expand Down
16 changes: 16 additions & 0 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use core::fmt;
use core::hash::Hash;
use core::marker::PhantomData;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
#[cfg(feature = "malloc_size_of")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
#[cfg(feature = "mint")]
use mint;
use num_traits::real::Real;
Expand Down Expand Up @@ -103,6 +105,13 @@ unsafe impl<T: Zeroable, U> Zeroable for Point2D<T, U> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: Pod, U: 'static> Pod for Point2D<T, U> {}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, U> MallocSizeOf for Point2D<T, U> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.x.size_of(ops) + self.y.size_of(ops)
}
}

impl<T, U> Eq for Point2D<T, U> where T: Eq {}

impl<T, U> PartialEq for Point2D<T, U>
Expand Down Expand Up @@ -896,6 +905,13 @@ unsafe impl<T: Zeroable, U> Zeroable for Point3D<T, U> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: Pod, U: 'static> Pod for Point3D<T, U> {}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, U> MallocSizeOf for Point3D<T, U> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.x.size_of(ops) + self.y.size_of(ops) + self.z.size_of(ops)
}
}

impl<T, U> Eq for Point3D<T, U> where T: Eq {}

impl<T, U> PartialEq for Point3D<T, U>
Expand Down
9 changes: 9 additions & 0 deletions src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::vector::Vector2D;

#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};
#[cfg(feature = "malloc_size_of")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use num_traits::{Float, NumCast};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -79,6 +81,13 @@ impl<T: Hash, U> Hash for Rect<T, U> {
}
}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, U> MallocSizeOf for Rect<T, U> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.origin.size_of(ops) + self.size.size_of(ops)
}
}

impl<T: Copy, U> Copy for Rect<T, U> {}

impl<T: Clone, U> Clone for Rect<T, U> {
Expand Down
9 changes: 9 additions & 0 deletions src/rigid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use core::{fmt, hash};

#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};
#[cfg(feature = "malloc_size_of")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use num_traits::real::Real;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -242,6 +244,13 @@ unsafe impl<T: Zeroable, Src, Dst> Zeroable for RigidTransform3D<T, Src, Dst> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: Pod, Src: 'static, Dst: 'static> Pod for RigidTransform3D<T, Src, Dst> {}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, Src, Dst> MallocSizeOf for RigidTransform3D<T, Src, Dst> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.rotation.size_of(ops) + self.translation.size_of(ops)
}
}

impl<T: Real + ApproxEq<T>, Src, Dst> From<Rotation3D<T, Src, Dst>>
for RigidTransform3D<T, Src, Dst>
{
Expand Down
16 changes: 16 additions & 0 deletions src/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use core::ops::{Add, Mul, Neg, Sub};

#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};
#[cfg(feature = "malloc_size_of")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use num_traits::real::Real;
use num_traits::{NumCast, One, Zero};
#[cfg(feature = "serde")]
Expand Down Expand Up @@ -89,6 +91,13 @@ unsafe impl<T: Zeroable, Src, Dst> Zeroable for Rotation2D<T, Src, Dst> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: Pod, Src: 'static, Dst: 'static> Pod for Rotation2D<T, Src, Dst> {}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, Src, Dst> MallocSizeOf for Rotation2D<T, Src, Dst> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.angle.size_of(ops)
}
}

impl<T, Src, Dst> Rotation2D<T, Src, Dst> {
/// Creates a rotation from an angle in radians.
#[inline]
Expand Down Expand Up @@ -345,6 +354,13 @@ unsafe impl<T: Zeroable, Src, Dst> Zeroable for Rotation3D<T, Src, Dst> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: Pod, Src: 'static, Dst: 'static> Pod for Rotation3D<T, Src, Dst> {}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, Src, Dst> MallocSizeOf for Rotation3D<T, Src, Dst> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.i.size_of(ops) + self.j.size_of(ops) + self.k.size_of(ops) + self.r.size_of(ops)
}
}

impl<T, Src, Dst> Rotation3D<T, Src, Dst> {
/// Creates a rotation around from a quaternion representation.
///
Expand Down
9 changes: 9 additions & 0 deletions src/scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use core::ops::{Add, Div, Mul, Sub};

#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};
#[cfg(feature = "malloc_size_of")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use num_traits::NumCast;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -340,6 +342,13 @@ unsafe impl<T: Zeroable, Src, Dst> Zeroable for Scale<T, Src, Dst> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: Pod, Src: 'static, Dst: 'static> Pod for Scale<T, Src, Dst> {}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, Src, Dst> MallocSizeOf for Scale<T, Src, Dst> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.0.size_of(ops)
}
}

// scale0 * scale1
// (A,B) * (B,C) = (A,C)
impl<T: Mul, A, B, C> Mul<Scale<T, B, C>> for Scale<T, A, B> {
Expand Down
12 changes: 12 additions & 0 deletions src/side_offsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAss

#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};
#[cfg(feature = "malloc_size_of")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -66,6 +68,16 @@ unsafe impl<T: Zeroable, U> Zeroable for SideOffsets2D<T, U> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: Pod, U: 'static> Pod for SideOffsets2D<T, U> {}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, U> MallocSizeOf for SideOffsets2D<T, U> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.top.size_of(ops)
+ self.right.size_of(ops)
+ self.bottom.size_of(ops)
+ self.left.size_of(ops)
}
}

impl<T: Copy, U> Copy for SideOffsets2D<T, U> {}

impl<T: Clone, U> Clone for SideOffsets2D<T, U> {
Expand Down
16 changes: 16 additions & 0 deletions src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAss

#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};
#[cfg(feature = "malloc_size_of")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
#[cfg(feature = "mint")]
use mint;
use num_traits::{Float, NumCast, Signed};
Expand Down Expand Up @@ -107,6 +109,13 @@ unsafe impl<T: Zeroable, U> Zeroable for Size2D<T, U> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: Pod, U: 'static> Pod for Size2D<T, U> {}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, U> MallocSizeOf for Size2D<T, U> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.width.size_of(ops) + self.height.size_of(ops)
}
}

impl<T, U> Eq for Size2D<T, U> where T: Eq {}

impl<T, U> PartialEq for Size2D<T, U>
Expand Down Expand Up @@ -1019,6 +1028,13 @@ unsafe impl<T: Zeroable, U> Zeroable for Size3D<T, U> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: Pod, U: 'static> Pod for Size3D<T, U> {}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, U> MallocSizeOf for Size3D<T, U> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.width.size_of(ops) + self.height.size_of(ops) + self.depth.size_of(ops)
}
}

impl<T, U> Eq for Size3D<T, U> where T: Eq {}

impl<T, U> PartialEq for Size3D<T, U>
Expand Down
14 changes: 14 additions & 0 deletions src/transform2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use core::ops::{Add, Div, Mul, Sub};

#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};
#[cfg(feature = "malloc_size_of")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
#[cfg(feature = "mint")]
use mint;
use num_traits::NumCast;
Expand Down Expand Up @@ -98,6 +100,18 @@ unsafe impl<T: Zeroable, Src, Dst> Zeroable for Transform2D<T, Src, Dst> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: Pod, Src: 'static, Dst: 'static> Pod for Transform2D<T, Src, Dst> {}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf, Src, Dst> MallocSizeOf for Transform2D<T, Src, Dst> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.m11.size_of(ops)
+ self.m12.size_of(ops)
+ self.m21.size_of(ops)
+ self.m22.size_of(ops)
+ self.m31.size_of(ops)
+ self.m32.size_of(ops)
}
}

impl<T: Copy, Src, Dst> Copy for Transform2D<T, Src, Dst> {}

impl<T: Clone, Src, Dst> Clone for Transform2D<T, Src, Dst> {
Expand Down
Loading