From 8e733c444de02a26cd04a935c6cc7097ff16eda5 Mon Sep 17 00:00:00 2001 From: Pantonshire Date: Thu, 21 Jul 2022 13:34:56 +0100 Subject: [PATCH] Remove debugging info and use new serde::Deserialize impl for experimental InliningString --- src/strings/experimental.rs | 37 +++++++++++++++++++++++++++++++++++-- src/strings/inlining.rs | 14 +------------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/strings/experimental.rs b/src/strings/experimental.rs index 9cddc3e..15d435f 100644 --- a/src/strings/experimental.rs +++ b/src/strings/experimental.rs @@ -527,8 +527,41 @@ impl<'de, const N: usize> serde::Deserialize<'de> for InliningString { where D: serde::Deserializer<'de> { - serde::Deserialize::deserialize(deserializer) - .map(Self::new::<&'de str>) + use serde::de::{Error, Unexpected, Visitor}; + + struct InliningStringVisitor; + + impl<'de, const N: usize> Visitor<'de> for InliningStringVisitor { + type Value = InliningString; + + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("a string") + } + + fn visit_str(self, v: &str) -> Result { + Ok(Self::Value::new(v)) + } + + fn visit_string(self, v: String) -> Result { + Ok(Self::Value::new(v)) + } + + fn visit_bytes(self, v: &[u8]) -> Result { + str::from_utf8(v) + .map(Self::Value::new) + .map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self)) + } + + fn visit_byte_buf(self, v: Vec) -> Result { + String::from_utf8(v) + .map(Self::Value::new) + .map_err(|err| { + Error::invalid_value(Unexpected::Bytes(&err.into_bytes()), &self) + }) + } + } + + deserializer.deserialize_string(InliningStringVisitor) } } diff --git a/src/strings/inlining.rs b/src/strings/inlining.rs index 1c7e5b5..a7dc1d3 100644 --- a/src/strings/inlining.rs +++ b/src/strings/inlining.rs @@ -5,7 +5,7 @@ use core::{ fmt, hash::{Hash, Hasher}, ops, - str::{self, from_utf8}, + str, }; #[cfg(not(feature = "std"))] @@ -348,32 +348,20 @@ impl<'de, const N: usize> serde::Deserialize<'de> for InliningString { } fn visit_str(self, v: &str) -> Result { - #[cfg(feature = "std")] { - println!("visit &str \"{}\"", v); - } Ok(Self::Value::new(v)) } fn visit_string(self, v: String) -> Result { - #[cfg(feature = "std")] { - println!("visit String \"{}\"", v); - } Ok(Self::Value::new(v)) } fn visit_bytes(self, v: &[u8]) -> Result { - #[cfg(feature = "std")] { - println!("visit &[u8] {:?}", v); - } str::from_utf8(v) .map(Self::Value::new) .map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self)) } fn visit_byte_buf(self, v: Vec) -> Result { - #[cfg(feature = "std")] { - println!("visit Vec {:?}", v); - } String::from_utf8(v) .map(Self::Value::new) .map_err(|err| {