as_base

Crates.ioas_base
lib.rsas_base
version0.1.3
sourcesrc
created_at2023-08-29 19:41:54.215422
updated_at2023-09-01 05:12:17.831849
descriptionCast trait objects to some base class
homepage
repositoryhttps://github.com/m-mueller678/as_base
max_upload_size
id958397
size3,923
(m-mueller678)

documentation

README

as_base

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)
}
Commit count: 8

cargo fmt