intervals_general/
bound_pair.rs

1#[cfg(not(feature = "serde"))]
2mod without_serde {
3    /// A BoundPair represents valid left and right Interval bounds
4    ///
5    /// For Intervals containing finite bounds, the BoundPair construction
6    /// ensures well-formed left and right bounds prior to Interval enum
7    /// construction (e.g. left < right).
8    #[derive(Debug, Copy, Clone, PartialEq)]
9    pub struct BoundPair<T> {
10        pub(crate) left: T,
11        pub(crate) right: T,
12    }
13}
14
15#[cfg(feature = "serde")]
16mod with_serde {
17    use serde::{Deserialize, Serialize};
18
19    /// A BoundPair represents valid left and right Interval bounds
20    ///
21    /// For Intervals containing finite bounds, the BoundPair construction
22    /// ensures well-formed left and right bounds prior to Interval enum
23    /// construction (e.g. left < right).
24    #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
25    pub struct BoundPair<T> {
26        pub(crate) left: T,
27        pub(crate) right: T,
28    }
29}
30
31#[cfg(feature = "serde")]
32pub use with_serde::BoundPair;
33#[cfg(not(feature = "serde"))]
34pub use without_serde::BoundPair;
35
36impl<T> BoundPair<T>
37where
38    T: Copy,
39    T: PartialOrd,
40{
41    /// Create a new Bound Pair with lower and upper bounds.
42    ///
43    /// If the bounds are mal-formed, i.e. !(left < right), return None.
44    ///
45    /// # Examples
46    ///
47    /// ```
48    /// use intervals_general::bound_pair::BoundPair;
49    /// # fn main() -> std::result::Result<(), String> {
50    /// let bounds = BoundPair::new(1.0, 2.0).ok_or("invalid BoundPair")?;
51    /// # Ok(())
52    /// # }
53    /// ```
54    ///
55    /// # Failures
56    ///
57    /// ```
58    /// use intervals_general::bound_pair::BoundPair;
59    /// assert_eq!(BoundPair::new(2, 1), None);
60    /// assert_eq!(BoundPair::new(2.0, 2.0), None);
61    /// ```
62    pub fn new(left: T, right: T) -> Option<BoundPair<T>> {
63        if left >= right {
64            None
65        } else {
66            Some(BoundPair { left, right })
67        }
68    }
69
70    /// Fetch an immutable reference to the left bound
71    ///
72    /// # Examples
73    ///
74    /// ```
75    /// use intervals_general::bound_pair::BoundPair;
76    /// # fn main() -> std::result::Result<(), String> {
77    /// let bounds = BoundPair::new(1.0, 2.0).ok_or("invalid BoundPair")?;
78    /// assert_eq!(*bounds.left(), 1.0);
79    /// # Ok(())
80    /// # }
81    /// ```
82    pub fn left(&self) -> &T {
83        &self.left
84    }
85
86    /// Fetch an immutable reference to the right bound
87    ///
88    /// # Examples
89    ///
90    /// ```
91    /// use intervals_general::bound_pair::BoundPair;
92    /// # fn main() -> std::result::Result<(), String> {
93    /// let bounds = BoundPair::new(1.0, 2.0).ok_or("invalid BoundPair")?;
94    /// assert_eq!(*bounds.right(), 2.0);
95    /// # Ok(())
96    /// # }
97    /// ```
98    pub fn right(&self) -> &T {
99        &self.right
100    }
101}