pub trait Object: Sized {
fn id(&self) -> usize {
std::ptr::addr_of!(*self) as usize
}
fn r#type(&self) -> String {
std::any::type_name::<Self>().to_string()
}
fn is<T: Object>(&self, other: &Option<T>) -> bool {
self.id() == other.id()
}
fn __getattribute__(&self, _name: impl AsRef<str>) -> Option<impl Object> {
std::option::Option::<i32>::None
}
fn __setattribute__<T: Object>(&mut self, _name: impl AsRef<str>, _value: T) {
unimplemented!()
}
fn __delattribute__(&mut self, _name: impl AsRef<str>) {
unimplemented!()
}
fn __dir__(&self) -> Vec<impl AsRef<str>> {
unimplemented!();
vec![
"__class__",
"__class_getitem__",
"__contains__",
"__delattr__",
"__delitem__",
"__dir__",
"__doc__",
"__eq__",
"__format__",
"__ge__",
"__getattribute__",
"__getitem__",
"__getstate__",
"__gt__",
"__hash__",
"__init__",
"__init_subclass__",
"__ior__",
"__iter__",
"__le__",
"__len__",
"__lt__",
"__ne__",
"__new__",
"__or__",
"__reduce__",
"__reduce_ex__",
"__repr__",
"__reversed__",
"__ror__",
"__setattr__",
"__setitem__",
"__sizeof__",
"__str__",
"__subclasshook__",
"clear",
"copy",
"fromkeys",
"get",
"items",
"keys",
"pop",
"popitem",
"setdefault",
"update",
"values",
]
}
}
impl Object for i8 {}
impl Object for i16 {}
impl Object for i32 {}
impl Object for i64 {}
impl Object for i128 {}
impl Object for u8 {}
impl Object for u16 {}
impl Object for u32 {}
impl Object for u64 {}
impl Object for u128 {}
impl Object for String {}
impl Object for &str {}
impl Object for bool {}
impl Object for f32 {}
impl Object for f64 {}
impl Object for char {}
impl<T: Object> Object for Option<T> {
fn is<U: Object>(&self, other: &Option<U>) -> bool {
match (self, other) {
(Some(_), std::option::Option::None) => false,
(std::option::Option::None, Some(_)) => false,
(Some(a), Some(_b)) => a.is(other),
(std::option::Option::None, std::option::Option::None) => true,
}
}
}
#[allow(non_upper_case_globals)]
pub static None: Option<String> = std::option::Option::<String>::None;
#[allow(non_upper_case_globals)]
pub static NotImplemented: Option<&str> = Some("NotImplemented");
#[allow(non_upper_case_globals)]
pub static Ellipsis: &str = "...";
pub mod number;
pub mod namespace;
pub use namespace::*;
pub mod class;
pub use class::*;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_id() {
let x = 5;
let y = 6;
println!("x: {:p}, type: {}", &x, x.r#type());
assert_eq!(Object::id(&x), Object::id(&x));
assert_ne!(Object::id(&x), Object::id(&y));
}
#[test]
fn test_none() {
let x = &None;
let y: Option<i32> = std::option::Option::None;
assert_eq!(x.is(&None), true);
assert_ne!(x.is(&NotImplemented), true);
assert_eq!(y.is(&None), true);
assert_ne!(y.is(&NotImplemented), true);
}
}