1#![unstable(feature = "kernel_ptr", issue = "none")]
2
3use core::fmt;
4use core::marker::PhantomData;
5use core::ptr::NonNull;
6
7#[cfg(feature = "full")]
8mod user_ptr;
9#[cfg(feature = "full")]
10pub use user_ptr::*;
11
12pub struct Unique<T: ?Sized> {
13 pointer: NonNull<T>,
14 _marker: PhantomData<T>,
15}
16
17unsafe impl<T: Send + ?Sized> Send for Unique<T> { }
22
23unsafe impl<T: Sync + ?Sized> Sync for Unique<T> { }
28
29impl<T: Sized> Unique<T> {
30 pub fn empty() -> Self {
35 unsafe {
36 Unique::new(NonNull::dangling().as_ptr())
37 }
38 }
39}
40
41impl<T: ?Sized> Unique<T> {
42 pub const unsafe fn new(ptr: *mut T) -> Unique<T> {
48 Unique { pointer: NonNull::new_unchecked(ptr), _marker: PhantomData }
49 }
50
51 pub fn as_ptr(self) -> *mut T {
53 self.pointer.as_ptr()
54 }
55
56 pub unsafe fn as_ref(&self) -> &T {
62 &*self.as_ptr()
63 }
64
65 pub unsafe fn as_mut(&mut self) -> &mut T {
71 &mut *self.as_ptr()
72 }
73}
74
75impl<T: ?Sized> Clone for Unique<T> {
76 fn clone(&self) -> Self {
77 *self
78 }
79}
80
81impl<T: ?Sized> Copy for Unique<T> {}
82
83impl<T: ?Sized> fmt::Pointer for Unique<T> {
84 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85 fmt::Pointer::fmt(&self.as_ptr(), f)
86 }
87}
88
89#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
90#[path = "x86_64.rs"]
91#[cfg(feature = "full")]
92#[unstable(feature = "kernel_internals", issue = "none")]
93pub mod impls;