sway_ir/
pretty.rs

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
pub use sway_ir_macros::*;
use {crate::Context, std::fmt};

pub struct WithContext<'a, 'c, 'eng, T: ?Sized> {
    thing: &'a T,
    context: &'c Context<'eng>,
}

pub trait DebugWithContext {
    fn fmt_with_context(&self, formatter: &mut fmt::Formatter, context: &Context) -> fmt::Result;

    fn with_context<'a, 'c, 'eng>(
        &'a self,
        context: &'c Context<'eng>,
    ) -> WithContext<'a, 'c, 'eng, Self> {
        WithContext {
            thing: self,
            context,
        }
    }
}

impl<'t, T> DebugWithContext for &'t T
where
    T: fmt::Debug,
{
    fn fmt_with_context(&self, formatter: &mut fmt::Formatter, _context: &Context) -> fmt::Result {
        fmt::Debug::fmt(self, formatter)
    }
}

impl<'a, 'c, 'eng, T> fmt::Debug for WithContext<'a, 'c, 'eng, T>
where
    T: DebugWithContext,
{
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        let WithContext { thing, context } = self;
        (*thing).fmt_with_context(formatter, context)
    }
}

impl<T> DebugWithContext for Vec<T>
where
    T: DebugWithContext,
{
    fn fmt_with_context(&self, formatter: &mut fmt::Formatter, context: &Context) -> fmt::Result {
        formatter
            .debug_list()
            .entries(self.iter().map(|value| (*value).with_context(context)))
            .finish()
    }
}

impl<T> DebugWithContext for [T]
where
    T: DebugWithContext,
{
    fn fmt_with_context(&self, formatter: &mut fmt::Formatter, context: &Context) -> fmt::Result {
        formatter
            .debug_list()
            .entries(self.iter().map(|value| (*value).with_context(context)))
            .finish()
    }
}

impl<T> DebugWithContext for Option<T>
where
    T: DebugWithContext,
{
    fn fmt_with_context(&self, formatter: &mut fmt::Formatter, context: &Context) -> fmt::Result {
        match self {
            Some(value) => formatter
                .debug_tuple("Some")
                .field(&(*value).with_context(context))
                .finish(),
            None => formatter.write_str("None"),
        }
    }
}

macro_rules! tuple_impl (
    ($($ty:ident,)*) => {
        impl<$($ty,)*> DebugWithContext for ($($ty,)*)
        where
            $($ty: DebugWithContext,)*
        {
            #[allow(unused_mut)]
            #[allow(unused_variables)]
            #[allow(non_snake_case)]
            fn fmt_with_context(&self, formatter: &mut fmt::Formatter, context: &Context) -> fmt::Result {
                let ($($ty,)*) = self;
                let mut debug_tuple = &mut formatter.debug_tuple("");
                $(
                    debug_tuple = debug_tuple.field(&(*$ty).with_context(context));
                )*
                debug_tuple.finish()
            }
        }
    };
);

macro_rules! tuple_impls (
    () => {
        tuple_impl!();
    };
    ($head:ident, $($tail:ident,)*) => {
        tuple_impls!($($tail,)*);
        tuple_impl!($head, $($tail,)*);
    };
);

tuple_impls!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,);