I thought I could try more or less to build an object from scratch from scratch without using blocks impl. To clarify:
trait SomeTrait {
fn fn_1(&self);
fn fn_2(&self, a: i64);
fn fn_3(&self, a: i64, b: i64);
}
struct TraitObject {
data: *mut (),
vtable: *mut (),
}
fn dtor(this: *mut ()) {
}
fn imp_1(this: *mut ()) {
}
fn imp_2(this: *mut (), a: i64) {
}
fn imp_3(this: *mut (), a: i64, b: i64) {
}
fn main() {
let data = &... as *mut ();
let vtable = [dtor as *mut (),
8 as *mut (),
8 as *mut (),
imp_1 as *mut (),
imp_2 as *mut (),
imp_3 as *mut ()];
let to = TraitObject {
data: data,
vtable: vtable.as_ptr() as *mut (),
};
let obj: &SomeTrait = unsafe { mem::transmute(to) };
obj.fn_1();
obj.fn_2(123);
obj.fn_3(123, 456);
}
From what I understand, the order in which member functions appear in a characteristic definition does not always coincide with function pointers in VTable. Is there a way to determine the offsets of each of the trait methods in VTable?
source
share