-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanyobject.rs
39 lines (35 loc) · 1.07 KB
/
anyobject.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::any::Any;
// So that we can use const and static with Any
#[repr(C)]
#[derive(Clone, Eq, Hash, PartialEq)]
pub struct AnyObject {
pub data: *mut (),
pub vtable: *mut (),
}
impl AnyObject {
/// "Set" value
pub fn anonymise<T: 'static>(value: T) -> AnyObject {
let any: Box<dyn Any> = Box::new(value) as Box<dyn Any>;
let any: *mut dyn Any = Box::into_raw(any);
unsafe { std::mem::transmute(any) }
}
/// "Get" Value
/// SAFETY: Make sure that the object exists.
pub fn deanonymise<T: 'static>(&self) -> Option<&T> {
unsafe {
let any: *const *const dyn Any = self as *const AnyObject as *const *const dyn Any;
let any: &dyn Any = &*(*any as *const dyn Any);
any.downcast_ref()
}
}
}
impl Drop for AnyObject {
fn drop(&mut self) {
unsafe {
let any: *mut *mut dyn Any = self as *mut AnyObject as *mut *mut dyn Any;
let any: *mut dyn Any = *any;
let any: Box<dyn Any> = Box::from_raw(any);
drop(any);
}
}
}