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
use std::mem::MaybeUninit;
use std::ops::{Deref, DerefMut};
use std::rc::{Rc, Weak};

#[derive(Debug)]
pub struct SharedPtr<T:?Sized>{
    rc:MaybeUninit<Rc<T>>
}

impl<T>  SharedPtr<T>{
    #[inline]
    pub fn new(v:T)->SharedPtr<T>{
        SharedPtr{
            rc:MaybeUninit::new(Rc::new(v))
        }
    }
    #[inline]
    pub fn write(&mut self, v:T){
        if self.is_null(){
            self.rc.write(Rc::new(v));
        }
        else{
            unsafe{
                self.rc.assume_init_drop();
                self.rc.write(Rc::new(v));
            }
        }

    }
    #[inline]
    pub fn assume_init(self)->Option<Rc<T>>{
        if self.is_null() {
            None
        }else{
            unsafe {
                Some(std::mem::transmute::<Self,Rc<T>>(self))
            }
        }
    }

}

impl <T:?Sized> SharedPtr<T>{
    #[inline]
    pub fn zeroed()->SharedPtr<T>{
        SharedPtr{
            rc:MaybeUninit::zeroed()
        }
    }

    #[inline]
    pub fn weak(&self)->Option<Weak<T>>{
        if !self.is_null() {
            Some(Rc::downgrade(self))
        }else{
            None
        }
    }
    #[inline]
    pub fn set_null(&mut self){
        unsafe {
            if !self.is_null() {
                self.rc.assume_init_drop();
                self.rc=MaybeUninit::zeroed();
            }
        }
    }
    #[inline]
    pub fn is_null(&self)->bool {
        let p = self.rc.as_ptr() as *const usize;
        unsafe {
            p.read() == 0
        }
    }
    ///# Safety
    #[inline]
    pub unsafe fn get_mut_ref(&self) -> &mut T {
        &mut *(self.as_ref() as *const T as *mut T)
    }
}

impl<T:?Sized> Drop for SharedPtr<T>{
    #[inline]
    fn drop(&mut self) {
        if !self.is_null() {
            unsafe {
                self.rc.assume_init_drop()
            }
        }
    }
}

impl<T:?Sized> Deref for SharedPtr<T>{
    type Target = Rc<T>;
    #[inline]
    fn deref(&self) -> &Self::Target {
        unsafe {
            if self.is_null(){
                panic!("null shared deref")
            }

            self.rc.assume_init_ref()
        }
    }
}

impl <T:?Sized> DerefMut for SharedPtr<T>{
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe {
            if self.is_null(){
                panic!("null shared deref mut")
            }

            self.rc.assume_init_mut()
        }
    }
}

impl<T:?Sized> Clone for SharedPtr<T>{
    #[inline]
    fn clone(&self) -> Self {
        unsafe {
            if !self.is_null() {
                let rc = self.rc.assume_init_ref().clone();
                let may = MaybeUninit::<Rc<T>>::new(rc);
                SharedPtr {
                    rc: may
                }
            }else{
                SharedPtr {
                    rc:  MaybeUninit::<Rc<T>>::zeroed()
                }
            }
        }
    }
}

impl<T:?Sized> From<Rc<T>> for SharedPtr<T>{
    #[inline]
    fn from(r: Rc<T>) -> Self {
        unsafe {
            let ptr = &r as *const Rc<T> as *const SharedPtr<T>;
            std::mem::forget(r);
            ptr.read()
        }
    }
}

impl<T:?Sized> Default for  SharedPtr<T>{
    #[inline]
    fn default() -> Self {
        SharedPtr::<T>::zeroed()
    }
}