imath_traits/
bound.rs

1use std::fmt;
2use std::ops::Sub;
3
4pub trait Bound1<T>
5where
6    Self: Sized,
7    T: Sub<Output = T> + Copy + fmt::Debug,
8{
9    fn from_slice(slice: &[T; 2]) -> Self;
10    fn as_slice(&self) -> &[T; 2];
11    fn as_ptr(&self) -> *const T;
12
13    fn width(&self) -> T {
14        let s = self.as_slice();
15        s[1] - s[0]
16    }
17
18    fn bound_min(&self) -> T {
19        let s = self.as_slice();
20        s[0]
21    }
22
23    fn bound_max(&self) -> T {
24        let s = self.as_slice();
25        s[1]
26    }
27}
28
29impl<T> Bound1<T> for [T; 2]
30where
31    T: Sub<Output = T> + Copy + fmt::Debug,
32{
33    fn from_slice(slice: &[T; 2]) -> Self {
34        *slice
35    }
36
37    fn as_slice(&self) -> &[T; 2] {
38        self
39    }
40
41    fn as_ptr(&self) -> *const T {
42        self as *const T
43    }
44}
45
46pub trait Bound2<T>
47where
48    Self: Sized,
49    T: Sub<Output = T> + Copy + fmt::Debug,
50{
51    fn from_slice(slice: &[T; 4]) -> Self;
52    fn as_slice(&self) -> &[T; 4];
53    fn as_ptr(&self) -> *const T;
54
55    fn width(&self) -> T {
56        let s = self.as_slice();
57        s[2] - s[0]
58    }
59
60    fn height(&self) -> T {
61        let s = self.as_slice();
62        s[3] - s[1]
63    }
64
65    fn min_x(&self) -> T {
66        let s = self.as_slice();
67        s[0]
68    }
69
70    fn min_y(&self) -> T {
71        let s = self.as_slice();
72        s[1]
73    }
74
75    fn max_x(&self) -> T {
76        let s = self.as_slice();
77        s[2]
78    }
79
80    fn max_y(&self) -> T {
81        let s = self.as_slice();
82        s[3]
83    }
84}
85
86impl<T> Bound2<T> for [T; 4]
87where
88    T: Sub<Output = T> + Copy + fmt::Debug,
89{
90    fn from_slice(slice: &[T; 4]) -> Self {
91        *slice
92    }
93
94    fn as_slice(&self) -> &[T; 4] {
95        self
96    }
97
98    fn as_ptr(&self) -> *const T {
99        self as *const T
100    }
101}
102
103pub trait Bound3<T>
104where
105    Self: Sized,
106    T: Sub<Output = T> + Copy + fmt::Debug,
107{
108    fn from_slice(slice: &[T; 6]) -> Self;
109    fn as_slice(&self) -> &[T; 6];
110    fn as_ptr(&self) -> *const T;
111
112    fn width(&self) -> T {
113        let s = self.as_slice();
114        s[3] - s[0]
115    }
116
117    fn height(&self) -> T {
118        let s = self.as_slice();
119        s[4] - s[1]
120    }
121
122    fn depth(&self) -> T {
123        let s = self.as_slice();
124        s[5] - s[2]
125    }
126}
127
128impl<T> Bound3<T> for [T; 6]
129where
130    T: Copy,
131    T: Sub<Output = T> + Copy + fmt::Debug,
132{
133    fn from_slice(slice: &[T; 6]) -> Self {
134        *slice
135    }
136
137    fn as_slice(&self) -> &[T; 6] {
138        self
139    }
140
141    fn as_ptr(&self) -> *const T {
142        self as *const T
143    }
144}