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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use std::{
    ops::{Deref, DerefMut},
    fmt::{self,Display},
    marker::PhantomData,
    ptr::NonNull,
};

use crate::pointer_trait::{CanTransmuteElement,GetPointerKind,PK_MutReference};

/// Equivalent to `&mut T`.
#[repr(transparent)]
#[derive(StableAbi)]
#[sabi(
    bound="T:'a",
)]
pub struct RMut<'a,T>{
    ref_: NonNull<T>,
    _marker:PhantomData<&'a mut T>,
}

impl<'a,T> Display for RMut<'a,T>
where
    T:Display
{
    fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
        Display::fmt(&**self,f)
    }
}

unsafe impl<'a,T> Sync for RMut<'a,T>
where &'a T:Sync
{}

unsafe impl<'a,T> Send for RMut<'a,T>
where &'a T:Send
{}


shared_impls! {
    mod=static_ref_impls
    new_type=RMut['a][T],
    original_type=AAAA,
}


impl<'a,T> RMut<'a,T>{
    /// Constructs this RMut from a raw pointer.
    ///
    /// # Safety
    ///
    /// You must ensure that the raw pointer is valid for the `'a` lifetime,
    /// and that this is the only active pointer to that value.
    ///
    #[inline]
    pub unsafe fn from_raw(ref_:*mut T)->Self
    where
        T:'a,
    {
        Self{
            ref_: NonNull::new_unchecked(ref_),
            _marker:PhantomData,
        }
    }

    /// Constructs this RMut from a mutable reference
    ///
    #[inline]
    pub fn new(ref_:&'a mut T)->Self{
        unsafe{ Self::from_raw(ref_) }
    }

    /// Reborrows this `RMut`, with a shorter lifetime.
    pub fn reborrow(&mut self) -> RMut<'_, T> {
        RMut{
            ref_: self.ref_,
            _marker: PhantomData,
        }
    }

    /// Gets access to the reference.
    ///
    /// Use this to get a `&'a T`,
    /// instead of a reference borrowing from the pointer.
    ///
    #[inline]
    pub fn get(self)->&'a T{
        unsafe{ &*(self.ref_.as_ptr() as *const T) }
    }

    /// Gets access to the mutable reference.
    ///
    /// Use this to get a `&'a mut T`,
    /// instead of a reference borrowing from the pointer.
    ///
    #[inline]
    pub fn get_mut(self)->&'a mut T{
        unsafe{ &mut *self.ref_.as_ptr() }
    }

    /// Gets access to the referenced value,as a raw pointer.
    ///
    #[inline]
    pub fn into_raw(self)->*mut T{
        self.ref_.as_ptr()
    }

    /// Accesses the referenced value as a casted raw pointer.
    #[inline]
    pub fn cast_into_raw<U>(self)->*mut U{
        self.ref_.as_ptr() as *mut U
    }
}

impl<'a,T> Deref for RMut<'a,T>{
    type Target=T;

    #[inline(always)]
    fn deref(&self)->&T{
        unsafe{ &*(self.ref_.as_ptr() as *const T) }
    }
}

impl<'a,T> DerefMut for RMut<'a,T>{
    #[inline(always)]
    fn deref_mut(&mut self)->&mut T{
        unsafe{ &mut *self.ref_.as_ptr() }
    }
}

unsafe impl<'a,T> GetPointerKind for RMut<'a,T>{
    type Kind=PK_MutReference;
}

unsafe impl<'a,T,U> CanTransmuteElement<U> for RMut<'a,T>
where
    U:'a,
{
    type TransmutedPtr= RMut<'a,U>;
}



#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn construction_test(){
        unsafe{
            let zero: *mut i32 = &mut 3;
            assert_eq!(*RMut::from_raw(zero), 3);
        }

        assert_eq!(*RMut::new(&mut 99), 99);
    }

    #[test]
    fn access(){
        let mut num = 5;
        let mut mutref= RMut::new(&mut num);
        
        assert_eq!(*mutref, 5);
        *mutref = 21;
        assert_eq!(*mutref, 21);

        assert_eq!(*mutref.reborrow().get(), 21);
        
        assert_eq!(*mutref.reborrow().get_mut(), 21);
        *mutref.reborrow().get_mut() = 34;

        unsafe{
            let raw = mutref.reborrow().into_raw();
            assert_eq!(*raw, 34);
            *raw = 55;
        }
        assert_eq!(num, 55);
    }

    #[test]
    fn transmutes(){
        let mut num = !1;
        let mutref= RMut::new(&mut num);

        unsafe{
            let ptr = mutref.cast_into_raw::<i32>();

            assert_eq!(*ptr, -2);
            *ptr = 55;
        }

        assert_eq!(num, 55);
    }
}