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
/*!
A wrapper type for a mutable pointer with a lifetime.
*/

use std_::marker::PhantomData;



/// A wrapper type that associates a mutable raw pointer with a lifetime.
///
/// # Motivation
///
/// This type was declared to pass a mutable-reference-like type to 
/// multiple methods to borrow multiple fields individually.
/// If those methods took in mutable references it would cause 
/// undefined behavior to borrow multiple fields mutably,
/// since each call borrows the entire data structure.
#[repr(transparent)]
#[derive(Debug)]
pub struct MutRef<'a,T:?Sized>{
    pub ptr:*mut T,
    _marker:PhantomData<&'a mut T>,
}

impl<'a,T:?Sized> Copy for MutRef<'a,T>{}

impl<'a,T:?Sized> Clone for MutRef<'a,T>{
    #[inline(always)]
    fn clone(&self)->Self{
        *self
    }
}

impl<'a,T:?Sized> MutRef<'a,T>{
    /// Constructs a MutRef from a mutable reference.
    #[inline(always)]
    pub fn new(mut_ref:&'a mut T)->Self{
        Self{
            ptr:mut_ref,
            _marker:PhantomData,
        }
    }

    /// Constructs a MutRef from a mutable pointer.
    #[inline(always)]
    pub fn from_ptr(ptr:*mut T)->Self{
        Self{
            ptr,
            _marker:PhantomData,
        }
    }

    /// An unchecked cast from `MutRef<'a,T>` to `MutRef<'a,U>`.
    #[inline(always)]
    pub fn cast<U>(self)->MutRef<'a,U>{
        MutRef{
            ptr:self.ptr as *mut U,
            _marker:PhantomData,
        }
    }
}

impl<'a,T:?Sized> From<&'a mut T> for MutRef<'a,T>{
    #[inline(always)]
    fn from(mutref:&'a mut T)->Self{
        Self::new(mutref)
    }
}