prty_fmtmodifier/
lib.rs

1use std::fmt::*;
2
3pub trait Format<What> {
4    fn fmt(&self, w: &What, f: &mut Formatter) -> Result;
5}
6
7pub struct PrintAs<G, P>(pub G, pub P);
8pub struct PrintRefAs<'a, G: 'a, P>(pub &'a G, pub P);
9
10impl<G, P> Debug for PrintAs<G, P>
11where
12    P: Format<G>,
13{
14    fn fmt(&self, f: &mut Formatter) -> Result {
15        self.1.fmt(&self.0, f)
16    }
17}
18impl<'a, G, P> Debug for PrintRefAs<'a, G, P>
19where
20    P: Format<G>,
21{
22    fn fmt(&self, f: &mut Formatter) -> Result {
23        self.1.fmt(self.0, f)
24    }
25}
26
27impl<G, P: Default> From<G> for PrintAs<G, P> {
28    fn from(g: G) -> PrintAs<G, P> {
29        PrintAs(g.into(), P::default())
30    }
31}
32impl<G, P> ::std::ops::Deref for PrintAs<G, P> {
33    type Target = G;
34    fn deref(&self) -> &G {
35        &self.0
36    }
37}
38
39impl<G, P> ::std::ops::DerefMut for PrintAs<G, P> {
40    fn deref_mut(&mut self) -> &mut G {
41        &mut self.0
42    }
43}