Skip to content

Commit

Permalink
serde: add impl for String (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
hackaugusto authored Mar 11, 2024
1 parent c5d8dd9 commit ea1f342
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions utils/core/src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ impl<T: Serializable> Serializable for BTreeSet<T> {
}
}

impl Serializable for String {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
target.write_usize(self.len());
target.write_many(self.as_bytes());
}
}

// DESERIALIZABLE
// ================================================================================================

Expand Down Expand Up @@ -422,3 +429,13 @@ impl<T: Deserializable + Ord> Deserializable for BTreeSet<T> {
Ok(BTreeSet::from_iter(data))
}
}

impl Deserializable for String {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let len = source.read_usize()?;
let data = source.read_many(len)?;

String::from_utf8(data)
.map_err(|err| DeserializationError::InvalidValue(format!("{}", err)))
}
}

0 comments on commit ea1f342

Please sign in to comment.