use std::fmt::{Display, Formatter};
#[derive(Debug, Clone, Copy, PartialOrd, PartialEq)]
pub struct Object {
ptr: *mut (),
}
impl Object {
pub fn new<T>(val: *mut T) -> Self {
Self {
ptr: val as *mut ()
}
}
pub fn get<T: Clone>(&self) -> T {
unsafe { self.__get_unsafe() }
}
unsafe fn __get_unsafe<T: Clone>(&self) -> T {
(*(self.ptr as *mut T)).clone()
}
pub fn get_ptr<T>(&self) -> *mut T {
unsafe { self.__get_ptr_unsafe() }
}
unsafe fn __get_ptr_unsafe<T>(&self) -> *mut T {
self.ptr as *mut T
}
pub fn raw(&self) -> *mut () {
self.ptr
}
pub fn __value_to_string<T: ToString>(&self) -> String {
unsafe { self.__value_to_string_unsafe::<T>() }
}
unsafe fn __value_to_string_unsafe<T: ToString>(&self) -> String {
(*self.get_ptr::<T>()).to_string()
}
pub fn equals<T>(&self, other: T) -> bool
where T: PartialEq + Clone,
{
other.eq(&self.get::<T>())
}
}
impl Default for Object {
fn default() -> Self {
Self { ptr: std::ptr::null_mut(), }
}
}
impl Display for Object {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Object({:?})", self.ptr)
}
}
#[macro_export]
macro_rules! obj {
($val:expr) => {
$crate::Object::new(&mut $val)
};
() => {
$crate::Object::default()
}
}