Skip to main content

Differ

Struct Differ 

Source
pub struct Differ<'c, T> { /* private fields */ }
Expand description

A configurable diff, for when diff isn’t enough.

use fastanim_diff::{Differ, Op};

let a = ["a", "²", "+", "b", "²", "=", "c", "²"];
let b = ["a", "²", "=", "c", "²", "-", "b", "²"];
let ops = Differ::new(&a, &b, |t| *t)
    .class(|t| matches!(*t, "+" | "-").then_some("operator"))
    .run();
assert!(ops.contains(&Op::Replace { a: 2..3, b: 5..6 }));

Implementations§

Source§

impl<'c, T> Differ<'c, T>

Source

pub fn new<K: Eq + Hash>(a: &'c [T], b: &'c [T], key: impl Fn(&T) -> K) -> Self

Prepares a diff of a against b, comparing items by key.

Source

pub fn options(self, opts: DiffOptions) -> Self

Sets the options.

Source

pub fn cost(self, cost: impl Fn(usize, usize) -> f64 + 'c) -> Self

Sets the cost of pairing a[i] with b[j] in move detection and cross-hunk replacement pairing, typically the distance between their centroids. Lower is better. Defaults to the index distance |i - j|.

Source

pub fn class<C: Eq + Hash>(self, class: impl Fn(&T) -> Option<C>) -> Self

Enables cross-hunk replacement pairing: leftover deletions and insertions whose class is Some and equal may be paired into a single-item Op::Replace.

Source

pub fn run(&self) -> Vec<Op>

Computes the edit script.

Auto Trait Implementations§

§

impl<'c, T> !RefUnwindSafe for Differ<'c, T>

§

impl<'c, T> !Send for Differ<'c, T>

§

impl<'c, T> !Sync for Differ<'c, T>

§

impl<'c, T> !UnwindSafe for Differ<'c, T>

§

impl<'c, T> Freeze for Differ<'c, T>
where &'c [T]: Freeze,

§

impl<'c, T> Unpin for Differ<'c, T>
where &'c [T]: Unpin,

§

impl<'c, T> UnsafeUnpin for Differ<'c, T>
where &'c [T]: UnsafeUnpin,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.