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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use core::fmt::{self, Write};
use core::hash;
use core::mem;
use core::ptr;
use crate::item::{Item, Field, Value, Text};
#[derive(Eq)]
#[repr(C)]
pub struct Attr<'a> {
val: Value<'a>,
key: Text<'a>,
}
impl<'a> Attr<'a> {
pub fn new(key: Text<'a>, val: Value<'a>) -> Attr<'a> {
let attr = Attr {
val: val,
key: key,
};
unsafe {
let key_tag_ptr = attr.key.tag_ptr();
ptr::write(key_tag_ptr, *key_tag_ptr | Value::ATTR_FLAG);
}
attr
}
#[inline]
pub fn get_key(&self) -> &Text<'a> {
&self.key
}
#[inline]
pub fn get_val(&self) -> &Value<'a> {
&self.val
}
#[inline]
pub fn get_val_mut(&mut self) -> &mut Value<'a> {
&mut self.val
}
#[inline]
pub fn get_key_val(&self) -> (&Text<'a>, &Value<'a>) {
(&self.key, &self.val)
}
#[inline]
pub fn get_key_val_mut(&mut self) -> (&Text<'a>, &mut Value<'a>) {
(&self.key, &mut self.val)
}
#[inline]
pub fn as_field(&self) -> &Field<'a> {
unsafe { mem::transmute::<&Attr<'a>, &Field<'a>>(self) }
}
#[inline]
pub fn as_mut_field(&mut self) -> &mut Field<'a> {
unsafe { mem::transmute::<&mut Attr<'a>, &mut Field<'a>>(self) }
}
#[inline]
pub fn as_item(&self) -> &Item<'a> {
unsafe { mem::transmute::<&Attr<'a>, &Item<'a>>(self) }
}
#[inline]
pub fn as_mut_item(&mut self) -> &mut Item<'a> {
unsafe { mem::transmute::<&mut Attr<'a>, &mut Item<'a>>(self) }
}
#[inline]
pub fn into_key(mut self) -> Text<'a> {
let key = unsafe { ptr::read(&self.key) };
unsafe { ptr::drop_in_place(&mut self.val); }
mem::forget(self);
key
}
#[inline]
pub fn into_val(mut self) -> Value<'a> {
unsafe { ptr::drop_in_place(&mut self.key) };
let val = unsafe { ptr::read(&self.val) };
mem::forget(self);
val
}
#[inline]
pub fn into_key_val(self) -> (Text<'a>, Value<'a>) {
let key = unsafe { ptr::read(&self.key) };
let val = unsafe { ptr::read(&self.val) };
mem::forget(self);
(key, val)
}
#[inline]
pub fn into_field(self) -> Field<'a> {
unsafe { mem::transmute::<Attr<'a>, Field<'a>>(self) }
}
#[inline]
pub fn into_item(self) -> Item<'a> {
unsafe { mem::transmute::<Attr<'a>, Item<'a>>(self) }
}
}
impl<'a> AsRef<Field<'a>> for Attr<'a> {
#[inline]
fn as_ref(&self) -> &Field<'a> {
self.as_field()
}
}
impl<'a> AsMut<Field<'a>> for Attr<'a> {
#[inline]
fn as_mut(&mut self) -> &mut Field<'a> {
self.as_mut_field()
}
}
impl<'a> AsRef<Item<'a>> for Attr<'a> {
#[inline]
fn as_ref(&self) -> &Item<'a> {
self.as_item()
}
}
impl<'a> AsMut<Item<'a>> for Attr<'a> {
#[inline]
fn as_mut(&mut self) -> &mut Item<'a> {
self.as_mut_item()
}
}
impl<'a> PartialEq for Attr<'a> {
#[inline]
fn eq(&self, that: &Attr<'a>) -> bool {
self.key.eq(&that.key) && self.val.eq(&that.val)
}
}
impl<'a> hash::Hash for Attr<'a> {
#[inline]
fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
self.key.hash(hasher);
self.val.hash(hasher);
}
}
impl<'a> fmt::Debug for Attr<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("attr")?;
f.write_char('!')?;
f.write_char('(')?;
self.key.fmt(f)?;
if !self.val.is_extant() {
f.write_str(", ")?;
self.val.fmt(f)?;
}
f.write_char(')')
}
}