forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptr.rs
281 lines (260 loc) · 10.5 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
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
use crate::cmp::Ordering;
use crate::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
use crate::mem;
use crate::ops::Range;
macro_rules! impl_iterator_for_ptr_range {
($mutability:ident /* const or mut */) => {
/// Iteration of a pointer range, as is common in code that interfaces
/// with C++ iterators.
///
/// # Safety
///
/// Traversing a pointer range is always safe, but **using the resulting
/// pointers** is not!
///
/// The pointers between the start and end of a range "remember" the
/// [allocated object] that they refer into. Pointers resulting from
/// pointer arithmetic must not be used to read or write to any other
/// allocated object.
///
/// As a consequence, pointers from a range traversal are only
/// dereferenceable if start and end of the original range both point
/// into the same allocated object. Dereferencing a pointer obtained via
/// iteration when this is not the case is Undefined Behavior.
///
/// [allocated object]: crate::ptr#allocated-object
///
/// # Alignment
///
/// All the pointers in the range are at offsets of multiples of
/// `size_of::<T>()` bytes from the range's start, so if the range's
/// start is misaligned, then the pointers in the range are misaligned
/// as well. The alignedness of the range's end is not relevant.
///
/// # Example
///
#[doc = example!($mutability)]
#[stable(feature = "iterate_ptr_range", since = "1.58.0")]
impl<T> Iterator for Range<*$mutability T> {
type Item = *$mutability T;
fn next(&mut self) -> Option<Self::Item> {
if self.is_empty() {
None
} else {
let curr = self.start;
let next = curr.wrapping_add(1);
self.start = if (curr..self.end).contains(&next) {
next
} else {
// Saturate to self.end if the wrapping_add wrapped or
// landed beyond end.
self.end
};
Some(curr)
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
if self.is_empty() {
(0, Some(0))
} else if mem::size_of::<T>() == 0 {
// T is zero sized so there are infinity of them in the
// nonempty range.
(usize::MAX, None)
} else {
// In between self.start and self.end there are some number
// of whole elements of type T, followed by possibly a
// remainder element if T's size doesn't evenly divide the
// byte distance between the endpoints. The remainder
// element still counts as being part of this range, since
// the pointer to it does lie between self.start and
// self.end.
let byte_offset = self.end as usize - self.start as usize;
let number_of_whole_t = byte_offset / mem::size_of::<T>();
let remainder_bytes = byte_offset % mem::size_of::<T>();
let maybe_remainder_t = (remainder_bytes > 0) as usize;
let hint = number_of_whole_t + maybe_remainder_t;
(hint, Some(hint))
}
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
let _ = self.advance_by(n);
self.next()
}
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
fn min(mut self) -> Option<Self::Item> {
self.next()
}
fn max(mut self) -> Option<Self::Item> {
self.next_back()
}
fn is_sorted(self) -> bool {
true
}
fn advance_by(&mut self, n: usize) -> Result<(), usize> {
match self.size_hint().1 {
None => {
// T is zero sized. Advancing does nothing.
Ok(())
}
Some(len) => match n.cmp(&len) {
Ordering::Less => {
// Advance past n number of whole elements.
self.start = self.start.wrapping_add(n);
Ok(())
}
Ordering::Equal => {
// Advance past every single element in the
// iterator, including perhaps the remainder
// element, leaving an empty iterator.
self.start = self.end;
Ok(())
}
Ordering::Greater => {
// Advance too far.
self.start = self.end;
Err(len)
}
}
}
}
#[doc(hidden)]
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
self.start.wrapping_add(idx)
}
}
#[stable(feature = "iterate_ptr_range", since = "1.58.0")]
impl<T> DoubleEndedIterator for Range<*$mutability T> {
fn next_back(&mut self) -> Option<Self::Item> {
match self.size_hint().1 {
None => {
// T is zero sized so the iterator never progresses past
// start, even if going backwards.
Some(self.start)
}
Some(0) => {
None
}
Some(len) => {
self.end = self.start.wrapping_add(len - 1);
Some(self.end)
}
}
}
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
match self.size_hint().1 {
None => {
// T is zero sized.
Some(self.start)
}
Some(len) => {
if n < len {
self.end = self.start.wrapping_add(len - n - 1);
Some(self.end)
} else {
self.end = self.start;
None
}
}
}
}
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
match self.size_hint().1 {
None => {
// T is zero sized. Advancing does nothing.
Ok(())
}
Some(len) => match n.cmp(&len) {
Ordering::Less => {
// Advance leaving `len - n` elements in the
// iterator. Careful to preserve the remainder
// element if told to advance by 0.
if n > 0 {
self.end = self.start.wrapping_add(len - n);
}
Ok(())
}
Ordering::Equal => {
// Advance past every single element in the
// iterator, leaving an empty iterator.
self.end = self.start;
Ok(())
}
Ordering::Greater => {
// Advance too far.
self.end = self.start;
Err(len)
}
}
}
}
}
#[stable(feature = "iterate_ptr_range", since = "1.58.0")]
impl<T> FusedIterator for Range<*$mutability T> {}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<T> TrustedLen for Range<*$mutability T> {}
#[doc(hidden)]
#[unstable(feature = "trusted_random_access", issue = "none")]
unsafe impl<T> TrustedRandomAccess for Range<*$mutability T> {}
#[doc(hidden)]
#[unstable(feature = "trusted_random_access", issue = "none")]
unsafe impl<T> TrustedRandomAccessNoCoerce for Range<*$mutability T> {
const MAY_HAVE_SIDE_EFFECT: bool = false;
}
};
}
macro_rules! example {
(const) => {
doc_comment_to_literal! {
/// ```
/// // Designed to be called from C++ or C.
/// #[no_mangle]
/// unsafe extern "C" fn demo(start: *const u16, end: *const u16) {
/// for ptr in start..end {
/// println!("{}", *ptr);
/// }
/// }
///
/// fn main() {
/// let slice = &[1u16, 2, 3];
/// let range = slice.as_ptr_range();
/// unsafe { demo(range.start, range.end); }
/// }
/// ```
}
};
(mut) => {
doc_comment_to_literal! {
/// ```
/// #![feature(vec_spare_capacity)]
///
/// use core::ptr;
///
/// // Designed to be called from C++ or C.
/// #[no_mangle]
/// unsafe extern "C" fn demo(start: *mut u16, end: *mut u16) {
/// for (i, ptr) in (start..end).enumerate() {
/// ptr::write(ptr, i as u16);
/// }
/// }
///
/// fn main() {
/// let mut vec: Vec<u16> = Vec::with_capacity(100);
/// let range = vec.spare_capacity_mut().as_mut_ptr_range();
/// unsafe {
/// demo(range.start.cast::<u16>(), range.end.cast::<u16>());
/// vec.set_len(100);
/// }
/// }
/// ```
}
};
}
macro_rules! doc_comment_to_literal {
($(#[doc = $example:literal])*) => {
concat!($($example, '\n'),*)
};
}
impl_iterator_for_ptr_range!(const);
impl_iterator_for_ptr_range!(mut);