use crate::Diffable;
use core::ops::{Deref, DerefMut};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Leaf<T> {
pub before: T,
pub after: T,
}
impl<T> Leaf<T> {
#[inline]
pub fn as_ref(&self) -> Leaf<&T> {
Leaf { before: &self.before, after: &self.after }
}
#[inline]
pub fn as_mut(&mut self) -> Leaf<&mut T> {
Leaf { before: &mut self.before, after: &mut self.after }
}
#[inline]
pub fn as_deref(&self) -> Leaf<&T::Target>
where
T: Deref,
{
Leaf { before: &*self.before, after: &*self.after }
}
#[inline]
pub fn as_deref_mut(&mut self) -> Leaf<&mut T::Target>
where
T: DerefMut,
{
Leaf { before: &mut *self.before, after: &mut *self.after }
}
#[inline]
pub fn map<U, F>(self, mut f: F) -> Leaf<U>
where
F: FnMut(T) -> U,
{
Leaf { before: f(self.before), after: f(self.after) }
}
#[inline]
pub fn is_unchanged(&self) -> bool
where
T: Eq,
{
self.before == self.after
}
#[inline]
pub fn is_modified(&self) -> bool
where
T: Eq,
{
self.before != self.after
}
}
impl<'daft, T: ?Sized + Diffable> Leaf<&'daft T> {
#[inline]
pub fn diff_pair(self) -> T::Diff<'daft> {
self.before.diff(self.after)
}
}
impl<T> Leaf<&T> {
#[inline]
pub fn cloned(self) -> Leaf<T>
where
T: Clone,
{
Leaf { before: self.before.clone(), after: self.after.clone() }
}
#[inline]
pub fn copied(self) -> Leaf<T>
where
T: Copy,
{
Leaf { before: *self.before, after: *self.after }
}
}