-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom.rs
More file actions
171 lines (137 loc) · 3.61 KB
/
Copy pathcustom.rs
File metadata and controls
171 lines (137 loc) · 3.61 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::any::Any;
use std::fmt;
use std::sync::{Arc, RwLock};
use super::gen;
use super::new;
use super::base::{ObjectRef, ObjectTrait, TypeRef, TypeTrait};
use super::class::TYPE_TYPE;
use super::ns::Namespace;
// Custom Type ---------------------------------------------------------
/// TODO: This shouldn't need to be cloneable
#[derive(Clone)]
pub struct CustomType {
ns: Namespace,
module: ObjectRef,
name: String,
full_name: String,
}
impl CustomType {
pub fn new(module_ref: ObjectRef, name: String) -> Self {
let module = module_ref.read().unwrap();
let module = module.down_to_mod().unwrap();
let full_name = format!("{}.{name}", module.name());
let type_ref = Self {
ns: Namespace::with_entries(&[
// Class Attributes
("$module_name", new::str(module.name())),
("$full_name", new::str(&full_name)),
("$name", new::str(&name)),
]),
module: module_ref.clone(),
name,
full_name,
};
type_ref
}
}
gen::standard_object_impls!(CustomType);
impl TypeTrait for CustomType {
fn name(&self) -> &str {
self.name.as_str()
}
fn full_name(&self) -> &str {
self.full_name.as_str()
}
fn ns(&self) -> &Namespace {
&self.ns
}
fn module(&self) -> ObjectRef {
self.module.clone()
}
}
/// NOTE: This is customized so the module is correct.
impl ObjectTrait for CustomType {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn class(&self) -> TypeRef {
TYPE_TYPE.clone()
}
fn type_obj(&self) -> ObjectRef {
TYPE_TYPE.clone()
}
fn ns(&self) -> &Namespace {
&self.ns
}
fn ns_mut(&mut self) -> &mut Namespace {
&mut self.ns
}
fn as_type(&self) -> Option<&dyn TypeTrait> {
Some(self)
}
fn module(&self) -> ObjectRef {
self.module.clone()
}
}
// Custom Object -------------------------------------------------------
pub struct CustomObj {
type_obj: gen::obj_ref_t!(CustomType),
ns: Namespace,
}
gen::standard_object_impls!(CustomObj);
impl CustomObj {
pub fn new(type_obj: gen::obj_ref_t!(CustomType), attrs: Namespace) -> Self {
Self { type_obj, ns: attrs }
}
}
impl ObjectTrait for CustomObj {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn class(&self) -> TypeRef {
self.type_obj.clone()
}
fn type_obj(&self) -> ObjectRef {
self.type_obj.clone()
}
fn ns(&self) -> &Namespace {
&self.ns
}
fn ns_mut(&mut self) -> &mut Namespace {
&mut self.ns
}
fn as_type(&self) -> Option<&dyn TypeTrait> {
None
}
fn set_attr(
&mut self,
name: &str,
value: ObjectRef,
_this: ObjectRef,
) -> ObjectRef {
self.ns.set(name, value);
new::nil()
}
fn is_equal(&self, rhs: &dyn ObjectTrait) -> bool {
self.is(rhs) || rhs.is_always() || self.ns.is_equal(rhs.ns())
}
}
// Display -------------------------------------------------------------
impl fmt::Display for CustomObj {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let class = self.class();
let class = class.read().unwrap();
write!(f, "<{} object @ {}>", class.full_name(), self.id())
}
}
impl fmt::Debug for CustomObj {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self}")
}
}