physdes/generic.rs
1/// The `trait Overlap<T>` defines a method `overlaps` that checks if two objects of type `T` overlap
2/// with each other. The `overlaps` method takes a reference to another object of type `T` as a
3/// parameter and returns a boolean value indicating whether the two objects overlap or not. This trait
4/// can be implemented for any type that needs to support the `overlaps` functionality.
5///
6/// # Examples
7///
8/// ```
9/// use physdes::generic::Overlap;
10///
11/// let a: i32 = 42;
12/// let b: i32 = 42;
13/// assert!(a.overlaps(&b));
14///
15/// let a: i32 = 42;
16/// let b: i32 = 24;
17/// assert!(!a.overlaps(&b));
18/// ```
19pub trait Overlap<T> {
20 fn overlaps(&self, other: &T) -> bool;
21}
22
23/// Checks if two `i32` values are equal.
24///
25/// This implementation of the `Overlap` trait for `i32` simply checks if the two values are equal.
26///
27/// # Examples
28///
29/// ```
30/// use physdes::generic::Overlap;
31///
32/// let a: i32 = 42;
33/// let b: i32 = 42;
34/// assert!(a.overlaps(&b));
35///
36/// let a: i32 = 42;
37/// let b: i32 = 24;
38/// assert!(!a.overlaps(&b));
39/// ```
40impl Overlap<i32> for i32 {
41 /// The `overlaps` function in Rust checks if two references to `i32` values point to the same
42 /// memory location.
43 ///
44 /// Arguments:
45 ///
46 /// * `other`: The `other` parameter in the `overlaps` function is a reference to an `i32` value.
47 ///
48 /// Returns:
49 ///
50 /// The `overlaps` function is returning a boolean value indicating whether the value of `self` is
51 /// equal to the value of `other`.
52 #[inline]
53 fn overlaps(&self, other: &i32) -> bool {
54 self == other
55 }
56}
57
58/// The `trait Contain<T>` defines a method `contains` that checks if an object of type `T` is
59/// contained within another object. The `contains` method takes a reference to another object of type
60/// `T` as a parameter and returns a boolean value indicating whether the object is contained within the
61/// other object or not. This trait can be implemented for any type that needs to support the `contains`
62/// functionality.
63///
64/// # Examples
65///
66/// ```
67/// use physdes::generic::Contain;
68///
69/// let a: i32 = 42;
70/// let b: i32 = 42;
71/// assert!(a.contains(&b));
72///
73/// let a: i32 = 42;
74/// let b: i32 = 24;
75/// assert!(!a.contains(&b));
76/// ```
77pub trait Contain<T> {
78 fn contains(&self, other: &T) -> bool;
79}
80
81/// Checks if the current `i32` value is equal to the provided `i32` value.
82///
83/// This implementation of the `Contain` trait for `i32` simply compares the two values for equality.
84///
85/// # Examples
86///
87/// ```
88/// use physdes::generic::Contain;
89///
90/// let a: i32 = 42;
91/// let b: i32 = 42;
92/// assert!(a.contains(&b));
93///
94/// let a: i32 = 42;
95/// let b: i32 = 24;
96/// assert!(!a.contains(&b));
97/// ```
98impl Contain<i32> for i32 {
99 /// The function checks if a given value is equal to another value.
100 ///
101 /// Arguments:
102 ///
103 /// * `other`: The `other` parameter in the `contains` function is a reference to an `i32` value
104 /// that is being compared with `self`.
105 ///
106 /// Returns:
107 ///
108 /// The `contains` function is returning a boolean value indicating whether the value of `self` is
109 /// equal to the value of the reference `other`.
110 #[inline]
111 fn contains(&self, other: &i32) -> bool {
112 self == other
113 }
114}
115
116/// Defines a trait for calculating the minimum distance between two values of type `T`.
117///
118/// This trait provides a single method, `min_dist_with`, which takes a reference to another value of type `T`
119/// and returns the minimum distance between the two values as a `u32`.
120///
121/// # Examples
122///
123/// ```
124/// use physdes::generic::MinDist;
125///
126/// let a: i32 = 10;
127/// let b: i32 = 5;
128/// let distance = a.min_dist_with(&b);
129/// assert_eq!(distance, 5);
130///
131/// let a: i32 = 5;
132/// let b: i32 = 10;
133/// let distance = a.min_dist_with(&b);
134/// assert_eq!(distance, 5);
135/// ```
136pub trait MinDist<T> {
137 /// Calculates the minimum distance between the current value and the provided value.
138 ///
139 /// # Arguments
140 /// * `other` - A reference to the other value to compare against.
141 ///
142 /// # Returns
143 /// The minimum distance between the current value and the provided value, as a `u32`.
144 fn min_dist_with(&self, other: &T) -> u32;
145}
146
147/// Computes the absolute difference between two `i32` values.
148///
149/// This implementation of the `MinDist` trait for `i32` calculates the unsigned
150/// absolute difference between the current `i32` value and the provided `other`
151/// `i32` value.
152///
153/// # Examples
154///
155/// ```
156/// use physdes::generic::MinDist;
157///
158/// let a: i32 = 10;
159/// let b: i32 = 5;
160/// let distance = a.min_dist_with(&b);
161/// assert_eq!(distance, 5);
162///
163/// let a: i32 = 5;
164/// let b: i32 = 10;
165/// let distance = a.min_dist_with(&b);
166/// assert_eq!(distance, 5);
167/// ```
168impl MinDist<i32> for i32 {
169 #[inline]
170 fn min_dist_with(&self, other: &i32) -> u32 {
171 (self - other).unsigned_abs()
172 }
173}
174
175/// The `Displacement` trait defines a way to displace a value of type `T` by another value of type `T`.
176///
177/// The `displace` method takes a reference to a `T` and returns a new value of the associated `Output` type,
178/// which represents the displaced value.
179///
180/// # Examples
181///
182/// ```
183/// use physdes::generic::Displacement;
184///
185/// let a: i32 = 10;
186/// let b: i32 = 5;
187/// let displacement = a.displace(&b);
188/// assert_eq!(displacement, 5);
189///
190/// let a: i32 = 5;
191/// let b: i32 = 10;
192/// let displacement = a.displace(&b);
193/// assert_eq!(displacement, -5);
194/// ```
195pub trait Displacement<T: ?Sized> {
196 type Output;
197
198 /// Displace the current value by the provided `other` value.
199 fn displace(&self, other: &T) -> Self::Output;
200}
201
202/// Implements the `Displacement` trait for `i32` types, providing a `displace` method that subtracts
203/// the given `i32` value from the current `i32` value.
204///
205/// # Examples
206///
207/// ```
208/// use physdes::generic::Displacement;
209///
210/// let a: i32 = 10;
211/// let b: i32 = 5;
212/// let displacement = a.displace(&b);
213/// assert_eq!(displacement, 5);
214///
215/// let a: i32 = 5;
216/// let b: i32 = 10;
217/// let displacement = a.displace(&b);
218/// assert_eq!(displacement, -5);
219/// ```
220impl Displacement<i32> for i32 {
221 type Output = i32;
222
223 #[inline]
224 fn displace(&self, other: &i32) -> Self::Output {
225 self - other
226 }
227}
228
229#[cfg(test)]
230mod tests {
231 use super::*;
232
233 #[test]
234 fn test_overlap() {
235 assert!(1.overlaps(&1));
236 assert!(!1.overlaps(&2));
237 }
238
239 #[test]
240 fn test_contain() {
241 assert!(1.contains(&1));
242 assert!(!1.contains(&2));
243 }
244
245 #[test]
246 fn test_min_dist() {
247 assert_eq!(1.min_dist_with(&1), 0);
248 assert_eq!(1.min_dist_with(&2), 1);
249 assert_eq!(2.min_dist_with(&1), 1);
250 }
251
252 #[test]
253 fn test_displace() {
254 assert_eq!(1.displace(&1), 0);
255 assert_eq!(1.displace(&2), -1);
256 assert_eq!(2.displace(&1), 1);
257 }
258}