-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathns.rs
More file actions
121 lines (100 loc) · 3.15 KB
/
Copy pathns.rs
File metadata and controls
121 lines (100 loc) · 3.15 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
use std::fmt;
use indexmap::IndexMap;
use super::base::ObjectRef;
use super::map::Map;
pub type Objects = IndexMap<String, ObjectRef>;
/// A namespace is a container for object attributes. Note that the
/// `Namespace` type is not a *system* type.
///
/// TODO: This shouldn't need to be cloneable
#[derive(Clone)]
pub struct Namespace {
objects: Objects,
}
unsafe impl Send for Namespace {}
unsafe impl Sync for Namespace {}
impl Default for Namespace {
fn default() -> Self {
Self::new(IndexMap::default())
}
}
impl Namespace {
pub fn new(objects: Objects) -> Self {
Self { objects }
}
pub fn with_entries(entries: &[(&str, ObjectRef)]) -> Self {
let mut ns = Self::default();
ns.extend(entries);
ns
}
pub fn clear(&mut self) {
self.objects.clear()
}
pub fn contains_key(&self, name: &str) -> bool {
self.objects.contains_key(name)
}
pub fn iter(&self) -> indexmap::map::Iter<'_, String, ObjectRef> {
self.objects.iter()
}
pub fn len(&self) -> usize {
self.objects.len()
}
pub fn get(&self, name: &str) -> Option<ObjectRef> {
self.objects.get(name).cloned()
}
/// Add an object, settings its initial value as specified (usually
/// nil).
pub fn insert<S: Into<String>>(&mut self, name: S, obj: ObjectRef) {
self.objects.insert(name.into(), obj);
}
/// Set an object's value. This will only succeed if the object
/// already exists in the namespace.
pub fn set(&mut self, name: &str, obj: ObjectRef) -> bool {
if self.objects.contains_key(name) {
self.objects.insert(name.to_owned(), obj);
true
} else {
false
}
}
pub fn extend(&mut self, entries: &[(&str, ObjectRef)]) {
self.objects.extend(entries.iter().map(|(k, v)| (k.to_string(), v.clone())));
}
pub fn extend_from_map(&mut self, map: &Map) {
for (name, val) in map.entries().read().unwrap().iter() {
self.insert(name, val.clone());
}
}
pub fn is_equal(&self, other: &Namespace) -> bool {
if self.len() != other.len() {
// Namespaces have a different number of entries, so
// they can't be equal.
return false;
}
if !self.objects.keys().all(|k| other.objects.contains_key(k)) {
// Namespaces have differing keys, so they can't be
// equal.
return false;
}
// Otherwise, compare all entries for equality.
for (name, lhs_val) in self.objects.iter() {
let lhs_val = lhs_val.read().unwrap();
let rhs_val = &other.objects[name].read().unwrap();
if !lhs_val.is_equal(&**rhs_val) {
return false;
}
}
true
}
}
// Display -------------------------------------------------------------
impl fmt::Display for Namespace {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<namespace>")
}
}
impl fmt::Debug for Namespace {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self}")
}
}