| 1 | use crate::Diffable; |
| 2 | use core::ops::{Deref, DerefMut}; |
| 3 | |
| 4 | /// A primitive or atomic change. |
| 5 | /// |
| 6 | /// `T` is normally a reference of some kind, but it can be any type. |
| 7 | /// |
| 8 | /// For more information, see the [crate-level documentation](crate). |
| 9 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 10 | pub struct Leaf<T> { |
| 11 | /// The value on the before side. |
| 12 | pub before: T, |
| 13 | |
| 14 | /// The value on the after side. |
| 15 | pub after: T, |
| 16 | } |
| 17 | |
| 18 | impl<T> Leaf<T> { |
| 19 | /// Convert from `&Leaf<T>` to `Leaf<&T>`. |
| 20 | #[inline] |
| 21 | pub fn as_ref(&self) -> Leaf<&T> { |
| 22 | Leaf { before: &self.before, after: &self.after } |
| 23 | } |
| 24 | |
| 25 | /// Convert from `&mut Leaf<T>` to `Leaf<&mut T>`. |
| 26 | #[inline] |
| 27 | pub fn as_mut(&mut self) -> Leaf<&mut T> { |
| 28 | Leaf { before: &mut self.before, after: &mut self.after } |
| 29 | } |
| 30 | |
| 31 | /// Convert from `Leaf<T>` or `&Leaf<T>` to `Leaf<&T::Target>`. |
| 32 | /// |
| 33 | /// # Example |
| 34 | /// |
| 35 | /// ``` |
| 36 | /// # #[cfg(feature = "std")] { |
| 37 | /// use daft::Leaf; |
| 38 | /// |
| 39 | /// let x: Leaf<String> = Leaf { before: "hello".to_owned(), after: "world".to_owned() }; |
| 40 | /// assert_eq!(x.as_deref(), Leaf { before: "hello", after: "world" }); |
| 41 | /// # } |
| 42 | /// ``` |
| 43 | #[inline] |
| 44 | pub fn as_deref(&self) -> Leaf<&T::Target> |
| 45 | where |
| 46 | T: Deref, |
| 47 | { |
| 48 | Leaf { before: &*self.before, after: &*self.after } |
| 49 | } |
| 50 | |
| 51 | /// Convert from `Leaf<T>` or `&mut Leaf<T>` to `Leaf<&mut T::Target>`. |
| 52 | /// |
| 53 | /// # Example |
| 54 | /// |
| 55 | /// ``` |
| 56 | /// # #[cfg(feature = "std")] { |
| 57 | /// use daft::Leaf; |
| 58 | /// |
| 59 | /// let mut x: Leaf<String> = Leaf { before: "hello".to_owned(), after: "world".to_owned() }; |
| 60 | /// assert_eq!( |
| 61 | /// x.as_deref_mut().map(|x| { |
| 62 | /// x.make_ascii_uppercase(); |
| 63 | /// x |
| 64 | /// }), |
| 65 | /// Leaf { before: "HELLO".to_owned().as_mut_str(), after: "WORLD".to_owned().as_mut_str() }, |
| 66 | /// ); |
| 67 | /// # } |
| 68 | /// ``` |
| 69 | #[inline] |
| 70 | pub fn as_deref_mut(&mut self) -> Leaf<&mut T::Target> |
| 71 | where |
| 72 | T: DerefMut, |
| 73 | { |
| 74 | Leaf { before: &mut *self.before, after: &mut *self.after } |
| 75 | } |
| 76 | |
| 77 | /// Map a `Leaf<T>` to a `Leaf<U>` by applying a function to the `before` |
| 78 | /// and `after` values. |
| 79 | /// |
| 80 | /// `f` will be called twice: first with `before`, then with `after`. |
| 81 | /// |
| 82 | /// # Example |
| 83 | /// |
| 84 | /// A `Leaf<&str>` can be converted to a `Leaf<String>` by calling `map` |
| 85 | /// with `String::from`: |
| 86 | /// |
| 87 | /// ``` |
| 88 | /// # #[cfg(feature = "std")] { |
| 89 | /// use daft::{Diffable, Leaf}; |
| 90 | /// |
| 91 | /// let before = "hello"; |
| 92 | /// let after = "world"; |
| 93 | /// |
| 94 | /// let leaf: Leaf<&str> = Leaf { before, after }; |
| 95 | /// let leaf: Leaf<String> = leaf.map(String::from); |
| 96 | /// # } |
| 97 | /// ``` |
| 98 | #[inline] |
| 99 | pub fn map<U, F>(self, mut f: F) -> Leaf<U> |
| 100 | where |
| 101 | F: FnMut(T) -> U, |
| 102 | { |
| 103 | Leaf { before: f(self.before), after: f(self.after) } |
| 104 | } |
| 105 | |
| 106 | /// Return true if before is the same as after. |
| 107 | /// |
| 108 | /// This is the same as `self.before == self.after`, but is easier to use in |
| 109 | /// a chained series of method calls. |
| 110 | /// |
| 111 | /// # Example |
| 112 | /// |
| 113 | /// ``` |
| 114 | /// use daft::{Diffable, Leaf}; |
| 115 | /// |
| 116 | /// let before = "hello"; |
| 117 | /// let after = "hello"; |
| 118 | /// |
| 119 | /// let leaf: Leaf<&str> = Leaf { before, after }; |
| 120 | /// assert!(leaf.is_unchanged()); |
| 121 | /// |
| 122 | /// let before = "hello"; |
| 123 | /// let after = "world"; |
| 124 | /// |
| 125 | /// let leaf: Leaf<&str> = Leaf { before, after }; |
| 126 | /// assert!(!leaf.is_unchanged()); |
| 127 | /// ``` |
| 128 | #[inline] |
| 129 | pub fn is_unchanged(&self) -> bool |
| 130 | where |
| 131 | T: Eq, |
| 132 | { |
| 133 | self.before == self.after |
| 134 | } |
| 135 | |
| 136 | /// Return true if before is different from after. |
| 137 | /// |
| 138 | /// This is the same as `self.before != self.after`, but is easier to use in |
| 139 | /// a chained series of method calls. |
| 140 | /// |
| 141 | /// # Example |
| 142 | /// |
| 143 | /// ``` |
| 144 | /// use daft::{Diffable, Leaf}; |
| 145 | /// |
| 146 | /// let before = "hello"; |
| 147 | /// let after = "hello"; |
| 148 | /// |
| 149 | /// let leaf: Leaf<&str> = Leaf { before, after }; |
| 150 | /// assert!(!leaf.is_modified()); |
| 151 | /// |
| 152 | /// let before = "hello"; |
| 153 | /// let after = "world"; |
| 154 | /// |
| 155 | /// let leaf: Leaf<&str> = Leaf { before, after }; |
| 156 | /// assert!(leaf.is_modified()); |
| 157 | /// ``` |
| 158 | #[inline] |
| 159 | pub fn is_modified(&self) -> bool |
| 160 | where |
| 161 | T: Eq, |
| 162 | { |
| 163 | self.before != self.after |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | impl<'daft, T: ?Sized + Diffable> Leaf<&'daft T> { |
| 168 | /// Perform a diff on [`before`][Self::before] and [`after`][Self::after], |
| 169 | /// returning `T::Diff`. |
| 170 | /// |
| 171 | /// This is useful when `T::Diff` is not a leaf node. |
| 172 | #[inline] |
| 173 | pub fn diff_pair(self) -> T::Diff<'daft> { |
| 174 | self.before.diff(self.after) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | impl<T> Leaf<&T> { |
| 179 | /// Create a clone of the leaf with owned values. |
| 180 | /// |
| 181 | /// # Example |
| 182 | /// |
| 183 | /// ``` |
| 184 | /// # #[cfg(feature = "std")] { |
| 185 | /// use daft::{Diffable, Leaf}; |
| 186 | /// |
| 187 | /// let before = String::from("hello"); |
| 188 | /// let after = String::from("world"); |
| 189 | /// |
| 190 | /// let leaf: Leaf<&String> = Leaf { before: &before, after: &after }; |
| 191 | /// // cloned turns a Leaf<&String> into a Leaf<String>. |
| 192 | /// let leaf: Leaf<String> = leaf.cloned(); |
| 193 | /// assert_eq!(leaf.before, "hello"); |
| 194 | /// assert_eq!(leaf.after, "world"); |
| 195 | /// # } |
| 196 | /// ``` |
| 197 | #[inline] |
| 198 | pub fn cloned(self) -> Leaf<T> |
| 199 | where |
| 200 | T: Clone, |
| 201 | { |
| 202 | Leaf { before: self.before.clone(), after: self.after.clone() } |
| 203 | } |
| 204 | |
| 205 | /// Create a copy of the leaf with owned values. |
| 206 | /// |
| 207 | /// # Example |
| 208 | /// |
| 209 | /// ``` |
| 210 | /// use daft::{Diffable, Leaf}; |
| 211 | /// |
| 212 | /// let before = "hello"; |
| 213 | /// let after = "world"; |
| 214 | /// |
| 215 | /// let leaf: Leaf<&&str> = Leaf { before: &before, after: &after }; |
| 216 | /// // copied turns a Leaf<&&str> into a Leaf<&str>. |
| 217 | /// let leaf: Leaf<&str> = leaf.copied(); |
| 218 | /// assert_eq!(leaf.before, "hello"); |
| 219 | /// assert_eq!(leaf.after, "world"); |
| 220 | /// ``` |
| 221 | #[inline] |
| 222 | pub fn copied(self) -> Leaf<T> |
| 223 | where |
| 224 | T: Copy, |
| 225 | { |
| 226 | Leaf { before: *self.before, after: *self.after } |
| 227 | } |
| 228 | } |