roussillon_type_system/value/
reference.rs1use std::cell::RefCell;
2use std::rc::Rc;
3
4use crate::types::concept::Type;
5use crate::types::primitive::Primitive;
6use crate::value::concept::{DataValue, ValueCell};
7
8#[derive(Clone, Debug)]
9pub struct Reference {
10 to_type: Type,
11 address: usize,
12}
13
14impl Reference {
15 pub fn new(to_type: Type, address: usize) -> Self {
16 Self { to_type, address }
17 }
18 pub fn get_address(&self) -> usize {
19 self.address
20 }
21 pub fn to_cell(self) -> ValueCell { Rc::new(RefCell::new(self)) }
22 pub fn referenced(&self) -> &Type { &self.to_type }
23 pub fn from(t: Type, raw: &[u8]) -> Self {
24 Self::new(t, usize::from_be_bytes(raw.try_into().unwrap_or_default()))
25 }
26}
27
28impl PartialEq for Reference {
29 fn eq(&self, other: &Self) -> bool {
30 self.to_type.typename() == other.to_type.typename()
31 && self.address == other.address
32 }
33}
34
35impl Eq for Reference {}
36
37impl DataValue for Reference {
38 fn data_type(&self) -> Type {
39 Primitive::Reference(self.to_type.clone(), 8).to_rc()
40 }
41
42 fn raw(&self) -> Vec<u8> {
43 self.address.to_be_bytes().to_vec()
44 }
45
46 fn set(&mut self, raw: &[u8]) {
47 self.address = usize::from_be_bytes(raw.try_into().unwrap())
48 }
49}