use cpp_oop::gen_oop; use cpp_oop::CastPtr; gen_oop! { #[default] struct Foo { foo: u32, } struct FooLayout { foo: fn(this: &Self, a: i32) -> u32, foo2: fn(this: &Self) -> u32, } impl FooMethodsInner for Foo { extern "C" fn foo_inner(_this: &Self, _a: i32) -> u32 { 1 } extern "C" fn foo2_inner(_this: &Self) -> u32 { 2 } } } gen_oop! { #[default] #[base(Foo)] struct Bar { bar: u32, } struct BarLayout { bar: fn(this: &Self) -> u32, bar2: fn(this: &Self) -> u32, } impl BarMethodsInner for Bar { extern "C" fn bar_inner(_this: &Self) -> u32 { 3 } extern "C" fn bar2_inner(_this: &Self) -> u32 { 4 } } impl FooMethodsInner for Bar { extern "C" fn foo_inner(_this: &Self, _a: i32) -> u32 { 10 } #[unused] extern "C" fn foo2_inner(_this: &Self) -> u32 {} } } gen_oop! { #[default] #[base(Bar)] struct Baz { baz: u32, } struct BazLayout { baz: fn(this: &Self) -> u32, } impl BazMethodsInner for Baz { extern "C" fn baz_inner(_this: &Self) -> u32 { 5 } } impl BarMethodsInner for Baz { extern "C" fn foo_inner(_this: &Self, _a: i32) -> u32 { 100 } #[unused] extern "C" fn foo2_inner(_this: &Self) -> u32 {} extern "C" fn bar_inner(_this: &Self) -> u32 { 30 } #[unused] extern "C" fn bar2_inner(_this: &Self) -> u32 {} } } #[test] fn layout() { assert_eq!(std::mem::size_of::(), std::mem::size_of::()); assert_eq!(std::mem::size_of::(), std::mem::size_of::() * 2); assert_eq!(std::mem::size_of::(), std::mem::size_of::() * 3); } #[test] fn basic() { let bz = Baz::default(); let bz_ptr = &bz as *const Baz; let b_ptr: *const Bar = Bar::cast_const(bz_ptr); let f_ptr: *const Foo = Foo::cast_const(b_ptr); assert_eq!(Foo::foo_inner(&bz.base_bar.base_foo, 0), 1); // manual selection of the method to call assert_eq!(Bar::foo_inner(&bz.base_bar, 0), 10); // manual selection of the method to call assert_eq!(Baz::foo_inner(&bz, 0), 100); // manual selection of the method to call assert_eq!(Foo::foo2_inner(&bz.base_bar.base_foo), 2); // manual selection of the method to call // Bar::foo2_inner will give an error cuz its unused // Baz::foo2_inner will give an error cuz its unused assert_eq!(Bar::bar_inner(&bz.base_bar), 3); // manual selection of the method to call assert_eq!(Baz::bar_inner(&bz), 30); // manual selection of the method to call assert_eq!(Bar::bar2_inner(&bz.base_bar), 4); // manual selection of the method to call // Baz::bar2_inner will give an error cuz its unused assert_eq!(Baz::baz_inner(&bz), 5); // manual selection of the method to call assert_eq!(bz.foo(0), 100); // static dispatch assert_eq!(bz.foo2(), 2); // static dispatch assert_eq!(bz.bar(), 30); // static dispatch assert_eq!(bz.bar2(), 4); // static dispatch assert_eq!(bz.baz(), 5); // static dispatch assert_eq!(f_ptr.foo(0), 1); // this one has no vtable (no virtual methods) so it will use static dispatch assert_eq!(f_ptr.foo2(), 2); // this one has no vtable (no virtual methods) so it will use static dispatch assert_eq!(b_ptr.foo(0), 10); // this one has no vtable (no virtual methods) so it will use static dispatch assert_eq!(b_ptr.foo2(), 2); // this one has no vtable (no virtual methods) so it will use static dispatch assert_eq!(b_ptr.bar(), 3); // this one has no vtable (no virtual methods) so it will use static dispatch assert_eq!(b_ptr.bar2(), 4); // this one has no vtable (no virtual methods) so it will use static dispatch assert_eq!(bz_ptr.foo(0), 100); // this one has no vtable (no virtual methods) so it will use static dispatch assert_eq!(bz_ptr.foo2(), 2); // this one has no vtable (no virtual methods) so it will use static dispatch assert_eq!(bz_ptr.bar(), 30); // this one has no vtable (no virtual methods) so it will use static dispatch assert_eq!(bz_ptr.bar2(), 4); // this one has no vtable (no virtual methods) so it will use static dispatch assert_eq!(bz_ptr.baz(), 5); // this one has no vtable (no virtual methods) so it will use static dispatch }