pub trait Diff: Debug {
// Required method
fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
where D: Differ;
}Expand description
A type that can be compared structurally to discover differences.
The job of a Diff impl is to use knowledge of the structure of some type
(Self) to check two copies for differences, and report findings to a
Differ.
Differ has methods for each different flavor of Rust type, and you can
only call one of them (as they consume the Differ). So, the first task
when implementing Diff is to decide which one to call.
Very simple types may just dispatch to same or difference, like this
impl for a newtype around u32:
use visit_diff::{Diff, Differ};
#[derive(Debug)]
struct MyInt(u32);
impl Diff for MyInt {
fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>
where D: Differ
{
if a.0 == b.0 {
out.same(&a, &b)
} else {
out.difference(&a, &b)
}
}
}
use visit_diff::any_difference;
assert_eq!(any_difference(&MyInt(1), &MyInt(1)), false);
assert_eq!(any_difference(&MyInt(1), &MyInt(0)), true);(Note: in reality, you’d probably want to #[derive(Diff)] for a type this
simple.)
More complicated types would use other methods on Differ to describe
their structure. See the documentation of that trait for more.
Required Methods§
Sourcefn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>where
D: Differ,
fn diff<D>(a: &Self, b: &Self, out: D) -> Result<D::Ok, D::Err>where
D: Differ,
Inspect a and b and tell out about any differences.
All (reasonable) implementations of this method have the same basic
structure: they call a method on out, and then follow the instructions
from that method to get a result.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl Diff for SocketAddr
impl Diff for SocketAddr
Source§impl Diff for SocketAddrV4
impl Diff for SocketAddrV4
Source§impl Diff for SocketAddrV6
impl Diff for SocketAddrV6
Source§impl<A, B, C, D> Diff for (A, B, C, D)
impl<A, B, C, D> Diff for (A, B, C, D)
Source§impl<A, B, C, D, E> Diff for (A, B, C, D, E)
impl<A, B, C, D, E> Diff for (A, B, C, D, E)
Source§impl<A, B, C, D, E, F> Diff for (A, B, C, D, E, F)
impl<A, B, C, D, E, F> Diff for (A, B, C, D, E, F)
Source§impl<A, B, C, D, E, F, G> Diff for (A, B, C, D, E, F, G)
impl<A, B, C, D, E, F, G> Diff for (A, B, C, D, E, F, G)
Source§impl<A, B, C, D, E, F, G, H> Diff for (A, B, C, D, E, F, G, H)
impl<A, B, C, D, E, F, G, H> Diff for (A, B, C, D, E, F, G, H)
Source§impl<A, B, C, D, E, F, G, H, I> Diff for (A, B, C, D, E, F, G, H, I)
impl<A, B, C, D, E, F, G, H, I> Diff for (A, B, C, D, E, F, G, H, I)
Source§impl<T> Diff for RangeToInclusive<T>
impl<T> Diff for RangeToInclusive<T>
Source§impl<T: ?Sized + Diff> Diff for RefCell<T>
Note that this will panic if the RefCell is mutably borrowed.
impl<T: ?Sized + Diff> Diff for RefCell<T>
Note that this will panic if the RefCell is mutably borrowed.