BTreeMapDiff

Struct BTreeMapDiff 

Source
pub struct BTreeMapDiff<'daft, K: Ord + Eq, V> {
    pub common: BTreeMap<&'daft K, Leaf<&'daft V>>,
    pub added: BTreeMap<&'daft K, &'daft V>,
    pub removed: BTreeMap<&'daft K, &'daft V>,
}
Available on crate feature alloc only.
Expand description

A diff of two BTreeMap instances.

The diff contains three elements:

  • common: Entries that are present in both maps, with their values stored as a Leaf.
  • added: Entries present in after, but not in before.
  • removed: Entries present in before, but not in after.

If V implements Eq, common can be split into unchanged and modified entries. Additionally, if V implements Diffable, modified_diff can be used to recursively diff modified entries.

§Example

use daft::{BTreeMapDiff, Diffable, Leaf};
use std::collections::BTreeMap;

let a: BTreeMap<usize, &str> =
    [(0, "lorem"), (1, "ipsum"), (2, "dolor")].into_iter().collect();
let b: BTreeMap<usize, &str> =
   [(1, "ipsum"), (2, "sit"), (3, "amet")].into_iter().collect();

let changes = a.diff(&b);
let expected = BTreeMapDiff {
    // Keys are stored by reference and matched by equality.
    common: [
        (&1, Leaf { before: &"ipsum", after: &"ipsum" }),
        (&2, Leaf { before: &"dolor", after: &"sit" }),
    ].into_iter().collect(),
    added: [(&3, &"amet")].into_iter().collect(),
    removed: [(&0, &"lorem")].into_iter().collect(),
};

assert_eq!(changes, expected);

// If the values are `Eq`, it's also possible to get lists of
// modified and unchanged entries.
assert!(changes.is_unchanged(&1));
assert!(changes.is_modified(&2));
let unchanged = changes.unchanged().collect::<Vec<_>>();
let modified = changes.modified().collect::<Vec<_>>();

assert_eq!(unchanged, [(&1, &"ipsum")]);
assert_eq!(modified, [(&2, Leaf { before: &"dolor", after: &"sit" })]);

Fields§

§common: BTreeMap<&'daft K, Leaf<&'daft V>>

Entries common to both maps.

Values are stored as Leafs to references.

§added: BTreeMap<&'daft K, &'daft V>

Entries present in the after map, but not in before.

§removed: BTreeMap<&'daft K, &'daft V>

Entries present in the before map, but not in after.

Implementations§

Source§

impl<'daft, K: Ord + Eq, V> BTreeMapDiff<'daft, K, V>

Source

pub fn new() -> Self

Create a new, empty BTreeMapDiff instance.

Source§

impl<'daft, K: Ord + Eq, V: Eq> BTreeMapDiff<'daft, K, V>

Source

pub fn unchanged(&self) -> impl Iterator<Item = (&'daft K, &'daft V)> + '_

Return an iterator over unchanged keys and values.

Source

pub fn is_unchanged(&self, key: &K) -> bool

Return true if the value corresponding to the key is unchanged.

Source

pub fn get_unchanged(&self, key: &K) -> Option<&'daft V>

Return the value associated with the key if it is unchanged, otherwise None.

Source

pub fn unchanged_keys(&self) -> impl Iterator<Item = &'daft K> + '_

Return an iterator over unchanged keys.

Source

pub fn unchanged_values(&self) -> impl Iterator<Item = &'daft V> + '_

Return an iterator over unchanged values.

Source

pub fn modified(&self) -> impl Iterator<Item = (&'daft K, Leaf<&'daft V>)> + '_

Return an iterator over modified keys and values.

Source

pub fn is_modified(&self, key: &K) -> bool

Return true if the value corresponding to the key is modified.

Source

pub fn get_modified(&self, key: &K) -> Option<Leaf<&'daft V>>

Return the Leaf associated with the key if it is modified, otherwise None.

Source

pub fn modified_keys(&self) -> impl Iterator<Item = &'daft K> + '_

Return an iterator over modified keys.

Source

pub fn modified_values(&self) -> impl Iterator<Item = Leaf<&'daft V>> + '_

Return an iterator over modified values.

Source

pub fn modified_diff( &self, ) -> impl Iterator<Item = (&'daft K, V::Diff<'daft>)> + '_
where V: Diffable,

Return an iterator over modified keys and values, performing a diff on the values.

This is useful when V::Diff is a complex type, not just a Leaf.

Source

pub fn modified_values_diff(&self) -> impl Iterator<Item = V::Diff<'daft>> + '_
where V: Diffable,

Return an iterator over modified values, performing a diff on them.

This is useful when V::Diff is a complex type, not just a Leaf.

Trait Implementations§

Source§

impl<'daft, K: Debug + Ord + Eq, V: Debug> Debug for BTreeMapDiff<'daft, K, V>

Source§

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

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

impl<'daft, K: Ord + Eq, V> Default for BTreeMapDiff<'daft, K, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'daft, K: PartialEq + Ord + Eq, V: PartialEq> PartialEq for BTreeMapDiff<'daft, K, V>

Source§

fn eq(&self, other: &BTreeMapDiff<'daft, K, V>) -> 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<'daft, K: Eq + Ord + Eq, V: Eq> Eq for BTreeMapDiff<'daft, K, V>

Source§

impl<'daft, K: Ord + Eq, V> StructuralPartialEq for BTreeMapDiff<'daft, K, V>

Auto Trait Implementations§

§

impl<'daft, K, V> Freeze for BTreeMapDiff<'daft, K, V>

§

impl<'daft, K, V> RefUnwindSafe for BTreeMapDiff<'daft, K, V>

§

impl<'daft, K, V> Send for BTreeMapDiff<'daft, K, V>
where K: Sync, V: Sync,

§

impl<'daft, K, V> Sync for BTreeMapDiff<'daft, K, V>
where K: Sync, V: Sync,

§

impl<'daft, K, V> Unpin for BTreeMapDiff<'daft, K, V>

§

impl<'daft, K, V> UnwindSafe for BTreeMapDiff<'daft, K, V>

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> 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, 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.