dyncast 0.1.0

Downcasting made easy
Documentation
  • Coverage
  • 40%
    2 out of 5 items documented2 out of 5 items with examples
  • Size
  • Source code size: 40.52 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.68 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 14s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • cynecx/dyncast
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • cynecx

dyncast

Proof of concept.

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

Fair warning: The soundness of this approach has not been validated.

use std::any::Any;

use dyncast::{dyncast, DyncastExt};

#[dyncast]
trait Boba {
    fn supper(&self);
}

struct A;

#[dyncast]
impl Boba for A {
    fn supper(&self) {
        println!("a")
    }
}

struct B;

#[dyncast]
impl Boba for B {
    fn supper(&self) {
        println!("b")
    }
}

#[dyncast]
trait Soba {}

#[test]
fn boba() {
    let a = A;
    let b = B;

    let a = &a as &dyn Any;
    let b = &b as &dyn Any;

    assert!(a.dyncast_to::<dyn Boba>().is_some());
    assert!(b.dyncast_to::<dyn Boba>().is_some());

    assert!(a.dyncast_to::<dyn Soba>().is_none());
    assert!(b.dyncast_to::<dyn Soba>().is_none());
}

Platform support

This crate has been tested and validated on the following platforms:

  • macOS x86_64, aarch64
  • Linux x86_64, aarch64
  • Windows 1X x86_64