Crates.io | dyncast |
lib.rs | dyncast |
version | 0.1.0 |
source | src |
created_at | 2023-04-22 16:41:21.618424 |
updated_at | 2024-04-13 20:17:05.047478 |
description | Downcasting made easy |
homepage | |
repository | https://github.com/cynecx/dyncast |
max_upload_size | |
id | 846047 |
size | 39,446 |
Proof of concept.
This library provides opt-in type downcasting to dyn Trait
s.
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());
}
This crate has been tested and validated on the following platforms:
x86_64
, aarch64
x86_64
, aarch64
x86_64