osiris_typed/types/
pointers.rs

1use std::cell::Ref;
2use osiris_data::data::atomic::Word;
3use osiris_data::data::composite::Array;
4use osiris_data::data::identification::{Address, Area};
5use osiris_data::memory::Memory;
6
7use crate::types::DataType;
8use crate::types::numeral::Size;
9
10#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Default)]
11pub struct Reference(Address);
12
13impl Reference {
14    pub fn new(address: Address) -> Self { Self(address) }
15    pub fn to_address(&self) -> Address { self.0 }
16    pub fn to_word(&self) -> Word { Word::new(self.0.to_u64()) }
17    pub fn offset(&self, size: Size) -> Self { Reference(self.0.offset(size.to_word().to_u64())) }
18    pub fn load_raw(&self, memory: Ref<Memory>, size: Size) -> Option<Array> {
19        let area = Area::region(self.to_address(), size.to_word().to_u64() as usize);
20        match memory.array(area) {
21            Ok(a) => Some(a),
22            Err(_) => None
23        }
24    }
25
26    pub fn load_word(&self, memory: Ref<Memory>) -> Option<Word> {
27        match memory.load(self.to_address()) {
28            Ok(w) => Some(w),
29            Err(_) => None
30        }
31    }
32}
33
34impl DataType for Reference {
35    fn typename(&self) -> String { "&".to_string() }
36
37    fn to_array(&self) -> Array { Array::from(&[self.to_word()]) }
38
39    fn size(&self) -> usize { 1 }
40}
41
42#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Default)]
43pub struct Range {
44    pub start: Reference,
45    pub end: Reference,
46}
47
48impl DataType for Range {
49    fn typename(&self) -> String { format!("&{}", self.end.0.to_u64() - self.start.0.to_u64()) }
50    fn to_array(&self) -> Array {
51        Array::from(&[self.start.to_word(), self.end.to_word()])
52    }
53    fn size(&self) -> usize { 2 }
54}