-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcell.rs
More file actions
73 lines (54 loc) · 1.59 KB
/
Copy pathcell.rs
File metadata and controls
73 lines (54 loc) · 1.59 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
use std::any::Any;
use std::fmt;
use std::sync::{Arc, RwLock};
use once_cell::sync::Lazy;
use crate::vm::RuntimeBoolResult;
use super::gen;
use super::new;
use super::base::{ObjectRef, ObjectTrait, TypeRef, TypeTrait};
use super::class::TYPE_TYPE;
use super::ns::Namespace;
// Cell Type -----------------------------------------------------------
gen::type_and_impls!(CellType, Cell);
pub static CELL_TYPE: Lazy<gen::obj_ref_t!(CellType)> =
Lazy::new(|| gen::obj_ref!(CellType::new()));
// Cell Object ---------------------------------------------------------
pub struct Cell {
ns: Namespace,
value: ObjectRef,
}
gen::standard_object_impls!(Cell);
impl Cell {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self { ns: Namespace::default(), value: new::nil() }
}
pub fn with_value(value: ObjectRef) -> Self {
let mut cell = Self::new();
cell.set_value(value);
cell
}
pub fn value(&self) -> ObjectRef {
self.value.clone()
}
pub fn set_value(&mut self, new_value: ObjectRef) {
self.value = new_value;
}
}
impl ObjectTrait for Cell {
gen::object_trait_header!(CELL_TYPE);
fn bool_val(&self) -> RuntimeBoolResult {
Ok(false)
}
}
// Display -------------------------------------------------------------
impl fmt::Display for Cell {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "&({:?})", &*self.value.read().unwrap())
}
}
impl fmt::Debug for Cell {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self}")
}
}