ruby_marshal/value_arena/
value.rs1use crate::TypedValueHandle;
2use crate::ValueHandle;
3
4#[derive(Debug)]
6pub enum Value {
7 Nil(NilValue),
9
10 Bool(BoolValue),
12
13 Fixnum(FixnumValue),
15
16 Symbol(SymbolValue),
18
19 Array(ArrayValue),
21
22 Hash(HashValue),
24
25 Object(ObjectValue),
27
28 String(StringValue),
30
31 UserDefined(UserDefinedValue),
33}
34
35impl Value {
36 pub fn as_symbol(&self) -> Option<&SymbolValue> {
38 match self {
39 Self::Symbol(value) => Some(value),
40 _ => None,
41 }
42 }
43
44 pub fn as_object(&self) -> Option<&ObjectValue> {
46 match self {
47 Self::Object(value) => Some(value),
48 _ => None,
49 }
50 }
51
52 pub fn as_string(&self) -> Option<&StringValue> {
54 match self {
55 Self::String(value) => Some(value),
56 _ => None,
57 }
58 }
59
60 pub fn kind(&self) -> ValueKind {
62 match self {
63 Self::Nil(_) => ValueKind::Nil,
64 Self::Bool(_) => ValueKind::Bool,
65 Self::Fixnum(_) => ValueKind::Fixnum,
66 Self::Symbol(_) => ValueKind::Symbol,
67 Self::Array(_) => ValueKind::Array,
68 Self::Hash(_) => ValueKind::Hash,
69 Self::Object(_) => ValueKind::Object,
70 Self::String(_) => ValueKind::String,
71 Self::UserDefined(_) => ValueKind::UserDefined,
72 }
73 }
74}
75
76impl From<NilValue> for Value {
77 fn from(value: NilValue) -> Self {
78 Self::Nil(value)
79 }
80}
81
82impl From<BoolValue> for Value {
83 fn from(value: BoolValue) -> Self {
84 Self::Bool(value)
85 }
86}
87
88impl From<FixnumValue> for Value {
89 fn from(value: FixnumValue) -> Self {
90 Self::Fixnum(value)
91 }
92}
93
94impl From<SymbolValue> for Value {
95 fn from(value: SymbolValue) -> Self {
96 Self::Symbol(value)
97 }
98}
99
100impl From<ArrayValue> for Value {
101 fn from(value: ArrayValue) -> Self {
102 Self::Array(value)
103 }
104}
105
106impl From<HashValue> for Value {
107 fn from(value: HashValue) -> Self {
108 Self::Hash(value)
109 }
110}
111
112impl From<ObjectValue> for Value {
113 fn from(value: ObjectValue) -> Self {
114 Self::Object(value)
115 }
116}
117
118impl From<StringValue> for Value {
119 fn from(value: StringValue) -> Self {
120 Self::String(value)
121 }
122}
123
124impl From<UserDefinedValue> for Value {
125 fn from(value: UserDefinedValue) -> Self {
126 Self::UserDefined(value)
127 }
128}
129
130#[derive(Debug)]
132pub struct NilValue;
133
134#[derive(Debug, Copy, Clone)]
136pub struct BoolValue {
137 value: bool,
138}
139
140impl BoolValue {
141 pub(super) fn new(value: bool) -> Self {
143 Self { value }
144 }
145
146 pub fn value(self) -> bool {
148 self.value
149 }
150}
151
152#[derive(Debug, Copy, Clone)]
154pub struct FixnumValue {
155 value: i32,
156}
157
158impl FixnumValue {
159 pub(super) fn new(value: i32) -> Self {
161 Self { value }
162 }
163
164 pub fn value(self) -> i32 {
166 self.value
167 }
168}
169
170#[derive(Debug)]
172pub struct SymbolValue {
173 value: Vec<u8>,
174}
175
176impl SymbolValue {
177 pub(super) fn new(value: Vec<u8>) -> Self {
179 Self { value }
180 }
181
182 pub fn value(&self) -> &[u8] {
184 &self.value
185 }
186}
187
188#[derive(Debug)]
190pub struct ArrayValue {
191 value: Vec<ValueHandle>,
192}
193
194impl ArrayValue {
195 pub(crate) fn new(value: Vec<ValueHandle>) -> Self {
197 Self { value }
198 }
199
200 pub fn value(&self) -> &[ValueHandle] {
202 &self.value
203 }
204
205 pub fn len(&self) -> usize {
207 self.value.len()
208 }
209
210 pub fn is_empty(&self) -> bool {
212 self.value.is_empty()
213 }
214}
215
216#[derive(Debug)]
218pub struct HashValue {
219 value: Vec<(ValueHandle, ValueHandle)>,
220 default_value: Option<ValueHandle>,
221}
222
223impl HashValue {
224 pub(crate) fn new(
226 value: Vec<(ValueHandle, ValueHandle)>,
227 default_value: Option<ValueHandle>,
228 ) -> Self {
229 Self {
230 value,
231 default_value,
232 }
233 }
234
235 pub fn value(&self) -> &[(ValueHandle, ValueHandle)] {
237 &self.value
238 }
239
240 pub fn default_value(&self) -> Option<ValueHandle> {
242 self.default_value
243 }
244}
245
246#[derive(Debug)]
248pub struct ObjectValue {
249 name: TypedValueHandle<SymbolValue>,
250 instance_variables: Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>,
251}
252
253impl ObjectValue {
254 pub(crate) fn new(
256 name: TypedValueHandle<SymbolValue>,
257 instance_variables: Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>,
258 ) -> Self {
259 Self {
260 name,
261 instance_variables,
262 }
263 }
264
265 pub fn name(&self) -> TypedValueHandle<SymbolValue> {
267 self.name
268 }
269
270 pub fn instance_variables(&self) -> &[(TypedValueHandle<SymbolValue>, ValueHandle)] {
272 &self.instance_variables
273 }
274}
275
276#[derive(Debug)]
278pub struct StringValue {
279 value: Vec<u8>,
280 instance_variables: Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>>,
281}
282
283impl StringValue {
284 pub(crate) fn new(value: Vec<u8>) -> Self {
286 Self {
287 value,
288 instance_variables: None,
289 }
290 }
291
292 pub fn value(&self) -> &[u8] {
294 &self.value
295 }
296
297 pub fn instance_variables(&self) -> Option<&[(TypedValueHandle<SymbolValue>, ValueHandle)]> {
299 self.instance_variables.as_deref()
300 }
301
302 pub(crate) fn set_instance_variables(
307 &mut self,
308 mut instance_variables: Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>>,
309 ) -> Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>> {
310 std::mem::swap(&mut self.instance_variables, &mut instance_variables);
311 instance_variables
312 }
313}
314
315#[derive(Debug)]
317pub struct UserDefinedValue {
318 name: TypedValueHandle<SymbolValue>,
319 value: Vec<u8>,
320 instance_variables: Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>>,
321}
322
323impl UserDefinedValue {
324 pub(crate) fn new(name: TypedValueHandle<SymbolValue>, value: Vec<u8>) -> Self {
326 Self {
327 name,
328 value,
329 instance_variables: None,
330 }
331 }
332
333 pub fn name(&self) -> TypedValueHandle<SymbolValue> {
335 self.name
336 }
337
338 pub fn value(&self) -> &[u8] {
340 &self.value
341 }
342
343 pub fn instance_variables(&self) -> Option<&[(TypedValueHandle<SymbolValue>, ValueHandle)]> {
345 self.instance_variables.as_deref()
346 }
347
348 pub(crate) fn set_instance_variables(
353 &mut self,
354 mut instance_variables: Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>>,
355 ) -> Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>> {
356 std::mem::swap(&mut self.instance_variables, &mut instance_variables);
357 instance_variables
358 }
359}
360
361#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
363pub enum ValueKind {
364 Nil,
365 Bool,
366 Fixnum,
367 Symbol,
368 Array,
369 Hash,
370 Object,
371 String,
372 UserDefined,
373}