support Cow<str>
This commit is contained in:
@@ -171,3 +171,38 @@ impl <K: OverTheWire + Ord, V: OverTheWire> OverTheWire for BTreeMap<K,V> {
|
||||
u32::size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
//╶───╴Copy On Write╶────────────────────────────────────────────────────────╴
|
||||
|
||||
|
||||
// Pretty much identical to String but allows for refs to be used
|
||||
impl<'l> OverTheWire for alloc::borrow::Cow<'l, str> {
|
||||
|
||||
fn serialize<T: Writer>(&self, writer: &mut T) -> e::Result<()> {
|
||||
(self.len() as u32).serialize(writer)?;
|
||||
writer.write(self.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn deserialize<T: Reader>(reader: &mut T) -> e::Result<Self> {
|
||||
let len = u32::deserialize(reader)?;
|
||||
|
||||
#[cfg(feature = "extra-validation")]
|
||||
if len as usize > reader.remainder_hint() {
|
||||
return Err(MalformedData)?;
|
||||
}
|
||||
|
||||
let mut vec = Vec::with_capacity(len as usize);
|
||||
unsafe { vec.set_len(len as usize) };
|
||||
reader.read(vec.as_mut_slice())?;
|
||||
|
||||
let s = String::from_utf8(vec)
|
||||
.map_err(|_|MalformedData)?;
|
||||
|
||||
Ok(alloc::borrow::Cow::Owned(s))
|
||||
}
|
||||
|
||||
fn size_hint() -> usize {
|
||||
u32::size_hint()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user