pub trait DynXso: 'static {
type Registry: 'static;
// Required methods
fn registry() -> &'static Self::Registry;
fn try_downcast<T: 'static>(self: Box<Self>) -> Result<Box<T>, Box<Self>>
where Self: MayContain<T>;
fn try_downcast_ref<T: 'static>(&self) -> Option<&T>
where Self: MayContain<T>;
fn try_downcast_mut<T: 'static>(&mut self) -> Option<&mut T>
where Self: MayContain<T>;
fn is<T: 'static>(&self) -> bool
where Self: MayContain<T>;
fn type_id(&self) -> TypeId;
}
Expand description
§Dynamic XSO type
This trait provides the infrastructure for dynamic XSO types. In particular, it provides:
- Access to a
BuilderRegistry
which allows constructing an instance of the dynamic XSO type from XML. - Downcasts to specific types.
Like MayContain
, it is typically implemented on dyn Trait
for some
Trait
and it is best generated using
derive_dyn_traits
.
This trait explicitly provides the methods provided by Any
. The reason
for this duplication is that with DynXso
being intended to be
implemented on dyn Trait
, code using this trait cannot cast the value
to dyn Any
to access the downcast
-related methods (type_id
would,
in fact, work if DynXso
had a bound on Any
, but not the downcasts).
Hint: It should not be necessary for user code to directly interact with this trait.
Required Associated Types§
Sourcetype Registry: 'static
type Registry: 'static
Builder registry type for this dynamic type.
The Registry
type should implement the following traits:
DynXsoRegistryAdd
is required to makeXso::<Self>::register_type()
available.DynXsoRegistryLookup
is required to makeFromXml
available onXso<Self>
(and, by extension, onXsoVec<Self>
).
However, any type with static lifetime can be used, even without the trait implementations above, if the limitations are acceptable.
Required Methods§
Sourcefn registry() -> &'static Self::Registry
fn registry() -> &'static Self::Registry
Return the builder registry for this dynamic type.
See Registry
for details.
Sourcefn try_downcast<T: 'static>(self: Box<Self>) -> Result<Box<T>, Box<Self>>where
Self: MayContain<T>,
fn try_downcast<T: 'static>(self: Box<Self>) -> Result<Box<T>, Box<Self>>where
Self: MayContain<T>,
Try to downcast a boxed dynamic XSO to a specific type.
If self
contains a T
(and thus, the downcast succeeds), Ok(_)
is returned. Otherwise, Err(self)
is returned, allowing to chain
this function with other downcast attempts.
This is similar to downcast
on dyn Any
.
Sourcefn try_downcast_ref<T: 'static>(&self) -> Option<&T>where
Self: MayContain<T>,
fn try_downcast_ref<T: 'static>(&self) -> Option<&T>where
Self: MayContain<T>,
Try to downcast a dynamic XSO to a reference to a specific type.
If self
contains a T
(and thus, the downcast succeeds), Some(_)
is returned. Otherwise, None
.
This is similar to downcast_ref
on dyn Any
.
Sourcefn try_downcast_mut<T: 'static>(&mut self) -> Option<&mut T>where
Self: MayContain<T>,
fn try_downcast_mut<T: 'static>(&mut self) -> Option<&mut T>where
Self: MayContain<T>,
Try to downcast a dynamic XSO to a mutable reference to a specific type.
If self
contains a T
(and thus, the downcast succeeds), Some(_)
is returned. Otherwise, None
.
This is similar to downcast_mut
on dyn Any
.
Sourcefn is<T: 'static>(&self) -> boolwhere
Self: MayContain<T>,
fn is<T: 'static>(&self) -> boolwhere
Self: MayContain<T>,
Return true if self
contains a T
.
This is similar to is
on dyn Any
.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.