metaemu_state/
unique.rs

1use std::ops::{Deref, DerefMut};
2
3use fugue::ir::{Address, Translator};
4
5use crate::flat::FlatState;
6use crate::traits::{State, StateOps, StateValue};
7
8pub use crate::flat::Error;
9
10#[derive(Debug, Clone, Eq, PartialEq, Hash)]
11#[repr(transparent)]
12pub struct UniqueState<T: StateValue>(FlatState<T>);
13
14impl<T: StateValue> AsRef<Self> for UniqueState<T> {
15    #[inline(always)]
16    fn as_ref(&self) -> &Self {
17        self
18    }
19}
20
21impl<T: StateValue> AsMut<Self> for UniqueState<T> {
22    #[inline(always)]
23    fn as_mut(&mut self) -> &mut Self {
24        self
25    }
26}
27
28impl<T: StateValue> AsRef<FlatState<T>> for UniqueState<T> {
29    #[inline(always)]
30    fn as_ref(&self) -> &FlatState<T> {
31        &self.0
32    }
33}
34
35impl<T: StateValue> AsMut<FlatState<T>> for UniqueState<T> {
36    #[inline(always)]
37    fn as_mut(&mut self) -> &mut FlatState<T> {
38        &mut self.0
39    }
40}
41
42impl<T: StateValue> Deref for UniqueState<T> {
43    type Target = FlatState<T>;
44
45    fn deref(&self) -> &Self::Target {
46        &self.0
47    }
48}
49
50impl<T: StateValue> DerefMut for UniqueState<T> {
51    fn deref_mut(&mut self) -> &mut Self::Target {
52        &mut self.0
53    }
54}
55
56impl<T: StateValue> From<UniqueState<T>> for FlatState<T> {
57    fn from(t: UniqueState<T>) -> Self {
58        t.0
59    }
60}
61
62impl<V: StateValue> State for UniqueState<V> {
63    type Error = Error;
64
65    #[inline(always)]
66    fn fork(&self) -> Self {
67        Self(self.0.fork())
68    }
69
70    #[inline(always)]
71    fn restore(&mut self, other: &Self) {
72        self.0.restore(&other.0)
73    }
74}
75
76impl<V: StateValue> StateOps for UniqueState<V> {
77    type Value = V;
78
79    #[inline(always)]
80    fn len(&self) -> usize {
81        self.0.len()
82    }
83
84    #[inline(always)]
85    fn copy_values<F, T>(&mut self, from: F, to: T, size: usize) -> Result<(), Self::Error>
86    where F: Into<Address>,
87          T: Into<Address> {
88        self.0.copy_values(from, to, size)
89    }
90
91    #[inline(always)]
92    fn get_values<A>(&self, address: A, bytes: &mut [Self::Value]) -> Result<(), Self::Error>
93    where A: Into<Address> {
94        self.0.get_values(address, bytes)
95    }
96
97    #[inline(always)]
98    fn view_values<A>(&self, address: A, size: usize) -> Result<&[Self::Value], Self::Error>
99    where A: Into<Address> {
100        self.0.view_values(address, size)
101    }
102
103    #[inline(always)]
104    fn view_values_mut<A>(&mut self, address: A, size: usize) -> Result<&mut [Self::Value], Self::Error>
105    where A: Into<Address> {
106        self.0.view_values_mut(address, size)
107    }
108
109    #[inline(always)]
110    fn set_values<A>(&mut self, address: A, bytes: &[Self::Value]) -> Result<(), Self::Error>
111    where A: Into<Address> {
112        self.0.set_values(address, bytes)
113    }
114}
115
116impl<T: StateValue> UniqueState<T> {
117    pub fn new(translator: &Translator) -> Self {
118        let space = translator.manager().unique_space();
119        let size = translator.unique_space_size();
120
121        log::debug!("unique space size: {} bytes", size);
122
123        Self(FlatState::new(space, size))
124    }
125}