mirror of
https://github.com/astral-sh/uv.git
synced 2025-07-12 07:45:00 +00:00
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:
parent
275db0668d
commit
c37af945b3
19 changed files with 350 additions and 104 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue