1pub struct Ptr<T> {
2 ptr: *const T, }
4
5impl<T> Ptr<T> {
6 pub fn null() -> Self {
7 Self {
8 ptr: core::ptr::null_mut(),
9 }
10 }
11 pub fn new(ptr: *const T) -> Self {
12 Self { ptr }
13 }
14
15 pub fn is_null(&self) -> bool {
16 self.ptr.is_null()
17 }
18
19 pub fn get(&self) -> &T {
20 unsafe { &*self.ptr }
21 }
22
23 pub fn get_mut(&mut self) -> &mut T {
24 unsafe { &mut *(self.ptr as *mut T) }
25 }
26
27 pub fn into_mut<'a>(self) -> &'a mut T {
28 unsafe { &mut *(self.ptr as *mut T) }
29 }
30
31 pub fn into_ref<'a>(self) -> &'a T {
32 unsafe { &*self.ptr }
33 }
34
35 pub fn as_ptr(&self) -> *mut T {
36 self.ptr as *mut T
37 }
38}
39
40impl<T> Default for Ptr<T> {
41 fn default() -> Self {
42 Self {
43 ptr: core::ptr::null_mut(),
44 }
45 }
46}
47
48impl<T> Clone for Ptr<T> {
49 fn clone(&self) -> Self {
50 Self { ptr: self.ptr }
51 }
52}
53
54impl<T> Copy for Ptr<T> {}
55
56impl<T> core::ops::Deref for Ptr<T> {
57 type Target = T;
58
59 fn deref(&self) -> &Self::Target {
60 unsafe { &*self.ptr }
61 }
62}
63
64impl<T> core::ops::DerefMut for Ptr<T> {
65 fn deref_mut(&mut self) -> &mut Self::Target {
66 unsafe { &mut *(self.ptr as *mut T) }
67 }
68}
69impl<T> core::fmt::Debug for Ptr<T> {
70 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
71 write!(f, "Ptr({:?})", self.ptr)
72 }
73}
74
75impl<T> core::fmt::Pointer for Ptr<T> {
76 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
77 write!(f, "{:p}", self.ptr)
78 }
79}
80
81impl<T> core::fmt::Display for Ptr<T> {
82 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
83 write!(f, "{:p}", self.ptr)
84 }
85}
86
87impl<T> core::cmp::PartialEq for Ptr<T> {
88 fn eq(&self, other: &Self) -> bool {
89 self.ptr == other.ptr
90 }
91}
92
93impl<T> core::cmp::Eq for Ptr<T> {}
94
95impl<T> core::cmp::PartialOrd for Ptr<T> {
96 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
97 self.ptr.partial_cmp(&other.ptr)
98 }
99}
100
101impl<T> core::cmp::Ord for Ptr<T> {
102 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
103 self.ptr.cmp(&other.ptr)
104 }
105}
106
107impl<T> core::hash::Hash for Ptr<T> {
108 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
109 self.ptr.hash(state)
110 }
111}
112
113impl<T> core::ops::Add<usize> for Ptr<T> {
114 type Output = Self;
115
116 fn add(self, rhs: usize) -> Self::Output {
117 Self {
118 ptr: self.ptr.wrapping_add(rhs),
119 }
120 }
121}
122
123impl<T> core::ops::AddAssign<usize> for Ptr<T> {
124 fn add_assign(&mut self, rhs: usize) {
125 self.ptr = self.ptr.wrapping_add(rhs);
126 }
127}
128
129impl<T> core::ops::Sub<usize> for Ptr<T> {
130 type Output = Self;
131
132 fn sub(self, rhs: usize) -> Self::Output {
133 Self {
134 ptr: self.ptr.wrapping_sub(rhs),
135 }
136 }
137}
138
139impl<T> core::ops::SubAssign<usize> for Ptr<T> {
140 fn sub_assign(&mut self, rhs: usize) {
141 self.ptr = self.ptr.wrapping_sub(rhs);
142 }
143}