1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// devela::text::fmt::debug
//
//! Defines [`DebugExt`].
//
use crate::;
/// Extension for contextual debugging.
///
/// Types implementing this trait support formatting with a caller-supplied context.
/// The context is defined per-type via the `Ctx` associated type, allowing each type
/// to expose whatever formatting modes it needs without affecting other types.
///
/// # Example
/// ```
/// # use devela::{DebugExt, FmtResult, Formatter};
/// enum Mode { Hex, Dec }
/// struct Value(u32);
/// impl DebugExt for Value {
/// type Ctx = Mode;
/// fn fmt_with(&self, f: &mut Formatter, mode: &Self::Ctx) -> FmtResult<()> {
/// match mode {
/// Mode::Hex => write!(f, "0x{:X}", self.0),
/// Mode::Dec => write!(f, "{}", self.0),
/// }
/// }
/// }
/// let v = Value(255);
/// // caller chooses representation:
/// // v.fmt_with(f, &Mode::Hex) → "0xFF"
/// // v.fmt_with(f, &Mode::Dec) → "255"
/// ```