1use std::fmt::{Display, Formatter};
2
3
4
5#[derive(Debug, Clone, Copy, PartialOrd, PartialEq)]
7pub struct Object {
8 ptr: *mut (),
9}
10
11
12impl Object {
13 pub fn new<T>(val: *mut T) -> Self {
14 Self {
15 ptr: val as *mut ()
16 }
17 }
18
19
20 pub fn get<T: Clone>(&self) -> T {
30 unsafe { self.__get_unsafe() }
31 }
32
33 unsafe fn __get_unsafe<T: Clone>(&self) -> T {
34 (*(self.ptr as *mut T)).clone()
35 }
36
37
38 pub fn get_ptr<T>(&self) -> *mut T {
48 unsafe { self.__get_ptr_unsafe() }
49 }
50
51 unsafe fn __get_ptr_unsafe<T>(&self) -> *mut T {
52 self.ptr as *mut T
53 }
54
55
56 pub fn raw(&self) -> *mut () {
58 self.ptr
59 }
60
61
62 pub fn __value_to_string<T: ToString>(&self) -> String {
70 unsafe { self.__value_to_string_unsafe::<T>() }
71 }
72
73 unsafe fn __value_to_string_unsafe<T: ToString>(&self) -> String {
74 (*self.get_ptr::<T>()).to_string()
75 }
76
77
78 pub fn equals<T>(&self, other: T) -> bool
79 where T: PartialEq + Clone,
80 {
81 other.eq(&self.get::<T>())
82 }
83}
84
85
86impl Default for Object {
87 fn default() -> Self {
88 Self { ptr: std::ptr::null_mut(), }
89 }
90}
91
92
93impl Display for Object {
94 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
95 write!(f, "Object({:?})", self.ptr)
96 }
97}
98
99
100
101#[macro_export]
108macro_rules! obj {
109 ($val:expr) => {
110 $crate::Object::new(&mut $val)
111 };
112 () => {
113 $crate::Object::default()
114 }
115}
116
117
118
119
120