-
Notifications
You must be signed in to change notification settings - Fork 9
/
ptr.rs
199 lines (164 loc) · 4.83 KB
/
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
189
190
191
192
193
194
195
196
197
198
use crate::{Sized, Option, Copy};
use crate::mem::MaybeUninit;
use crate::Option::{Some, None};
use crate::clone::Clone;
use crate::convert::From;
use crate::marker::PhantomData;
#[lang = "drop_in_place"]
#[inline(always)]
pub unsafe fn drop_in_place<T: ?Sized>(x: *mut T){
::__lccc::__maybe_adl__::__evaluate_destructor_at(x)
}
#[lang = "const_ptr"]
impl<T: ?Sized> *const T{
pub const fn is_null(self)->bool{
(::__lccc::xir!("convert reinterpret *uint(8)":[self: *const T]:[yield: *const u8]))== unsafe { crate::mem::zeroed() }
}
}
#[lang = "mut_ptr"]
impl<T: ?Sized> *mut T{
pub const fn is_null(self)->bool{
(::__lccc::xir!("convert reinterpret *uint(8)":[self: *const T]:[yield: *const u8]))==unsafe{crate::mem::zeroed()}
}
}
impl<T> *mut [T]{
#[unstable(feature="slice_ptr_len",issue="71146")]
pub fn len(self) -> usize{
let raw: crate::slice::RawSlice<T> = crate::mem::transmute(self);
raw.len
}
#[unstable(feature="slice_ptr_get",issue="74265")]
pub fn as_mut_ptr(self) -> *mut T{
let raw: crate::slice::RawSlice<T> = crate::mem::transmute(self);
raw.ptr as *mut T
}
}
impl<T> *const [T]{
#[unstable(feature="slice_ptr_len",issue="71146")]
pub fn len(self) -> usize{
let raw: crate::slice::RawSlice<T> = crate::mem::transmute(self);
raw.len
}
#[unstable(feature="slice_ptr_get",issue="74265")]
pub fn as_ptr(self) -> *const T{
let raw: crate::slice::RawSlice<T> = crate::mem::transmute(self);
raw.ptr as *const T
}
}
pub fn slice_from_raw_parts(ptr: *const T,len: usize) -> *const [T]{
crate::mem::transmute(crate::slice::RawSlice<T>{
ptr: ptr as *mut T,
len
})
}
pub fn slice_from_raw_parts_mut(ptr: *mut T,len: usize) -> *mut [T]{
crate::mem::transmute(crate::slice::RawSlice<T>{
ptr,
len
})
}
#[repr(transparent)]
#[__lccc::transparent_as_unreified_field] // Fields with the type just become the transparent field type.
pub struct NonNull<T: ?Sized>{
#[__lccc::xlang_pointer_attributes(nonnull)]
ptr: *const T
}
impl<T: ?Sized> Clone for NonNull<T>{
fn clone(&self) -> Self {
NonNull{ptr: self.ptr}
}
}
impl<T: ?Sized> Copy for NonNull<T>{}
impl<T> NonNull<T>{
pub const fn dangling() -> Self{
NonNull{ptr: unsafe{crate::intrinsics::align_of::<T>()} as *const T}
}
}
impl<T: ?Sized> NonNull<T>{
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self{
NonNull{ptr}
}
pub fn new(ptr: *mut T) -> Option<Self>{
if ptr.is_null(){
Some(NonNull{ptr})
}else{
None
}
}
pub fn as_ptr(self) -> *mut T{
self.ptr as *mut _
}
pub unsafe fn as_ref(&self) -> &T{
&*self.ptr
}
pub unsafe fn as_mut(&mut self) -> &mut T{
&mut *(self.ptr as *mut _)
}
pub const fn cast<U>(self) -> NonNull<U>{
NonNull{ptr: self.ptr as *const U}
}
}
impl<T: ?Sized> From<&T> for NonNull<T>{
fn from(t: &T) -> Self {
NonNull{ptr: t}
}
}
#[repr(transparent)]
#[unstable(feature="lccc_unique_ptr")]
#[__lccc::transparent_as_unreified_field]
pub struct Unique<T: ?Sized>{
#[__lccc::xlang_pointer_attributes(unique,aligned)]
ptr: NonNull<T>,
data: PhantomData<T>
}
impl<T> Unique<T>{
pub const fn dangling() -> Self{
Self{ptr: NonNull::dangling(),data: PhantomData}
}
}
impl<T: ?Sized> Unique<T>{
pub const unsafe fn new_unchecked(ptr: *mut T) -> Unique<T>{
Self{ptr: NonNull::new_unchecked(ptr),data: PhantomData}
}
pub unsafe fn new_unchecked_nullable(ptr: *mut T) -> Option<Unique<T>>{
NonNull::new(ptr).map(|ptr|Self{ptr,data:PhantomData})
}
pub fn as_ptr(&self) -> *const T{
::__lccc::builtins::rust::kill_mutation(self.ptr.as_ptr() as *const T)
}
pub fn as_mut(&mut self) -> *mut T{
self.ptr.as_ptr()
}
pub fn as_non_null_mut(&mut self) -> NonNull<T>{
self.ptr
}
pub fn into_inner(self) -> NonNull<Self>{
self.ptr
}
pub fn cast<U>(self) -> Unique<U>{
Unique{
ptr: self.ptr.cast<U>(),
data: PhantomData
}
}
}
pub unsafe fn swap<T>(x: *mut T, y: *mut T){
::__lccc::xir!("swap":[ref *x, ref *y]);
}
pub use crate::intrinsics::read;
pub use crate::intrinsics::write;
pub use crate::intrinsics::copy_nonoverlapping;
pub use crate::intrinsics::copy;
pub use crate::intrinsics::swap_nonoverlapping;
#[allow_internal_unstable(lccc_intrinsic_crate)]
macro_rules! raw_const{
($e:expr) => {
unsafe{::__lccc::xir!("address_of":[ref e]:[yield: *const ::__lccc::decltype!({e})])}
}
}
#[allow_internal_unstable(lccc_intrinsic_crate)]
macro_rules! raw_mut{
($e:expr) => {
unsafe{::__lccc::xir!("address_of":[ref e]:[yield: *mut ::__lccc::decltype!({e})])}
}
}