blob: 855dbdcfa15589d36da78bd7f92387ea3211094f (
plain) (
blame)
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
|
use std::marker::PhantomData;
// FIXME: remove this and use std::ptr::NonNull when Firefox requires Rust 1.25+
pub struct NonZeroPtr<T: 'static>(&'static T);
impl<T: 'static> NonZeroPtr<T> {
pub unsafe fn new_unchecked(ptr: *mut T) -> Self {
NonZeroPtr(&*ptr)
}
pub fn as_ptr(&self) -> *mut T {
self.0 as *const T as *mut T
}
}
pub struct Unique<T: 'static> {
ptr: NonZeroPtr<T>,
_marker: PhantomData<T>,
}
impl<T: 'static> Unique<T> {
pub unsafe fn new_unchecked(ptr: *mut T) -> Self {
Unique {
ptr: NonZeroPtr::new_unchecked(ptr),
_marker: PhantomData,
}
}
pub fn as_ptr(&self) -> *mut T {
self.ptr.as_ptr()
}
}
unsafe impl<T: Send + 'static> Send for Unique<T> {}
unsafe impl<T: Sync + 'static> Sync for Unique<T> {}
pub struct Shared<T: 'static> {
ptr: NonZeroPtr<T>,
_marker: PhantomData<T>,
// force it to be !Send/!Sync
_marker2: PhantomData<*const u8>,
}
impl<T: 'static> Shared<T> {
pub unsafe fn new_unchecked(ptr: *mut T) -> Self {
Shared {
ptr: NonZeroPtr::new_unchecked(ptr),
_marker: PhantomData,
_marker2: PhantomData,
}
}
pub unsafe fn as_mut(&self) -> &mut T {
&mut *self.ptr.as_ptr()
}
}
impl<'a, T> From<&'a mut T> for Shared<T> {
fn from(reference: &'a mut T) -> Self {
unsafe { Shared::new_unchecked(reference) }
}
}
|