Avoid using owned String in deserializers (#11764)

## Summary

This is the pattern I see in a variety of crates, and I believe this is
preferred if you don't _need_ an owned `String`, since you can avoid the
allocation. This could be pretty impactful for us?
This commit is contained in:
Charlie Marsh 2025-02-25 06:28:16 -08:00 committed by GitHub
parent 275db0668d
commit c37af945b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 350 additions and 104 deletions

View file

@ -63,8 +63,21 @@ impl schemars::JsonSchema for PythonVersion {
impl<'de> serde::Deserialize<'de> for PythonVersion {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
PythonVersion::from_str(&s).map_err(serde::de::Error::custom)
struct Visitor;
impl serde::de::Visitor<'_> for Visitor {
type Value = PythonVersion;
fn expecting(&self, f: &mut Formatter) -> std::fmt::Result {
f.write_str("a string")
}
fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
PythonVersion::from_str(v).map_err(serde::de::Error::custom)
}
}
deserializer.deserialize_str(Visitor)
}
}