| 1 | use std::fmt; |
| 2 | |
| 3 | use crate::{ |
| 4 | AliasTerm, AliasTy, Binder, CoercePredicate, ExistentialProjection, ExistentialTraitRef, FnSig, |
| 5 | HostEffectPredicate, Interner, NormalizesTo, OutlivesPredicate, ProjectionPredicate, |
| 6 | SubtypePredicate, TraitPredicate, TraitRef, |
| 7 | }; |
| 8 | |
| 9 | pub trait IrPrint<T> { |
| 10 | fn print(t: &T, fmt: &mut fmt::Formatter<'_>) -> fmt::Result; |
| 11 | fn print_debug(t: &T, fmt: &mut fmt::Formatter<'_>) -> fmt::Result; |
| 12 | } |
| 13 | |
| 14 | macro_rules! define_display_via_print { |
| 15 | ($($ty:ident),+ $(,)?) => { |
| 16 | $( |
| 17 | impl<I: Interner> fmt::Display for $ty<I> { |
| 18 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 19 | <I as IrPrint<$ty<I>>>::print(self, fmt) |
| 20 | } |
| 21 | } |
| 22 | )* |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | impl<I: Interner, T> fmt::Display for Binder<I, T> |
| 27 | where |
| 28 | I: IrPrint<Binder<I, T>>, |
| 29 | { |
| 30 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 31 | <I as IrPrint<Binder<I, T>>>::print(self, fmt) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | macro_rules! define_debug_via_print { |
| 36 | ($($ty:ident),+ $(,)?) => { |
| 37 | $( |
| 38 | impl<I: Interner> fmt::Debug for $ty<I> { |
| 39 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 40 | <I as IrPrint<$ty<I>>>::print_debug(self, fmt) |
| 41 | } |
| 42 | } |
| 43 | )* |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | define_display_via_print!( |
| 48 | TraitRef, |
| 49 | TraitPredicate, |
| 50 | ExistentialTraitRef, |
| 51 | ExistentialProjection, |
| 52 | ProjectionPredicate, |
| 53 | NormalizesTo, |
| 54 | SubtypePredicate, |
| 55 | CoercePredicate, |
| 56 | HostEffectPredicate, |
| 57 | AliasTy, |
| 58 | AliasTerm, |
| 59 | FnSig, |
| 60 | ); |
| 61 | |
| 62 | define_debug_via_print!(TraitRef, ExistentialTraitRef, ExistentialProjection); |
| 63 | |
| 64 | impl<I: Interner, T> fmt::Display for OutlivesPredicate<I, T> |
| 65 | where |
| 66 | I: IrPrint<OutlivesPredicate<I, T>>, |
| 67 | { |
| 68 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 69 | <I as IrPrint<OutlivesPredicate<I, T>>>::print(self, fmt) |
| 70 | } |
| 71 | } |
| 72 | |