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
// void_pointer.rs
// Aldaron's Memory Interface ( ami )
// Copyright 2017 (c) Aldaron's Tech
// Copyright 2017 (c) Jeron Lau
// Licensed under the MIT LICENSE

//! This module is for repurposing memory returned from or sent to FFI.

use core::ops::{ Add, Sub, Not, Shr, Shl, Deref, DerefMut, Index, IndexMut };
use core::marker::PhantomData;
use core::fmt::{ Display, Result, Formatter };
use core::ptr;

#[cfg(target_pointer_width = "32")]
type NativePtr = u32;

#[cfg(target_pointer_width = "64")]
type NativePtr = u64;

/// A type that represents a void* in C.
#[repr(C)]
#[derive(Copy, Clone, PartialEq)]
pub struct VoidPointer(NativePtr);

/// A type that represents a `T`* in C.
#[repr(C)]
#[derive(Copy, Clone, PartialEq)]
pub struct TypePointer<T> where T: ?Sized {
	native: NativePtr,
	marker: PhantomData<T>,
}

unsafe impl<T: Send> Send for TypePointer<T> {}
unsafe impl<T: Sync> Sync for TypePointer<T> {}

/// Equivalent of NULL in C.
pub const NULL : VoidPointer = VoidPointer(0);

impl VoidPointer {
	/// Return the pointer as an integer.
	#[inline(always)]
	pub fn as_int(&self) -> NativePtr {
		self.0
	}

	/// Cast to a TypePointer for indexing or dereferencing.
	#[inline(always)]
	pub fn as_type<T>(&self) -> TypePointer<T> {
		TypePointer {
			native: self.0,
			marker: PhantomData
		}
	}
	
	#[inline(always)]
	pub fn as_ptr(&mut self) -> &mut *mut ::Void {
		unsafe {
			::RawData::transmute(&mut self.0)
		}
	}
}

impl<T> TypePointer<T> {
	/// Inverse of `VoidPointer::as_type<T>()`
	#[inline(always)]
	pub fn as_void(&self) -> VoidPointer {
		VoidPointer(self.native)
	}

	#[inline(always)]
	pub unsafe fn copy_index(&self, index: usize) -> T {
		ptr::read(&self[index])
	}

	#[inline(always)]
	pub fn swap_index(&mut self, index: usize, value: T) -> T {
		let read = unsafe { self.copy_index(index) };

		self[index] = value;

		read
	}
}

/// A trait used for casting the void pointer to other pointer types.
pub trait PointerCast<T> {
	/// Cast a VoidPointer to a native pointer of any type.
	#[inline(always)]
	fn cast(&self) -> *mut T;
	/// Cast a native pointer of any type to a VoidPointer.
	#[inline(always)]
	fn from(pointer: *mut T) -> VoidPointer;
}

impl<T> PointerCast<T> for VoidPointer {
	#[inline(always)]
	fn from(pointer: *mut T) -> VoidPointer {
		unsafe {
			*(&pointer as *const *mut _ as *const _)
		}
	}

	#[inline(always)]
	fn cast(&self) -> *mut T {
		unsafe {
			*(&self as *const _ as *const *mut _)
		}
	}
}

impl<T> PointerCast<T> for TypePointer<T> {
	#[inline(always)]
	fn from(pointer: *mut T) -> VoidPointer {
		unsafe {
			*(&pointer as *const *mut _ as *const _)
		}
	}

	#[inline(always)]
	fn cast(&self) -> *mut T {
		unsafe {
			*(&self.native as *const _ as *const *mut _)
		}
	}
}

impl Add<usize> for VoidPointer {
	type Output = VoidPointer;

	#[inline(always)]
	fn add(self, other: usize) -> VoidPointer {
		VoidPointer(self.0 + (other as NativePtr))
	}
}

impl Sub<usize> for VoidPointer {
	type Output = VoidPointer;

	#[inline(always)]
	fn sub(self, other: usize) -> VoidPointer {
		VoidPointer(self.0 - (other as NativePtr))
	}
}

impl Shr<usize> for VoidPointer {
	type Output = VoidPointer;

	#[inline(always)]
	fn shr(self, rhs: usize) -> VoidPointer {
		VoidPointer(self.0 >> rhs)
	}
}

impl Shl<usize> for VoidPointer {
	type Output = VoidPointer;

	#[inline(always)]
	fn shl(self, rhs: usize) -> VoidPointer {
		VoidPointer(self.0 << rhs)
	}
}

impl Not for VoidPointer {
	type Output = VoidPointer;

	#[inline(always)]
	fn not(self) -> VoidPointer {
		VoidPointer(!self.0)
	}
}

impl<T> Deref for TypePointer<T> {
	type Target = T;

	#[inline(always)]
	fn deref(&self) -> &Self::Target {
		unsafe {
			&*self.cast()
		}
	}
}

impl<T> DerefMut for TypePointer<T> {
	#[inline(always)]
	fn deref_mut(&mut self) -> &mut Self::Target {
		unsafe {
			&mut *self.cast()
		}
	}
}

impl<T> Index<usize> for TypePointer<T> {
	type Output = T;

	#[inline(always)]
	fn index(&self, at: usize) -> &Self::Output {
		unsafe {
			&*self.cast().wrapping_offset(at as isize)
		}
	}
}

impl<T> IndexMut<usize> for TypePointer<T> {
	#[inline(always)]
	fn index_mut(&mut self, at: usize) -> &mut Self::Output {
		unsafe {
			&mut *self.cast().wrapping_offset(at as isize)
		}
	}
}

impl Deref for VoidPointer {
	type Target = u8;

	#[inline(always)]
	fn deref(&self) -> &u8 {
		unsafe {
			&*self.cast()
		}
	}
}

impl DerefMut for VoidPointer {
	#[inline(always)]
	fn deref_mut(&mut self) -> &mut u8 {
		unsafe {
			&mut *self.cast()
		}
	}
}

impl Index<usize> for VoidPointer {
	type Output = u8;

	#[inline(always)]
	fn index(&self, at: usize) -> &u8 {
		unsafe {
			&*self.cast().wrapping_offset(at as isize)
		}
	}
}

impl IndexMut<usize> for VoidPointer {
	#[inline(always)]
	fn index_mut(&mut self, at: usize) -> &mut u8 {
		unsafe {
			&mut *self.cast().wrapping_offset(at as isize)
		}
	}
}

impl Display for VoidPointer {
	#[inline(always)]
	fn fmt(&self, f: &mut Formatter) -> Result {
		write!(f, "{:x}", (*self).0)
	}
}