contained_core/
specs.rs

1/*
2    Appellation: specs <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4    Description: ... Summary ...
5*/
6use std::ops::{Index, IndexMut};
7use std::vec;
8
9/// [ArrayLike] describes the basic behaviors of an array-like structure
10pub trait ArrayLike<T: Clone + PartialEq + PartialOrd>:
11    AsMut<Vec<T>> + AsRef<Vec<T>> + Eq + IndexMut<usize, Output = T> + Iterable<usize, T> + Ord
12{
13    /// [ArrayLike::append] describes a method for appending another array to the end of the array
14    fn append(&mut self, elem: &mut Self) {
15        self.as_mut().append(elem.as_mut());
16    }
17    fn as_slice(&self) -> &[T] {
18        self.as_ref().as_slice()
19    }
20    /// The capacity of the array
21    fn capacity(&self) -> usize {
22        self.as_ref().capacity()
23    }
24    /// [ArrayLike::clear] describes a method for clearing the array
25    fn clear(&mut self) {
26        self.as_mut().clear();
27    }
28    /// [ArrayLike::contains] describes a method for checking if an element is present in the array
29    fn contains(&self, elem: &T) -> bool {
30        self.as_ref().contains(elem)
31    }
32    /// [ArrayLike::count] describes a method for counting the number of times an element appears in the array
33    fn count(&self, elem: &T) -> usize {
34        self.as_ref().iter().filter(|&x| x == elem).count()
35    }
36    /// [ArrayLike::dedup] describes a method for removing duplicate elements from the array
37    fn dedup(&mut self) {
38        self.as_mut().dedup();
39    }
40    /// [ArrayLike::dedup_by] describes a method for removing duplicate elements from the array using a custom comparison function
41    fn dedup_by<F>(&mut self, same_bucket: F)
42    where
43        F: FnMut(&mut T, &mut T) -> bool,
44    {
45        self.as_mut().dedup_by(same_bucket);
46    }
47    /// [ArrayLike::dedup_by_key] describes a method for removing duplicate elements from the array using a custom key extraction function
48    fn dedup_by_key<F, K>(&mut self, key: F)
49    where
50        F: FnMut(&mut T) -> K,
51        K: PartialEq<K>,
52    {
53        self.as_mut().dedup_by_key(key);
54    }
55    /// [ArrayLike::drain] describes a method for removing a range of elements from the array
56    fn drain(&mut self, range: std::ops::Range<usize>) -> vec::Drain<T> {
57        self.as_mut().drain(range)
58    }
59    /// [ArrayLike::filter] describes a method for filtering the array
60    fn filter(&self, predicate: impl Fn(&T) -> bool) -> Vec<T> {
61        self.as_ref()
62            .iter()
63            .filter(|&x| predicate(x))
64            .cloned()
65            .collect()
66    }
67    /// [ArrayLike::first] describes a method for getting a reference to the first element in the array
68    fn first(&self) -> Option<&T> {
69        self.as_ref().first()
70    }
71    /// [ArrayLike::get] describes a method for getting a reference to an element at a specific position
72    fn get(&self, index: usize) -> Option<&T> {
73        if index < self.len() {
74            Some(&self[index])
75        } else {
76            None
77        }
78    }
79    /// [ArrayLike::get_mut] describes a method for getting a mutable reference to an element at a specific position
80    fn get_mut(&mut self, index: usize) -> Option<&mut T> {
81        if index < self.len() {
82            Some(&mut self[index])
83        } else {
84            None
85        }
86    }
87    /// [ArrayLike::is_empty] checks if the array is empty
88    fn is_empty(&self) -> bool {
89        self.len() == 0
90    }
91    /// [ArrayLike::last] describes a method for gettings the last element in the array
92    fn last(&self) -> Option<&T> {
93        self.as_ref().last()
94    }
95    /// [ArrayLike::len] describes a method for getting the length of the array
96    fn len(&self) -> usize {
97        self.as_ref().len()
98    }
99    /// [ArrayLike::pop] describes a method for removing the last element from the array
100    fn pop(&mut self) -> Option<T> {
101        self.as_mut().pop()
102    }
103    /// [ArrayLike::push] describes a method for adding an element to the end of the array
104    fn push(&mut self, elem: T) {
105        self.as_mut().push(elem);
106    }
107    /// [ArrayLike::remove] describes a method for removing an element at a specific position
108    fn remove(&mut self, index: usize) -> T {
109        self.as_mut().remove(index)
110    }
111    fn reverse(&mut self) {
112        self.as_mut().reverse();
113    }
114    /// [ArrayLike::set] describes a method for setting the value of an element at a specific position
115    fn set(&mut self, index: usize, elem: T) {
116        self[index] = elem;
117    }
118    /// [ArrayLike::shrink_to] describes a method for shrinking the capacity of the array to a specific minimum
119    fn shrink_to(&mut self, min_capacity: usize) {
120        self.as_mut().shrink_to(min_capacity);
121    }
122    /// [ArrayLike::shrink_to_fit] describes a method for shrinking the capacity of the array to match its length
123    fn shrink_to_fit(&mut self) {
124        self.as_mut().shrink_to_fit();
125    }
126    /// [ArrayLike::splice] describes a method for removing a range of elements and replacing them with another array
127    fn splice(&mut self, range: std::ops::Range<usize>, replace_with: Vec<T>) -> Vec<T> {
128        self.as_mut().splice(range, replace_with).collect()
129    }
130    /// [ArrayLike::split_off] describes a method for splitting the array into two at a specific position
131    fn split_off(&mut self, at: usize) -> Vec<T> {
132        self.as_mut().split_off(at)
133    }
134    /// [ArrayLike::swap_remove] describes a method for removing an element at a specific position and returning it, replacing it with the last element
135    fn swap_remove(&mut self, index: usize) -> T {
136        self.as_mut().swap_remove(index)
137    }
138    /// [ArrayLike::truncate] describes a method for truncating the array to a specific length
139    fn truncate(&mut self, len: usize) {
140        self.as_mut().truncate(len);
141    }
142}
143
144pub trait AsBytes {
145    fn as_bytes(&self) -> &[u8];
146}
147
148impl<T> AsBytes for T
149where
150    T: AsRef<[u8]>,
151{
152    fn as_bytes(&self) -> &[u8] {
153        self.as_ref()
154    }
155}
156
157pub trait AsMutBytes {
158    fn as_mut_bytes(&mut self) -> &mut [u8];
159}
160
161impl<T> AsMutBytes for T
162where
163    T: AsMut<[u8]>,
164{
165    fn as_mut_bytes(&mut self) -> &mut [u8] {
166        self.as_mut()
167    }
168}
169
170/// [Include] describes the basic behaviors of a structure which can include a new element
171/// [Include] is designed to be an alternative to [ArrayLike::push] for structures which may or may not have a natural ordering
172pub trait Include<T> {
173    fn include(&mut self, elem: T);
174}
175
176pub trait TryInclude<T> {
177    type Error;
178
179    fn try_include<Output>(&mut self, elem: T) -> Result<Output, Self::Error>;
180}
181
182/// [Insert] describes the basic behaviors of a structure insert a new element given an index or key
183pub trait Insert<Idx, V> {
184    fn insert(&mut self, key: Idx, elem: V);
185}
186
187pub trait TryInsert<Idx, V> {
188    type Error;
189
190    fn try_insert<Output>(&mut self, key: Idx, elem: V) -> Result<Output, Self::Error>;
191}
192
193/// [Iterable] describes the basic behaviors of an iterable structure
194pub trait Iterable<Idx, T>
195where
196    Self: Extend<T>
197        + FromIterator<T>
198        + Index<Idx, Output = T>
199        + Insert<Idx, T>
200        + IntoIterator<Item = T>,
201{
202}