summaryrefslogtreecommitdiff
path: root/src/geom
diff options
context:
space:
mode:
authorMartin <mhaug@live.de>2021-06-17 14:18:43 +0200
committerGitHub <noreply@github.com>2021-06-17 14:18:43 +0200
commite14e8047890afad5896c9f38ccdd8551f869be64 (patch)
treee65a448e88c0de84ae0790a92a00fd903ba197da /src/geom
parente2cdda67dc0e16b9a482aa3a4bfd5991db06d143 (diff)
Constraints (#31)
Diffstat (limited to 'src/geom')
-rw-r--r--src/geom/gen.rs8
-rw-r--r--src/geom/linear.rs5
-rw-r--r--src/geom/size.rs5
-rw-r--r--src/geom/spec.rs20
4 files changed, 38 insertions, 0 deletions
diff --git a/src/geom/gen.rs b/src/geom/gen.rs
index 075b7377..57dc277d 100644
--- a/src/geom/gen.rs
+++ b/src/geom/gen.rs
@@ -23,6 +23,14 @@ impl<T> Gen<T> {
Self { cross: value.clone(), main: value }
}
+ /// Maps the individual fields with `f`.
+ pub fn map<F, U>(self, mut f: F) -> Gen<U>
+ where
+ F: FnMut(T) -> U,
+ {
+ Gen { cross: f(self.cross), main: f(self.main) }
+ }
+
/// Convert to the specific representation.
pub fn to_spec(self, main: SpecAxis) -> Spec<T> {
match main {
diff --git a/src/geom/linear.rs b/src/geom/linear.rs
index c3216b21..38d19b13 100644
--- a/src/geom/linear.rs
+++ b/src/geom/linear.rs
@@ -40,6 +40,11 @@ impl Linear {
pub fn is_zero(self) -> bool {
self.rel.is_zero() && self.abs.is_zero()
}
+
+ /// Whether there is a linear component.
+ pub fn is_relative(&self) -> bool {
+ !self.rel.is_zero()
+ }
}
impl Display for Linear {
diff --git a/src/geom/size.rs b/src/geom/size.rs
index 4b94d0ae..7967dbdc 100644
--- a/src/geom/size.rs
+++ b/src/geom/size.rs
@@ -50,6 +50,11 @@ impl Size {
Point::new(self.width, self.height)
}
+ /// Convert to a Spec.
+ pub fn to_spec(self) -> Spec<Length> {
+ Spec::new(self.width, self.height)
+ }
+
/// Convert to the generic representation.
pub fn to_gen(self, main: SpecAxis) -> Gen<Length> {
match main {
diff --git a/src/geom/spec.rs b/src/geom/spec.rs
index d0da3bca..f8f62f9f 100644
--- a/src/geom/spec.rs
+++ b/src/geom/spec.rs
@@ -26,6 +26,17 @@ impl<T> Spec<T> {
}
}
+ /// Maps the individual fields with `f`.
+ pub fn map<F, U>(self, mut f: F) -> Spec<U>
+ where
+ F: FnMut(T) -> U,
+ {
+ Spec {
+ horizontal: f(self.horizontal),
+ vertical: f(self.vertical),
+ }
+ }
+
/// Convert to the generic representation.
pub fn to_gen(self, main: SpecAxis) -> Gen<T> {
match main {
@@ -33,6 +44,15 @@ impl<T> Spec<T> {
SpecAxis::Vertical => Gen::new(self.horizontal, self.vertical),
}
}
+
+ /// Compare to whether two instances are equal when compared field-by-field
+ /// with `f`.
+ pub fn eq_by<U, F>(&self, other: &Spec<U>, eq: F) -> bool
+ where
+ F: Fn(&T, &U) -> bool,
+ {
+ eq(&self.vertical, &other.vertical) && eq(&self.horizontal, &other.horizontal)
+ }
}
impl Spec<Length> {