Skip to content

Commit dde8d29

Browse files
Permit deserialization of data structures from streams
Previously we were insisting on being able to borrow data from the source data, which isn't always possible. Implement visitors appropriately to avoid that.
1 parent 1039b23 commit dde8d29

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

collector/src/lib.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,24 @@ impl<'de> Deserialize<'de> for Sha {
7575
where
7676
D: serde::de::Deserializer<'de>,
7777
{
78-
let s: &'de str = <&'de str>::deserialize(deserializer)?;
79-
Ok(s.into())
78+
use serde::de::Visitor;
79+
struct ShaVisitor;
80+
impl<'de> Visitor<'de> for ShaVisitor {
81+
type Value = Sha;
82+
83+
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
84+
f.write_str("a string")
85+
}
86+
87+
fn visit_str<E>(self, s: &str) -> Result<Sha, E> {
88+
Ok(s.into())
89+
}
90+
91+
fn visit_borrowed_str<E>(self, s: &'de str) -> Result<Sha, E> {
92+
Ok(s.into())
93+
}
94+
}
95+
deserializer.deserialize_str(ShaVisitor)
8096
}
8197
}
8298

0 commit comments

Comments
 (0)