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