$ show servo/euclid
Euclid is a Rust library providing type-safe geometric primitives with compile-time unit checking to prevent mixing of different coordinate spaces. It implements 2D/3D points, vectors, transforms, and related types using phantom types for unit safety, enabling operations like transformations, intersections, and conversions while maintaining FFI compatibility. The library is used in the Servo browser engine for layout and rendering, featuring generic scalar types, operator overloading, and optional integrations with serde, bytemuck, and mint. Its core strength lies in the unit-tagged generic system that catches unit mismatches at compile time, a pattern applicable beyond graphics to any domain requiring dimensional analysis.
The LLM identified 3 files worth highlighting for reuse:
Defines Point2D<T, U> and Point3D<T, U> structs parameterized by scalar type T and unit type U (using phantom types). This pattern prevents accidental mixing of different coordinate spaces (e.g., pixels vs. millimeters) at compile time while allowing type-safe conversions via Scale trait. The unit system is pervasive across all geometric types and represents a reusable dimensional analysis pattern.
Implements Transform2D with methods like then(), pre_then(), and rotation() for composing affine transformations. The then() method enables readable chaining (e.g., transform.then(rotation).then(translation)) by encapsulating matrix multiplication. This composition pattern is cleanly abstracted, FFI-safe via #[repr(C)], and applicable to any transformation system requiring operation sequencing.
Provides ApproxEq trait with default_epsilon() and eq() methods, plus derive macros for structs. Solves floating-point comparison challenges with configurable tolerance, implemented generically for all geometric types. The trait macros reduce boilerplate while allowing custom epsilon per type, demonstrating a reusable numerical robustness pattern.