aliu/
basic.rs

1#[macro_export]
2macro_rules! const_assert {
3    ($cond:expr) => {
4        // Causes overflow if condition is false
5        let _ = [(); 0 - (!($cond) as usize)];
6    };
7    ($($xs:expr),+) => {
8        const_assert!($($xs)&&+);
9    };
10    ($($xs:expr);+ $(;)*) => {
11        const_assert!($($xs),+);
12    };
13}
14
15#[cfg(debug_assertions)]
16pub fn expect<V, E>(res: Result<V, E>) -> V
17where
18    E: core::fmt::Debug,
19{
20    return res.unwrap();
21}
22
23#[cfg(not(debug_assertions))]
24pub fn expect<V, E>(res: Result<V, E>) -> V {
25    let err = match res {
26        Ok(v) => return v,
27        Err(err) => err,
28    };
29
30    panic!("Expected value");
31}
32
33pub fn unwrap<V>(opt: Option<V>) -> V {
34    if let Some(v) = opt {
35        return v;
36    }
37
38    panic!("Expected value");
39}
40
41#[derive(Clone, Copy)]
42pub struct CopyRange<U = usize>
43where
44    U: Copy,
45{
46    pub start: U,
47    pub end: U,
48}
49
50#[inline(always)]
51pub fn r<U>(start: U, end: U) -> CopyRange<U>
52where
53    U: Copy,
54{
55    return CopyRange { start, end };
56}
57
58impl CopyRange<usize> {
59    #[inline(always)]
60    pub fn len(&self) -> usize {
61        return self.end - self.start;
62    }
63}
64
65impl CopyRange<u32> {
66    #[inline(always)]
67    pub fn len(&self) -> u32 {
68        return self.end - self.start;
69    }
70}
71
72impl<U> core::fmt::Debug for CopyRange<U>
73where
74    U: core::fmt::Display + Copy,
75{
76    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
77        return write!(f, "{}..{}", self.start, self.end);
78    }
79}
80
81pub trait SliceIndex<T>: Clone + core::fmt::Debug {
82    type IndexResult: ?Sized;
83
84    fn index(self, data: &[T]) -> Option<&Self::IndexResult>;
85
86    fn index_mut(self, data: &mut [T]) -> Option<&mut Self::IndexResult>;
87}
88
89impl<T> SliceIndex<T> for u8 {
90    type IndexResult = T;
91
92    #[inline(always)]
93    fn index(self, data: &[T]) -> Option<&Self::IndexResult> {
94        return data.get(self as usize);
95    }
96
97    #[inline(always)]
98    fn index_mut(self, data: &mut [T]) -> Option<&mut Self::IndexResult> {
99        return data.get_mut(self as usize);
100    }
101}
102
103impl<T> SliceIndex<T> for u16 {
104    type IndexResult = T;
105
106    #[inline(always)]
107    fn index(self, data: &[T]) -> Option<&Self::IndexResult> {
108        return data.get(self as usize);
109    }
110
111    #[inline(always)]
112    fn index_mut(self, data: &mut [T]) -> Option<&mut Self::IndexResult> {
113        return data.get_mut(self as usize);
114    }
115}
116
117impl<T> SliceIndex<T> for u32 {
118    type IndexResult = T;
119
120    #[inline(always)]
121    fn index(self, data: &[T]) -> Option<&Self::IndexResult> {
122        return data.get(self as usize);
123    }
124
125    #[inline(always)]
126    fn index_mut(self, data: &mut [T]) -> Option<&mut Self::IndexResult> {
127        return data.get_mut(self as usize);
128    }
129}
130
131impl<T> SliceIndex<T> for usize {
132    type IndexResult = T;
133
134    #[inline(always)]
135    fn index(self, data: &[T]) -> Option<&Self::IndexResult> {
136        return data.get(self);
137    }
138
139    #[inline(always)]
140    fn index_mut(self, data: &mut [T]) -> Option<&mut Self::IndexResult> {
141        return data.get_mut(self);
142    }
143}
144
145impl<T> SliceIndex<T> for CopyRange<u32> {
146    type IndexResult = [T];
147
148    #[inline(always)]
149    fn index(self, data: &[T]) -> Option<&Self::IndexResult> {
150        return data.get((self.start as usize)..(self.end as usize));
151    }
152
153    #[inline(always)]
154    fn index_mut(self, data: &mut [T]) -> Option<&mut Self::IndexResult> {
155        return data.get_mut((self.start as usize)..(self.end as usize));
156    }
157}
158
159impl<T> SliceIndex<T> for CopyRange<usize> {
160    type IndexResult = [T];
161
162    #[inline(always)]
163    fn index(self, data: &[T]) -> Option<&Self::IndexResult> {
164        return data.get(self.start..self.end);
165    }
166
167    #[inline(always)]
168    fn index_mut(self, data: &mut [T]) -> Option<&mut Self::IndexResult> {
169        return data.get_mut(self.start..self.end);
170    }
171}
172
173impl<T> SliceIndex<T> for core::ops::Range<u32> {
174    type IndexResult = [T];
175
176    #[inline(always)]
177    fn index(self, data: &[T]) -> Option<&Self::IndexResult> {
178        return data.get((self.start as usize)..(self.end as usize));
179    }
180
181    #[inline(always)]
182    fn index_mut(self, data: &mut [T]) -> Option<&mut Self::IndexResult> {
183        return data.get_mut((self.start as usize)..(self.end as usize));
184    }
185}
186
187impl<T> SliceIndex<T> for core::ops::Range<usize> {
188    type IndexResult = [T];
189
190    #[inline(always)]
191    fn index(self, data: &[T]) -> Option<&Self::IndexResult> {
192        return data.get(self);
193    }
194
195    #[inline(always)]
196    fn index_mut(self, data: &mut [T]) -> Option<&mut Self::IndexResult> {
197        return data.get_mut(self);
198    }
199}
200
201impl<T> SliceIndex<T> for core::ops::RangeTo<usize> {
202    type IndexResult = [T];
203
204    #[inline(always)]
205    fn index(self, data: &[T]) -> Option<&Self::IndexResult> {
206        return data.get(self);
207    }
208
209    #[inline(always)]
210    fn index_mut(self, data: &mut [T]) -> Option<&mut Self::IndexResult> {
211        return data.get_mut(self);
212    }
213}
214
215impl<T> SliceIndex<T> for core::ops::RangeFrom<usize> {
216    type IndexResult = [T];
217
218    #[inline(always)]
219    fn index(self, data: &[T]) -> Option<&Self::IndexResult> {
220        return data.get(self);
221    }
222
223    #[inline(always)]
224    fn index_mut(self, data: &mut [T]) -> Option<&mut Self::IndexResult> {
225        return data.get_mut(self);
226    }
227}
228
229impl<T> SliceIndex<T> for core::ops::RangeFull {
230    type IndexResult = [T];
231
232    #[inline(always)]
233    fn index(self, data: &[T]) -> Option<&Self::IndexResult> {
234        return data.get(self);
235    }
236
237    #[inline(always)]
238    fn index_mut(self, data: &mut [T]) -> Option<&mut Self::IndexResult> {
239        return data.get_mut(self);
240    }
241}
242
243pub const fn const_cond(cond: bool, if_true: usize, if_false: usize) -> usize {
244    (cond as usize) * if_true + (!cond as usize) * if_false
245}
246
247pub const fn const_max(a: usize, b: usize) -> usize {
248    const_cond(a > b, a, b)
249}