Remove debugging info and use new serde::Deserialize impl for experimental InliningString

main
Pantonshire 3 years ago
parent 45ae1c68eb
commit 8e733c444d

@ -527,8 +527,41 @@ impl<'de, const N: usize> serde::Deserialize<'de> for InliningString<N> {
where where
D: serde::Deserializer<'de> D: serde::Deserializer<'de>
{ {
serde::Deserialize::deserialize(deserializer) use serde::de::{Error, Unexpected, Visitor};
.map(Self::new::<&'de str>)
struct InliningStringVisitor<const N: usize>;
impl<'de, const N: usize> Visitor<'de> for InliningStringVisitor<N> {
type Value = InliningString<N>;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("a string")
}
fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E> {
Ok(Self::Value::new(v))
}
fn visit_string<E: Error>(self, v: String) -> Result<Self::Value, E> {
Ok(Self::Value::new(v))
}
fn visit_bytes<E: Error>(self, v: &[u8]) -> Result<Self::Value, E> {
str::from_utf8(v)
.map(Self::Value::new)
.map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self))
}
fn visit_byte_buf<E: Error>(self, v: Vec<u8>) -> Result<Self::Value, E> {
String::from_utf8(v)
.map(Self::Value::new)
.map_err(|err| {
Error::invalid_value(Unexpected::Bytes(&err.into_bytes()), &self)
})
}
}
deserializer.deserialize_string(InliningStringVisitor)
} }
} }

@ -5,7 +5,7 @@ use core::{
fmt, fmt,
hash::{Hash, Hasher}, hash::{Hash, Hasher},
ops, ops,
str::{self, from_utf8}, str,
}; };
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
@ -348,32 +348,20 @@ impl<'de, const N: usize> serde::Deserialize<'de> for InliningString<N> {
} }
fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E> { fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E> {
#[cfg(feature = "std")] {
println!("visit &str \"{}\"", v);
}
Ok(Self::Value::new(v)) Ok(Self::Value::new(v))
} }
fn visit_string<E: Error>(self, v: String) -> Result<Self::Value, E> { fn visit_string<E: Error>(self, v: String) -> Result<Self::Value, E> {
#[cfg(feature = "std")] {
println!("visit String \"{}\"", v);
}
Ok(Self::Value::new(v)) Ok(Self::Value::new(v))
} }
fn visit_bytes<E: Error>(self, v: &[u8]) -> Result<Self::Value, E> { fn visit_bytes<E: Error>(self, v: &[u8]) -> Result<Self::Value, E> {
#[cfg(feature = "std")] {
println!("visit &[u8] {:?}", v);
}
str::from_utf8(v) str::from_utf8(v)
.map(Self::Value::new) .map(Self::Value::new)
.map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self)) .map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self))
} }
fn visit_byte_buf<E: Error>(self, v: Vec<u8>) -> Result<Self::Value, E> { fn visit_byte_buf<E: Error>(self, v: Vec<u8>) -> Result<Self::Value, E> {
#[cfg(feature = "std")] {
println!("visit Vec<u8> {:?}", v);
}
String::from_utf8(v) String::from_utf8(v)
.map(Self::Value::new) .map(Self::Value::new)
.map_err(|err| { .map_err(|err| {

Loading…
Cancel
Save