1use core::fmt::{self, Debug, Display, Formatter};
2
3pub struct FmtDisplay<T>
5where
6 T: ?Sized,
7{
8 value: T,
9}
10
11impl<T> FmtDisplay<T> {
12 const fn new(value: T) -> Self {
13 Self { value }
14 }
15}
16
17impl<T> Debug for FmtDisplay<T>
18where
19 T: Display + ?Sized,
20{
21 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
22 self.value.fmt(f)
23 }
24}
25
26impl<T> Display for FmtDisplay<T>
27where
28 T: Display + ?Sized,
29{
30 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
31 self.value.fmt(f)
32 }
33}
34
35pub const fn fmt_display<T>(value: T) -> FmtDisplay<T>
56where
57 T: Display,
58{
59 FmtDisplay::new(value)
60}
61
62#[cfg(test)]
63mod tests {
64 use super::FmtDisplay;
65 use core::fmt::{self, Display, Formatter};
66
67 #[test]
68 fn test_fmt_display() {
69 struct Foo;
70
71 impl Display for Foo {
72 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
73 f.write_str("foo")
74 }
75 }
76
77 let fmt = super::fmt_display(Foo);
78 let unsized_fmt: &FmtDisplay<dyn Display> = &fmt;
79
80 assert_eq!(std::format!("{fmt:?}"), "foo");
81 assert_eq!(std::format!("{fmt}"), "foo");
82 assert_eq!(std::format!("{unsized_fmt:?}"), "foo");
83 assert_eq!(std::format!("{unsized_fmt}"), "foo");
84 }
85}