|
1 | 1 | //! Debug impls for types.
|
2 | 2 |
|
3 |
| -use std::fmt::{Debug, Display, Error, Formatter}; |
| 3 | +use std::fmt::{self, Debug, Display, Error, Formatter}; |
4 | 4 |
|
5 | 5 | use super::*;
|
6 | 6 |
|
| 7 | +/// Wrapper to allow forwarding to `Display::fmt`, `Debug::fmt`, etc. |
| 8 | +pub struct Fmt<F>(pub F) |
| 9 | +where |
| 10 | + F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result; |
| 11 | + |
| 12 | +impl<F> fmt::Display for Fmt<F> |
| 13 | +where |
| 14 | + F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result, |
| 15 | +{ |
| 16 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 17 | + (self.0)(f) |
| 18 | + } |
| 19 | +} |
| 20 | + |
7 | 21 | impl<I: Interner> Debug for TraitId<I> {
|
8 | 22 | fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
|
9 | 23 | I::debug_trait_id(*self, fmt).unwrap_or_else(|| write!(fmt, "TraitId({:?})", self.0))
|
@@ -959,14 +973,27 @@ impl<I: Interner> Debug for Constraint<I> {
|
959 | 973 | }
|
960 | 974 |
|
961 | 975 | impl<I: Interner> Display for ConstrainedSubst<I> {
|
| 976 | + #[rustfmt::skip] |
962 | 977 | fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
|
963 | 978 | let ConstrainedSubst { subst, constraints } = self;
|
964 | 979 |
|
965 |
| - write!( |
966 |
| - f, |
967 |
| - "substitution {}, lifetime constraints {:?}", |
968 |
| - subst, constraints, |
969 |
| - ) |
| 980 | + let mut first = true; |
| 981 | + |
| 982 | + let subst = format!("{}", Fmt(|f| Display::fmt(subst, f))); |
| 983 | + if subst != "[]" { |
| 984 | + write!(f, "substitution {}", subst)?; |
| 985 | + first = false; |
| 986 | + } |
| 987 | + |
| 988 | + let constraints = format!("{}", Fmt(|f| Debug::fmt(constraints, f))); |
| 989 | + if constraints != "[]" { |
| 990 | + if !first { write!(f, ", ")?; } |
| 991 | + write!(f, "lifetime constraints {}", constraints)?; |
| 992 | + first = false; |
| 993 | + } |
| 994 | + |
| 995 | + let _ = first; |
| 996 | + Ok(()) |
970 | 997 | }
|
971 | 998 | }
|
972 | 999 |
|
|
0 commit comments