From 83d7bad54ba5db3a44198d6df0ff2e81621683fa Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 26 Jan 2024 13:14:48 -0800 Subject: [PATCH] Format f64 in error messages using ryu --- src/error.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index 0e3323972..fbf9eb14e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -466,10 +466,14 @@ struct JsonUnexpected<'a>(de::Unexpected<'a>); impl<'a> Display for JsonUnexpected<'a> { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - if let de::Unexpected::Unit = self.0 { - formatter.write_str("null") - } else { - Display::fmt(&self.0, formatter) + match self.0 { + de::Unexpected::Unit => formatter.write_str("null"), + de::Unexpected::Float(value) => write!( + formatter, + "floating point `{}`", + ryu::Buffer::new().format(value), + ), + unexp => Display::fmt(&unexp, formatter), } } }