dynamic-object

Crates.iodynamic-object
lib.rsdynamic-object
version0.1.2
sourcesrc
created_at2022-09-01 15:12:47.279183
updated_at2022-09-04 14:06:36.961983
descriptionInheritance for rust
homepage
repositoryhttps://github.com/Frost-54/dynamic-object-rs.git
max_upload_size
id656792
size13,875
(Frost-54)

documentation

README

Dynamic-rs

Inheritance in rust

This library provides runtime type information for dynamic casting(upcast/downcast) using LLVM-style RTTI

Usages

#[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();
Commit count: 19

cargo fmt