Skip to main content

DynXso

Trait DynXso 

Source
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 is normally implemented only on trait-object types, i.e. on dyn Trait for some Trait. It provides the infrastructure for dynamic XSO types. In particular:

  • Access to a registry which allows constructing an instance of the dynamic XSO type from XML.
  • Downcasts to specific types.

Implementations of this trait are best generated using the linktime or xso_trait macros.

This trait explicitly provides the methods also 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§

Source

type Registry: 'static

Builder registry type for this dynamic type.

The Registry type should implement the following traits:

However, any type with static lifetime can be used, even without the trait implementations above, if the limitations are acceptable.

Required Methods§

Source

fn registry() -> &'static Self::Registry

Return the builder registry for this dynamic type.

See Registry for details.

Source

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.

Source

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.

Source

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.

Source

fn is<T: 'static>(&self) -> bool
where Self: MayContain<T>,

Return true if self contains a T.

This is similar to is on dyn Any.

Source

fn type_id(&self) -> TypeId

Return the TypeId of self.

This is similar to type_id on dyn Any.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§