pub fn debug_diff<T>(a: T, b: T) -> impl Debugwhere
T: Diff,
Expand description
Given two values that can be diffed, returns an object that will describe
their differences when formatted using Debug
.
The result can be used anywhere you’d normally format something with
Debug
, such as calls to println!
:
use visit_diff::{Diff, debug_diff};
#[derive(Diff, Debug)]
struct ExampleStruct {
name: &'static str,
age: usize,
}
let left = ExampleStruct { name: "Bob", age: 4 };
let right = ExampleStruct { name: "Rototron 3k", age: 5 };
println!("Compact: {:?}", debug_diff(&left, &right));
println!("Pretty: {:#?}", debug_diff(&left, &right));
This prints:
Compact: DIFF { L: ExampleStruct { name: "Bob", age: 4 }, R: ExampleStruct { name: "Rototron 3k", age: 5 } }
Pretty: DIFF {
L: ExampleStruct {
name: "Bob",
age: 4
},
R: ExampleStruct {
name: "Rototron 3k",
age: 5
}
}
This is showing the DIFF
at the top level, because all fields of the
structs are different. If only a single field is different, a more precise
diff happens:
let left = ExampleStruct { name: "Bob", age: 4 };
let right = ExampleStruct { name: "Bob", age: 5 };
println!("Compact: {:?}", debug_diff(&left, &right));
println!("Pretty: {:#?}", debug_diff(&left, &right));
This now prints:
Compact: ExampleStruct { name: "Bob", age: DIFF { L: 4, R: 5 } }
Pretty: ExampleStruct {
name: "Bob",
age: DIFF {
L: 4,
R: 5
}
}
If you’re curious: debug_diff
uses all_different
to decide to pull a
DIFF
indicator up one level of structure.
Examples found in repository?
examples/basic.rs (line 66)
34fn main() {
35 let a = Newtype(Top {
36 child1: Child1 {
37 name: "Sprocket",
38 size: 12,
39 },
40 others: vec![
41 Other::Prince,
42 Other::Bob {
43 last_name: "Roberts",
44 },
45 ],
46 });
47
48 let b = Newtype(Top {
49 // Note: both name and size are different.
50 child1: Child1 {
51 name: "Ralph",
52 size: usize::max_value(),
53 },
54 others: vec![
55 Other::Prince,
56 Other::Bob {
57 last_name: "Roberts",
58 },
59 // added
60 Other::Bob {
61 last_name: "Bobberson",
62 },
63 ],
64 });
65
66 println!("{:#?}", debug_diff(a, b));
67}