Debug

Trait Debug 

Source
pub trait Debug {
    // Required method
    fn fmt(&self, f: &mut Formatter);
}
Expand description

Pretty printing of a type

This is essensialy the same as std::fmt::Debug, but with a different Formatter type.

Generally speaking, you should just derive a Debug implementation.

The output will always be pretty-printed.

This trait can be used with #[derive] if all fields implement Debug

§Stability

Derived Debug formats are not stable, and so may change with future versions. Additionally, Debug implementations of types provided by the standard library (libstd, libcore, liballoc, etc.) are not stable, and may also change with future versions.

§Examples

Deriving an implementation:

use debug3::{pprint, Debug};

#[derive(Debug)]
struct Dish {
    name: &'static str,
    price: i32,
}

let eggs = Dish {
    name: "eggs",
    price: 5,
};
assert_eq!(pprint(eggs), "Dish { name: \"eggs\", price: 5 }");
// This is long enough to spill
let green_eggs_and_ham = Dish {
    name: "green eggs and ham",
    price: 8,
};
assert_eq!(
    pprint(green_eggs_and_ham),
    "\
Dish {
    name: \"green eggs and ham\",
    price: 8,
}"
);

There are a number of helper methods on the Formatter struct to help you with manual implementations, such as debug_struct.

Types must use the standard suite of debug representations provided by the Formatter trait (debug_struct, debug_tuple, debut_list, debug_set, debug_map) as their is no way to do something totaly custom by manually writing an arbitrary representation to the Formatter.

use debug3::{pprint, Debug, Formatter};

struct Point {
    theta: f64,
    r: f64,
}

impl Debug for Point {
    fn fmt(&self, f: &mut Formatter) {
        f.debug_struct("Point")
            .field("x", &(self.theta.cos() * self.r))
            .field("y", &(self.theta.sin() * self.r))
            .finish()
    }
}

let origin = Point { theta: 0.0, r: 0.0 };
assert_eq!(pprint(origin), "Point { x: 0.0, y: 0.0 }");

struct ListMap<K, V>(Vec<(K, V)>);

impl<K: Debug, V: Debug> Debug for ListMap<K, V> {
    fn fmt(&self, f: &mut Formatter) {
        let mut m = f.debug_map();
        for (k, v) in &self.0 {
            m.key(k);
            m.value(v);
        }
        m.finish();
    }
}

let small_map = ListMap(vec![(1, 2), (3, 4), (5, 6)]);
assert_eq!(pprint(small_map), "{1: 2, 3: 4, 5: 6}");

let large_map = ListMap(vec![
    (4, vec![2, 2]),
    (6, vec![2, 3]),
    (8, vec![2, 2, 2]),
    (9, vec![3, 3]),
    (10, vec![2, 5]),
    (12, vec![2, 2, 3]),
    (14, vec![2, 7]),
    (15, vec![3, 5]),
    (16, vec![2, 2, 2, 2]),
    (18, vec![2, 3, 3]),
    (20, vec![2, 2, 5]),
]);

assert_eq!(
    pprint(large_map),
    "\
{
    4: [2, 2],
    6: [2, 3],
    8: [2, 2, 2],
    9: [3, 3],
    10: [2, 5],
    12: [2, 2, 3],
    14: [2, 7],
    15: [3, 5],
    16: [2, 2, 2, 2],
    18: [2, 3, 3],
    20: [2, 2, 5],
}"
);

Required Methods§

Source

fn fmt(&self, f: &mut Formatter)

Formats the value using the given formatter.

§Examples
use debug3::{pprint, Debug, Formatter};

struct Position {
    longitude: f64,
    latitude: f64,
}

impl Debug for Position {
    fn fmt(&self, f: &mut Formatter) {
        f.debug_tuple("")
            .field(&self.longitude)
            .field(&self.latitude)
            .finish()
    }
}

let position = Position {
    longitude: 1.987,
    latitude: 2.983,
};
assert_eq!(pprint(position), "(1.987, 2.983)");

Implementations on Foreign Types§

Source§

impl Debug for IntErrorKind

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for ErrorKind

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for bool

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for char

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for f32

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for f64

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for i8

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for i16

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for i32

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for i64

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for i128

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for isize

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for str

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for u8

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for u16

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for u32

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for u64

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for u128

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for ()

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for usize

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for Box<dyn Error + '_>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for Box<dyn Error + Send + Sync + '_>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for String

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for Layout

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for Arguments<'_>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for ParseFloatError

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for ParseIntError

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for RangeFull

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for Duration

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for Error

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for Path

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for PathBuf

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for NonZeroI8

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for NonZeroI16

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for NonZeroI32

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for NonZeroI64

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for NonZeroI128

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for NonZeroIsize

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for NonZeroU8

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for NonZeroU16

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for NonZeroU32

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for NonZeroU64

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for NonZeroU128

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl Debug for NonZeroUsize

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<B> Debug for Cow<'_, B>
where B: Debug + ToOwned + ?Sized, <B as ToOwned>::Owned: Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<K, V, S> Debug for HashMap<K, V, S>
where K: Debug, V: Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<K: Debug, V: Debug> Debug for BTreeMap<K, V>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T0: Debug, T1: Debug, T2: Debug, T3: Debug, T4: Debug, T5: Debug, T6: Debug, T7: Debug, T8: Debug, T9: Debug, T10: Debug, T11> Debug for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
where T11: ?Sized + Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T1: Debug, T2: Debug, T3: Debug, T4: Debug, T5: Debug, T6: Debug, T7: Debug, T8: Debug, T9: Debug, T10: Debug, T11> Debug for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
where T11: ?Sized + Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T2: Debug, T3: Debug, T4: Debug, T5: Debug, T6: Debug, T7: Debug, T8: Debug, T9: Debug, T10: Debug, T11> Debug for (T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
where T11: ?Sized + Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T3: Debug, T4: Debug, T5: Debug, T6: Debug, T7: Debug, T8: Debug, T9: Debug, T10: Debug, T11> Debug for (T3, T4, T5, T6, T7, T8, T9, T10, T11)
where T11: ?Sized + Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T4: Debug, T5: Debug, T6: Debug, T7: Debug, T8: Debug, T9: Debug, T10: Debug, T11> Debug for (T4, T5, T6, T7, T8, T9, T10, T11)
where T11: ?Sized + Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T5: Debug, T6: Debug, T7: Debug, T8: Debug, T9: Debug, T10: Debug, T11> Debug for (T5, T6, T7, T8, T9, T10, T11)
where T11: ?Sized + Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T6: Debug, T7: Debug, T8: Debug, T9: Debug, T10: Debug, T11> Debug for (T6, T7, T8, T9, T10, T11)
where T11: ?Sized + Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T7: Debug, T8: Debug, T9: Debug, T10: Debug, T11> Debug for (T7, T8, T9, T10, T11)
where T11: ?Sized + Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T8: Debug, T9: Debug, T10: Debug, T11> Debug for (T8, T9, T10, T11)
where T11: ?Sized + Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T9: Debug, T10: Debug, T11> Debug for (T9, T10, T11)
where T11: ?Sized + Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T10: Debug, T11> Debug for (T10, T11)
where T11: ?Sized + Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T11> Debug for (T11,)
where T11: ?Sized + Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T> Debug for BTreeSet<T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T, S> Debug for HashSet<T, S>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: Copy + Debug> Debug for Cell<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: Debug> Debug for Option<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: Debug> Debug for [T]

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: Debug> Debug for BinaryHeap<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: Debug> Debug for LinkedList<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: Debug> Debug for VecDeque<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: Debug> Debug for Vec<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: Debug> Debug for Range<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: Debug> Debug for RangeFrom<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: Debug> Debug for RangeInclusive<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: Debug> Debug for RangeTo<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: Debug> Debug for RangeToInclusive<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: Debug, const N: usize> Debug for [T; N]

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: ?Sized + Debug> Debug for &T

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: ?Sized + Debug> Debug for &mut T

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: ?Sized + Debug> Debug for Box<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: ?Sized + Debug> Debug for Rc<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: ?Sized + Debug> Debug for Arc<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: ?Sized + Debug> Debug for Ref<'_, T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: ?Sized + Debug> Debug for RefCell<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: ?Sized + Debug> Debug for RefMut<'_, T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: ?Sized + Debug> Debug for Mutex<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: ?Sized> Debug for *const T

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: ?Sized> Debug for *mut T

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: ?Sized> Debug for UnsafeCell<T>

Source§

fn fmt(&self, f: &mut Formatter)

Source§

impl<T: ?Sized> Debug for PhantomData<T>

Source§

fn fmt(&self, f: &mut Formatter)

Implementors§