inter_val/
converters.rs

1use crate::{traits::IntoGeneral, Bound, BoundType, Exclusive, Inclusive, Interval};
2
3impl<T> From<T> for Bound<T, Inclusive> {
4    fn from(t: T) -> Self {
5        Self {
6            limit: t,
7            bound_type: Inclusive,
8        }
9    }
10}
11impl<T> From<T> for Bound<T, Exclusive> {
12    fn from(t: T) -> Self {
13        Self {
14            limit: t,
15            bound_type: Exclusive,
16        }
17    }
18}
19
20/// ```
21/// use inter_val::{BoundType, Inclusive, Interval};
22/// let src: Interval<i32, Inclusive> = Inclusive.at(0).to(Inclusive.at(10));
23/// let dst: Interval<i32, BoundType> = src.into();
24/// assert_eq!(dst.left().bound_type, BoundType::Inclusive);
25/// assert_eq!(dst.right().bound_type, BoundType::Inclusive);
26/// ```
27impl<T> From<Interval<T, Inclusive>> for Interval<T, BoundType> {
28    fn from(i: Interval<T, Inclusive>) -> Self {
29        i.into_general()
30    }
31}
32
33/// ```
34/// use inter_val::{BoundType, Exclusive, Interval};
35/// let src: Interval<i32, Exclusive> = Exclusive.at(0).to(Exclusive.at(10));
36/// let dst: Interval<i32, BoundType> = src.into();
37/// assert_eq!(dst.left().bound_type, BoundType::Exclusive);
38/// assert_eq!(dst.right().bound_type, BoundType::Exclusive);
39/// ```
40impl<T> From<Interval<T, Exclusive>> for Interval<T, BoundType> {
41    fn from(i: Interval<T, Exclusive>) -> Self {
42        i.into_general()
43    }
44}
45
46/// ```
47/// use inter_val::{BoundType, Inclusive, Exclusive, Interval};
48/// let src: Interval<i32, Inclusive, Exclusive> = Inclusive.at(0).to(Exclusive.at(10));
49/// let dst: Interval<i32, BoundType> = src.into();
50/// assert_eq!(dst.left().bound_type, BoundType::Inclusive);
51/// assert_eq!(dst.right().bound_type, BoundType::Exclusive);
52/// ```
53impl<T> From<Interval<T, Inclusive, Exclusive>> for Interval<T, BoundType> {
54    fn from(i: Interval<T, Inclusive, Exclusive>) -> Self {
55        i.into_general()
56    }
57}
58
59/// ```
60/// use inter_val::{BoundType, Inclusive, Exclusive, Interval};
61/// let src: Interval<i32, Exclusive, Inclusive> = Exclusive.at(0).to(Inclusive.at(10));
62/// let dst: Interval<i32, BoundType> = src.into();
63/// assert_eq!(dst.left().bound_type, BoundType::Exclusive);
64/// assert_eq!(dst.right().bound_type, BoundType::Inclusive);
65/// ```
66impl<T> From<Interval<T, Exclusive, Inclusive>> for Interval<T, BoundType> {
67    fn from(i: Interval<T, Exclusive, Inclusive>) -> Self {
68        i.into_general()
69    }
70}
71
72/// ```
73/// use std::any::{Any, TypeId};
74/// use inter_val::{Inclusive, Interval};
75/// let a: Interval<_, _, _> = 3.into();
76/// assert_eq!(a.type_id(), TypeId::of::<Interval<i32, Inclusive, Inclusive>>());
77/// assert_eq!(a.left().limit, 3);
78/// assert_eq!(a.right().limit, 3);
79impl<T: PartialOrd + Clone> From<T> for Interval<T, Inclusive> {
80    fn from(t: T) -> Self {
81        Self::new(t.clone().into(), t.into())
82    }
83}