pub struct ThinBox<Trait: ?Sized + 'static>(/* private fields */);
Expand description
A Box of a trait with a size of size_of::<usize>
The ThinBox can be created from a Box which implement the HasVPtr
trait Shape { fn area(&self) -> f32; }
#[vptr(Shape)]
#[derive(Default)]
struct Rectangle { w: f32, h : f32 }
impl Shape for Rectangle { fn area(&self) -> f32 { self.w * self.h } }
let r = Box::new(Rectangle { w: 5., h: 10., ..Default::default() });
let thin = ThinBox::from_box(r);
assert_eq!(thin.area(), 50.);
The size is the size of a pointer
assert_eq!(mem::size_of::<ThinBox<dyn Trait>>(), mem::size_of::<usize>());
assert_eq!(mem::size_of::<Option<ThinBox<dyn Trait>>>(), mem::size_of::<usize>());
Implementations§
Source§impl<Trait: ?Sized + 'static> ThinBox<Trait>
impl<Trait: ?Sized + 'static> ThinBox<Trait>
Sourcepub fn as_thin_ref(b: &ThinBox<Trait>) -> ThinRef<'_, Trait>
pub fn as_thin_ref(b: &ThinBox<Trait>) -> ThinRef<'_, Trait>
As a ThinRef
Sourcepub fn as_thin_ref_mut(b: &mut ThinBox<Trait>) -> ThinRefMut<'_, Trait>
pub fn as_thin_ref_mut(b: &mut ThinBox<Trait>) -> ThinRefMut<'_, Trait>
As a ThinRefMut
Trait Implementations§
Auto Trait Implementations§
impl<Trait> Freeze for ThinBox<Trait>where
Trait: ?Sized,
impl<Trait> RefUnwindSafe for ThinBox<Trait>where
Trait: RefUnwindSafe + ?Sized,
impl<Trait> !Send for ThinBox<Trait>
impl<Trait> !Sync for ThinBox<Trait>
impl<Trait> Unpin for ThinBox<Trait>where
Trait: ?Sized,
impl<Trait> UnwindSafe for ThinBox<Trait>where
Trait: RefUnwindSafe + ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more