]> git.lizzy.rs Git - rust.git/blob - src/libcore/ptr.rs
Documentation and naming improvements
[rust.git] / src / libcore / ptr.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // FIXME: talk about offset, copy_memory, copy_nonoverlapping_memory
12
13 //! Raw, unsafe pointers, `*const T`, and `*mut T`.
14 //!
15 //! *[See also the pointer primitive types](../../std/primitive.pointer.html).*
16
17 #![stable(feature = "rust1", since = "1.0.0")]
18
19 use convert::From;
20 use intrinsics;
21 use ops::CoerceUnsized;
22 use fmt;
23 use hash;
24 use marker::{PhantomData, Unsize};
25 use mem;
26 use nonzero::NonZero;
27
28 use cmp::Ordering::{self, Less, Equal, Greater};
29
30 #[stable(feature = "rust1", since = "1.0.0")]
31 pub use intrinsics::copy_nonoverlapping;
32
33 #[stable(feature = "rust1", since = "1.0.0")]
34 pub use intrinsics::copy;
35
36 #[stable(feature = "rust1", since = "1.0.0")]
37 pub use intrinsics::write_bytes;
38
39 /// Executes the destructor (if any) of the pointed-to value.
40 ///
41 /// This has two use cases:
42 ///
43 /// * It is *required* to use `drop_in_place` to drop unsized types like
44 ///   trait objects, because they can't be read out onto the stack and
45 ///   dropped normally.
46 ///
47 /// * It is friendlier to the optimizer to do this over `ptr::read` when
48 ///   dropping manually allocated memory (e.g. when writing Box/Rc/Vec),
49 ///   as the compiler doesn't need to prove that it's sound to elide the
50 ///   copy.
51 ///
52 /// # Safety
53 ///
54 /// This has all the same safety problems as `ptr::read` with respect to
55 /// invalid pointers, types, and double drops.
56 #[stable(feature = "drop_in_place", since = "1.8.0")]
57 #[lang = "drop_in_place"]
58 #[allow(unconditional_recursion)]
59 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
60     // Code here does not matter - this is replaced by the
61     // real drop glue by the compiler.
62     drop_in_place(to_drop);
63 }
64
65 /// Creates a null raw pointer.
66 ///
67 /// # Examples
68 ///
69 /// ```
70 /// use std::ptr;
71 ///
72 /// let p: *const i32 = ptr::null();
73 /// assert!(p.is_null());
74 /// ```
75 #[inline]
76 #[stable(feature = "rust1", since = "1.0.0")]
77 pub const fn null<T>() -> *const T { 0 as *const T }
78
79 /// Creates a null mutable raw pointer.
80 ///
81 /// # Examples
82 ///
83 /// ```
84 /// use std::ptr;
85 ///
86 /// let p: *mut i32 = ptr::null_mut();
87 /// assert!(p.is_null());
88 /// ```
89 #[inline]
90 #[stable(feature = "rust1", since = "1.0.0")]
91 pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
92
93 /// Swaps the values at two mutable locations of the same type, without
94 /// deinitializing either.
95 ///
96 /// The values pointed at by `x` and `y` may overlap, unlike `mem::swap` which
97 /// is otherwise equivalent. If the values do overlap, then the overlapping
98 /// region of memory from `x` will be used. This is demonstrated in the
99 /// examples section below.
100 ///
101 /// # Safety
102 ///
103 /// This function copies the memory through the raw pointers passed to it
104 /// as arguments.
105 ///
106 /// Ensure that these pointers are valid before calling `swap`.
107 ///
108 /// # Examples
109 ///
110 /// Swapping two non-overlapping regions:
111 ///
112 /// ```
113 /// use std::ptr;
114 ///
115 /// let mut array = [0, 1, 2, 3];
116 ///
117 /// let x = array[0..].as_mut_ptr() as *mut [u32; 2];
118 /// let y = array[2..].as_mut_ptr() as *mut [u32; 2];
119 ///
120 /// unsafe {
121 ///     ptr::swap(x, y);
122 ///     assert_eq!([2, 3, 0, 1], array);
123 /// }
124 /// ```
125 ///
126 /// Swapping two overlapping regions:
127 ///
128 /// ```
129 /// use std::ptr;
130 ///
131 /// let mut array = [0, 1, 2, 3];
132 ///
133 /// let x = array[0..].as_mut_ptr() as *mut [u32; 3];
134 /// let y = array[1..].as_mut_ptr() as *mut [u32; 3];
135 ///
136 /// unsafe {
137 ///     ptr::swap(x, y);
138 ///     assert_eq!([1, 0, 1, 2], array);
139 /// }
140 /// ```
141 #[inline]
142 #[stable(feature = "rust1", since = "1.0.0")]
143 pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
144     // Give ourselves some scratch space to work with
145     let mut tmp: T = mem::uninitialized();
146
147     // Perform the swap
148     copy_nonoverlapping(x, &mut tmp, 1);
149     copy(y, x, 1); // `x` and `y` may overlap
150     copy_nonoverlapping(&tmp, y, 1);
151
152     // y and t now point to the same thing, but we need to completely forget `tmp`
153     // because it's no longer relevant.
154     mem::forget(tmp);
155 }
156
157 /// Swaps a sequence of values at two mutable locations of the same type.
158 ///
159 /// # Safety
160 ///
161 /// The two arguments must each point to the beginning of `count` locations
162 /// of valid memory, and the two memory ranges must not overlap.
163 ///
164 /// # Examples
165 ///
166 /// Basic usage:
167 ///
168 /// ```
169 /// #![feature(swap_nonoverlapping)]
170 ///
171 /// use std::ptr;
172 ///
173 /// let mut x = [1, 2, 3, 4];
174 /// let mut y = [7, 8, 9];
175 ///
176 /// unsafe {
177 ///     ptr::swap_nonoverlapping(x.as_mut_ptr(), y.as_mut_ptr(), 2);
178 /// }
179 ///
180 /// assert_eq!(x, [7, 8, 3, 4]);
181 /// assert_eq!(y, [1, 2, 9]);
182 /// ```
183 #[inline]
184 #[unstable(feature = "swap_nonoverlapping", issue = "42818")]
185 pub unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
186     let x = x as *mut u8;
187     let y = y as *mut u8;
188     let len = mem::size_of::<T>() * count;
189     swap_nonoverlapping_bytes(x, y, len)
190 }
191
192 #[inline]
193 unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
194     // The approach here is to utilize simd to swap x & y efficiently. Testing reveals
195     // that swapping either 32 bytes or 64 bytes at a time is most efficient for intel
196     // Haswell E processors. LLVM is more able to optimize if we give a struct a
197     // #[repr(simd)], even if we don't actually use this struct directly.
198     //
199     // FIXME repr(simd) broken on emscripten and redox
200     // It's also broken on big-endian powerpc64 and s390x.  #42778
201     #[cfg_attr(not(any(target_os = "emscripten", target_os = "redox",
202                        target_endian = "big")),
203                repr(simd))]
204     struct Block(u64, u64, u64, u64);
205     struct UnalignedBlock(u64, u64, u64, u64);
206
207     let block_size = mem::size_of::<Block>();
208
209     // Loop through x & y, copying them `Block` at a time
210     // The optimizer should unroll the loop fully for most types
211     // N.B. We can't use a for loop as the `range` impl calls `mem::swap` recursively
212     let mut i = 0;
213     while i + block_size <= len {
214         // Create some uninitialized memory as scratch space
215         // Declaring `t` here avoids aligning the stack when this loop is unused
216         let mut t: Block = mem::uninitialized();
217         let t = &mut t as *mut _ as *mut u8;
218         let x = x.offset(i as isize);
219         let y = y.offset(i as isize);
220
221         // Swap a block of bytes of x & y, using t as a temporary buffer
222         // This should be optimized into efficient SIMD operations where available
223         copy_nonoverlapping(x, t, block_size);
224         copy_nonoverlapping(y, x, block_size);
225         copy_nonoverlapping(t, y, block_size);
226         i += block_size;
227     }
228
229     if i < len {
230         // Swap any remaining bytes
231         let mut t: UnalignedBlock = mem::uninitialized();
232         let rem = len - i;
233
234         let t = &mut t as *mut _ as *mut u8;
235         let x = x.offset(i as isize);
236         let y = y.offset(i as isize);
237
238         copy_nonoverlapping(x, t, rem);
239         copy_nonoverlapping(y, x, rem);
240         copy_nonoverlapping(t, y, rem);
241     }
242 }
243
244 /// Replaces the value at `dest` with `src`, returning the old
245 /// value, without dropping either.
246 ///
247 /// # Safety
248 ///
249 /// This is only unsafe because it accepts a raw pointer.
250 /// Otherwise, this operation is identical to `mem::replace`.
251 #[inline]
252 #[stable(feature = "rust1", since = "1.0.0")]
253 pub unsafe fn replace<T>(dest: *mut T, mut src: T) -> T {
254     mem::swap(&mut *dest, &mut src); // cannot overlap
255     src
256 }
257
258 /// Reads the value from `src` without moving it. This leaves the
259 /// memory in `src` unchanged.
260 ///
261 /// # Safety
262 ///
263 /// Beyond accepting a raw pointer, this is unsafe because it semantically
264 /// moves the value out of `src` without preventing further usage of `src`.
265 /// If `T` is not `Copy`, then care must be taken to ensure that the value at
266 /// `src` is not used before the data is overwritten again (e.g. with `write`,
267 /// `write_bytes`, or `copy`). Note that `*src = foo` counts as a use
268 /// because it will attempt to drop the value previously at `*src`.
269 ///
270 /// The pointer must be aligned; use `read_unaligned` if that is not the case.
271 ///
272 /// # Examples
273 ///
274 /// Basic usage:
275 ///
276 /// ```
277 /// let x = 12;
278 /// let y = &x as *const i32;
279 ///
280 /// unsafe {
281 ///     assert_eq!(std::ptr::read(y), 12);
282 /// }
283 /// ```
284 #[inline]
285 #[stable(feature = "rust1", since = "1.0.0")]
286 pub unsafe fn read<T>(src: *const T) -> T {
287     let mut tmp: T = mem::uninitialized();
288     copy_nonoverlapping(src, &mut tmp, 1);
289     tmp
290 }
291
292 /// Reads the value from `src` without moving it. This leaves the
293 /// memory in `src` unchanged.
294 ///
295 /// Unlike `read`, the pointer may be unaligned.
296 ///
297 /// # Safety
298 ///
299 /// Beyond accepting a raw pointer, this is unsafe because it semantically
300 /// moves the value out of `src` without preventing further usage of `src`.
301 /// If `T` is not `Copy`, then care must be taken to ensure that the value at
302 /// `src` is not used before the data is overwritten again (e.g. with `write`,
303 /// `write_bytes`, or `copy`). Note that `*src = foo` counts as a use
304 /// because it will attempt to drop the value previously at `*src`.
305 ///
306 /// # Examples
307 ///
308 /// Basic usage:
309 ///
310 /// ```
311 /// let x = 12;
312 /// let y = &x as *const i32;
313 ///
314 /// unsafe {
315 ///     assert_eq!(std::ptr::read_unaligned(y), 12);
316 /// }
317 /// ```
318 #[inline]
319 #[stable(feature = "ptr_unaligned", since = "1.17.0")]
320 pub unsafe fn read_unaligned<T>(src: *const T) -> T {
321     let mut tmp: T = mem::uninitialized();
322     copy_nonoverlapping(src as *const u8,
323                         &mut tmp as *mut T as *mut u8,
324                         mem::size_of::<T>());
325     tmp
326 }
327
328 /// Overwrites a memory location with the given value without reading or
329 /// dropping the old value.
330 ///
331 /// # Safety
332 ///
333 /// This operation is marked unsafe because it accepts a raw pointer.
334 ///
335 /// It does not drop the contents of `dst`. This is safe, but it could leak
336 /// allocations or resources, so care must be taken not to overwrite an object
337 /// that should be dropped.
338 ///
339 /// Additionally, it does not drop `src`. Semantically, `src` is moved into the
340 /// location pointed to by `dst`.
341 ///
342 /// This is appropriate for initializing uninitialized memory, or overwriting
343 /// memory that has previously been `read` from.
344 ///
345 /// The pointer must be aligned; use `write_unaligned` if that is not the case.
346 ///
347 /// # Examples
348 ///
349 /// Basic usage:
350 ///
351 /// ```
352 /// let mut x = 0;
353 /// let y = &mut x as *mut i32;
354 /// let z = 12;
355 ///
356 /// unsafe {
357 ///     std::ptr::write(y, z);
358 ///     assert_eq!(std::ptr::read(y), 12);
359 /// }
360 /// ```
361 #[inline]
362 #[stable(feature = "rust1", since = "1.0.0")]
363 pub unsafe fn write<T>(dst: *mut T, src: T) {
364     intrinsics::move_val_init(&mut *dst, src)
365 }
366
367 /// Overwrites a memory location with the given value without reading or
368 /// dropping the old value.
369 ///
370 /// Unlike `write`, the pointer may be unaligned.
371 ///
372 /// # Safety
373 ///
374 /// This operation is marked unsafe because it accepts a raw pointer.
375 ///
376 /// It does not drop the contents of `dst`. This is safe, but it could leak
377 /// allocations or resources, so care must be taken not to overwrite an object
378 /// that should be dropped.
379 ///
380 /// Additionally, it does not drop `src`. Semantically, `src` is moved into the
381 /// location pointed to by `dst`.
382 ///
383 /// This is appropriate for initializing uninitialized memory, or overwriting
384 /// memory that has previously been `read` from.
385 ///
386 /// # Examples
387 ///
388 /// Basic usage:
389 ///
390 /// ```
391 /// let mut x = 0;
392 /// let y = &mut x as *mut i32;
393 /// let z = 12;
394 ///
395 /// unsafe {
396 ///     std::ptr::write_unaligned(y, z);
397 ///     assert_eq!(std::ptr::read_unaligned(y), 12);
398 /// }
399 /// ```
400 #[inline]
401 #[stable(feature = "ptr_unaligned", since = "1.17.0")]
402 pub unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
403     copy_nonoverlapping(&src as *const T as *const u8,
404                         dst as *mut u8,
405                         mem::size_of::<T>());
406     mem::forget(src);
407 }
408
409 /// Performs a volatile read of the value from `src` without moving it. This
410 /// leaves the memory in `src` unchanged.
411 ///
412 /// Volatile operations are intended to act on I/O memory, and are guaranteed
413 /// to not be elided or reordered by the compiler across other volatile
414 /// operations.
415 ///
416 /// # Notes
417 ///
418 /// Rust does not currently have a rigorously and formally defined memory model,
419 /// so the precise semantics of what "volatile" means here is subject to change
420 /// over time. That being said, the semantics will almost always end up pretty
421 /// similar to [C11's definition of volatile][c11].
422 ///
423 /// The compiler shouldn't change the relative order or number of volatile
424 /// memory operations. However, volatile memory operations on zero-sized types
425 /// (e.g. if a zero-sized type is passed to `read_volatile`) are no-ops
426 /// and may be ignored.
427 ///
428 /// [c11]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
429 ///
430 /// # Safety
431 ///
432 /// Beyond accepting a raw pointer, this is unsafe because it semantically
433 /// moves the value out of `src` without preventing further usage of `src`.
434 /// If `T` is not `Copy`, then care must be taken to ensure that the value at
435 /// `src` is not used before the data is overwritten again (e.g. with `write`,
436 /// `write_bytes`, or `copy`). Note that `*src = foo` counts as a use
437 /// because it will attempt to drop the value previously at `*src`.
438 ///
439 /// # Examples
440 ///
441 /// Basic usage:
442 ///
443 /// ```
444 /// let x = 12;
445 /// let y = &x as *const i32;
446 ///
447 /// unsafe {
448 ///     assert_eq!(std::ptr::read_volatile(y), 12);
449 /// }
450 /// ```
451 #[inline]
452 #[stable(feature = "volatile", since = "1.9.0")]
453 pub unsafe fn read_volatile<T>(src: *const T) -> T {
454     intrinsics::volatile_load(src)
455 }
456
457 /// Performs a volatile write of a memory location with the given value without
458 /// reading or dropping the old value.
459 ///
460 /// Volatile operations are intended to act on I/O memory, and are guaranteed
461 /// to not be elided or reordered by the compiler across other volatile
462 /// operations.
463 ///
464 /// # Notes
465 ///
466 /// Rust does not currently have a rigorously and formally defined memory model,
467 /// so the precise semantics of what "volatile" means here is subject to change
468 /// over time. That being said, the semantics will almost always end up pretty
469 /// similar to [C11's definition of volatile][c11].
470 ///
471 /// The compiler shouldn't change the relative order or number of volatile
472 /// memory operations. However, volatile memory operations on zero-sized types
473 /// (e.g. if a zero-sized type is passed to `write_volatile`) are no-ops
474 /// and may be ignored.
475 ///
476 /// [c11]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
477 ///
478 /// # Safety
479 ///
480 /// This operation is marked unsafe because it accepts a raw pointer.
481 ///
482 /// It does not drop the contents of `dst`. This is safe, but it could leak
483 /// allocations or resources, so care must be taken not to overwrite an object
484 /// that should be dropped.
485 ///
486 /// This is appropriate for initializing uninitialized memory, or overwriting
487 /// memory that has previously been `read` from.
488 ///
489 /// # Examples
490 ///
491 /// Basic usage:
492 ///
493 /// ```
494 /// let mut x = 0;
495 /// let y = &mut x as *mut i32;
496 /// let z = 12;
497 ///
498 /// unsafe {
499 ///     std::ptr::write_volatile(y, z);
500 ///     assert_eq!(std::ptr::read_volatile(y), 12);
501 /// }
502 /// ```
503 #[inline]
504 #[stable(feature = "volatile", since = "1.9.0")]
505 pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
506     intrinsics::volatile_store(dst, src);
507 }
508
509 #[lang = "const_ptr"]
510 impl<T: ?Sized> *const T {
511     /// Returns `true` if the pointer is null.
512     ///
513     /// Note that unsized types have many possible null pointers, as only the
514     /// raw data pointer is considered, not their length, vtable, etc.
515     /// Therefore, two pointers that are null may still not compare equal to
516     /// each other.
517     ///
518     /// # Examples
519     ///
520     /// Basic usage:
521     ///
522     /// ```
523     /// let s: &str = "Follow the rabbit";
524     /// let ptr: *const u8 = s.as_ptr();
525     /// assert!(!ptr.is_null());
526     /// ```
527     #[stable(feature = "rust1", since = "1.0.0")]
528     #[inline]
529     pub fn is_null(self) -> bool {
530         // Compare via a cast to a thin pointer, so fat pointers are only
531         // considering their "data" part for null-ness.
532         (self as *const u8) == null()
533     }
534
535     /// Returns `None` if the pointer is null, or else returns a reference to
536     /// the value wrapped in `Some`.
537     ///
538     /// # Safety
539     ///
540     /// While this method and its mutable counterpart are useful for
541     /// null-safety, it is important to note that this is still an unsafe
542     /// operation because the returned value could be pointing to invalid
543     /// memory.
544     ///
545     /// Additionally, the lifetime `'a` returned is arbitrarily chosen and does
546     /// not necessarily reflect the actual lifetime of the data.
547     ///
548     /// # Examples
549     ///
550     /// Basic usage:
551     ///
552     /// ```
553     /// let ptr: *const u8 = &10u8 as *const u8;
554     ///
555     /// unsafe {
556     ///     if let Some(val_back) = ptr.as_ref() {
557     ///         println!("We got back the value: {}!", val_back);
558     ///     }
559     /// }
560     /// ```
561     #[stable(feature = "ptr_as_ref", since = "1.9.0")]
562     #[inline]
563     pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
564         if self.is_null() {
565             None
566         } else {
567             Some(&*self)
568         }
569     }
570
571     /// Calculates the offset from a pointer.
572     ///
573     /// `count` is in units of T; e.g. a `count` of 3 represents a pointer
574     /// offset of `3 * size_of::<T>()` bytes.
575     ///
576     /// # Safety
577     ///
578     /// If any of the following conditions are violated, the result is Undefined
579     /// Behavior:
580     ///
581     /// * Both the starting and resulting pointer must be either in bounds or one
582     ///   byte past the end of an allocated object.
583     ///
584     /// * The computed offset, **in bytes**, cannot overflow an `isize`.
585     ///
586     /// * The offset being in bounds cannot rely on "wrapping around" the address
587     ///   space. That is, the infinite-precision sum, **in bytes** must fit in a usize.
588     ///
589     /// The compiler and standard library generally tries to ensure allocations
590     /// never reach a size where an offset is a concern. For instance, `Vec`
591     /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
592     /// `vec.as_ptr().offset(vec.len() as isize)` is always safe.
593     ///
594     /// Most platforms fundamentally can't even construct such an allocation.
595     /// For instance, no known 64-bit platform can ever serve a request
596     /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
597     /// However, some 32-bit and 16-bit platforms may successfully serve a request for
598     /// more than `isize::MAX` bytes with things like Physical Address
599     /// Extension. As such, memory acquired directly from allocators or memory
600     /// mapped files *may* be too large to handle with this function.
601     ///
602     /// Consider using `wrapping_offset` instead if these constraints are
603     /// difficult to satisfy. The only advantage of this method is that it
604     /// enables more aggressive compiler optimizations.
605     ///
606     /// # Examples
607     ///
608     /// Basic usage:
609     ///
610     /// ```
611     /// let s: &str = "123";
612     /// let ptr: *const u8 = s.as_ptr();
613     ///
614     /// unsafe {
615     ///     println!("{}", *ptr.offset(1) as char);
616     ///     println!("{}", *ptr.offset(2) as char);
617     /// }
618     /// ```
619     #[stable(feature = "rust1", since = "1.0.0")]
620     #[inline]
621     pub unsafe fn offset(self, count: isize) -> *const T where T: Sized {
622         intrinsics::offset(self, count)
623     }
624
625     /// Calculates the offset from a pointer using wrapping arithmetic.
626     ///
627     /// `count` is in units of T; e.g. a `count` of 3 represents a pointer
628     /// offset of `3 * size_of::<T>()` bytes.
629     ///
630     /// # Safety
631     ///
632     /// The resulting pointer does not need to be in bounds, but it is
633     /// potentially hazardous to dereference (which requires `unsafe`).
634     ///
635     /// Always use `.offset(count)` instead when possible, because `offset`
636     /// allows the compiler to optimize better.
637     ///
638     /// # Examples
639     ///
640     /// Basic usage:
641     ///
642     /// ```
643     /// // Iterate using a raw pointer in increments of two elements
644     /// let data = [1u8, 2, 3, 4, 5];
645     /// let mut ptr: *const u8 = data.as_ptr();
646     /// let step = 2;
647     /// let end_rounded_up = ptr.wrapping_offset(6);
648     ///
649     /// // This loop prints "1, 3, 5, "
650     /// while ptr != end_rounded_up {
651     ///     unsafe {
652     ///         print!("{}, ", *ptr);
653     ///     }
654     ///     ptr = ptr.wrapping_offset(step);
655     /// }
656     /// ```
657     #[stable(feature = "ptr_wrapping_offset", since = "1.16.0")]
658     #[inline]
659     pub fn wrapping_offset(self, count: isize) -> *const T where T: Sized {
660         unsafe {
661             intrinsics::arith_offset(self, count)
662         }
663     }
664
665     /// Calculates the distance between two pointers. The returned value is in
666     /// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
667     ///
668     /// If the address different between the two pointers ia not a multiple of
669     /// `mem::size_of::<T>()` then the result of the division is rounded towards
670     /// zero.
671     ///
672     /// This function returns `None` if `T` is a zero-sized type.
673     ///
674     /// # Examples
675     ///
676     /// Basic usage:
677     ///
678     /// ```
679     /// #![feature(offset_to)]
680     ///
681     /// fn main() {
682     ///     let a = [0; 5];
683     ///     let ptr1: *const i32 = &a[1];
684     ///     let ptr2: *const i32 = &a[3];
685     ///     assert_eq!(ptr1.offset_to(ptr2), Some(2));
686     ///     assert_eq!(ptr2.offset_to(ptr1), Some(-2));
687     ///     assert_eq!(unsafe { ptr1.offset(2) }, ptr2);
688     ///     assert_eq!(unsafe { ptr2.offset(-2) }, ptr1);
689     /// }
690     /// ```
691     #[unstable(feature = "offset_to", issue = "41079")]
692     #[inline]
693     pub fn offset_to(self, other: *const T) -> Option<isize> where T: Sized {
694         let size = mem::size_of::<T>();
695         if size == 0 {
696             None
697         } else {
698             let diff = (other as isize).wrapping_sub(self as isize);
699             Some(diff / size as isize)
700         }
701     }
702
703     /// Calculates the distance between two pointers. The returned value is in
704     /// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
705     ///
706     /// This function is the inverse of [`offset`].
707     ///
708     /// [`offset`]: #method.offset
709     /// [`wrapping_offset_from`]: #method.wrapping_offset_from
710     ///
711     /// # Safety
712     ///
713     /// If any of the following conditions are violated, the result is Undefined
714     /// Behavior:
715     ///
716     /// * Both the starting and other pointer must be either in bounds or one
717     ///   byte past the end of the same allocated object.
718     ///
719     /// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
720     ///
721     /// * The distance between the pointers, in bytes, must be an exact multiple
722     ///   of the size of `T`.
723     ///
724     /// * The distance being in bounds cannot rely on "wrapping around" the address space.
725     ///
726     /// The compiler and standard library generally try to ensure allocations
727     /// never reach a size where an offset is a concern. For instance, `Vec`
728     /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
729     /// `ptr_into_vec.offset_from(vec.as_ptr())` is always safe.
730     ///
731     /// Most platforms fundamentally can't even construct such an allocation.
732     /// For instance, no known 64-bit platform can ever serve a request
733     /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
734     /// However, some 32-bit and 16-bit platforms may successfully serve a request for
735     /// more than `isize::MAX` bytes with things like Physical Address
736     /// Extension. As such, memory acquired directly from allocators or memory
737     /// mapped files *may* be too large to handle with this function.
738     ///
739     /// Consider using [`wrapping_offset_from`] instead if these constraints are
740     /// difficult to satisfy. The only advantage of this method is that it
741     /// enables more aggressive compiler optimizations.
742     ///
743     /// # Panics
744     ///
745     /// This function panics if `T` is a Zero-Sized Type ("ZST").
746     ///
747     /// # Examples
748     ///
749     /// Basic usage:
750     ///
751     /// ```
752     /// #![feature(ptr_offset_from)]
753     ///
754     /// let a = [0; 5];
755     /// let ptr1: *const i32 = &a[1];
756     /// let ptr2: *const i32 = &a[3];
757     /// unsafe {
758     ///     assert_eq!(ptr2.offset_from(ptr1), 2);
759     ///     assert_eq!(ptr1.offset_from(ptr2), -2);
760     ///     assert_eq!(ptr1.offset(2), ptr2);
761     ///     assert_eq!(ptr2.offset(-2), ptr1);
762     /// }
763     /// ```
764     #[unstable(feature = "ptr_offset_from", issue = "41079")]
765     #[inline]
766     pub unsafe fn offset_from(self, origin: *const T) -> isize where T: Sized {
767         let pointee_size = mem::size_of::<T>();
768         assert!(0 < pointee_size && pointee_size <= isize::max_value() as usize);
769
770         // This is the same sequence that Clang emits for pointer subtraction.
771         // It can be neither `nsw` nor `nuw` because the input is treated as
772         // unsigned but then the output is treated as signed, so neither works.
773         let d = isize::wrapping_sub(self as _, origin as _);
774         intrinsics::exact_div(d, pointee_size as _)
775     }
776
777     /// Calculates the distance between two pointers. The returned value is in
778     /// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
779     ///
780     /// If the address different between the two pointers is not a multiple of
781     /// `mem::size_of::<T>()` then the result of the division is rounded towards
782     /// zero.
783     ///
784     /// Though this method is safe for any two pointers, note that its result
785     /// will be mostly useless if the two pointers aren't into the same allocated
786     /// object, for example if they point to two different local variables.
787     ///
788     /// # Panics
789     ///
790     /// This function panics if `T` is a zero-sized type.
791     ///
792     /// # Examples
793     ///
794     /// Basic usage:
795     ///
796     /// ```
797     /// #![feature(ptr_wrapping_offset_from)]
798     ///
799     /// let a = [0; 5];
800     /// let ptr1: *const i32 = &a[1];
801     /// let ptr2: *const i32 = &a[3];
802     /// assert_eq!(ptr2.wrapping_offset_from(ptr1), 2);
803     /// assert_eq!(ptr1.wrapping_offset_from(ptr2), -2);
804     /// assert_eq!(ptr1.wrapping_offset(2), ptr2);
805     /// assert_eq!(ptr2.wrapping_offset(-2), ptr1);
806     ///
807     /// let ptr1: *const i32 = 3 as _;
808     /// let ptr2: *const i32 = 13 as _;
809     /// assert_eq!(ptr2.wrapping_offset_from(ptr1), 2);
810     /// ```
811     #[unstable(feature = "ptr_wrapping_offset_from", issue = "41079")]
812     #[inline]
813     pub fn wrapping_offset_from(self, origin: *const T) -> isize where T: Sized {
814         let pointee_size = mem::size_of::<T>();
815         assert!(0 < pointee_size && pointee_size <= isize::max_value() as usize);
816
817         let d = isize::wrapping_sub(self as _, origin as _);
818         d.wrapping_div(pointee_size as _)
819     }
820
821     /// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
822     ///
823     /// `count` is in units of T; e.g. a `count` of 3 represents a pointer
824     /// offset of `3 * size_of::<T>()` bytes.
825     ///
826     /// # Safety
827     ///
828     /// If any of the following conditions are violated, the result is Undefined
829     /// Behavior:
830     ///
831     /// * Both the starting and resulting pointer must be either in bounds or one
832     ///   byte past the end of an allocated object.
833     ///
834     /// * The computed offset, **in bytes**, cannot overflow an `isize`.
835     ///
836     /// * The offset being in bounds cannot rely on "wrapping around" the address
837     ///   space. That is, the infinite-precision sum must fit in a `usize`.
838     ///
839     /// The compiler and standard library generally tries to ensure allocations
840     /// never reach a size where an offset is a concern. For instance, `Vec`
841     /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
842     /// `vec.as_ptr().add(vec.len())` is always safe.
843     ///
844     /// Most platforms fundamentally can't even construct such an allocation.
845     /// For instance, no known 64-bit platform can ever serve a request
846     /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
847     /// However, some 32-bit and 16-bit platforms may successfully serve a request for
848     /// more than `isize::MAX` bytes with things like Physical Address
849     /// Extension. As such, memory acquired directly from allocators or memory
850     /// mapped files *may* be too large to handle with this function.
851     ///
852     /// Consider using `wrapping_offset` instead if these constraints are
853     /// difficult to satisfy. The only advantage of this method is that it
854     /// enables more aggressive compiler optimizations.
855     ///
856     /// # Examples
857     ///
858     /// Basic usage:
859     ///
860     /// ```
861     /// let s: &str = "123";
862     /// let ptr: *const u8 = s.as_ptr();
863     ///
864     /// unsafe {
865     ///     println!("{}", *ptr.add(1) as char);
866     ///     println!("{}", *ptr.add(2) as char);
867     /// }
868     /// ```
869     #[stable(feature = "pointer_methods", since = "1.26.0")]
870     #[inline]
871     pub unsafe fn add(self, count: usize) -> Self
872         where T: Sized,
873     {
874         self.offset(count as isize)
875     }
876
877     /// Calculates the offset from a pointer (convenience for
878     /// `.offset((count as isize).wrapping_neg())`).
879     ///
880     /// `count` is in units of T; e.g. a `count` of 3 represents a pointer
881     /// offset of `3 * size_of::<T>()` bytes.
882     ///
883     /// # Safety
884     ///
885     /// If any of the following conditions are violated, the result is Undefined
886     /// Behavior:
887     ///
888     /// * Both the starting and resulting pointer must be either in bounds or one
889     ///   byte past the end of an allocated object.
890     ///
891     /// * The computed offset cannot exceed `isize::MAX` **bytes**.
892     ///
893     /// * The offset being in bounds cannot rely on "wrapping around" the address
894     ///   space. That is, the infinite-precision sum must fit in a usize.
895     ///
896     /// The compiler and standard library generally tries to ensure allocations
897     /// never reach a size where an offset is a concern. For instance, `Vec`
898     /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
899     /// `vec.as_ptr().add(vec.len()).sub(vec.len())` is always safe.
900     ///
901     /// Most platforms fundamentally can't even construct such an allocation.
902     /// For instance, no known 64-bit platform can ever serve a request
903     /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
904     /// However, some 32-bit and 16-bit platforms may successfully serve a request for
905     /// more than `isize::MAX` bytes with things like Physical Address
906     /// Extension. As such, memory acquired directly from allocators or memory
907     /// mapped files *may* be too large to handle with this function.
908     ///
909     /// Consider using `wrapping_offset` instead if these constraints are
910     /// difficult to satisfy. The only advantage of this method is that it
911     /// enables more aggressive compiler optimizations.
912     ///
913     /// # Examples
914     ///
915     /// Basic usage:
916     ///
917     /// ```
918     /// let s: &str = "123";
919     ///
920     /// unsafe {
921     ///     let end: *const u8 = s.as_ptr().add(3);
922     ///     println!("{}", *end.sub(1) as char);
923     ///     println!("{}", *end.sub(2) as char);
924     /// }
925     /// ```
926     #[stable(feature = "pointer_methods", since = "1.26.0")]
927     #[inline]
928     pub unsafe fn sub(self, count: usize) -> Self
929         where T: Sized,
930     {
931         self.offset((count as isize).wrapping_neg())
932     }
933
934     /// Calculates the offset from a pointer using wrapping arithmetic.
935     /// (convenience for `.wrapping_offset(count as isize)`)
936     ///
937     /// `count` is in units of T; e.g. a `count` of 3 represents a pointer
938     /// offset of `3 * size_of::<T>()` bytes.
939     ///
940     /// # Safety
941     ///
942     /// The resulting pointer does not need to be in bounds, but it is
943     /// potentially hazardous to dereference (which requires `unsafe`).
944     ///
945     /// Always use `.add(count)` instead when possible, because `add`
946     /// allows the compiler to optimize better.
947     ///
948     /// # Examples
949     ///
950     /// Basic usage:
951     ///
952     /// ```
953     /// // Iterate using a raw pointer in increments of two elements
954     /// let data = [1u8, 2, 3, 4, 5];
955     /// let mut ptr: *const u8 = data.as_ptr();
956     /// let step = 2;
957     /// let end_rounded_up = ptr.wrapping_add(6);
958     ///
959     /// // This loop prints "1, 3, 5, "
960     /// while ptr != end_rounded_up {
961     ///     unsafe {
962     ///         print!("{}, ", *ptr);
963     ///     }
964     ///     ptr = ptr.wrapping_add(step);
965     /// }
966     /// ```
967     #[stable(feature = "pointer_methods", since = "1.26.0")]
968     #[inline]
969     pub fn wrapping_add(self, count: usize) -> Self
970         where T: Sized,
971     {
972         self.wrapping_offset(count as isize)
973     }
974
975     /// Calculates the offset from a pointer using wrapping arithmetic.
976     /// (convenience for `.wrapping_offset((count as isize).wrapping_sub())`)
977     ///
978     /// `count` is in units of T; e.g. a `count` of 3 represents a pointer
979     /// offset of `3 * size_of::<T>()` bytes.
980     ///
981     /// # Safety
982     ///
983     /// The resulting pointer does not need to be in bounds, but it is
984     /// potentially hazardous to dereference (which requires `unsafe`).
985     ///
986     /// Always use `.sub(count)` instead when possible, because `sub`
987     /// allows the compiler to optimize better.
988     ///
989     /// # Examples
990     ///
991     /// Basic usage:
992     ///
993     /// ```
994     /// // Iterate using a raw pointer in increments of two elements (backwards)
995     /// let data = [1u8, 2, 3, 4, 5];
996     /// let mut ptr: *const u8 = data.as_ptr();
997     /// let start_rounded_down = ptr.wrapping_sub(2);
998     /// ptr = ptr.wrapping_add(4);
999     /// let step = 2;
1000     /// // This loop prints "5, 3, 1, "
1001     /// while ptr != start_rounded_down {
1002     ///     unsafe {
1003     ///         print!("{}, ", *ptr);
1004     ///     }
1005     ///     ptr = ptr.wrapping_sub(step);
1006     /// }
1007     /// ```
1008     #[stable(feature = "pointer_methods", since = "1.26.0")]
1009     #[inline]
1010     pub fn wrapping_sub(self, count: usize) -> Self
1011         where T: Sized,
1012     {
1013         self.wrapping_offset((count as isize).wrapping_neg())
1014     }
1015
1016     /// Reads the value from `self` without moving it. This leaves the
1017     /// memory in `self` unchanged.
1018     ///
1019     /// # Safety
1020     ///
1021     /// Beyond accepting a raw pointer, this is unsafe because it semantically
1022     /// moves the value out of `self` without preventing further usage of `self`.
1023     /// If `T` is not `Copy`, then care must be taken to ensure that the value at
1024     /// `self` is not used before the data is overwritten again (e.g. with `write`,
1025     /// `write_bytes`, or `copy`). Note that `*self = foo` counts as a use
1026     /// because it will attempt to drop the value previously at `*self`.
1027     ///
1028     /// The pointer must be aligned; use `read_unaligned` if that is not the case.
1029     ///
1030     /// # Examples
1031     ///
1032     /// Basic usage:
1033     ///
1034     /// ```
1035     /// let x = 12;
1036     /// let y = &x as *const i32;
1037     ///
1038     /// unsafe {
1039     ///     assert_eq!(y.read(), 12);
1040     /// }
1041     /// ```
1042     #[stable(feature = "pointer_methods", since = "1.26.0")]
1043     #[inline]
1044     pub unsafe fn read(self) -> T
1045         where T: Sized,
1046     {
1047         read(self)
1048     }
1049
1050     /// Performs a volatile read of the value from `self` without moving it. This
1051     /// leaves the memory in `self` unchanged.
1052     ///
1053     /// Volatile operations are intended to act on I/O memory, and are guaranteed
1054     /// to not be elided or reordered by the compiler across other volatile
1055     /// operations.
1056     ///
1057     /// # Notes
1058     ///
1059     /// Rust does not currently have a rigorously and formally defined memory model,
1060     /// so the precise semantics of what "volatile" means here is subject to change
1061     /// over time. That being said, the semantics will almost always end up pretty
1062     /// similar to [C11's definition of volatile][c11].
1063     ///
1064     /// The compiler shouldn't change the relative order or number of volatile
1065     /// memory operations. However, volatile memory operations on zero-sized types
1066     /// (e.g. if a zero-sized type is passed to `read_volatile`) are no-ops
1067     /// and may be ignored.
1068     ///
1069     /// [c11]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
1070     ///
1071     /// # Safety
1072     ///
1073     /// Beyond accepting a raw pointer, this is unsafe because it semantically
1074     /// moves the value out of `self` without preventing further usage of `self`.
1075     /// If `T` is not `Copy`, then care must be taken to ensure that the value at
1076     /// `self` is not used before the data is overwritten again (e.g. with `write`,
1077     /// `write_bytes`, or `copy`). Note that `*self = foo` counts as a use
1078     /// because it will attempt to drop the value previously at `*self`.
1079     ///
1080     /// # Examples
1081     ///
1082     /// Basic usage:
1083     ///
1084     /// ```
1085     /// let x = 12;
1086     /// let y = &x as *const i32;
1087     ///
1088     /// unsafe {
1089     ///     assert_eq!(y.read_volatile(), 12);
1090     /// }
1091     /// ```
1092     #[stable(feature = "pointer_methods", since = "1.26.0")]
1093     #[inline]
1094     pub unsafe fn read_volatile(self) -> T
1095         where T: Sized,
1096     {
1097         read_volatile(self)
1098     }
1099
1100     /// Reads the value from `self` without moving it. This leaves the
1101     /// memory in `self` unchanged.
1102     ///
1103     /// Unlike `read`, the pointer may be unaligned.
1104     ///
1105     /// # Safety
1106     ///
1107     /// Beyond accepting a raw pointer, this is unsafe because it semantically
1108     /// moves the value out of `self` without preventing further usage of `self`.
1109     /// If `T` is not `Copy`, then care must be taken to ensure that the value at
1110     /// `self` is not used before the data is overwritten again (e.g. with `write`,
1111     /// `write_bytes`, or `copy`). Note that `*self = foo` counts as a use
1112     /// because it will attempt to drop the value previously at `*self`.
1113     ///
1114     /// # Examples
1115     ///
1116     /// Basic usage:
1117     ///
1118     /// ```
1119     /// let x = 12;
1120     /// let y = &x as *const i32;
1121     ///
1122     /// unsafe {
1123     ///     assert_eq!(y.read_unaligned(), 12);
1124     /// }
1125     /// ```
1126     #[stable(feature = "pointer_methods", since = "1.26.0")]
1127     #[inline]
1128     pub unsafe fn read_unaligned(self) -> T
1129         where T: Sized,
1130     {
1131         read_unaligned(self)
1132     }
1133
1134     /// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
1135     /// and destination may overlap.
1136     ///
1137     /// NOTE: this has the *same* argument order as `ptr::copy`.
1138     ///
1139     /// This is semantically equivalent to C's `memmove`.
1140     ///
1141     /// # Safety
1142     ///
1143     /// Care must be taken with the ownership of `self` and `dest`.
1144     /// This method semantically moves the values of `self` into `dest`.
1145     /// However it does not drop the contents of `self`, or prevent the contents
1146     /// of `dest` from being dropped or used.
1147     ///
1148     /// # Examples
1149     ///
1150     /// Efficiently create a Rust vector from an unsafe buffer:
1151     ///
1152     /// ```
1153     /// # #[allow(dead_code)]
1154     /// unsafe fn from_buf_raw<T: Copy>(ptr: *const T, elts: usize) -> Vec<T> {
1155     ///     let mut dst = Vec::with_capacity(elts);
1156     ///     dst.set_len(elts);
1157     ///     ptr.copy_to(dst.as_mut_ptr(), elts);
1158     ///     dst
1159     /// }
1160     /// ```
1161     #[stable(feature = "pointer_methods", since = "1.26.0")]
1162     #[inline]
1163     pub unsafe fn copy_to(self, dest: *mut T, count: usize)
1164         where T: Sized,
1165     {
1166         copy(self, dest, count)
1167     }
1168
1169     /// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
1170     /// and destination may *not* overlap.
1171     ///
1172     /// NOTE: this has the *same* argument order as `ptr::copy_nonoverlapping`.
1173     ///
1174     /// `copy_nonoverlapping` is semantically equivalent to C's `memcpy`.
1175     ///
1176     /// # Safety
1177     ///
1178     /// Beyond requiring that the program must be allowed to access both regions
1179     /// of memory, it is Undefined Behavior for source and destination to
1180     /// overlap. Care must also be taken with the ownership of `self` and
1181     /// `self`. This method semantically moves the values of `self` into `dest`.
1182     /// However it does not drop the contents of `dest`, or prevent the contents
1183     /// of `self` from being dropped or used.
1184     ///
1185     /// # Examples
1186     ///
1187     /// Efficiently create a Rust vector from an unsafe buffer:
1188     ///
1189     /// ```
1190     /// # #[allow(dead_code)]
1191     /// unsafe fn from_buf_raw<T: Copy>(ptr: *const T, elts: usize) -> Vec<T> {
1192     ///     let mut dst = Vec::with_capacity(elts);
1193     ///     dst.set_len(elts);
1194     ///     ptr.copy_to_nonoverlapping(dst.as_mut_ptr(), elts);
1195     ///     dst
1196     /// }
1197     /// ```
1198     #[stable(feature = "pointer_methods", since = "1.26.0")]
1199     #[inline]
1200     pub unsafe fn copy_to_nonoverlapping(self, dest: *mut T, count: usize)
1201         where T: Sized,
1202     {
1203         copy_nonoverlapping(self, dest, count)
1204     }
1205
1206     /// Computes the byte offset that needs to be applied in order to
1207     /// make the pointer aligned to `align`.
1208     /// If it is not possible to align the pointer, the implementation returns
1209     /// `usize::max_value()`.
1210     ///
1211     /// There are no guarantees whatsover that offsetting the pointer will not
1212     /// overflow or go beyond the allocation that the pointer points into.
1213     /// It is up to the caller to ensure that the returned offset is correct
1214     /// in all terms other than alignment.
1215     ///
1216     /// # Examples
1217     ///
1218     /// Accessing adjacent `u8` as `u16`
1219     ///
1220     /// ```
1221     /// # #![feature(align_offset)]
1222     /// # fn foo(n: usize) {
1223     /// # use std::mem::align_of;
1224     /// # unsafe {
1225     /// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
1226     /// let ptr = &x[n] as *const u8;
1227     /// let offset = ptr.align_offset(align_of::<u16>());
1228     /// if offset < x.len() - n - 1 {
1229     ///     let u16_ptr = ptr.offset(offset as isize) as *const u16;
1230     ///     assert_ne!(*u16_ptr, 500);
1231     /// } else {
1232     ///     // while the pointer can be aligned via `offset`, it would point
1233     ///     // outside the allocation
1234     /// }
1235     /// # } }
1236     /// ```
1237     #[unstable(feature = "align_offset", issue = "44488")]
1238     pub fn align_offset(self, align: usize) -> usize {
1239         unsafe {
1240             intrinsics::align_offset(self as *const _, align)
1241         }
1242     }
1243 }
1244
1245 #[lang = "mut_ptr"]
1246 impl<T: ?Sized> *mut T {
1247     /// Returns `true` if the pointer is null.
1248     ///
1249     /// Note that unsized types have many possible null pointers, as only the
1250     /// raw data pointer is considered, not their length, vtable, etc.
1251     /// Therefore, two pointers that are null may still not compare equal to
1252     /// each other.
1253     ///
1254     /// # Examples
1255     ///
1256     /// Basic usage:
1257     ///
1258     /// ```
1259     /// let mut s = [1, 2, 3];
1260     /// let ptr: *mut u32 = s.as_mut_ptr();
1261     /// assert!(!ptr.is_null());
1262     /// ```
1263     #[stable(feature = "rust1", since = "1.0.0")]
1264     #[inline]
1265     pub fn is_null(self) -> bool {
1266         // Compare via a cast to a thin pointer, so fat pointers are only
1267         // considering their "data" part for null-ness.
1268         (self as *mut u8) == null_mut()
1269     }
1270
1271     /// Returns `None` if the pointer is null, or else returns a reference to
1272     /// the value wrapped in `Some`.
1273     ///
1274     /// # Safety
1275     ///
1276     /// While this method and its mutable counterpart are useful for
1277     /// null-safety, it is important to note that this is still an unsafe
1278     /// operation because the returned value could be pointing to invalid
1279     /// memory.
1280     ///
1281     /// Additionally, the lifetime `'a` returned is arbitrarily chosen and does
1282     /// not necessarily reflect the actual lifetime of the data.
1283     ///
1284     /// # Examples
1285     ///
1286     /// Basic usage:
1287     ///
1288     /// ```
1289     /// let ptr: *mut u8 = &mut 10u8 as *mut u8;
1290     ///
1291     /// unsafe {
1292     ///     if let Some(val_back) = ptr.as_ref() {
1293     ///         println!("We got back the value: {}!", val_back);
1294     ///     }
1295     /// }
1296     /// ```
1297     #[stable(feature = "ptr_as_ref", since = "1.9.0")]
1298     #[inline]
1299     pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
1300         if self.is_null() {
1301             None
1302         } else {
1303             Some(&*self)
1304         }
1305     }
1306
1307     /// Calculates the offset from a pointer.
1308     ///
1309     /// `count` is in units of T; e.g. a `count` of 3 represents a pointer
1310     /// offset of `3 * size_of::<T>()` bytes.
1311     ///
1312     /// # Safety
1313     ///
1314     /// If any of the following conditions are violated, the result is Undefined
1315     /// Behavior:
1316     ///
1317     /// * Both the starting and resulting pointer must be either in bounds or one
1318     ///   byte past the end of an allocated object.
1319     ///
1320     /// * The computed offset, **in bytes**, cannot overflow an `isize`.
1321     ///
1322     /// * The offset being in bounds cannot rely on "wrapping around" the address
1323     ///   space. That is, the infinite-precision sum, **in bytes** must fit in a usize.
1324     ///
1325     /// The compiler and standard library generally tries to ensure allocations
1326     /// never reach a size where an offset is a concern. For instance, `Vec`
1327     /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
1328     /// `vec.as_ptr().offset(vec.len() as isize)` is always safe.
1329     ///
1330     /// Most platforms fundamentally can't even construct such an allocation.
1331     /// For instance, no known 64-bit platform can ever serve a request
1332     /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
1333     /// However, some 32-bit and 16-bit platforms may successfully serve a request for
1334     /// more than `isize::MAX` bytes with things like Physical Address
1335     /// Extension. As such, memory acquired directly from allocators or memory
1336     /// mapped files *may* be too large to handle with this function.
1337     ///
1338     /// Consider using `wrapping_offset` instead if these constraints are
1339     /// difficult to satisfy. The only advantage of this method is that it
1340     /// enables more aggressive compiler optimizations.
1341     ///
1342     /// # Examples
1343     ///
1344     /// Basic usage:
1345     ///
1346     /// ```
1347     /// let mut s = [1, 2, 3];
1348     /// let ptr: *mut u32 = s.as_mut_ptr();
1349     ///
1350     /// unsafe {
1351     ///     println!("{}", *ptr.offset(1));
1352     ///     println!("{}", *ptr.offset(2));
1353     /// }
1354     /// ```
1355     #[stable(feature = "rust1", since = "1.0.0")]
1356     #[inline]
1357     pub unsafe fn offset(self, count: isize) -> *mut T where T: Sized {
1358         intrinsics::offset(self, count) as *mut T
1359     }
1360
1361     /// Calculates the offset from a pointer using wrapping arithmetic.
1362     /// `count` is in units of T; e.g. a `count` of 3 represents a pointer
1363     /// offset of `3 * size_of::<T>()` bytes.
1364     ///
1365     /// # Safety
1366     ///
1367     /// The resulting pointer does not need to be in bounds, but it is
1368     /// potentially hazardous to dereference (which requires `unsafe`).
1369     ///
1370     /// Always use `.offset(count)` instead when possible, because `offset`
1371     /// allows the compiler to optimize better.
1372     ///
1373     /// # Examples
1374     ///
1375     /// Basic usage:
1376     ///
1377     /// ```
1378     /// // Iterate using a raw pointer in increments of two elements
1379     /// let mut data = [1u8, 2, 3, 4, 5];
1380     /// let mut ptr: *mut u8 = data.as_mut_ptr();
1381     /// let step = 2;
1382     /// let end_rounded_up = ptr.wrapping_offset(6);
1383     ///
1384     /// while ptr != end_rounded_up {
1385     ///     unsafe {
1386     ///         *ptr = 0;
1387     ///     }
1388     ///     ptr = ptr.wrapping_offset(step);
1389     /// }
1390     /// assert_eq!(&data, &[0, 2, 0, 4, 0]);
1391     /// ```
1392     #[stable(feature = "ptr_wrapping_offset", since = "1.16.0")]
1393     #[inline]
1394     pub fn wrapping_offset(self, count: isize) -> *mut T where T: Sized {
1395         unsafe {
1396             intrinsics::arith_offset(self, count) as *mut T
1397         }
1398     }
1399
1400     /// Returns `None` if the pointer is null, or else returns a mutable
1401     /// reference to the value wrapped in `Some`.
1402     ///
1403     /// # Safety
1404     ///
1405     /// As with `as_ref`, this is unsafe because it cannot verify the validity
1406     /// of the returned pointer, nor can it ensure that the lifetime `'a`
1407     /// returned is indeed a valid lifetime for the contained data.
1408     ///
1409     /// # Examples
1410     ///
1411     /// Basic usage:
1412     ///
1413     /// ```
1414     /// let mut s = [1, 2, 3];
1415     /// let ptr: *mut u32 = s.as_mut_ptr();
1416     /// let first_value = unsafe { ptr.as_mut().unwrap() };
1417     /// *first_value = 4;
1418     /// println!("{:?}", s); // It'll print: "[4, 2, 3]".
1419     /// ```
1420     #[stable(feature = "ptr_as_ref", since = "1.9.0")]
1421     #[inline]
1422     pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {
1423         if self.is_null() {
1424             None
1425         } else {
1426             Some(&mut *self)
1427         }
1428     }
1429
1430     /// Calculates the distance between two pointers. The returned value is in
1431     /// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
1432     ///
1433     /// If the address different between the two pointers ia not a multiple of
1434     /// `mem::size_of::<T>()` then the result of the division is rounded towards
1435     /// zero.
1436     ///
1437     /// This function returns `None` if `T` is a zero-sized type.
1438     ///
1439     /// # Examples
1440     ///
1441     /// Basic usage:
1442     ///
1443     /// ```
1444     /// #![feature(offset_to)]
1445     ///
1446     /// fn main() {
1447     ///     let mut a = [0; 5];
1448     ///     let ptr1: *mut i32 = &mut a[1];
1449     ///     let ptr2: *mut i32 = &mut a[3];
1450     ///     assert_eq!(ptr1.offset_to(ptr2), Some(2));
1451     ///     assert_eq!(ptr2.offset_to(ptr1), Some(-2));
1452     ///     assert_eq!(unsafe { ptr1.offset(2) }, ptr2);
1453     ///     assert_eq!(unsafe { ptr2.offset(-2) }, ptr1);
1454     /// }
1455     /// ```
1456     #[unstable(feature = "offset_to", issue = "41079")]
1457     #[inline]
1458     pub fn offset_to(self, other: *const T) -> Option<isize> where T: Sized {
1459         let size = mem::size_of::<T>();
1460         if size == 0 {
1461             None
1462         } else {
1463             let diff = (other as isize).wrapping_sub(self as isize);
1464             Some(diff / size as isize)
1465         }
1466     }
1467
1468     /// Calculates the distance between two pointers. The returned value is in
1469     /// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
1470     ///
1471     /// This function is the inverse of [`offset`].
1472     ///
1473     /// [`offset`]: #method.offset-1
1474     /// [`wrapping_offset_from`]: #method.wrapping_offset_from-1
1475     ///
1476     /// # Safety
1477     ///
1478     /// If any of the following conditions are violated, the result is Undefined
1479     /// Behavior:
1480     ///
1481     /// * Both the starting and other pointer must be either in bounds or one
1482     ///   byte past the end of the same allocated object.
1483     ///
1484     /// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
1485     ///
1486     /// * The distance between the pointers, in bytes, must be an exact multiple
1487     ///   of the size of `T`.
1488     ///
1489     /// * The distance being in bounds cannot rely on "wrapping around" the address space.
1490     ///
1491     /// The compiler and standard library generally try to ensure allocations
1492     /// never reach a size where an offset is a concern. For instance, `Vec`
1493     /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
1494     /// `ptr_into_vec.offset_from(vec.as_ptr())` is always safe.
1495     ///
1496     /// Most platforms fundamentally can't even construct such an allocation.
1497     /// For instance, no known 64-bit platform can ever serve a request
1498     /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
1499     /// However, some 32-bit and 16-bit platforms may successfully serve a request for
1500     /// more than `isize::MAX` bytes with things like Physical Address
1501     /// Extension. As such, memory acquired directly from allocators or memory
1502     /// mapped files *may* be too large to handle with this function.
1503     ///
1504     /// Consider using [`wrapping_offset_from`] instead if these constraints are
1505     /// difficult to satisfy. The only advantage of this method is that it
1506     /// enables more aggressive compiler optimizations.
1507     ///
1508     /// # Panics
1509     ///
1510     /// This function panics if `T` is a Zero-Sized Type ("ZST").
1511     ///
1512     /// # Examples
1513     ///
1514     /// Basic usage:
1515     ///
1516     /// ```
1517     /// #![feature(ptr_offset_from)]
1518     ///
1519     /// let a = [0; 5];
1520     /// let ptr1: *mut i32 = &mut a[1];
1521     /// let ptr2: *mut i32 = &mut a[3];
1522     /// unsafe {
1523     ///     assert_eq!(ptr2.offset_from(ptr1), 2);
1524     ///     assert_eq!(ptr1.offset_from(ptr2), -2);
1525     ///     assert_eq!(ptr1.offset(2), ptr2);
1526     ///     assert_eq!(ptr2.offset(-2), ptr1);
1527     /// }
1528     /// ```
1529     #[unstable(feature = "ptr_offset_from", issue = "41079")]
1530     #[inline]
1531     pub unsafe fn offset_from(self, origin: *const T) -> isize where T: Sized {
1532         (self as *const T).offset_from(origin)
1533     }
1534
1535     /// Calculates the distance between two pointers. The returned value is in
1536     /// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
1537     ///
1538     /// If the address different between the two pointers is not a multiple of
1539     /// `mem::size_of::<T>()` then the result of the division is rounded towards
1540     /// zero.
1541     ///
1542     /// Though this method is safe for any two pointers, note that its result
1543     /// will be mostly useless if the two pointers aren't into the same allocated
1544     /// object, for example if they point to two different local variables.
1545     ///
1546     /// # Panics
1547     ///
1548     /// This function panics if `T` is a zero-sized type.
1549     ///
1550     /// # Examples
1551     ///
1552     /// Basic usage:
1553     ///
1554     /// ```
1555     /// #![feature(ptr_wrapping_offset_from)]
1556     ///
1557     /// let a = [0; 5];
1558     /// let ptr1: *mut i32 = &mut a[1];
1559     /// let ptr2: *mut i32 = &mut a[3];
1560     /// assert_eq!(ptr2.wrapping_offset_from(ptr1), 2);
1561     /// assert_eq!(ptr1.wrapping_offset_from(ptr2), -2);
1562     /// assert_eq!(ptr1.wrapping_offset(2), ptr2);
1563     /// assert_eq!(ptr2.wrapping_offset(-2), ptr1);
1564     ///
1565     /// let ptr1: *mut i32 = 3 as _;
1566     /// let ptr2: *mut i32 = 13 as _;
1567     /// assert_eq!(ptr2.wrapping_offset_from(ptr1), 2);
1568     /// ```
1569     #[unstable(feature = "ptr_wrapping_offset_from", issue = "41079")]
1570     #[inline]
1571     pub fn wrapping_offset_from(self, origin: *const T) -> isize where T: Sized {
1572         (self as *const T).wrapping_offset_from(origin)
1573     }
1574
1575     /// Computes the byte offset that needs to be applied in order to
1576     /// make the pointer aligned to `align`.
1577     /// If it is not possible to align the pointer, the implementation returns
1578     /// `usize::max_value()`.
1579     ///
1580     /// There are no guarantees whatsover that offsetting the pointer will not
1581     /// overflow or go beyond the allocation that the pointer points into.
1582     /// It is up to the caller to ensure that the returned offset is correct
1583     /// in all terms other than alignment.
1584     ///
1585     /// # Examples
1586     ///
1587     /// Accessing adjacent `u8` as `u16`
1588     ///
1589     /// ```
1590     /// # #![feature(align_offset)]
1591     /// # fn foo(n: usize) {
1592     /// # use std::mem::align_of;
1593     /// # unsafe {
1594     /// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
1595     /// let ptr = &x[n] as *const u8;
1596     /// let offset = ptr.align_offset(align_of::<u16>());
1597     /// if offset < x.len() - n - 1 {
1598     ///     let u16_ptr = ptr.offset(offset as isize) as *const u16;
1599     ///     assert_ne!(*u16_ptr, 500);
1600     /// } else {
1601     ///     // while the pointer can be aligned via `offset`, it would point
1602     ///     // outside the allocation
1603     /// }
1604     /// # } }
1605     /// ```
1606     #[unstable(feature = "align_offset", issue = "44488")]
1607     pub fn align_offset(self, align: usize) -> usize {
1608         unsafe {
1609             intrinsics::align_offset(self as *const _, align)
1610         }
1611     }
1612
1613     /// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
1614     ///
1615     /// `count` is in units of T; e.g. a `count` of 3 represents a pointer
1616     /// offset of `3 * size_of::<T>()` bytes.
1617     ///
1618     /// # Safety
1619     ///
1620     /// If any of the following conditions are violated, the result is Undefined
1621     /// Behavior:
1622     ///
1623     /// * Both the starting and resulting pointer must be either in bounds or one
1624     ///   byte past the end of an allocated object.
1625     ///
1626     /// * The computed offset, **in bytes**, cannot overflow an `isize`.
1627     ///
1628     /// * The offset being in bounds cannot rely on "wrapping around" the address
1629     ///   space. That is, the infinite-precision sum must fit in a `usize`.
1630     ///
1631     /// The compiler and standard library generally tries to ensure allocations
1632     /// never reach a size where an offset is a concern. For instance, `Vec`
1633     /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
1634     /// `vec.as_ptr().add(vec.len())` is always safe.
1635     ///
1636     /// Most platforms fundamentally can't even construct such an allocation.
1637     /// For instance, no known 64-bit platform can ever serve a request
1638     /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
1639     /// However, some 32-bit and 16-bit platforms may successfully serve a request for
1640     /// more than `isize::MAX` bytes with things like Physical Address
1641     /// Extension. As such, memory acquired directly from allocators or memory
1642     /// mapped files *may* be too large to handle with this function.
1643     ///
1644     /// Consider using `wrapping_offset` instead if these constraints are
1645     /// difficult to satisfy. The only advantage of this method is that it
1646     /// enables more aggressive compiler optimizations.
1647     ///
1648     /// # Examples
1649     ///
1650     /// Basic usage:
1651     ///
1652     /// ```
1653     /// let s: &str = "123";
1654     /// let ptr: *const u8 = s.as_ptr();
1655     ///
1656     /// unsafe {
1657     ///     println!("{}", *ptr.add(1) as char);
1658     ///     println!("{}", *ptr.add(2) as char);
1659     /// }
1660     /// ```
1661     #[stable(feature = "pointer_methods", since = "1.26.0")]
1662     #[inline]
1663     pub unsafe fn add(self, count: usize) -> Self
1664         where T: Sized,
1665     {
1666         self.offset(count as isize)
1667     }
1668
1669     /// Calculates the offset from a pointer (convenience for
1670     /// `.offset((count as isize).wrapping_neg())`).
1671     ///
1672     /// `count` is in units of T; e.g. a `count` of 3 represents a pointer
1673     /// offset of `3 * size_of::<T>()` bytes.
1674     ///
1675     /// # Safety
1676     ///
1677     /// If any of the following conditions are violated, the result is Undefined
1678     /// Behavior:
1679     ///
1680     /// * Both the starting and resulting pointer must be either in bounds or one
1681     ///   byte past the end of an allocated object.
1682     ///
1683     /// * The computed offset cannot exceed `isize::MAX` **bytes**.
1684     ///
1685     /// * The offset being in bounds cannot rely on "wrapping around" the address
1686     ///   space. That is, the infinite-precision sum must fit in a usize.
1687     ///
1688     /// The compiler and standard library generally tries to ensure allocations
1689     /// never reach a size where an offset is a concern. For instance, `Vec`
1690     /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
1691     /// `vec.as_ptr().add(vec.len()).sub(vec.len())` is always safe.
1692     ///
1693     /// Most platforms fundamentally can't even construct such an allocation.
1694     /// For instance, no known 64-bit platform can ever serve a request
1695     /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
1696     /// However, some 32-bit and 16-bit platforms may successfully serve a request for
1697     /// more than `isize::MAX` bytes with things like Physical Address
1698     /// Extension. As such, memory acquired directly from allocators or memory
1699     /// mapped files *may* be too large to handle with this function.
1700     ///
1701     /// Consider using `wrapping_offset` instead if these constraints are
1702     /// difficult to satisfy. The only advantage of this method is that it
1703     /// enables more aggressive compiler optimizations.
1704     ///
1705     /// # Examples
1706     ///
1707     /// Basic usage:
1708     ///
1709     /// ```
1710     /// let s: &str = "123";
1711     ///
1712     /// unsafe {
1713     ///     let end: *const u8 = s.as_ptr().add(3);
1714     ///     println!("{}", *end.sub(1) as char);
1715     ///     println!("{}", *end.sub(2) as char);
1716     /// }
1717     /// ```
1718     #[stable(feature = "pointer_methods", since = "1.26.0")]
1719     #[inline]
1720     pub unsafe fn sub(self, count: usize) -> Self
1721         where T: Sized,
1722     {
1723         self.offset((count as isize).wrapping_neg())
1724     }
1725
1726     /// Calculates the offset from a pointer using wrapping arithmetic.
1727     /// (convenience for `.wrapping_offset(count as isize)`)
1728     ///
1729     /// `count` is in units of T; e.g. a `count` of 3 represents a pointer
1730     /// offset of `3 * size_of::<T>()` bytes.
1731     ///
1732     /// # Safety
1733     ///
1734     /// The resulting pointer does not need to be in bounds, but it is
1735     /// potentially hazardous to dereference (which requires `unsafe`).
1736     ///
1737     /// Always use `.add(count)` instead when possible, because `add`
1738     /// allows the compiler to optimize better.
1739     ///
1740     /// # Examples
1741     ///
1742     /// Basic usage:
1743     ///
1744     /// ```
1745     /// // Iterate using a raw pointer in increments of two elements
1746     /// let data = [1u8, 2, 3, 4, 5];
1747     /// let mut ptr: *const u8 = data.as_ptr();
1748     /// let step = 2;
1749     /// let end_rounded_up = ptr.wrapping_add(6);
1750     ///
1751     /// // This loop prints "1, 3, 5, "
1752     /// while ptr != end_rounded_up {
1753     ///     unsafe {
1754     ///         print!("{}, ", *ptr);
1755     ///     }
1756     ///     ptr = ptr.wrapping_add(step);
1757     /// }
1758     /// ```
1759     #[stable(feature = "pointer_methods", since = "1.26.0")]
1760     #[inline]
1761     pub fn wrapping_add(self, count: usize) -> Self
1762         where T: Sized,
1763     {
1764         self.wrapping_offset(count as isize)
1765     }
1766
1767     /// Calculates the offset from a pointer using wrapping arithmetic.
1768     /// (convenience for `.wrapping_offset((count as isize).wrapping_sub())`)
1769     ///
1770     /// `count` is in units of T; e.g. a `count` of 3 represents a pointer
1771     /// offset of `3 * size_of::<T>()` bytes.
1772     ///
1773     /// # Safety
1774     ///
1775     /// The resulting pointer does not need to be in bounds, but it is
1776     /// potentially hazardous to dereference (which requires `unsafe`).
1777     ///
1778     /// Always use `.sub(count)` instead when possible, because `sub`
1779     /// allows the compiler to optimize better.
1780     ///
1781     /// # Examples
1782     ///
1783     /// Basic usage:
1784     ///
1785     /// ```
1786     /// // Iterate using a raw pointer in increments of two elements (backwards)
1787     /// let data = [1u8, 2, 3, 4, 5];
1788     /// let mut ptr: *const u8 = data.as_ptr();
1789     /// let start_rounded_down = ptr.wrapping_sub(2);
1790     /// ptr = ptr.wrapping_add(4);
1791     /// let step = 2;
1792     /// // This loop prints "5, 3, 1, "
1793     /// while ptr != start_rounded_down {
1794     ///     unsafe {
1795     ///         print!("{}, ", *ptr);
1796     ///     }
1797     ///     ptr = ptr.wrapping_sub(step);
1798     /// }
1799     /// ```
1800     #[stable(feature = "pointer_methods", since = "1.26.0")]
1801     #[inline]
1802     pub fn wrapping_sub(self, count: usize) -> Self
1803         where T: Sized,
1804     {
1805         self.wrapping_offset((count as isize).wrapping_neg())
1806     }
1807
1808     /// Reads the value from `self` without moving it. This leaves the
1809     /// memory in `self` unchanged.
1810     ///
1811     /// # Safety
1812     ///
1813     /// Beyond accepting a raw pointer, this is unsafe because it semantically
1814     /// moves the value out of `self` without preventing further usage of `self`.
1815     /// If `T` is not `Copy`, then care must be taken to ensure that the value at
1816     /// `self` is not used before the data is overwritten again (e.g. with `write`,
1817     /// `write_bytes`, or `copy`). Note that `*self = foo` counts as a use
1818     /// because it will attempt to drop the value previously at `*self`.
1819     ///
1820     /// The pointer must be aligned; use `read_unaligned` if that is not the case.
1821     ///
1822     /// # Examples
1823     ///
1824     /// Basic usage:
1825     ///
1826     /// ```
1827     /// let x = 12;
1828     /// let y = &x as *const i32;
1829     ///
1830     /// unsafe {
1831     ///     assert_eq!(y.read(), 12);
1832     /// }
1833     /// ```
1834     #[stable(feature = "pointer_methods", since = "1.26.0")]
1835     #[inline]
1836     pub unsafe fn read(self) -> T
1837         where T: Sized,
1838     {
1839         read(self)
1840     }
1841
1842     /// Performs a volatile read of the value from `self` without moving it. This
1843     /// leaves the memory in `self` unchanged.
1844     ///
1845     /// Volatile operations are intended to act on I/O memory, and are guaranteed
1846     /// to not be elided or reordered by the compiler across other volatile
1847     /// operations.
1848     ///
1849     /// # Notes
1850     ///
1851     /// Rust does not currently have a rigorously and formally defined memory model,
1852     /// so the precise semantics of what "volatile" means here is subject to change
1853     /// over time. That being said, the semantics will almost always end up pretty
1854     /// similar to [C11's definition of volatile][c11].
1855     ///
1856     /// The compiler shouldn't change the relative order or number of volatile
1857     /// memory operations. However, volatile memory operations on zero-sized types
1858     /// (e.g. if a zero-sized type is passed to `read_volatile`) are no-ops
1859     /// and may be ignored.
1860     ///
1861     /// [c11]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
1862     ///
1863     /// # Safety
1864     ///
1865     /// Beyond accepting a raw pointer, this is unsafe because it semantically
1866     /// moves the value out of `self` without preventing further usage of `self`.
1867     /// If `T` is not `Copy`, then care must be taken to ensure that the value at
1868     /// `self` is not used before the data is overwritten again (e.g. with `write`,
1869     /// `write_bytes`, or `copy`). Note that `*self = foo` counts as a use
1870     /// because it will attempt to drop the value previously at `*self`.
1871     ///
1872     /// # Examples
1873     ///
1874     /// Basic usage:
1875     ///
1876     /// ```
1877     /// let x = 12;
1878     /// let y = &x as *const i32;
1879     ///
1880     /// unsafe {
1881     ///     assert_eq!(y.read_volatile(), 12);
1882     /// }
1883     /// ```
1884     #[stable(feature = "pointer_methods", since = "1.26.0")]
1885     #[inline]
1886     pub unsafe fn read_volatile(self) -> T
1887         where T: Sized,
1888     {
1889         read_volatile(self)
1890     }
1891
1892     /// Reads the value from `self` without moving it. This leaves the
1893     /// memory in `self` unchanged.
1894     ///
1895     /// Unlike `read`, the pointer may be unaligned.
1896     ///
1897     /// # Safety
1898     ///
1899     /// Beyond accepting a raw pointer, this is unsafe because it semantically
1900     /// moves the value out of `self` without preventing further usage of `self`.
1901     /// If `T` is not `Copy`, then care must be taken to ensure that the value at
1902     /// `self` is not used before the data is overwritten again (e.g. with `write`,
1903     /// `write_bytes`, or `copy`). Note that `*self = foo` counts as a use
1904     /// because it will attempt to drop the value previously at `*self`.
1905     ///
1906     /// # Examples
1907     ///
1908     /// Basic usage:
1909     ///
1910     /// ```
1911     /// let x = 12;
1912     /// let y = &x as *const i32;
1913     ///
1914     /// unsafe {
1915     ///     assert_eq!(y.read_unaligned(), 12);
1916     /// }
1917     /// ```
1918     #[stable(feature = "pointer_methods", since = "1.26.0")]
1919     #[inline]
1920     pub unsafe fn read_unaligned(self) -> T
1921         where T: Sized,
1922     {
1923         read_unaligned(self)
1924     }
1925
1926     /// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
1927     /// and destination may overlap.
1928     ///
1929     /// NOTE: this has the *same* argument order as `ptr::copy`.
1930     ///
1931     /// This is semantically equivalent to C's `memmove`.
1932     ///
1933     /// # Safety
1934     ///
1935     /// Care must be taken with the ownership of `self` and `dest`.
1936     /// This method semantically moves the values of `self` into `dest`.
1937     /// However it does not drop the contents of `self`, or prevent the contents
1938     /// of `dest` from being dropped or used.
1939     ///
1940     /// # Examples
1941     ///
1942     /// Efficiently create a Rust vector from an unsafe buffer:
1943     ///
1944     /// ```
1945     /// # #[allow(dead_code)]
1946     /// unsafe fn from_buf_raw<T: Copy>(ptr: *const T, elts: usize) -> Vec<T> {
1947     ///     let mut dst = Vec::with_capacity(elts);
1948     ///     dst.set_len(elts);
1949     ///     ptr.copy_to(dst.as_mut_ptr(), elts);
1950     ///     dst
1951     /// }
1952     /// ```
1953     #[stable(feature = "pointer_methods", since = "1.26.0")]
1954     #[inline]
1955     pub unsafe fn copy_to(self, dest: *mut T, count: usize)
1956         where T: Sized,
1957     {
1958         copy(self, dest, count)
1959     }
1960
1961     /// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
1962     /// and destination may *not* overlap.
1963     ///
1964     /// NOTE: this has the *same* argument order as `ptr::copy_nonoverlapping`.
1965     ///
1966     /// `copy_nonoverlapping` is semantically equivalent to C's `memcpy`.
1967     ///
1968     /// # Safety
1969     ///
1970     /// Beyond requiring that the program must be allowed to access both regions
1971     /// of memory, it is Undefined Behavior for source and destination to
1972     /// overlap. Care must also be taken with the ownership of `self` and
1973     /// `self`. This method semantically moves the values of `self` into `dest`.
1974     /// However it does not drop the contents of `dest`, or prevent the contents
1975     /// of `self` from being dropped or used.
1976     ///
1977     /// # Examples
1978     ///
1979     /// Efficiently create a Rust vector from an unsafe buffer:
1980     ///
1981     /// ```
1982     /// # #[allow(dead_code)]
1983     /// unsafe fn from_buf_raw<T: Copy>(ptr: *const T, elts: usize) -> Vec<T> {
1984     ///     let mut dst = Vec::with_capacity(elts);
1985     ///     dst.set_len(elts);
1986     ///     ptr.copy_to_nonoverlapping(dst.as_mut_ptr(), elts);
1987     ///     dst
1988     /// }
1989     /// ```
1990     #[stable(feature = "pointer_methods", since = "1.26.0")]
1991     #[inline]
1992     pub unsafe fn copy_to_nonoverlapping(self, dest: *mut T, count: usize)
1993         where T: Sized,
1994     {
1995         copy_nonoverlapping(self, dest, count)
1996     }
1997
1998     /// Copies `count * size_of<T>` bytes from `src` to `self`. The source
1999     /// and destination may overlap.
2000     ///
2001     /// NOTE: this has the *opposite* argument order of `ptr::copy`.
2002     ///
2003     /// This is semantically equivalent to C's `memmove`.
2004     ///
2005     /// # Safety
2006     ///
2007     /// Care must be taken with the ownership of `src` and `self`.
2008     /// This method semantically moves the values of `src` into `self`.
2009     /// However it does not drop the contents of `self`, or prevent the contents
2010     /// of `src` from being dropped or used.
2011     ///
2012     /// # Examples
2013     ///
2014     /// Efficiently create a Rust vector from an unsafe buffer:
2015     ///
2016     /// ```
2017     /// # #[allow(dead_code)]
2018     /// unsafe fn from_buf_raw<T: Copy>(ptr: *const T, elts: usize) -> Vec<T> {
2019     ///     let mut dst: Vec<T> = Vec::with_capacity(elts);
2020     ///     dst.set_len(elts);
2021     ///     dst.as_mut_ptr().copy_from(ptr, elts);
2022     ///     dst
2023     /// }
2024     /// ```
2025     #[stable(feature = "pointer_methods", since = "1.26.0")]
2026     #[inline]
2027     pub unsafe fn copy_from(self, src: *const T, count: usize)
2028         where T: Sized,
2029     {
2030         copy(src, self, count)
2031     }
2032
2033     /// Copies `count * size_of<T>` bytes from `src` to `self`. The source
2034     /// and destination may *not* overlap.
2035     ///
2036     /// NOTE: this has the *opposite* argument order of `ptr::copy_nonoverlapping`.
2037     ///
2038     /// `copy_nonoverlapping` is semantically equivalent to C's `memcpy`.
2039     ///
2040     /// # Safety
2041     ///
2042     /// Beyond requiring that the program must be allowed to access both regions
2043     /// of memory, it is Undefined Behavior for source and destination to
2044     /// overlap. Care must also be taken with the ownership of `src` and
2045     /// `self`. This method semantically moves the values of `src` into `self`.
2046     /// However it does not drop the contents of `self`, or prevent the contents
2047     /// of `src` from being dropped or used.
2048     ///
2049     /// # Examples
2050     ///
2051     /// Efficiently create a Rust vector from an unsafe buffer:
2052     ///
2053     /// ```
2054     /// # #[allow(dead_code)]
2055     /// unsafe fn from_buf_raw<T: Copy>(ptr: *const T, elts: usize) -> Vec<T> {
2056     ///     let mut dst: Vec<T> = Vec::with_capacity(elts);
2057     ///     dst.set_len(elts);
2058     ///     dst.as_mut_ptr().copy_from_nonoverlapping(ptr, elts);
2059     ///     dst
2060     /// }
2061     /// ```
2062     #[stable(feature = "pointer_methods", since = "1.26.0")]
2063     #[inline]
2064     pub unsafe fn copy_from_nonoverlapping(self, src: *const T, count: usize)
2065         where T: Sized,
2066     {
2067         copy_nonoverlapping(src, self, count)
2068     }
2069
2070     /// Executes the destructor (if any) of the pointed-to value.
2071     ///
2072     /// This has two use cases:
2073     ///
2074     /// * It is *required* to use `drop_in_place` to drop unsized types like
2075     ///   trait objects, because they can't be read out onto the stack and
2076     ///   dropped normally.
2077     ///
2078     /// * It is friendlier to the optimizer to do this over `ptr::read` when
2079     ///   dropping manually allocated memory (e.g. when writing Box/Rc/Vec),
2080     ///   as the compiler doesn't need to prove that it's sound to elide the
2081     ///   copy.
2082     ///
2083     /// # Safety
2084     ///
2085     /// This has all the same safety problems as `ptr::read` with respect to
2086     /// invalid pointers, types, and double drops.
2087     #[stable(feature = "pointer_methods", since = "1.26.0")]
2088     #[inline]
2089     pub unsafe fn drop_in_place(self) {
2090         drop_in_place(self)
2091     }
2092
2093     /// Overwrites a memory location with the given value without reading or
2094     /// dropping the old value.
2095     ///
2096     /// # Safety
2097     ///
2098     /// This operation is marked unsafe because it writes through a raw pointer.
2099     ///
2100     /// It does not drop the contents of `self`. This is safe, but it could leak
2101     /// allocations or resources, so care must be taken not to overwrite an object
2102     /// that should be dropped.
2103     ///
2104     /// Additionally, it does not drop `val`. Semantically, `val` is moved into the
2105     /// location pointed to by `self`.
2106     ///
2107     /// This is appropriate for initializing uninitialized memory, or overwriting
2108     /// memory that has previously been `read` from.
2109     ///
2110     /// The pointer must be aligned; use `write_unaligned` if that is not the case.
2111     ///
2112     /// # Examples
2113     ///
2114     /// Basic usage:
2115     ///
2116     /// ```
2117     /// let mut x = 0;
2118     /// let y = &mut x as *mut i32;
2119     /// let z = 12;
2120     ///
2121     /// unsafe {
2122     ///     y.write(z);
2123     ///     assert_eq!(y.read(), 12);
2124     /// }
2125     /// ```
2126     #[stable(feature = "pointer_methods", since = "1.26.0")]
2127     #[inline]
2128     pub unsafe fn write(self, val: T)
2129         where T: Sized,
2130     {
2131         write(self, val)
2132     }
2133
2134     /// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
2135     /// bytes of memory starting at `self` to `val`.
2136     ///
2137     /// # Examples
2138     ///
2139     /// ```
2140     /// let mut vec = vec![0; 4];
2141     /// unsafe {
2142     ///     let vec_ptr = vec.as_mut_ptr();
2143     ///     vec_ptr.write_bytes(b'a', 2);
2144     /// }
2145     /// assert_eq!(vec, [b'a', b'a', 0, 0]);
2146     /// ```
2147     #[stable(feature = "pointer_methods", since = "1.26.0")]
2148     #[inline]
2149     pub unsafe fn write_bytes(self, val: u8, count: usize)
2150         where T: Sized,
2151     {
2152         write_bytes(self, val, count)
2153     }
2154
2155     /// Performs a volatile write of a memory location with the given value without
2156     /// reading or dropping the old value.
2157     ///
2158     /// Volatile operations are intended to act on I/O memory, and are guaranteed
2159     /// to not be elided or reordered by the compiler across other volatile
2160     /// operations.
2161     ///
2162     /// # Notes
2163     ///
2164     /// Rust does not currently have a rigorously and formally defined memory model,
2165     /// so the precise semantics of what "volatile" means here is subject to change
2166     /// over time. That being said, the semantics will almost always end up pretty
2167     /// similar to [C11's definition of volatile][c11].
2168     ///
2169     /// The compiler shouldn't change the relative order or number of volatile
2170     /// memory operations. However, volatile memory operations on zero-sized types
2171     /// (e.g. if a zero-sized type is passed to `write_volatile`) are no-ops
2172     /// and may be ignored.
2173     ///
2174     /// [c11]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
2175     ///
2176     /// # Safety
2177     ///
2178     /// This operation is marked unsafe because it accepts a raw pointer.
2179     ///
2180     /// It does not drop the contents of `self`. This is safe, but it could leak
2181     /// allocations or resources, so care must be taken not to overwrite an object
2182     /// that should be dropped.
2183     ///
2184     /// This is appropriate for initializing uninitialized memory, or overwriting
2185     /// memory that has previously been `read` from.
2186     ///
2187     /// # Examples
2188     ///
2189     /// Basic usage:
2190     ///
2191     /// ```
2192     /// let mut x = 0;
2193     /// let y = &mut x as *mut i32;
2194     /// let z = 12;
2195     ///
2196     /// unsafe {
2197     ///     y.write_volatile(z);
2198     ///     assert_eq!(y.read_volatile(), 12);
2199     /// }
2200     /// ```
2201     #[stable(feature = "pointer_methods", since = "1.26.0")]
2202     #[inline]
2203     pub unsafe fn write_volatile(self, val: T)
2204         where T: Sized,
2205     {
2206         write_volatile(self, val)
2207     }
2208
2209     /// Overwrites a memory location with the given value without reading or
2210     /// dropping the old value.
2211     ///
2212     /// Unlike `write`, the pointer may be unaligned.
2213     ///
2214     /// # Safety
2215     ///
2216     /// This operation is marked unsafe because it writes through a raw pointer.
2217     ///
2218     /// It does not drop the contents of `self`. This is safe, but it could leak
2219     /// allocations or resources, so care must be taken not to overwrite an object
2220     /// that should be dropped.
2221     ///
2222     /// Additionally, it does not drop `self`. Semantically, `self` is moved into the
2223     /// location pointed to by `val`.
2224     ///
2225     /// This is appropriate for initializing uninitialized memory, or overwriting
2226     /// memory that has previously been `read` from.
2227     ///
2228     /// # Examples
2229     ///
2230     /// Basic usage:
2231     ///
2232     /// ```
2233     /// let mut x = 0;
2234     /// let y = &mut x as *mut i32;
2235     /// let z = 12;
2236     ///
2237     /// unsafe {
2238     ///     y.write_unaligned(z);
2239     ///     assert_eq!(y.read_unaligned(), 12);
2240     /// }
2241     /// ```
2242     #[stable(feature = "pointer_methods", since = "1.26.0")]
2243     #[inline]
2244     pub unsafe fn write_unaligned(self, val: T)
2245         where T: Sized,
2246     {
2247         write_unaligned(self, val)
2248     }
2249
2250     /// Replaces the value at `self` with `src`, returning the old
2251     /// value, without dropping either.
2252     ///
2253     /// # Safety
2254     ///
2255     /// This is only unsafe because it accepts a raw pointer.
2256     /// Otherwise, this operation is identical to `mem::replace`.
2257     #[stable(feature = "pointer_methods", since = "1.26.0")]
2258     #[inline]
2259     pub unsafe fn replace(self, src: T) -> T
2260         where T: Sized,
2261     {
2262         replace(self, src)
2263     }
2264
2265     /// Swaps the values at two mutable locations of the same type, without
2266     /// deinitializing either. They may overlap, unlike `mem::swap` which is
2267     /// otherwise equivalent.
2268     ///
2269     /// # Safety
2270     ///
2271     /// This function copies the memory through the raw pointers passed to it
2272     /// as arguments.
2273     ///
2274     /// Ensure that these pointers are valid before calling `swap`.
2275     #[stable(feature = "pointer_methods", since = "1.26.0")]
2276     #[inline]
2277     pub unsafe fn swap(self, with: *mut T)
2278         where T: Sized,
2279     {
2280         swap(self, with)
2281     }
2282 }
2283
2284 // Equality for pointers
2285 #[stable(feature = "rust1", since = "1.0.0")]
2286 impl<T: ?Sized> PartialEq for *const T {
2287     #[inline]
2288     fn eq(&self, other: &*const T) -> bool { *self == *other }
2289 }
2290
2291 #[stable(feature = "rust1", since = "1.0.0")]
2292 impl<T: ?Sized> Eq for *const T {}
2293
2294 #[stable(feature = "rust1", since = "1.0.0")]
2295 impl<T: ?Sized> PartialEq for *mut T {
2296     #[inline]
2297     fn eq(&self, other: &*mut T) -> bool { *self == *other }
2298 }
2299
2300 #[stable(feature = "rust1", since = "1.0.0")]
2301 impl<T: ?Sized> Eq for *mut T {}
2302
2303 /// Compare raw pointers for equality.
2304 ///
2305 /// This is the same as using the `==` operator, but less generic:
2306 /// the arguments have to be `*const T` raw pointers,
2307 /// not anything that implements `PartialEq`.
2308 ///
2309 /// This can be used to compare `&T` references (which coerce to `*const T` implicitly)
2310 /// by their address rather than comparing the values they point to
2311 /// (which is what the `PartialEq for &T` implementation does).
2312 ///
2313 /// # Examples
2314 ///
2315 /// ```
2316 /// use std::ptr;
2317 ///
2318 /// let five = 5;
2319 /// let other_five = 5;
2320 /// let five_ref = &five;
2321 /// let same_five_ref = &five;
2322 /// let other_five_ref = &other_five;
2323 ///
2324 /// assert!(five_ref == same_five_ref);
2325 /// assert!(five_ref == other_five_ref);
2326 ///
2327 /// assert!(ptr::eq(five_ref, same_five_ref));
2328 /// assert!(!ptr::eq(five_ref, other_five_ref));
2329 /// ```
2330 #[stable(feature = "ptr_eq", since = "1.17.0")]
2331 #[inline]
2332 pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
2333     a == b
2334 }
2335
2336 // Impls for function pointers
2337 macro_rules! fnptr_impls_safety_abi {
2338     ($FnTy: ty, $($Arg: ident),*) => {
2339         #[stable(feature = "fnptr_impls", since = "1.4.0")]
2340         impl<Ret, $($Arg),*> PartialEq for $FnTy {
2341             #[inline]
2342             fn eq(&self, other: &Self) -> bool {
2343                 *self as usize == *other as usize
2344             }
2345         }
2346
2347         #[stable(feature = "fnptr_impls", since = "1.4.0")]
2348         impl<Ret, $($Arg),*> Eq for $FnTy {}
2349
2350         #[stable(feature = "fnptr_impls", since = "1.4.0")]
2351         impl<Ret, $($Arg),*> PartialOrd for $FnTy {
2352             #[inline]
2353             fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
2354                 (*self as usize).partial_cmp(&(*other as usize))
2355             }
2356         }
2357
2358         #[stable(feature = "fnptr_impls", since = "1.4.0")]
2359         impl<Ret, $($Arg),*> Ord for $FnTy {
2360             #[inline]
2361             fn cmp(&self, other: &Self) -> Ordering {
2362                 (*self as usize).cmp(&(*other as usize))
2363             }
2364         }
2365
2366         #[stable(feature = "fnptr_impls", since = "1.4.0")]
2367         impl<Ret, $($Arg),*> hash::Hash for $FnTy {
2368             fn hash<HH: hash::Hasher>(&self, state: &mut HH) {
2369                 state.write_usize(*self as usize)
2370             }
2371         }
2372
2373         #[stable(feature = "fnptr_impls", since = "1.4.0")]
2374         impl<Ret, $($Arg),*> fmt::Pointer for $FnTy {
2375             fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2376                 fmt::Pointer::fmt(&(*self as *const ()), f)
2377             }
2378         }
2379
2380         #[stable(feature = "fnptr_impls", since = "1.4.0")]
2381         impl<Ret, $($Arg),*> fmt::Debug for $FnTy {
2382             fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2383                 fmt::Pointer::fmt(&(*self as *const ()), f)
2384             }
2385         }
2386     }
2387 }
2388
2389 macro_rules! fnptr_impls_args {
2390     ($($Arg: ident),+) => {
2391         fnptr_impls_safety_abi! { extern "Rust" fn($($Arg),*) -> Ret, $($Arg),* }
2392         fnptr_impls_safety_abi! { extern "C" fn($($Arg),*) -> Ret, $($Arg),* }
2393         fnptr_impls_safety_abi! { extern "C" fn($($Arg),* , ...) -> Ret, $($Arg),* }
2394         fnptr_impls_safety_abi! { unsafe extern "Rust" fn($($Arg),*) -> Ret, $($Arg),* }
2395         fnptr_impls_safety_abi! { unsafe extern "C" fn($($Arg),*) -> Ret, $($Arg),* }
2396         fnptr_impls_safety_abi! { unsafe extern "C" fn($($Arg),* , ...) -> Ret, $($Arg),* }
2397     };
2398     () => {
2399         // No variadic functions with 0 parameters
2400         fnptr_impls_safety_abi! { extern "Rust" fn() -> Ret, }
2401         fnptr_impls_safety_abi! { extern "C" fn() -> Ret, }
2402         fnptr_impls_safety_abi! { unsafe extern "Rust" fn() -> Ret, }
2403         fnptr_impls_safety_abi! { unsafe extern "C" fn() -> Ret, }
2404     };
2405 }
2406
2407 fnptr_impls_args! { }
2408 fnptr_impls_args! { A }
2409 fnptr_impls_args! { A, B }
2410 fnptr_impls_args! { A, B, C }
2411 fnptr_impls_args! { A, B, C, D }
2412 fnptr_impls_args! { A, B, C, D, E }
2413 fnptr_impls_args! { A, B, C, D, E, F }
2414 fnptr_impls_args! { A, B, C, D, E, F, G }
2415 fnptr_impls_args! { A, B, C, D, E, F, G, H }
2416 fnptr_impls_args! { A, B, C, D, E, F, G, H, I }
2417 fnptr_impls_args! { A, B, C, D, E, F, G, H, I, J }
2418 fnptr_impls_args! { A, B, C, D, E, F, G, H, I, J, K }
2419 fnptr_impls_args! { A, B, C, D, E, F, G, H, I, J, K, L }
2420
2421 // Comparison for pointers
2422 #[stable(feature = "rust1", since = "1.0.0")]
2423 impl<T: ?Sized> Ord for *const T {
2424     #[inline]
2425     fn cmp(&self, other: &*const T) -> Ordering {
2426         if self < other {
2427             Less
2428         } else if self == other {
2429             Equal
2430         } else {
2431             Greater
2432         }
2433     }
2434 }
2435
2436 #[stable(feature = "rust1", since = "1.0.0")]
2437 impl<T: ?Sized> PartialOrd for *const T {
2438     #[inline]
2439     fn partial_cmp(&self, other: &*const T) -> Option<Ordering> {
2440         Some(self.cmp(other))
2441     }
2442
2443     #[inline]
2444     fn lt(&self, other: &*const T) -> bool { *self < *other }
2445
2446     #[inline]
2447     fn le(&self, other: &*const T) -> bool { *self <= *other }
2448
2449     #[inline]
2450     fn gt(&self, other: &*const T) -> bool { *self > *other }
2451
2452     #[inline]
2453     fn ge(&self, other: &*const T) -> bool { *self >= *other }
2454 }
2455
2456 #[stable(feature = "rust1", since = "1.0.0")]
2457 impl<T: ?Sized> Ord for *mut T {
2458     #[inline]
2459     fn cmp(&self, other: &*mut T) -> Ordering {
2460         if self < other {
2461             Less
2462         } else if self == other {
2463             Equal
2464         } else {
2465             Greater
2466         }
2467     }
2468 }
2469
2470 #[stable(feature = "rust1", since = "1.0.0")]
2471 impl<T: ?Sized> PartialOrd for *mut T {
2472     #[inline]
2473     fn partial_cmp(&self, other: &*mut T) -> Option<Ordering> {
2474         Some(self.cmp(other))
2475     }
2476
2477     #[inline]
2478     fn lt(&self, other: &*mut T) -> bool { *self < *other }
2479
2480     #[inline]
2481     fn le(&self, other: &*mut T) -> bool { *self <= *other }
2482
2483     #[inline]
2484     fn gt(&self, other: &*mut T) -> bool { *self > *other }
2485
2486     #[inline]
2487     fn ge(&self, other: &*mut T) -> bool { *self >= *other }
2488 }
2489
2490 /// A wrapper around a raw non-null `*mut T` that indicates that the possessor
2491 /// of this wrapper owns the referent. Useful for building abstractions like
2492 /// `Box<T>`, `Vec<T>`, `String`, and `HashMap<K, V>`.
2493 ///
2494 /// Unlike `*mut T`, `Unique<T>` behaves "as if" it were an instance of `T`.
2495 /// It implements `Send`/`Sync` if `T` is `Send`/`Sync`. It also implies
2496 /// the kind of strong aliasing guarantees an instance of `T` can expect:
2497 /// the referent of the pointer should not be modified without a unique path to
2498 /// its owning Unique.
2499 ///
2500 /// If you're uncertain of whether it's correct to use `Unique` for your purposes,
2501 /// consider using `NonNull`, which has weaker semantics.
2502 ///
2503 /// Unlike `*mut T`, the pointer must always be non-null, even if the pointer
2504 /// is never dereferenced. This is so that enums may use this forbidden value
2505 /// as a discriminant -- `Option<Unique<T>>` has the same size as `Unique<T>`.
2506 /// However the pointer may still dangle if it isn't dereferenced.
2507 ///
2508 /// Unlike `*mut T`, `Unique<T>` is covariant over `T`. This should always be correct
2509 /// for any type which upholds Unique's aliasing requirements.
2510 #[unstable(feature = "ptr_internals", issue = "0",
2511            reason = "use NonNull instead and consider PhantomData<T> \
2512                      (if you also use #[may_dangle]), Send, and/or Sync")]
2513 pub struct Unique<T: ?Sized> {
2514     pointer: NonZero<*const T>,
2515     // NOTE: this marker has no consequences for variance, but is necessary
2516     // for dropck to understand that we logically own a `T`.
2517     //
2518     // For details, see:
2519     // https://github.com/rust-lang/rfcs/blob/master/text/0769-sound-generic-drop.md#phantom-data
2520     _marker: PhantomData<T>,
2521 }
2522
2523 #[unstable(feature = "ptr_internals", issue = "0")]
2524 impl<T: ?Sized> fmt::Debug for Unique<T> {
2525     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2526         fmt::Pointer::fmt(&self.as_ptr(), f)
2527     }
2528 }
2529
2530 /// `Unique` pointers are `Send` if `T` is `Send` because the data they
2531 /// reference is unaliased. Note that this aliasing invariant is
2532 /// unenforced by the type system; the abstraction using the
2533 /// `Unique` must enforce it.
2534 #[unstable(feature = "ptr_internals", issue = "0")]
2535 unsafe impl<T: Send + ?Sized> Send for Unique<T> { }
2536
2537 /// `Unique` pointers are `Sync` if `T` is `Sync` because the data they
2538 /// reference is unaliased. Note that this aliasing invariant is
2539 /// unenforced by the type system; the abstraction using the
2540 /// `Unique` must enforce it.
2541 #[unstable(feature = "ptr_internals", issue = "0")]
2542 unsafe impl<T: Sync + ?Sized> Sync for Unique<T> { }
2543
2544 #[unstable(feature = "ptr_internals", issue = "0")]
2545 impl<T: Sized> Unique<T> {
2546     /// Creates a new `Unique` that is dangling, but well-aligned.
2547     ///
2548     /// This is useful for initializing types which lazily allocate, like
2549     /// `Vec::new` does.
2550     // FIXME: rename to dangling() to match NonNull?
2551     pub fn empty() -> Self {
2552         unsafe {
2553             let ptr = mem::align_of::<T>() as *mut T;
2554             Unique::new_unchecked(ptr)
2555         }
2556     }
2557 }
2558
2559 #[unstable(feature = "ptr_internals", issue = "0")]
2560 impl<T: ?Sized> Unique<T> {
2561     /// Creates a new `Unique`.
2562     ///
2563     /// # Safety
2564     ///
2565     /// `ptr` must be non-null.
2566     pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
2567         Unique { pointer: NonZero::new_unchecked(ptr), _marker: PhantomData }
2568     }
2569
2570     /// Creates a new `Unique` if `ptr` is non-null.
2571     pub fn new(ptr: *mut T) -> Option<Self> {
2572         NonZero::new(ptr as *const T).map(|nz| Unique { pointer: nz, _marker: PhantomData })
2573     }
2574
2575     /// Acquires the underlying `*mut` pointer.
2576     pub fn as_ptr(self) -> *mut T {
2577         self.pointer.get() as *mut T
2578     }
2579
2580     /// Dereferences the content.
2581     ///
2582     /// The resulting lifetime is bound to self so this behaves "as if"
2583     /// it were actually an instance of T that is getting borrowed. If a longer
2584     /// (unbound) lifetime is needed, use `&*my_ptr.as_ptr()`.
2585     pub unsafe fn as_ref(&self) -> &T {
2586         &*self.as_ptr()
2587     }
2588
2589     /// Mutably dereferences the content.
2590     ///
2591     /// The resulting lifetime is bound to self so this behaves "as if"
2592     /// it were actually an instance of T that is getting borrowed. If a longer
2593     /// (unbound) lifetime is needed, use `&mut *my_ptr.as_ptr()`.
2594     pub unsafe fn as_mut(&mut self) -> &mut T {
2595         &mut *self.as_ptr()
2596     }
2597 }
2598
2599 #[unstable(feature = "ptr_internals", issue = "0")]
2600 impl<T: ?Sized> Clone for Unique<T> {
2601     fn clone(&self) -> Self {
2602         *self
2603     }
2604 }
2605
2606 #[unstable(feature = "ptr_internals", issue = "0")]
2607 impl<T: ?Sized> Copy for Unique<T> { }
2608
2609 #[unstable(feature = "ptr_internals", issue = "0")]
2610 impl<T: ?Sized, U: ?Sized> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> { }
2611
2612 #[unstable(feature = "ptr_internals", issue = "0")]
2613 impl<T: ?Sized> fmt::Pointer for Unique<T> {
2614     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2615         fmt::Pointer::fmt(&self.as_ptr(), f)
2616     }
2617 }
2618
2619 #[unstable(feature = "ptr_internals", issue = "0")]
2620 impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> {
2621     fn from(reference: &'a mut T) -> Self {
2622         Unique { pointer: NonZero::from(reference), _marker: PhantomData }
2623     }
2624 }
2625
2626 #[unstable(feature = "ptr_internals", issue = "0")]
2627 impl<'a, T: ?Sized> From<&'a T> for Unique<T> {
2628     fn from(reference: &'a T) -> Self {
2629         Unique { pointer: NonZero::from(reference), _marker: PhantomData }
2630     }
2631 }
2632
2633 #[unstable(feature = "ptr_internals", issue = "0")]
2634 impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
2635     fn from(p: NonNull<T>) -> Self {
2636         Unique { pointer: p.pointer, _marker: PhantomData }
2637     }
2638 }
2639
2640 /// Previous name of `NonNull`.
2641 #[rustc_deprecated(since = "1.25.0", reason = "renamed to `NonNull`")]
2642 #[unstable(feature = "shared", issue = "27730")]
2643 pub type Shared<T> = NonNull<T>;
2644
2645 /// `*mut T` but non-zero and covariant.
2646 ///
2647 /// This is often the correct thing to use when building data structures using
2648 /// raw pointers, but is ultimately more dangerous to use because of its additional
2649 /// properties. If you're not sure if you should use `NonNull<T>`, just use `*mut T`!
2650 ///
2651 /// Unlike `*mut T`, the pointer must always be non-null, even if the pointer
2652 /// is never dereferenced. This is so that enums may use this forbidden value
2653 /// as a discriminant -- `Option<NonNull<T>>` has the same size as `NonNull<T>`.
2654 /// However the pointer may still dangle if it isn't dereferenced.
2655 ///
2656 /// Unlike `*mut T`, `NonNull<T>` is covariant over `T`. If this is incorrect
2657 /// for your use case, you should include some PhantomData in your type to
2658 /// provide invariance, such as `PhantomData<Cell<T>>` or `PhantomData<&'a mut T>`.
2659 /// Usually this won't be necessary; covariance is correct for most safe abstractions,
2660 /// such as Box, Rc, Arc, Vec, and LinkedList. This is the case because they
2661 /// provide a public API that follows the normal shared XOR mutable rules of Rust.
2662 #[stable(feature = "nonnull", since = "1.25.0")]
2663 pub struct NonNull<T: ?Sized> {
2664     pointer: NonZero<*const T>,
2665 }
2666
2667 /// `NonNull` pointers are not `Send` because the data they reference may be aliased.
2668 // NB: This impl is unnecessary, but should provide better error messages.
2669 #[stable(feature = "nonnull", since = "1.25.0")]
2670 impl<T: ?Sized> !Send for NonNull<T> { }
2671
2672 /// `NonNull` pointers are not `Sync` because the data they reference may be aliased.
2673 // NB: This impl is unnecessary, but should provide better error messages.
2674 #[stable(feature = "nonnull", since = "1.25.0")]
2675 impl<T: ?Sized> !Sync for NonNull<T> { }
2676
2677 impl<T: Sized> NonNull<T> {
2678     /// Creates a new `NonNull` that is dangling, but well-aligned.
2679     ///
2680     /// This is useful for initializing types which lazily allocate, like
2681     /// `Vec::new` does.
2682     #[stable(feature = "nonnull", since = "1.25.0")]
2683     pub fn dangling() -> Self {
2684         unsafe {
2685             let ptr = mem::align_of::<T>() as *mut T;
2686             NonNull::new_unchecked(ptr)
2687         }
2688     }
2689 }
2690
2691 impl<T: ?Sized> NonNull<T> {
2692     /// Creates a new `NonNull`.
2693     ///
2694     /// # Safety
2695     ///
2696     /// `ptr` must be non-null.
2697     #[stable(feature = "nonnull", since = "1.25.0")]
2698     pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
2699         NonNull { pointer: NonZero::new_unchecked(ptr) }
2700     }
2701
2702     /// Creates a new `NonNull` if `ptr` is non-null.
2703     #[stable(feature = "nonnull", since = "1.25.0")]
2704     pub fn new(ptr: *mut T) -> Option<Self> {
2705         NonZero::new(ptr as *const T).map(|nz| NonNull { pointer: nz })
2706     }
2707
2708     /// Acquires the underlying `*mut` pointer.
2709     #[stable(feature = "nonnull", since = "1.25.0")]
2710     pub fn as_ptr(self) -> *mut T {
2711         self.pointer.get() as *mut T
2712     }
2713
2714     /// Dereferences the content.
2715     ///
2716     /// The resulting lifetime is bound to self so this behaves "as if"
2717     /// it were actually an instance of T that is getting borrowed. If a longer
2718     /// (unbound) lifetime is needed, use `&*my_ptr.as_ptr()`.
2719     #[stable(feature = "nonnull", since = "1.25.0")]
2720     pub unsafe fn as_ref(&self) -> &T {
2721         &*self.as_ptr()
2722     }
2723
2724     /// Mutably dereferences the content.
2725     ///
2726     /// The resulting lifetime is bound to self so this behaves "as if"
2727     /// it were actually an instance of T that is getting borrowed. If a longer
2728     /// (unbound) lifetime is needed, use `&mut *my_ptr.as_ptr()`.
2729     #[stable(feature = "nonnull", since = "1.25.0")]
2730     pub unsafe fn as_mut(&mut self) -> &mut T {
2731         &mut *self.as_ptr()
2732     }
2733
2734     /// Cast to a pointer of another type
2735     #[unstable(feature = "nonnull_cast", issue = "47653")]
2736     pub fn cast<U>(self) -> NonNull<U> {
2737         unsafe {
2738             NonNull::new_unchecked(self.as_ptr() as *mut U)
2739         }
2740     }
2741 }
2742
2743 #[stable(feature = "nonnull", since = "1.25.0")]
2744 impl<T: ?Sized> Clone for NonNull<T> {
2745     fn clone(&self) -> Self {
2746         *self
2747     }
2748 }
2749
2750 #[stable(feature = "nonnull", since = "1.25.0")]
2751 impl<T: ?Sized> Copy for NonNull<T> { }
2752
2753 #[unstable(feature = "coerce_unsized", issue = "27732")]
2754 impl<T: ?Sized, U: ?Sized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> { }
2755
2756 #[stable(feature = "nonnull", since = "1.25.0")]
2757 impl<T: ?Sized> fmt::Debug for NonNull<T> {
2758     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2759         fmt::Pointer::fmt(&self.as_ptr(), f)
2760     }
2761 }
2762
2763 #[stable(feature = "nonnull", since = "1.25.0")]
2764 impl<T: ?Sized> fmt::Pointer for NonNull<T> {
2765     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2766         fmt::Pointer::fmt(&self.as_ptr(), f)
2767     }
2768 }
2769
2770 #[stable(feature = "nonnull", since = "1.25.0")]
2771 impl<T: ?Sized> Eq for NonNull<T> {}
2772
2773 #[stable(feature = "nonnull", since = "1.25.0")]
2774 impl<T: ?Sized> PartialEq for NonNull<T> {
2775     fn eq(&self, other: &Self) -> bool {
2776         self.as_ptr() == other.as_ptr()
2777     }
2778 }
2779
2780 #[stable(feature = "nonnull", since = "1.25.0")]
2781 impl<T: ?Sized> Ord for NonNull<T> {
2782     fn cmp(&self, other: &Self) -> Ordering {
2783         self.as_ptr().cmp(&other.as_ptr())
2784     }
2785 }
2786
2787 #[stable(feature = "nonnull", since = "1.25.0")]
2788 impl<T: ?Sized> PartialOrd for NonNull<T> {
2789     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
2790         self.as_ptr().partial_cmp(&other.as_ptr())
2791     }
2792 }
2793
2794 #[stable(feature = "nonnull", since = "1.25.0")]
2795 impl<T: ?Sized> hash::Hash for NonNull<T> {
2796     fn hash<H: hash::Hasher>(&self, state: &mut H) {
2797         self.as_ptr().hash(state)
2798     }
2799 }
2800
2801 #[unstable(feature = "ptr_internals", issue = "0")]
2802 impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
2803     fn from(unique: Unique<T>) -> Self {
2804         NonNull { pointer: unique.pointer }
2805     }
2806 }
2807
2808 #[stable(feature = "nonnull", since = "1.25.0")]
2809 impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
2810     fn from(reference: &'a mut T) -> Self {
2811         NonNull { pointer: NonZero::from(reference) }
2812     }
2813 }
2814
2815 #[stable(feature = "nonnull", since = "1.25.0")]
2816 impl<'a, T: ?Sized> From<&'a T> for NonNull<T> {
2817     fn from(reference: &'a T) -> Self {
2818         NonNull { pointer: NonZero::from(reference) }
2819     }
2820 }