Skip to content

Commit 2500741

Browse files
authored
Implement MallocSizeOf for euclid types (#541)
* Implement MallocSizeOf trait for euclid type Signed-off-by: Nico Burns <[email protected]> * Add CI task for checking malloc_size_of feature Signed-off-by: Nico Burns <[email protected]> * Bump version number Signed-off-by: Nico Burns <[email protected]> * Remove use of dep syntax for MSRV compatibility Signed-off-by: Nico Burns <[email protected]> * Bump MSRV to 1.63 Signed-off-by: Nico Burns <[email protected]> --------- Signed-off-by: Nico Burns <[email protected]>
1 parent f17b125 commit 2500741

18 files changed

+208
-3
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ jobs:
1515
strategy:
1616
matrix:
1717
features: ["", "--features serde", "--no-default-features --features libm"]
18-
version: ["1.56.0", "stable", "beta", "nightly"]
18+
version: ["1.63.0", "stable", "beta", "nightly"]
1919
include:
2020
- version: stable
2121
features: --features mint
2222
- version: stable
2323
features: --features bytemuck
2424
- version: stable
2525
features: --features arbitrary
26+
- version: stable
27+
features: --features malloc_size_of
2628
- version: nightly
2729
features: --features unstable
2830
- version: nightly

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
22
name = "euclid"
3-
version = "0.22.11"
3+
version = "0.22.12"
44
authors = ["The Servo Project Developers"]
55
edition = "2021"
6-
rust-version = "1.56.0"
6+
rust-version = "1.63.0"
77
description = "Geometry primitives"
88
documentation = "https://docs.rs/euclid/"
99
repository = "https://github.com/servo/euclid"
@@ -20,6 +20,7 @@ libm = ["num-traits/libm"]
2020
[dependencies]
2121
num-traits = { version = "0.2.15", default-features = false }
2222
serde = { version = "1.0", default-features = false, features = ["serde_derive"], optional = true }
23+
malloc_size_of = { version = "0.1", default-features = false, optional = true }
2324
mint = { version = "0.5.1", optional = true }
2425
arbitrary = { version = "1", optional = true }
2526
bytemuck = { version = "1.9", optional = true }

src/angle.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, Sub, S
1717

1818
#[cfg(feature = "bytemuck")]
1919
use bytemuck::{Pod, Zeroable};
20+
#[cfg(feature = "malloc_size_of")]
21+
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
2022
use num_traits::real::Real;
2123
use num_traits::{Float, FloatConst, NumCast, One, Zero};
2224
#[cfg(feature = "serde")]
@@ -52,6 +54,13 @@ where
5254
}
5355
}
5456

57+
#[cfg(feature = "malloc_size_of")]
58+
impl<T: MallocSizeOf> MallocSizeOf for Angle<T> {
59+
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
60+
self.radians.size_of(ops)
61+
}
62+
}
63+
5564
impl<T> Angle<T> {
5665
#[inline]
5766
pub fn radians(radians: T) -> Self {

src/box2d.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use crate::vector::{vec2, Vector2D};
1919

2020
#[cfg(feature = "bytemuck")]
2121
use bytemuck::{Pod, Zeroable};
22+
#[cfg(feature = "malloc_size_of")]
23+
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
2224
use num_traits::{Float, NumCast};
2325
#[cfg(feature = "serde")]
2426
use serde::{Deserialize, Serialize};
@@ -151,6 +153,13 @@ impl<T, U> Box2D<T, U> {
151153
}
152154
}
153155

156+
#[cfg(feature = "malloc_size_of")]
157+
impl<T: MallocSizeOf, U> MallocSizeOf for Box2D<T, U> {
158+
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
159+
self.min.size_of(ops) + self.max.size_of(ops)
160+
}
161+
}
162+
154163
impl<T, U> Box2D<T, U>
155164
where
156165
T: PartialOrd,

src/box3d.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use crate::vector::Vector3D;
1717

1818
#[cfg(feature = "bytemuck")]
1919
use bytemuck::{Pod, Zeroable};
20+
#[cfg(feature = "malloc_size_of")]
21+
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
2022
use num_traits::{Float, NumCast};
2123
#[cfg(feature = "serde")]
2224
use serde::{Deserialize, Serialize};
@@ -90,6 +92,13 @@ unsafe impl<T: Zeroable, U> Zeroable for Box3D<T, U> {}
9092
#[cfg(feature = "bytemuck")]
9193
unsafe impl<T: Pod, U: 'static> Pod for Box3D<T, U> {}
9294

95+
#[cfg(feature = "malloc_size_of")]
96+
impl<T: MallocSizeOf, U> MallocSizeOf for Box3D<T, U> {
97+
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
98+
self.min.size_of(ops) + self.max.size_of(ops)
99+
}
100+
}
101+
93102
impl<T, U> Box3D<T, U> {
94103
/// Constructor.
95104
#[inline]

src/homogen.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use core::fmt;
1919
use core::hash::Hash;
2020
use core::marker::PhantomData;
2121
use core::ops::Div;
22+
#[cfg(feature = "malloc_size_of")]
23+
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
2224
#[cfg(feature = "serde")]
2325
use serde;
2426

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

108+
#[cfg(feature = "malloc_size_of")]
109+
impl<T: MallocSizeOf, U> MallocSizeOf for HomogeneousVector<T, U> {
110+
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
111+
self.x.size_of(ops) + self.y.size_of(ops) + self.z.size_of(ops) + self.w.size_of(ops)
112+
}
113+
}
114+
106115
impl<T, U> Eq for HomogeneousVector<T, U> where T: Eq {}
107116

108117
impl<T, U> PartialEq for HomogeneousVector<T, U>

src/length.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use core::iter::Sum;
2323
use core::marker::PhantomData;
2424
use core::ops::{Add, Div, Mul, Neg, Sub};
2525
use core::ops::{AddAssign, DivAssign, MulAssign, SubAssign};
26+
#[cfg(feature = "malloc_size_of")]
27+
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
2628
use num_traits::{NumCast, Saturating};
2729
#[cfg(feature = "serde")]
2830
use serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -91,6 +93,13 @@ unsafe impl<T: Zeroable, U> Zeroable for Length<T, U> {}
9193
#[cfg(feature = "bytemuck")]
9294
unsafe impl<T: Pod, U: 'static> Pod for Length<T, U> {}
9395

96+
#[cfg(feature = "malloc_size_of")]
97+
impl<T: MallocSizeOf, U> MallocSizeOf for Length<T, U> {
98+
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
99+
self.0.size_of(ops)
100+
}
101+
}
102+
94103
impl<T, U> Length<T, U> {
95104
/// Associate a value with a unit of measure.
96105
#[inline]

src/point.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use core::fmt;
2020
use core::hash::Hash;
2121
use core::marker::PhantomData;
2222
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
23+
#[cfg(feature = "malloc_size_of")]
24+
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
2325
#[cfg(feature = "mint")]
2426
use mint;
2527
use num_traits::real::Real;
@@ -103,6 +105,13 @@ unsafe impl<T: Zeroable, U> Zeroable for Point2D<T, U> {}
103105
#[cfg(feature = "bytemuck")]
104106
unsafe impl<T: Pod, U: 'static> Pod for Point2D<T, U> {}
105107

108+
#[cfg(feature = "malloc_size_of")]
109+
impl<T: MallocSizeOf, U> MallocSizeOf for Point2D<T, U> {
110+
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
111+
self.x.size_of(ops) + self.y.size_of(ops)
112+
}
113+
}
114+
106115
impl<T, U> Eq for Point2D<T, U> where T: Eq {}
107116

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

908+
#[cfg(feature = "malloc_size_of")]
909+
impl<T: MallocSizeOf, U> MallocSizeOf for Point3D<T, U> {
910+
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
911+
self.x.size_of(ops) + self.y.size_of(ops) + self.z.size_of(ops)
912+
}
913+
}
914+
899915
impl<T, U> Eq for Point3D<T, U> where T: Eq {}
900916

901917
impl<T, U> PartialEq for Point3D<T, U>

src/rect.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use crate::vector::Vector2D;
1818

1919
#[cfg(feature = "bytemuck")]
2020
use bytemuck::{Pod, Zeroable};
21+
#[cfg(feature = "malloc_size_of")]
22+
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
2123
use num_traits::{Float, NumCast};
2224
#[cfg(feature = "serde")]
2325
use serde::{Deserialize, Serialize};
@@ -79,6 +81,13 @@ impl<T: Hash, U> Hash for Rect<T, U> {
7981
}
8082
}
8183

84+
#[cfg(feature = "malloc_size_of")]
85+
impl<T: MallocSizeOf, U> MallocSizeOf for Rect<T, U> {
86+
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
87+
self.origin.size_of(ops) + self.size.size_of(ops)
88+
}
89+
}
90+
8291
impl<T: Copy, U> Copy for Rect<T, U> {}
8392

8493
impl<T: Clone, U> Clone for Rect<T, U> {

src/rigid.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use core::{fmt, hash};
1010

1111
#[cfg(feature = "bytemuck")]
1212
use bytemuck::{Pod, Zeroable};
13+
#[cfg(feature = "malloc_size_of")]
14+
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
1315
use num_traits::real::Real;
1416
#[cfg(feature = "serde")]
1517
use serde::{Deserialize, Serialize};
@@ -242,6 +244,13 @@ unsafe impl<T: Zeroable, Src, Dst> Zeroable for RigidTransform3D<T, Src, Dst> {}
242244
#[cfg(feature = "bytemuck")]
243245
unsafe impl<T: Pod, Src: 'static, Dst: 'static> Pod for RigidTransform3D<T, Src, Dst> {}
244246

247+
#[cfg(feature = "malloc_size_of")]
248+
impl<T: MallocSizeOf, Src, Dst> MallocSizeOf for RigidTransform3D<T, Src, Dst> {
249+
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
250+
self.rotation.size_of(ops) + self.translation.size_of(ops)
251+
}
252+
}
253+
245254
impl<T: Real + ApproxEq<T>, Src, Dst> From<Rotation3D<T, Src, Dst>>
246255
for RigidTransform3D<T, Src, Dst>
247256
{

0 commit comments

Comments
 (0)