display_with 0.1.0

Return opaque impl Display and/or impl Debug types via display_with and debug_with.
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented3 out of 3 items with examples
  • Size
  • Source code size: 18.03 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 588.06 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • JohnScience/display_with
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • JohnScience

display_with

Crates.io Downloads Documentation License Dependency Status

Return opaque impl Display and/or impl Debug types via display_with and debug_with.

Installation

cargo add display_with

Usage

use display_with::{display_with, debug_with};

let display = display_with(|f| write!(f, "Hello, world!"));
assert_eq!(format!("{display}"), "Hello, world!");

let debug = debug_with(|f| write!(f, "Hello, world!"));
assert_eq!(format!("{debug:?}"), "Hello, world!");

This can be combined with the format_args! macro to use the opaque types with the write! and writeln! macros.

use core::fmt::Write;
use display_with::{display_with, debug_with};
fn main() -> std::fmt::Result {
    let display = display_with(|f| write!(f, "Hello, world!"));
    let mut s = String::new();
    // Unlike `s.push_str(&format!("{display}"))`, this doesn't require an extra allocation.
    write!(&mut s, "{}", format_args!("{display}"))?;
    Ok(())
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.