Crate as_base

Source
Expand description

§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)
}

Traits§

AsBase
Marker trait for base casting.
AsBaseMut
Marker trait to indicate that transmuting &mut Self to &mut T is safe.
AsBaseMutExt
Extension method for AsBaseMut .
AsBasePin
Marker trait to indicate that transmuting std::pin::Pin<&Self> to std::pin::Pin<&T> is safe.
AsBasePinExt
Extension method for AsBasePin .
AsBasePinMut
Marker trait to indicate that transmuting std::pin::Pin<&mut Self> to std::pin::Pin<&mut T> is safe.
AsBasePinMutExt
Extension method for AsBasePinMut .
AsBaseRef
Marker trait to indicate that transmuting &Self to &T is safe.
AsBaseRefExt
Extension method for AsBaseRef .

Derive Macros§

AsBase
Implements AsBase.
AsBaseMut
More specific version of AsBase.
AsBasePin
More specific version of AsBase.
AsBasePinMut
More specific version of AsBase.
AsBaseRef
More specific version of AsBase.