feat: impl FromXml for Box<T>

This commit is contained in:
gibbz00 2024-10-18 19:06:07 +02:00 committed by Dirkjan Ochtman
parent f17e2e0350
commit 03d99af431
1 changed files with 26 additions and 0 deletions

View File

@ -498,6 +498,32 @@ impl<T: ToXml + ?Sized> ToXml for Box<T> {
} }
} }
impl<'xml, T: FromXml<'xml>> FromXml<'xml> for Box<T> {
#[inline]
fn matches(id: Id<'_>, field: Option<Id<'_>>) -> bool {
T::matches(id, field)
}
fn deserialize<'cx>(
into: &mut Self::Accumulator,
field: &'static str,
deserializer: &mut Deserializer<'cx, 'xml>,
) -> Result<(), Error> {
if into.is_some() {
return Err(Error::DuplicateValue(field));
}
let mut value = T::Accumulator::default();
T::deserialize(&mut value, field, deserializer)?;
*into = Some(Box::new(value.try_done(field)?));
Ok(())
}
type Accumulator = Option<Self>;
const KIND: Kind = T::KIND;
}
fn encode(input: &str) -> Result<Cow<'_, str>, Error> { fn encode(input: &str) -> Result<Cow<'_, str>, Error> {
let mut result = String::with_capacity(input.len()); let mut result = String::with_capacity(input.len());
let mut last_end = 0; let mut last_end = 0;