1pub use core::fmt::*;
2
3pub(crate) struct FormatWith<T, F = fn(&T, &mut Formatter<'_>) -> Result>
7where
8 F: Fn(&T, &mut Formatter<'_>) -> Result,
9{
10 value: T,
11 fmt: F,
12}
13
14#[derive(Clone)]
15pub(crate) struct FmtOption<'a, T> {
16 opt: Option<&'a T>,
17 or_else: &'a str,
18}
19
20#[cfg(any(test, feature = "tracing", loom))]
23#[inline]
24#[must_use]
25pub(crate) fn ptr<T: Pointer>(value: T) -> FormatWith<T> {
26 FormatWith {
27 value,
28 fmt: Pointer::fmt,
29 }
30}
31
32#[inline]
33#[must_use]
34pub(crate) fn display<T: Display>(value: T) -> FormatWith<T> {
35 FormatWith {
36 value,
37 fmt: Display::fmt,
38 }
39}
40
41impl<T, F> Debug for FormatWith<T, F>
42where
43 F: Fn(&T, &mut Formatter<'_>) -> Result,
44{
45 #[inline]
46 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
47 (self.fmt)(&self.value, f)
48 }
49}
50
51#[must_use]
54#[inline]
55pub(crate) const fn opt<T>(value: &Option<T>) -> FmtOption<'_, T> {
56 FmtOption {
57 opt: value.as_ref(),
58 or_else: "",
59 }
60}
61
62impl<'a, T> FmtOption<'a, T> {
63 #[must_use]
64 #[inline]
65 pub(crate) fn or_else(self, or_else: &'a str) -> Self {
66 Self { or_else, ..self }
67 }
68}
69
70impl<T: Debug> Debug for FmtOption<'_, T> {
71 #[inline]
72 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
73 match self.opt {
74 Some(val) => Debug::fmt(val, f),
75 None => f.write_str(self.or_else),
76 }
77 }
78}
79
80impl<T: Display> Display for FmtOption<'_, T> {
81 #[inline]
82 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
83 match self.opt {
84 Some(val) => val.fmt(f),
85 None => f.write_str(self.or_else),
86 }
87 }
88}