custom_display/
custom_display.rs

1use std::fmt;
2
3use fmt_ext::{display::*, DisplayExt};
4
5// Oops, this type does not implement `Display`...
6struct ExternCrateType(i32);
7
8// Create a type that will implement custom display...
9struct ExternCrateTypeDisplay;
10
11// Implement custom display...
12impl CustomDisplay for ExternCrateTypeDisplay {
13    type Target = ExternCrateType;
14
15    fn fmt_target(target: &Self::Target, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        write!(f, "Extern type has a value - {}!", target.0)
17    }
18}
19
20// Attach custom display implementation to the target type...
21impl AttachDisplay<ExternCrateTypeDisplay> for ExternCrateType {}
22
23// Look! We have just call `display` method on the target type...
24fn main() {
25    let foo = ExternCrateType(42);
26    println!("{}", foo.display());
27}