demes_forward/
current_size.rs

1use crate::DemesForwardError;
2
3/// The current size of a deme.
4///
5/// Unlike [`demes::DemeSize`], this type
6/// allows for values of 0.0, which means
7/// that there are no individuals in the
8/// deme at the current time.
9#[repr(transparent)]
10#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
11pub struct CurrentSize(f64);
12
13impl TryFrom<f64> for CurrentSize {
14    type Error = DemesForwardError;
15
16    fn try_from(value: f64) -> Result<Self, Self::Error> {
17        if value.is_sign_positive() && value.is_finite() {
18            Ok(Self(value))
19        } else {
20            Err(DemesForwardError::InvalidDemeSize(value))
21        }
22    }
23}
24
25impl TryFrom<demes::DemeSize> for CurrentSize {
26    type Error = DemesForwardError;
27
28    fn try_from(value: demes::DemeSize) -> Result<Self, Self::Error> {
29        Self::try_from(f64::from(value))
30    }
31}
32
33impl PartialEq<CurrentSize> for f64 {
34    fn eq(&self, other: &CurrentSize) -> bool {
35        self.eq(&other.0)
36    }
37}
38
39impl PartialEq<f64> for CurrentSize {
40    fn eq(&self, other: &f64) -> bool {
41        self.0.eq(other)
42    }
43}
44
45impl PartialOrd<CurrentSize> for f64 {
46    fn partial_cmp(&self, other: &CurrentSize) -> Option<std::cmp::Ordering> {
47        self.partial_cmp(&other.0)
48    }
49}
50
51impl PartialOrd<f64> for CurrentSize {
52    fn partial_cmp(&self, other: &f64) -> Option<std::cmp::Ordering> {
53        self.0.partial_cmp(other)
54    }
55}