Add support for generic type arguments

This commit is contained in:
Dirkjan Ochtman 2022-11-21 20:33:11 -08:00
parent 413ec05c52
commit 055a654a3d
3 changed files with 40 additions and 1 deletions

View File

@ -79,6 +79,12 @@ impl<'input> ContainerMeta<'input> {
.extend(xml_generics.lifetimes().map(|lt| lt.lifetime.clone()));
xml_generics.params.push(xml.into());
for param in xml_generics.type_params_mut() {
param
.bounds
.push(syn::parse_str("::instant_xml::FromXml<'xml>").unwrap());
}
xml_generics
}

View File

@ -91,7 +91,14 @@ fn serialize_struct(
));
}
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let mut generics = input.generics.clone();
for param in generics.type_params_mut() {
param
.bounds
.push(syn::parse_str("::instant_xml::ToXml").unwrap());
}
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let tag = meta.tag();
let ident = &input.ident;

View File

@ -0,0 +1,26 @@
use instant_xml::{from_str, to_string, FromXml, ToXml};
#[derive(Debug, Eq, FromXml, ToXml, PartialEq)]
struct Foo<T> {
inner: T,
}
#[derive(Debug, Eq, FromXml, ToXml, PartialEq)]
struct Bar {
bar: String,
}
#[allow(clippy::disallowed_names)]
#[test]
fn serialize_generics() {
let foo = Foo {
inner: Bar {
bar: "Bar".to_owned(),
},
};
let xml = "<Foo><Bar><bar>Bar</bar></Bar></Foo>";
assert_eq!(to_string(&foo).unwrap(), xml);
assert_eq!(from_str::<Foo<Bar>>(xml).unwrap(), foo);
}