1 个不稳定版本
0.0.0 | 2023年12月4日 |
---|
#35 in #usize
4KB
检查一个类型是否实现了特质
use instance::{define_interface, Interface};
define_interface!(TypeSize);
trait TypeSize {
fn type_size(&self) -> usize;
}
struct WasmI32(i32);
struct WasmI64(i64);
impl TypeSize for WasmI32 {
fn type_size(&self) -> usize {
4
}
}
fn main() {
let v1 = WasmI32(0);
let object = v1.cast();
println!("{:?}", object.map(|v| v.meta_data())); // 4
let v2 = WasmI64(0);
let object = Interface::<dyn TypeSize>::recast(&v2);
println!("{:?}", object.map(|v| v.meta_data())); // None
}