| 1 | use super::*; |
| 2 | use core::ffi::c_void; |
| 3 | use core::marker::PhantomData; |
| 4 | |
| 5 | #[doc (hidden)] |
| 6 | #[repr (C)] |
| 7 | pub struct ScopedHeap { |
| 8 | pub vtable: *const c_void, |
| 9 | pub this: *const c_void, |
| 10 | } |
| 11 | |
| 12 | #[doc (hidden)] |
| 13 | pub struct ScopedInterface<'a, T: Interface> { |
| 14 | interface: T, |
| 15 | lifetime: PhantomData<&'a T>, |
| 16 | } |
| 17 | |
| 18 | impl<T: Interface> ScopedInterface<'_, T> { |
| 19 | pub fn new(interface: T) -> Self { |
| 20 | Self { |
| 21 | interface, |
| 22 | lifetime: PhantomData, |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | impl<T: Interface> core::ops::Deref for ScopedInterface<'_, T> { |
| 28 | type Target = T; |
| 29 | |
| 30 | fn deref(&self) -> &T { |
| 31 | &self.interface |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl<T: Interface> Drop for ScopedInterface<'_, T> { |
| 36 | fn drop(&mut self) { |
| 37 | unsafe { |
| 38 | let _ = Box::from_raw(self.interface.as_raw() as *const _ as *mut ScopedHeap); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |