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