Crates.io | as_base |
lib.rs | as_base |
version | 0.1.3 |
source | src |
created_at | 2023-08-29 19:41:54.215422 |
updated_at | 2023-09-01 05:12:17.831849 |
description | Cast trait objects to some base class |
homepage | |
repository | https://github.com/m-mueller678/as_base |
max_upload_size | |
id | 958397 |
size | 3,923 |
This crate allows directly accessing fields within a trait object similar to C++ public base classes. No virtual dispatch is involved, the base object always begins at the same address as the enclosing object.
use as_base::*;
struct BaseType {
x: u64,
}
trait MyTrait: AsBase<BaseType> {}
#[derive(AsBase)]
#[repr(C)]
struct Implementor {
pub base: BaseType,
}
impl MyTrait for Implementor {}
fn main() {
let x = Implementor {
base: BaseType { x: 42 },
};
let dyn_reference = &x as &dyn MyTrait;
assert_eq!(dyn_reference.as_base().x, 42)
}