1pub mod time;
2
3#[derive(Copy, Clone, Debug)]
7pub struct SmallArrayBackedSlice<const N: usize, T> {
8 len: u8,
9 bytes: [T; N],
10}
11
12impl<const N: usize, T: Default + Copy> SmallArrayBackedSlice<N, T> {
15 const CHECK_N: () = {
19 assert!(N <= u8::MAX as usize);
20 };
21
22 pub fn new(bytes: &[T]) -> Option<Self> {
24 #[allow(clippy::let_unit_value)]
26 let _ = Self::CHECK_N;
27
28 if bytes.len() > N {
29 return None;
30 }
31
32 let mut rv = Self::empty();
33
34 rv.len = bytes.len().try_into().unwrap();
35 rv.bytes[..bytes.len()].copy_from_slice(bytes);
36
37 Some(rv)
38 }
39
40 pub fn empty() -> Self {
41 #[allow(clippy::let_unit_value)]
43 let _ = Self::CHECK_N;
44
45 Self {
46 len: 0,
47 bytes: [T::default(); N],
48 }
49 }
50}
51
52impl<const N: usize, T> std::ops::Deref for SmallArrayBackedSlice<N, T> {
53 type Target = [T];
54
55 fn deref(&self) -> &Self::Target {
56 &self.bytes[..(self.len as usize)]
57 }
58}
59
60impl<const N: usize, T> AsRef<[T]> for SmallArrayBackedSlice<N, T> {
61 fn as_ref(&self) -> &[T] {
62 self
63 }
64}
65
66pub(crate) fn remove_from_list<T: Eq>(list: &mut std::collections::LinkedList<T>, item: &T) {
68 if let Some(pos) = list.iter().position(|e| e == item) {
69 let mut split_list = list.split_off(pos);
70 split_list.pop_front();
71 list.append(&mut split_list);
72 }
73}
74
75#[cfg(test)]
76mod tests {
77 use super::*;
78
79 #[test]
80 fn test_new_small_slice() {
81 SmallArrayBackedSlice::<3, u8>::empty();
82 SmallArrayBackedSlice::<3, u8>::new(&[]).unwrap();
83 SmallArrayBackedSlice::<3, u8>::new(&[1]).unwrap();
84 SmallArrayBackedSlice::<3, u8>::new(&[1, 2]).unwrap();
85 SmallArrayBackedSlice::<3, u8>::new(&[1, 2, 3]).unwrap();
86 assert!(SmallArrayBackedSlice::<3, u8>::new(&[1, 2, 3, 4]).is_none());
87 }
88
89 #[test]
90 fn test_deref_small_slice() {
91 let slice = SmallArrayBackedSlice::<3, u8>::empty();
92 assert!(slice.is_empty());
93
94 let slice = SmallArrayBackedSlice::<3, u8>::new(&[]).unwrap();
95 assert!(slice.is_empty());
96
97 let slice = SmallArrayBackedSlice::<3, u8>::new(&[1]).unwrap();
98 assert_eq!(slice.len(), 1);
99
100 let slice = SmallArrayBackedSlice::<3, u8>::new(&[1, 2, 3]).unwrap();
101 assert_eq!(slice.len(), 3);
102 assert_eq!(&*slice, &[1, 2, 3]);
103 }
104
105 #[test]
106 fn test_remove_from_list() {
107 let mut list: std::collections::LinkedList<u8> =
108 [1, 6, 2, 7, 3, 6, 4, 0].into_iter().collect();
109
110 fn to_vec<T: Clone>(list: &std::collections::LinkedList<T>) -> Vec<T> {
111 list.clone().into_iter().collect()
112 }
113
114 remove_from_list(&mut list, &3);
115 assert_eq!(&to_vec(&list), &[1, 6, 2, 7, 6, 4, 0]);
116
117 remove_from_list(&mut list, &6);
118 assert_eq!(&to_vec(&list), &[1, 2, 7, 6, 4, 0]);
119
120 remove_from_list(&mut list, &6);
121 assert_eq!(&to_vec(&list), &[1, 2, 7, 4, 0]);
122
123 remove_from_list(&mut list, &1);
124 assert_eq!(&to_vec(&list), &[2, 7, 4, 0]);
125
126 remove_from_list(&mut list, &0);
127 assert_eq!(&to_vec(&list), &[2, 7, 4]);
128
129 remove_from_list(&mut list, &7);
130 assert_eq!(&to_vec(&list), &[2, 4]);
131
132 remove_from_list(&mut list, &4);
133 assert_eq!(&to_vec(&list), &[2]);
134
135 remove_from_list(&mut list, &3);
136 assert_eq!(&to_vec(&list), &[2]);
137
138 remove_from_list(&mut list, &2);
139 assert_eq!(&to_vec(&list), &[]);
140
141 remove_from_list(&mut list, &2);
142 assert_eq!(&to_vec(&list), &[]);
143 }
144}