fmt_ext 0.1.2

A simple interface for extending external types with custom implementations of `Debug` and `Display` traits.
Documentation
  • Coverage
  • 88.89%
    16 out of 18 items documented2 out of 12 items with examples
  • Size
  • Source code size: 11.62 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.3 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • ilyavennik/fmt-ext
    0 0 1
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ilyavenner

fmt_ext

Crates.io docs.rs

A simple interface for extending external types with custom implementations of Debug and Display traits.

The example below shows how implement custom debug formatting for slices that additionally print their length.

use std::{fmt, marker::PhantomData};

use fmt_ext::{debug::*, DebugExt};

// Create a type that will implement custom debug...
struct SliceWithLenDebug<T>(PhantomData<T>);

// Implement custom debug...
impl<T> CustomDebug for SliceWithLenDebug<T>
where
    T: fmt::Debug,
{
    type Target = [T];

    fn fmt_target(target: &Self::Target, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Slice {{ len: {}, items: {:?} }}", target.len(), target)
    }
}

// Attach custom debug implementation to the target type...
impl<T> AttachDebug<SliceWithLenDebug<T>> for [T] {}

// Look! Now we have just call `debug` method on the target type...
fn main() {
    let numbers = [0, 1, 2, 3];
    println!("{:?}", numbers.debug());

    let strings = vec!["I", "am", "a", "custom", "debug"];
    println!("{:?}", strings.debug());
}

All examples are in this directory.

Support of #![no_std]

fmt_ext supports #![no_std] mode by default.

License

fmt_ext is distributed under the terms of the MIT license.