Leaf

Struct Leaf 

Source
pub struct Leaf<T> {
    pub before: T,
    pub after: T,
}
Expand description

A primitive or atomic change.

T is normally a reference of some kind, but it can be any type.

For more information, see the crate-level documentation.

Fields§

§before: T

The value on the before side.

§after: T

The value on the after side.

Implementations§

Source§

impl<T> Leaf<T>

Source

pub fn as_ref(&self) -> Leaf<&T>

Convert from &Leaf<T> to Leaf<&T>.

Source

pub fn as_mut(&mut self) -> Leaf<&mut T>

Convert from &mut Leaf<T> to Leaf<&mut T>.

Source

pub fn as_deref(&self) -> Leaf<&T::Target>
where T: Deref,

Convert from Leaf<T> or &Leaf<T> to Leaf<&T::Target>.

§Example
use daft::Leaf;

let x: Leaf<String> = Leaf { before: "hello".to_owned(), after: "world".to_owned() };
assert_eq!(x.as_deref(), Leaf { before: "hello", after: "world" });
Source

pub fn as_deref_mut(&mut self) -> Leaf<&mut T::Target>
where T: DerefMut,

Convert from Leaf<T> or &mut Leaf<T> to Leaf<&mut T::Target>.

§Example
use daft::Leaf;

let mut x: Leaf<String> = Leaf { before: "hello".to_owned(), after: "world".to_owned() };
assert_eq!(
    x.as_deref_mut().map(|x| {
        x.make_ascii_uppercase();
        x
    }),
    Leaf { before: "HELLO".to_owned().as_mut_str(), after: "WORLD".to_owned().as_mut_str() },
);
Source

pub fn map<U, F>(self, f: F) -> Leaf<U>
where F: FnMut(T) -> U,

Map a Leaf<T> to a Leaf<U> by applying a function to the before and after values.

f will be called twice: first with before, then with after.

§Example

A Leaf<&str> can be converted to a Leaf<String> by calling map with String::from:

use daft::{Diffable, Leaf};

let before = "hello";
let after = "world";

let leaf: Leaf<&str> = Leaf { before, after };
let leaf: Leaf<String> = leaf.map(String::from);
Source

pub fn is_unchanged(&self) -> bool
where T: Eq,

Return true if before is the same as after.

This is the same as self.before == self.after, but is easier to use in a chained series of method calls.

§Example
use daft::{Diffable, Leaf};

let before = "hello";
let after = "hello";

let leaf: Leaf<&str> = Leaf { before, after };
assert!(leaf.is_unchanged());

let before = "hello";
let after = "world";

let leaf: Leaf<&str> = Leaf { before, after };
assert!(!leaf.is_unchanged());
Source

pub fn is_modified(&self) -> bool
where T: Eq,

Return true if before is different from after.

This is the same as self.before != self.after, but is easier to use in a chained series of method calls.

§Example
use daft::{Diffable, Leaf};

let before = "hello";
let after = "hello";

let leaf: Leaf<&str> = Leaf { before, after };
assert!(!leaf.is_modified());

let before = "hello";
let after = "world";

let leaf: Leaf<&str> = Leaf { before, after };
assert!(leaf.is_modified());
Source§

impl<'daft, T: ?Sized + Diffable> Leaf<&'daft T>

Source

pub fn diff_pair(self) -> T::Diff<'daft>

Perform a diff on before and after, returning T::Diff.

This is useful when T::Diff is not a leaf node.

Source§

impl<T> Leaf<&T>

Source

pub fn cloned(self) -> Leaf<T>
where T: Clone,

Create a clone of the leaf with owned values.

§Example
use daft::{Diffable, Leaf};

let before = String::from("hello");
let after = String::from("world");

let leaf: Leaf<&String> = Leaf { before: &before, after: &after };
// cloned turns a Leaf<&String> into a Leaf<String>.
let leaf: Leaf<String> = leaf.cloned();
assert_eq!(leaf.before, "hello");
assert_eq!(leaf.after, "world");
Source

pub fn copied(self) -> Leaf<T>
where T: Copy,

Create a copy of the leaf with owned values.

§Example
use daft::{Diffable, Leaf};

let before = "hello";
let after = "world";

let leaf: Leaf<&&str> = Leaf { before: &before, after: &after };
// copied turns a Leaf<&&str> into a Leaf<&str>.
let leaf: Leaf<&str> = leaf.copied();
assert_eq!(leaf.before, "hello");
assert_eq!(leaf.after, "world");

Trait Implementations§

Source§

impl<T: Clone> Clone for Leaf<T>

Source§

fn clone(&self) -> Leaf<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Leaf<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: PartialEq> PartialEq for Leaf<T>

Source§

fn eq(&self, other: &Leaf<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Copy> Copy for Leaf<T>

Source§

impl<T: Eq> Eq for Leaf<T>

Source§

impl<T> StructuralPartialEq for Leaf<T>

Auto Trait Implementations§

§

impl<T> Freeze for Leaf<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Leaf<T>
where T: RefUnwindSafe,

§

impl<T> Send for Leaf<T>
where T: Send,

§

impl<T> Sync for Leaf<T>
where T: Sync,

§

impl<T> Unpin for Leaf<T>
where T: Unpin,

§

impl<T> UnwindSafe for Leaf<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.