maybe-debug 0.1.0

impl Debug for Any (via specialization)
Documentation
  • Coverage
  • 100%
    16 out of 16 items documented3 out of 16 items with examples
  • Size
  • Source code size: 31.62 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.15 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Techcable/rust-maybe-debug
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Techcable

maybe-debug

Implement Debug for anything via specialization.

Lets say you have the following function and you want to insert a dbg!() statement inside the loop.

fn sort<T>(target: &mut [T]) {
    for (i, val) in target.iter().enumerate() {
        dbg!(i);
        // various sorting goodness
        dbg!(i, val); // ERROR: T is not Debug
    }
}

You can use maybe_debug::maybe_debug() to work around this. If T is Debug it will 'cast' it. If T is !Debug, it will fallback to a reasonable default (printing the type name).

fn sort<T>(target: &mut [T]) {
    for (i, val) in target.iter().enumerate() {
        maybe_debug::dbg!(i);
        // various sorting goodness
        maybe_debug::dbg!(i, val); // On nightly, will specialize if 'T: Debug'
    }
}

This has a fallback to work on stable Rust (without specialization). In that case, the "cast" always fails and maybe_debug will unconditionally use the fallback.