macro_rules! linktime {
(
$(
$(#[$meta:meta])*
$vis:vis trait $trait:ident $(: $bound:path $(, $bound2:path)*)? {
$($item:item)*
}
)*
) => { ... };
(
$(
$(#[$meta:meta])*
impl $trait_seg1:ident $(:: $trait_segn:ident)* for $ty:ty {
$($item:item)*
}
)*
) => { ... };
}Available on crate feature
linktime only.Expand description
Dynamic XSO traits using a link-time registry
This macro is used for (a) declaring DynXso
traits using a registry filled at link-time and (b) for registering types
with a trait.
§Declaring a link-time DynXso trait
To declare a trait as a link-time DynXso trait, wrap its declaration in
a linktime! invocation.
§Example
use xso::{dynxso::linktime, asxml::AsXmlDyn};
linktime! {
pub trait MyPayload: AsXmlDyn {}
}§Usage notes
- A bound on
Anyis automatically added, because it is a requirement for implementingDynXsoondyn Trait. - If you want to specify more than one trait bound, you have to separate
them with
,instead of+, due to rustc limitations with declarative macros. - A trait bound on
AsXmlDynis required (but has to be added manually, as seen in the above example) forXso<dyn Trait>to implementAsXml. - This macro automatically calls
crate::derive_dyn_traits!, but does not use the defaultBuilderRegistry, removing thestddependency at the cost of being unable to register more types at runtime.
§Implement a DynXso trait
To implement a trait declared with linktime!, wrap the impl block in
a linktime! invocation.
Doing so allows a Xso<dyn Trait>’s FromXml
implementation to parse the registered type.
§Example
use xso::{dynxso::{linktime, Xso}, FromXml, AsXml, asxml::AsXmlDyn, from_bytes};
linktime! {
trait Foo: AsXmlDyn {}
}
#[derive(FromXml, AsXml, Debug, Clone, Copy)]
#[xml(namespace = "urn:example", name = "a")]
struct A {}
#[derive(FromXml, AsXml, Debug, Clone, Copy)]
#[xml(namespace = "urn:example", name = "b")]
struct B {}
linktime! {
impl Foo for A {}
impl Foo for B {}
}
let foo: Xso<dyn Foo> = from_bytes(b"<a xmlns='urn:example'/>").unwrap();
let foo: Xso<dyn Foo> = from_bytes(b"<b xmlns='urn:example'/>").unwrap();§Usage notes
linktime!can be placed anywhere; unlike theXso::register_typecalls needed with theBuilderRegistry, registrations are handled at compile-time (or more precisely: at link-time, with a minimal startup overhead of postprocessing the list generated by the linker).