Crates.io | dynamic-object |
lib.rs | dynamic-object |
version | 0.1.2 |
source | src |
created_at | 2022-09-01 15:12:47.279183 |
updated_at | 2022-09-04 14:06:36.961983 |
description | Inheritance for rust |
homepage | |
repository | https://github.com/Frost-54/dynamic-object-rs.git |
max_upload_size | |
id | 656792 |
size | 13,875 |
Inheritance in rust
This library provides runtime type information for dynamic casting(upcast/downcast) using LLVM-style RTTI
#[subclass(DynamicObjectBase)]
struct Class {
value: u32,
foo: u32
}
#[subclass(Class, parent)]
struct Derived {
field: u32,
parent: Class,
}
let object = Derived {
parent: Class {
value: 548389,
foo: 72840548
},
field: 2153746,
};
let object = Object::<Derived>::new(Box::new(object));
assert!(object.field == 2153746);
assert!(object.parent.value == 548389);
assert!(object.parent.foo == 72840548);
// Downcast
let object = object.cast::<Class>();
assert!(object.value == 548389);
assert!(object.foo == 72840548);
// Upcast
let object = object.cast::<Derived>();
assert!(object.field == 2153746);
assert!(object.parent.value == 548389);
assert!(object.parent.foo == 72840548);
To use virtual methods
// Set second generic argument to your trait
type MyObject = Object<MyClass, Box<dyn MyTrait>>
// And access the vtable by
object.vtable().method();
// Or
object.vtable_mut().methid();