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
use std::cell::UnsafeCell;
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
use std::ops::{Deref, DerefMut};

/// # **Don't use it**  
/// This cell is not safe  
/// external mutable is just a placebo  
/// when using it, you must ensure safety by yourself  
#[derive(Debug, Default)]
pub struct ExtRefCell<T: ?Sized> {
    value: UnsafeCell<T>,
}
impl<T> ExtRefCell<T> {
    #[inline]
    pub const fn new(value: T) -> Self {
        Self {
            value: UnsafeCell::new(value),
        }
    }
    #[inline]
    pub fn into_inner(self) -> T {
        self.value.into_inner()
    }
    #[inline]
    pub fn get(&self) -> &T {
        unsafe { &*self.value.get() }
    }
    #[inline]
    pub unsafe fn get_mut(&self) -> &mut T {
        &mut *self.value.get()
    }
}
impl<T> Deref for ExtRefCell<T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.get()
    }
}
impl<T> DerefMut for ExtRefCell<T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { &mut *self.value.get() }
    }
}
impl<T: Clone> Clone for ExtRefCell<T> {
    #[inline]
    fn clone(&self) -> ExtRefCell<T> {
        ExtRefCell::new(self.deref().clone())
    }
}
impl<T> From<T> for ExtRefCell<T> {
    #[inline]
    fn from(t: T) -> ExtRefCell<T> {
        ExtRefCell::new(t)
    }
}
impl<T: PartialEq> PartialEq for ExtRefCell<T> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.get().eq(other.get())
    }
}
impl<T: Eq> Eq for ExtRefCell<T> {}
impl<T: PartialOrd> PartialOrd for ExtRefCell<T> {
    #[inline]
    fn partial_cmp(&self, other: &ExtRefCell<T>) -> Option<Ordering> {
        self.get().partial_cmp(other.get())
    }
    #[inline]
    fn lt(&self, other: &ExtRefCell<T>) -> bool {
        self.get().lt(other.get())
    }
    #[inline]
    fn le(&self, other: &ExtRefCell<T>) -> bool {
        self.get().le(other.get())
    }
    #[inline]
    fn gt(&self, other: &ExtRefCell<T>) -> bool {
        self.get().gt(other.get())
    }
    #[inline]
    fn ge(&self, other: &ExtRefCell<T>) -> bool {
        self.get().ge(other.get())
    }
}
impl<T: Ord> Ord for ExtRefCell<T> {
    #[inline]
    fn cmp(&self, other: &ExtRefCell<T>) -> Ordering {
        self.get().cmp(other.get())
    }
}

//\/////////////////////////////////////////////////////////////////////////////////////////////////

pub trait ExtRefCellExt {
    type Target;
    fn get_mut(&mut self) -> &mut Self::Target;
}
impl<T, D: Deref<Target = ExtRefCell<T>>> ExtRefCellExt for D {
    type Target = T;
    #[inline]
    fn get_mut(&mut self) -> &mut Self::Target {
        unsafe { &mut *self.value.get() }
    }
}