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
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Pretty<T, Repr> {
    pub value: T,
    _phantom: std::marker::PhantomData<fn(Repr) -> Repr>,
}

impl<T, Repr> Pretty<T, Repr> {
    pub const fn new(value: T) -> Self {
        Pretty {
            value,
            _phantom: std::marker::PhantomData,
        }
    }

    pub fn repr(self) -> Repr
    where
        Repr: From<T>,
    {
        Repr::from(self.value)
    }
}

impl<T, Repr> std::fmt::Display for Pretty<T, Repr>
where
    T: Clone,
    Repr: std::fmt::Display + From<T>,
{
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "{}", Repr::from(self.value.clone()))
    }
}