mutable/
cmp.rs

1/// See [SoftEq]
2
3use core::clone::Clone;
4use core::cmp::Eq;
5use core::marker::Sized;
6
7/// Define soft (partial) equality
8///
9/// Two elements are "softly equals" if they can be considered as two different stages of the same element, i.e. their hard parts are equals (c.f. below)
10/// This is mainly used by the [Vec] implementation of [Mutable], to determine when an item as mutated
11///
12/// We can distinguish two parts of a SoftEq element:
13/// - the hard part, which is unique for each element (a uid String, for example)
14/// - the soft part, which may variate for the same element (like a charge attribute for an electrical item)
15///
16/// ```
17/// # use mutable::cmp::SoftEq;
18/// # use mutable_derive::SoftEq;
19/// #[derive(SoftEq)]
20/// struct Item {
21///     #[softeq(uid)]
22///     id: String,
23///     charge: usize,
24/// }
25///
26/// # fn main() {
27/// let i0 = Item { id: "tear".to_string(), charge: 32 };
28/// let i1 = Item { id: "tear".to_string(), charge: 12 };
29/// let i2 = Item { id: "draktaar".to_string(), charge: 32 };
30///
31/// assert!(i0.se(&i1));
32/// assert!(i0.nse(&i2));
33/// assert!(i1.nse(&i2));
34/// # }
35/// ```
36pub trait SoftEq where Self: Sized {
37    /// The type of the hard part
38    type Uid: Eq + Clone;
39
40    /// Whether self and other are softly equals (their hard part is identical)
41    fn se(&self, other: &Self) -> bool { self.uid() == other.uid() }
42    /// Whether self and other are softly different (their hard part differs)
43    fn nse(&self, other: &Self) -> bool { !self.se(other) }
44
45    /// The hard part of self
46    fn uid(&self) -> Self::Uid;
47}