summaryrefslogtreecommitdiff
path: root/src/geom/fr.rs
blob: 974d675eec5587bf1c775c4d7da8511e2360907f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use decorum::N64;

use super::*;

/// A fractional length.
#[derive(Default, Copy, Clone, PartialEq, PartialOrd, Hash)]
pub struct Fractional(N64);

impl Fractional {
    /// Takes up zero space: `0fr`.
    pub fn zero() -> Self {
        Self(N64::from(0.0))
    }

    /// Takes up as much space as all other items with this fractional size: `1fr`.
    pub fn one() -> Self {
        Self(N64::from(1.0))
    }

    /// Create a new fractional value.
    pub fn new(ratio: f64) -> Self {
        Self(N64::from(ratio))
    }

    /// Get the underlying ratio.
    pub fn get(self) -> f64 {
        self.0.into()
    }

    /// Whether the ratio is zero.
    pub fn is_zero(self) -> bool {
        self.0 == 0.0
    }
}

impl Display for Fractional {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}fr", self.get())
    }
}

impl Debug for Fractional {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        Display::fmt(self, f)
    }
}

impl Neg for Fractional {
    type Output = Self;

    fn neg(self) -> Self {
        Self(-self.0)
    }
}

impl Add for Fractional {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Self(self.0 + other.0)
    }
}

sub_impl!(Fractional - Fractional -> Fractional);

impl Mul<f64> for Fractional {
    type Output = Self;

    fn mul(self, other: f64) -> Self {
        Self(self.0 * other)
    }
}

impl Mul<Fractional> for f64 {
    type Output = Fractional;

    fn mul(self, other: Fractional) -> Fractional {
        other * self
    }
}

impl Div<f64> for Fractional {
    type Output = Self;

    fn div(self, other: f64) -> Self {
        Self(self.0 / other)
    }
}

impl Div for Fractional {
    type Output = f64;

    fn div(self, other: Self) -> f64 {
        self.get() / other.get()
    }
}

assign_impl!(Fractional += Fractional);
assign_impl!(Fractional -= Fractional);
assign_impl!(Fractional *= f64);
assign_impl!(Fractional /= f64);