1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! References that point into `static` data

#![deny(missing_docs)]
#![deny(warnings)]
#![no_std]

use core::ops;

/// A reference that points into `static` data
pub unsafe trait StaticRef<T>: ops::Deref<Target = T> {}

unsafe impl<'a, T> StaticRef<T> for Ref<'a, T> {}
unsafe impl<'a, T> StaticRef<T> for RefMut<'a, T> {}
unsafe impl<T> StaticRef<T> for &'static T {}

/// `&'a T` that points into `static` data
pub struct Ref<'a, T>
where
    T: 'static,
{
    ref_: &'a T,
}

impl<'a, T> Clone for Ref<'a, T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<'a, T> Copy for Ref<'a, T> {}

impl<'a, T> Ref<'a, T> {
    /// Asserts that `ref_` points into `static` data
    pub unsafe fn new(ref_: &'a T) -> Self {
        Ref { ref_: ref_ }
    }
}

impl<'a, T> ops::Deref for Ref<'a, T> {
    type Target = T;

    fn deref(&self) -> &T {
        self.ref_
    }
}

/// `&'a mut T` that points into `static` data
pub struct RefMut<'a, T>
where
    T: 'static,
{
    ref_mut: &'a mut T,
}

impl<'a, T> RefMut<'a, T> {
    /// Asserts that `ref_mut` points into `static` data
    pub unsafe fn new(ref_mut: &'a mut T) -> Self {
        RefMut { ref_mut: ref_mut }
    }
}

impl<'a, T> ops::Deref for RefMut<'a, T> {
    type Target = T;

    fn deref(&self) -> &T {
        self.ref_mut
    }
}

impl<'a, T> ops::DerefMut for RefMut<'a, T> {
    fn deref_mut(&mut self) -> &mut T {
        self.ref_mut
    }
}