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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//! unmem crate contains some interesting functions. I tried to make them as safe as possible. However, remember that even not marked as unsafe they still can be dangerous.
//! All of these functions may cause UB!

#![no_std]
#![allow(unused_mut)]
#![allow(unused_unsafe)]
#![allow(non_camel_case_types)]
#![feature(box_syntax)]
#![feature(decl_macro)]
#![feature(const_mut_refs)]
#![feature(const_ptr_read)]

extern crate alloc;
use alloc::boxed::Box;

/// Changes value of immutable variable.
/// 
/// Example:
/// ```no_run
/// let foo: u8 = 19;
/// change(&foo, 86); // foo = 86
/// ```
pub fn change<T>(src: &T, to: T) {
    unsafe {
        *(src as *const T as *mut T) = to;
    }
}

/// Gives you a mutable reference from immutable.
pub const fn get_mut<T>(src: &T) -> &mut T {
    unsafe {
        &mut (*(src as *const T as *mut T))
    }
}

/// Read value of *const non-Copy T.
pub use core::ptr::read;

/// Similar to read but for *mut T.
pub const unsafe fn read_mut<T>(src: *mut T) -> T {
    read(src as *const T)
}

/// Analogue of memset.
pub fn set<T>(to: &mut T, val: u8, size: usize) -> &mut T {
    unsafe {
        core::ptr::write_bytes(to as *mut T, val, *&size);
        to
    }
}

/// Internal of transmute macro.
pub unsafe fn cast<FROM, TO>(src: FROM) -> TO {
    unsafe {
        read(&src as *const FROM as *const TO)
    }
}

/// Takes variable of one type and returns it as another type. EXTREMELY UNSAFE. Undefined Behavior or panic guaranteed.
/// 
/// Example:
/// ```no_run
/// let foo: u8 = 48;
/// let bar: char = transmute!(u8 => char, foo); // '0'
/// ```
pub macro transmute($from:ty => $to:ty, $src:expr) {
    unsafe {
        $crate::cast::<$from, $to>($src)
    }
}

/// Writes value to address.
pub unsafe fn write<T>(to: usize, val: T) {
    unsafe {
        let mut ptr: usize = to;
        *(ptr as *mut T) = val;
    }
}

/// Returns value from address.
pub const unsafe fn get<T>(from: usize) -> &'static T {
    unsafe {
        &*(from as *mut T)
    }
}

/// Drops given variable.
pub fn drop<T>(_src: T) {
    ()
}

/// Returns address from reference.
/// 
/// Example:
/// ```no_run
/// let foo: u8 = 16;
/// let bar: usize = get_address(&foo);
/// ```
pub fn get_address<T>(src: &T) -> usize {
    unsafe {
        src as *const T as usize
    }
}

/// Get value in memory related to src.
pub unsafe fn orient<T>(src: &T, val: isize) -> &T {
    unsafe {
        &*((get_address(get_mut(src)) as isize + val) as usize as *mut T)
    }
}

/// Similar to orient but returns &mut.
pub unsafe fn orient_mut<T>(src: &T, val: isize) -> &mut T {
    unsafe {
        &mut *((get_address(get_mut(src)) as isize + val) as usize as *mut T)
    }
}

/// Mean safe wrapper around raw pointers.
#[repr(transparent)]
#[derive(Clone, Copy)]
pub struct Ptr<T: ?Sized> {
    ptr: *mut T
}

impl<T> Ptr<T> {

    /// Not recommended, use Ptr::from_ref instead.
    #[inline]
    pub fn new(src: T) -> Self {
        Self::from_ref(&src)
    }

    /// Get value of T.
    pub const fn get(&self) -> T {
        unsafe {
            read_mut(self.ptr)
        }
    }

    /// Ptr<T> -> &T;
    #[inline]
    pub const fn as_ref(&self) -> &T {
        unsafe {
            &*self.ptr
        }
    }

    /// Ptr<T> -> &mut T;
    #[inline]
    pub const fn as_mut_ref(&self) -> &mut T {
        unsafe {
            &mut *self.ptr
        }
    }

    /// Replace inner's value to passed.
    pub fn set(&mut self, to: T) -> &mut Self {
        *self.as_mut_ref() = to;
        self
    }

    /// Ptr<T> -> Box<T>.
    #[inline]
    pub fn as_box(&self) -> Box<T> {
        box self.get()
    }

    /// Ptr<T> -> *const T;
    #[inline]
    pub const fn as_ptr(&self) -> *const T {
        self.as_ref() as *const T
    }

    /// Ptr<T> -> *mut T;
    #[inline]
    pub const fn as_mut_ptr(&self) -> *mut T {
        self.ptr
    }

    /// Ptr<T> -> usize;
    #[inline]
    pub fn as_adr(&self) -> usize {
        unsafe {
            (*self).ptr as usize
        }
    }

    /// Casting usize as ptr may cause UB.
    #[inline]
    pub const unsafe fn from_adr(src: usize) -> Self {
        Self {
            ptr: src as *mut T
        }
    }

    /// Box<T> -> Ptr<T>.
    #[inline]
    pub fn from_box(src: Box<T>) -> Self {
        Self {
            ptr: Box::leak(src) as *mut T
        }
    }

    /// &T -> Ptr<T>.
    #[inline]
    pub fn from_ref(src: &T) -> Self {
        Self {
            ptr: src as *const T as *mut T
        }
    }

    /// &mut T -> Ptr<T>.
    #[inline]
    pub const fn from_mut_ref(src: &mut T) -> Self {
        Self {
            ptr: src as *mut T
        }
    }

    /// Pointers are not guaranteed to be not null!
    #[inline]
    pub unsafe fn from_ptr(src: *const T) -> Self {
        Self {
            ptr: src as *mut T
        }
    }

    /// Pointers are not guaranteed to not be null!
    #[inline]
    pub const unsafe fn from_mut_ptr(src: *mut T) -> Self {
        Self {
            ptr: src
        }
    }

    /// May cause UB!
    pub unsafe fn orient(&self, shift: isize) -> Self {
        Self::from_mut_ptr(get_mut(orient(self.as_ref(), shift)) as *mut T)
    }

    /// Check if our ptr is null.
    #[inline]
    pub fn is_null(&self) -> bool {
        self.as_adr() == 0
    }

    /// Returns size of 'T' from Ptr<T>.
    #[inline]
    pub const fn size(&self) -> usize {
        core::mem::size_of::<T>()
    }

    /// NonNull<T> -> Ptr<T>.
    #[inline]
    pub const fn from_non_null(src: core::ptr::NonNull<T>) -> Self {
        Self {
            ptr: src.as_ptr()
        }
    }

    /// Ptr<T> -> NonNull<T>.
    #[inline]
    pub fn as_non_null(&self) -> core::ptr::NonNull<T> {
        unsafe {
            core::ptr::NonNull::new_unchecked(self.ptr)
        }
    }

    /// Example:
    /// ```no_run
    /// let foo: Ptr<u8> = Ptr::from_ref(&48);
    /// let bar: char = unsafe {foo.transmute::<char>()};
    /// ```
    pub unsafe fn transmute<TO>(&self) -> TO {
        transmute!(T => TO, self.get())
    }
}

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

    fn deref(&self) -> &Self::Target {
        self.as_ref()
    }
}

impl<T> core::ops::DerefMut for Ptr<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.as_mut_ref()
    }
}

impl<T> core::ops::Index<isize> for Ptr<T> {
    type Output = T;

    fn index(&self, index: isize) -> &Self::Output {
        unsafe {
            orient(&*self.ptr, index)
        }
    }
}

impl<T> core::ops::IndexMut<isize> for Ptr<T> {
    fn index_mut(&mut self, index: isize) -> &mut Self::Output {
        unsafe {
            orient_mut(&*self.ptr, index)
        }
    }
}

impl<T: core::fmt::Display> core::fmt::Display for Ptr<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", **self)
    }
}

impl<T: core::fmt::Debug> core::fmt::Debug for Ptr<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{:?}", **self)
    }
}

#[cfg(target_pointer_width = "32")]
pub type fsize = f32;
#[cfg(target_pointer_width = "64")]
pub type fsize = f64;