wolf_crypto/
ptr.rs

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
#![allow(dead_code)] // WIP

use core::ops;
use core::fmt;

#[repr(transparent)]
pub struct MutPtr<T> {
    inner: *mut T
}

impl<T> MutPtr<T> {
    #[cfg(debug_assertions)]
    #[inline]
    #[track_caller]
    pub fn new(inner: *mut T) -> Self {
        assert!(!inner.is_null(), "`MutPtr` initialized with null pointer");
        Self { inner }
    }

    #[cfg(not(debug_assertions))]
    #[inline]
    pub const fn new(inner: *mut T) -> Self {
        unsafe { core::mem::transmute(inner) }
    }

    #[cfg(debug_assertions)]
    #[inline]
    #[track_caller]
    pub fn assert_not_null(&self) {
        assert!(
            !self.inner.is_null(),
            "`MutPtr` was null under safe operation, this is not allowed"
        );
    }

    #[cfg(not(debug_assertions))]
    #[inline(always)]
    pub const fn assert_not_null(&self) {}

    #[inline]
    pub const unsafe fn null() -> Self {
        Self { inner: core::ptr::null_mut() }
    }

    #[inline]
    pub const unsafe fn get_unchecked(&self) -> *mut T {
        self.inner
    }

    #[cfg(debug_assertions)]
    #[inline]
    #[track_caller]
    pub fn get(&self) -> *mut T {
        self.assert_not_null();
        self.inner
    }

    #[cfg(not(debug_assertions))]
    #[inline]
    pub const fn get(&self) -> *mut T {
        self.inner
    }

    // Not unsafe as MutPtr copy is unsafe, we consume the type to prevent any further aliasing.
    #[inline]
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn into_const(self) -> ConstPtr<T> {
        ConstPtr::new(self.inner.cast_const())
    }

    #[cfg_attr(not(debug_assertions), inline(always))]
    #[cfg_attr(debug_assertions, track_caller)]
    pub unsafe fn copy(&self) -> Self {
        Self::new(self.inner)
    }
}

impl<T> ops::Deref for MutPtr<T> {
    type Target = T;

    #[cfg_attr(debug_assertions, track_caller)]
    #[inline]
    fn deref(&self) -> &Self::Target {
        unsafe { &*self.get() }
    }
}

impl<T> ops::DerefMut for MutPtr<T> {
    #[cfg_attr(debug_assertions, track_caller)]
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { &mut *self.get() }
    }
}

impl<T> fmt::Debug for MutPtr<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "MutPtr {{ is_null: {} }}", self.inner.is_null())
    }
}

#[repr(transparent)]
pub struct ConstPtr<T> {
    inner: *const T
}

impl<T> ConstPtr<T> {
    #[cfg(debug_assertions)]
    #[inline]
    #[track_caller]
    pub fn new(inner: *const T) -> Self {
        assert!(!inner.is_null(), "`ConstPtr` initialized with null pointer");
        Self { inner }
    }

    #[cfg(not(debug_assertions))]
    #[inline]
    pub const fn new(inner: *const T) -> Self {
        unsafe { core::mem::transmute(inner) }
    }

    #[cfg(debug_assertions)]
    #[inline]
    #[track_caller]
    pub fn assert_not_null(&self) {
        assert!(
            !self.inner.is_null(),
            "`ConstPtr` was null under safe operation, this is not allowed"
        );
    }

    #[cfg(not(debug_assertions))]
    #[inline(always)]
    pub const fn assert_not_null(&self) {}

    #[inline]
    pub const unsafe fn null() -> Self {
        Self { inner: core::ptr::null() }
    }

    #[inline]
    pub const unsafe fn get_unchecked(&self) -> *const T {
        self.inner
    }

    #[cfg(debug_assertions)]
    #[inline]
    #[track_caller]
    pub fn get(&self) -> *const T {
        self.assert_not_null();
        self.inner
    }

    #[cfg(not(debug_assertions))]
    #[inline]
    pub const fn get(&self) -> *const T {
        self.inner
    }

    // unsafe as copy for ConstPtr is not unsafe, mutable aliasing invariant.
    #[inline]
    #[cfg_attr(debug_assertions, track_caller)]
    pub unsafe fn into_mut(self) -> MutPtr<T> {
        MutPtr::new(self.inner.cast_mut())
    }

    #[cfg_attr(not(debug_assertions), inline(always))]
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn copy(&self) -> Self {
        Self::new(self.inner)
    }
}

impl<T> ops::Deref for ConstPtr<T> {
    type Target = T;

    #[cfg_attr(debug_assertions, track_caller)]
    #[inline]
    fn deref(&self) -> &Self::Target {
        unsafe { &*self.get() }
    }
}

impl<T> fmt::Debug for ConstPtr<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "ConstPtr {{ is_null: {} }}", self.inner.is_null())
    }
}