From b785403f5f96404e1580ddcf3ce22e02d178248b Mon Sep 17 00:00:00 2001 From: Philippe-Cholet <44676486+Philippe-Cholet@users.noreply.github.com> Date: Tue, 23 Jan 2024 11:48:23 +0100 Subject: [PATCH] `ExactlyOneError`: implement Debug differently It could be derived. It apparently was decided not to in #484, probably to not leak implementation details that could confuse the user. However, it would not render well for pretty formats such as `"{:#?}"`. I think we should call methods on `f` instead of using `write!`. --- src/exactly_one_err.rs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/exactly_one_err.rs b/src/exactly_one_err.rs index 90d898778..19b9e1918 100644 --- a/src/exactly_one_err.rs +++ b/src/exactly_one_err.rs @@ -102,25 +102,17 @@ where I::Item: Debug, { fn fmt(&self, f: &mut Formatter) -> FmtResult { + let mut dbg = f.debug_struct("ExactlyOneError"); match &self.first_two { Some(Either::Left([first, second])) => { - write!( - f, - "ExactlyOneError[First: {:?}, Second: {:?}, RemainingIter: {:?}]", - first, second, self.inner - ) + dbg.field("first", first).field("second", second); } Some(Either::Right(second)) => { - write!( - f, - "ExactlyOneError[Second: {:?}, RemainingIter: {:?}]", - second, self.inner - ) - } - None => { - write!(f, "ExactlyOneError[RemainingIter: {:?}]", self.inner) + dbg.field("second", second); } + None => {} } + dbg.field("inner", &self.inner).finish() } }