#cast #testing #xdc #xdc-struct

no-std xdc

eXperimental Dynamic Casting - support library

4 releases (breaking)

Uses new Rust 2024

0.4.0 Apr 13, 2025
0.3.0 Apr 12, 2025
0.2.0 Apr 12, 2025
0.1.0 Jul 2, 2023

#1344 in Rust patterns

Download history 3/week @ 2025-02-25 1/week @ 2025-03-04 292/week @ 2025-04-08 75/week @ 2025-04-15

367 downloads per month

0BSD license

10KB
151 lines

xdc

This crate contains the casting logic for the wider xdc ecosystem. It is intended to be used in conjunction with xdc_macros in order to allow for allowing dynamic casting


lib.rs:

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();

Dependencies

~0.4–0.9MB
~19K SLoC