Crate xdc

Source
Expand description

eXperimental Dynamic Casting for Rust

§Example

use xdc::*;
trait Parent : ObjBase {}
trait Foo : Parent {}
trait Bar : Parent {}

struct Test {}
xdc_struct!(Test);

impl Parent for Test {}
xdc_impl!(Parent, Test);

impl Foo for Test {}
xdc_impl!(Foo, Test);

impl Bar for Test {}
xdc_impl!(Bar, Test);

let mut example = Test {};

let foo_example: &dyn Foo = &example;
let bar_example: &dyn Bar = xdc::try_cast(foo_example).unwrap();

let foo_example_mut: &mut dyn Foo = &mut example;
let bar_example_mut: &mut dyn Bar = xdc::try_cast_mut(foo_example_mut).unwrap();

let foo_example_box: Box<dyn Foo> = Box::new(example);
let bar_example_box: Box<dyn Bar> = xdc::try_cast_boxed(foo_example_box).unwrap();

Re-exports§

pub use linkme;

Macros§

metadata_entry
Will be used to generate a MetadataEntry describing how to cast to “$type struct being referred to using a $trait trait object pointer”
mident
Macro Identifier toolbox macro
xdc_impl
xdc_impl_
xdc_struct
xdc_struct_

Structs§

FatPointer
The Rust fat pointer structure as defined by the compiler
MetadataEntry
The metadata needed to allow for casting

Traits§

ObjBase
Base trait that will be added to all cast-able structs

Functions§

try_cast
Dynamic cast between immutable trait objects
try_cast_boxed
Dynamic cast between Boxed trait objects
try_cast_mut
Dynamic cast between mutable trait objects
type_id
Get a workaround for the TypeId of a given type