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.
5pub trait Overlap<T> {
6    fn overlaps(&self, other: &T) -> bool;
7}
8
9/// Checks if two `i32` values are equal.
10///
11/// This implementation of the `Overlap` trait for `i32` simply checks if the two values are equal.
12///
13/// # Examples
14///
15/// use your_crate::generic::Overlap;
16///
17/// let a: i32 = 42;
18/// let b: i32 = 42;
19/// assert!(a.overlaps(&b));
20impl Overlap<i32> for i32 {
21    /// The `overlaps` function in Rust checks if two references to `i32` values point to the same
22    /// memory location.
23    ///
24    /// Arguments:
25    ///
26    /// * `other`: The `other` parameter in the `overlaps` function is a reference to an `i32` value.
27    ///
28    /// Returns:
29    ///
30    /// The `overlaps` function is returning a boolean value indicating whether the value of `self` is
31    /// equal to the value of `other`.
32    #[inline]
33    fn overlaps(&self, other: &i32) -> bool {
34        self == other
35    }
36}
37
38/// The `trait Contain<T>` defines a method `contains` that checks if an object of type `T` is
39/// contained within another object. The `contains` method takes a reference to another object of type
40/// `T` as a parameter and returns a boolean value indicating whether the object is contained within the
41/// other object or not. This trait can be implemented for any type that needs to support the `contains`
42/// functionality.
43pub trait Contain<T> {
44    fn contains(&self, other: &T) -> bool;
45}
46
47/// Checks if the current `i32` value is equal to the provided `i32` value.
48///
49/// This implementation of the `Contain` trait for `i32` simply compares the two values for equality.
50///
51/// # Examples
52///
53/// use your_crate::generic::Overlap;
54///
55/// let a: i32 = 42;
56/// let b: i32 = 42;
57/// assert!(a.contains(&b));
58impl Contain<i32> for i32 {
59    /// The function checks if a given value is equal to another value.
60    ///
61    /// Arguments:
62    ///
63    /// * `other`: The `other` parameter in the `contains` function is a reference to an `i32` value
64    ///             that is being compared with `self`.
65    ///
66    /// Returns:
67    ///
68    /// The `contains` function is returning a boolean value indicating whether the value of `self` is
69    /// equal to the value of the reference `other`.
70    #[inline]
71    fn contains(&self, other: &i32) -> bool {
72        self == other
73    }
74}
75
76/// Defines a trait for calculating the minimum distance between two values of type `T`.
77///
78/// This trait provides a single method, `min_dist_with`, which takes a reference to another value of type `T`
79/// and returns the minimum distance between the two values as a `u32`.
80pub trait MinDist<T> {
81    /// Calculates the minimum distance between the current value and the provided value.
82    ///
83    /// # Arguments
84    /// * `other` - A reference to the other value to compare against.
85    ///
86    /// # Returns
87    /// The minimum distance between the current value and the provided value, as a `u32`.
88    fn min_dist_with(&self, other: &T) -> u32;
89}
90
91/// Computes the absolute difference between two `i32` values.
92///
93/// This implementation of the `MinDist` trait for `i32` calculates the unsigned
94/// absolute difference between the current `i32` value and the provided `other`
95/// `i32` value.
96///
97/// # Examples
98///
99/// use your_crate::MinDist;
100///
101/// let a: i32 = 10;
102/// let b: i32 = 5;
103/// let distance = a.min_dist_with(&b); // distance = 5
104impl MinDist<i32> for i32 {
105    #[inline]
106    fn min_dist_with(&self, other: &i32) -> u32 {
107        (self - other).unsigned_abs()
108    }
109}
110
111/// The `Displacement` trait defines a way to displace a value of type `T` by another value of type `T`.
112///
113/// The `displace` method takes a reference to a `T` and returns a new value of the associated `Output` type,
114/// which represents the displaced value.
115pub trait Displacement<T: ?Sized> {
116    type Output;
117
118    /// Displace the current value by the provided `other` value.
119    fn displace(&self, other: &T) -> Self::Output;
120}
121
122/// Implements the `Displacement` trait for `i32` types, providing a `displace` method that subtracts
123/// the given `i32` value from the current `i32` value.
124impl Displacement<i32> for i32 {
125    type Output = i32;
126
127    #[inline]
128    fn displace(&self, other: &i32) -> Self::Output {
129        self - other
130    }
131}
132
133#[cfg(test)]
134mod tests {
135    use super::*;
136
137    #[test]
138    fn test_overlap() {
139        assert!(1.overlaps(&1));
140        assert!(!1.overlaps(&2));
141    }
142
143    #[test]
144    fn test_contain() {
145        assert!(1.contains(&1));
146        assert!(!1.contains(&2));
147    }
148
149    #[test]
150    fn test_min_dist() {
151        assert_eq!(1.min_dist_with(&1), 0);
152        assert_eq!(1.min_dist_with(&2), 1);
153        assert_eq!(2.min_dist_with(&1), 1);
154    }
155
156    #[test]
157    fn test_displace() {
158        assert_eq!(1.displace(&1), 0);
159        assert_eq!(1.displace(&2), -1);
160        assert_eq!(2.displace(&1), 1);
161    }
162}