scaly/containers/
reference.rs

1use containers::hashset::{Equal, Hash};
2use memory::Page;
3use std::ops::Deref;
4use std::ops::DerefMut;
5
6#[derive(Copy, Clone)]
7pub struct Ref<T> {
8    pub data: *mut T,
9}
10
11impl<T> Ref<T> {
12    pub fn new(_rp: *mut Page, object: T) -> Ref<T> {
13        Ref {
14            data: unsafe { (*_rp).allocate(object) },
15        }
16    }
17
18    pub fn get_page(&self) -> *mut Page {
19        Page::get(self.data as usize)
20    }
21}
22
23impl<T> From<*mut T> for Ref<T> {
24    fn from(p: *mut T) -> Self {
25        Ref { data: p }
26    }
27}
28
29impl<T> Deref for Ref<T> {
30    type Target = T;
31
32    fn deref(&self) -> &T {
33        unsafe { &*self.data }
34    }
35}
36
37impl<T> DerefMut for Ref<T> {
38    fn deref_mut(&mut self) -> &mut T {
39        unsafe { &mut *self.data }
40    }
41}
42
43impl<T: Equal<T>> Equal<T> for Ref<T> {
44    fn equals(&self, other: &T) -> bool {
45        unsafe { (*self.data).equals(other) }
46    }
47}
48
49impl<T: Hash<T>> Hash<T> for Ref<T> {
50    fn hash(&self) -> usize {
51        unsafe { (*self.data).hash() }
52    }
53}
54
55#[test]
56fn test_ref() {
57    let mut a: f64 = 2.0;
58    let b: *mut f64 = &mut a as *mut f64;
59    let c: Ref<f64> = Ref::from(b);
60    let _ = c.get_page();
61}