Implement PartialEq for SVGMeasure

main
Elnu 1 year ago
parent d4c7f52955
commit 3b9c6d58de

@ -1,4 +1,5 @@
use super::SVGUnit;
use std::cmp::Ordering;
use std::fmt;
use std::{
fmt::Display,
@ -40,6 +41,18 @@ impl PartialEq for SVGMeasure {
}
}
impl PartialOrd for SVGMeasure {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(if self == other {
Ordering::Equal
} else if self.to_user_units() > other.to_user_units() {
Ordering::Greater
} else {
Ordering::Less
})
}
}
impl Display for SVGMeasure {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", self.measure, self.unit)

@ -44,3 +44,11 @@ fn ne() {
let b = SVGMeasure::new(10.0, SVGUnit::Pixel);
assert_ne!(a, b);
}
#[test]
fn cmp() {
let m_10cm = SVGMeasure::new(10.0, SVGUnit::Centimeter);
let m_5mm = SVGMeasure::new(5.0, SVGUnit::Millimeter);
assert!(m_10cm > m_5mm);
assert!(m_5mm < m_10cm);
}

Loading…
Cancel
Save