Crate dyncast

Source
Expand description

§dyncast

github crates-io

Fair warning: This crate is a proof of concept. The soundness of this crate has not been validated. Use at your own risk.

This library provides opt-in type downcasting to dyn Traits.

The entrypoint for this library is the dyncast proc-macro, which should be applied on every trait and impl, you’d want to enable downcasting to. The proc-macro will additionally implement Dyncast for the selected trait (dyn Trait), which can be used to check whether a concrete type implements such trait.

§Example

use std::any::Any;

use dyncast::{dyncast, DyncastExt};

#[dyncast]
trait Foo {
    fn bar(&self);
}

#[dyncast]
impl Foo for () {
    fn bar(&self) {
        println!("a")
    }
}

#[test]
fn boba() {
    let a = &() as &dyn Any;
    assert!(a.dyncast_to::<dyn Foo>().is_some());
}

Traits§

Dyncast
DyncastExt
Provides a shorthand method dyncast_to.

Attribute Macros§

dyncast
This proc-macro can be used on trait definitions and trait impls.