1pub use sway_ir_macros::*;
2use {crate::Context, std::fmt};
3
4pub struct WithContext<'a, 'c, 'eng, T: ?Sized> {
5 thing: &'a T,
6 context: &'c Context<'eng>,
7}
8
9pub trait DebugWithContext {
10 fn fmt_with_context(&self, formatter: &mut fmt::Formatter, context: &Context) -> fmt::Result;
11
12 fn with_context<'a, 'c, 'eng>(
13 &'a self,
14 context: &'c Context<'eng>,
15 ) -> WithContext<'a, 'c, 'eng, Self> {
16 WithContext {
17 thing: self,
18 context,
19 }
20 }
21}
22
23impl<T> DebugWithContext for &T
24where
25 T: fmt::Debug,
26{
27 fn fmt_with_context(&self, formatter: &mut fmt::Formatter, _context: &Context) -> fmt::Result {
28 fmt::Debug::fmt(self, formatter)
29 }
30}
31
32impl<T> fmt::Debug for WithContext<'_, '_, '_, T>
33where
34 T: DebugWithContext,
35{
36 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
37 let WithContext { thing, context } = self;
38 (*thing).fmt_with_context(formatter, context)
39 }
40}
41
42impl<T> DebugWithContext for Vec<T>
43where
44 T: DebugWithContext,
45{
46 fn fmt_with_context(&self, formatter: &mut fmt::Formatter, context: &Context) -> fmt::Result {
47 formatter
48 .debug_list()
49 .entries(self.iter().map(|value| (*value).with_context(context)))
50 .finish()
51 }
52}
53
54impl<T> DebugWithContext for [T]
55where
56 T: DebugWithContext,
57{
58 fn fmt_with_context(&self, formatter: &mut fmt::Formatter, context: &Context) -> fmt::Result {
59 formatter
60 .debug_list()
61 .entries(self.iter().map(|value| (*value).with_context(context)))
62 .finish()
63 }
64}
65
66impl<T> DebugWithContext for Option<T>
67where
68 T: DebugWithContext,
69{
70 fn fmt_with_context(&self, formatter: &mut fmt::Formatter, context: &Context) -> fmt::Result {
71 match self {
72 Some(value) => formatter
73 .debug_tuple("Some")
74 .field(&(*value).with_context(context))
75 .finish(),
76 None => formatter.write_str("None"),
77 }
78 }
79}
80
81macro_rules! tuple_impl (
82 ($($ty:ident,)*) => {
83 impl<$($ty,)*> DebugWithContext for ($($ty,)*)
84 where
85 $($ty: DebugWithContext,)*
86 {
87 #[allow(unused_mut)]
88 #[allow(unused_variables)]
89 #[allow(non_snake_case)]
90 fn fmt_with_context(&self, formatter: &mut fmt::Formatter, context: &Context) -> fmt::Result {
91 let ($($ty,)*) = self;
92 let mut debug_tuple = &mut formatter.debug_tuple("");
93 $(
94 debug_tuple = debug_tuple.field(&(*$ty).with_context(context));
95 )*
96 debug_tuple.finish()
97 }
98 }
99 };
100);
101
102macro_rules! tuple_impls (
103 () => {
104 tuple_impl!();
105 };
106 ($head:ident, $($tail:ident,)*) => {
107 tuple_impls!($($tail,)*);
108 tuple_impl!($head, $($tail,)*);
109 };
110);
111
112tuple_impls!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,);