]> git.lizzy.rs Git - rust.git/blob - library/core/src/ptr/mut_ptr.rs
a6424041542d9054c79e060f23772de186088cc3
[rust.git] / library / core / src / ptr / mut_ptr.rs
1 use super::*;
2 use crate::cmp::Ordering::{self, Equal, Greater, Less};
3 use crate::intrinsics;
4 use crate::slice::{self, SliceIndex};
5
6 #[lang = "mut_ptr"]
7 impl<T: ?Sized> *mut T {
8     /// Returns `true` if the pointer is null.
9     ///
10     /// Note that unsized types have many possible null pointers, as only the
11     /// raw data pointer is considered, not their length, vtable, etc.
12     /// Therefore, two pointers that are null may still not compare equal to
13     /// each other.
14     ///
15     /// ## Behavior during const evaluation
16     ///
17     /// When this function is used during const evaluation, it may return `false` for pointers
18     /// that turn out to be null at runtime. Specifically, when a pointer to some memory
19     /// is offset beyond its bounds in such a way that the resulting pointer is null,
20     /// the function will still return `false`. There is no way for CTFE to know
21     /// the absolute position of that memory, so we cannot tell if the pointer is
22     /// null or not.
23     ///
24     /// # Examples
25     ///
26     /// Basic usage:
27     ///
28     /// ```
29     /// let mut s = [1, 2, 3];
30     /// let ptr: *mut u32 = s.as_mut_ptr();
31     /// assert!(!ptr.is_null());
32     /// ```
33     #[stable(feature = "rust1", since = "1.0.0")]
34     #[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
35     #[inline]
36     pub const fn is_null(self) -> bool {
37         // Compare via a cast to a thin pointer, so fat pointers are only
38         // considering their "data" part for null-ness.
39         (self as *mut u8).guaranteed_eq(null_mut())
40     }
41
42     /// Casts to a pointer of another type.
43     #[stable(feature = "ptr_cast", since = "1.38.0")]
44     #[rustc_const_stable(feature = "const_ptr_cast", since = "1.38.0")]
45     #[inline(always)]
46     pub const fn cast<U>(self) -> *mut U {
47         self as _
48     }
49
50     /// Decompose a (possibly wide) pointer into is address and metadata components.
51     ///
52     /// The pointer can be later reconstructed with [`from_raw_parts_mut`].
53     #[unstable(feature = "ptr_metadata", issue = "81513")]
54     #[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
55     #[inline]
56     pub const fn to_raw_parts(self) -> (*mut (), <T as super::Pointee>::Metadata) {
57         (self.cast(), super::metadata(self))
58     }
59
60     /// Returns `None` if the pointer is null, or else returns a shared reference to
61     /// the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_ref`]
62     /// must be used instead.
63     ///
64     /// For the mutable counterpart see [`as_mut`].
65     ///
66     /// [`as_uninit_ref`]: #method.as_uninit_ref-1
67     /// [`as_mut`]: #method.as_mut
68     ///
69     /// # Safety
70     ///
71     /// When calling this method, you have to ensure that *either* the pointer is null *or*
72     /// all of the following is true:
73     ///
74     /// * The pointer must be properly aligned.
75     ///
76     /// * It must be "dereferencable" in the sense defined in [the module documentation].
77     ///
78     /// * The pointer must point to an initialized instance of `T`.
79     ///
80     /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
81     ///   arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
82     ///   In particular, for the duration of this lifetime, the memory the pointer points to must
83     ///   not get mutated (except inside `UnsafeCell`).
84     ///
85     /// This applies even if the result of this method is unused!
86     /// (The part about being initialized is not yet fully decided, but until
87     /// it is, the only safe approach is to ensure that they are indeed initialized.)
88     ///
89     /// [the module documentation]: crate::ptr#safety
90     ///
91     /// # Examples
92     ///
93     /// Basic usage:
94     ///
95     /// ```
96     /// let ptr: *mut u8 = &mut 10u8 as *mut u8;
97     ///
98     /// unsafe {
99     ///     if let Some(val_back) = ptr.as_ref() {
100     ///         println!("We got back the value: {}!", val_back);
101     ///     }
102     /// }
103     /// ```
104     ///
105     /// # Null-unchecked version
106     ///
107     /// If you are sure the pointer can never be null and are looking for some kind of
108     /// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>`, know that you can
109     /// dereference the pointer directly.
110     ///
111     /// ```
112     /// let ptr: *mut u8 = &mut 10u8 as *mut u8;
113     ///
114     /// unsafe {
115     ///     let val_back = &*ptr;
116     ///     println!("We got back the value: {}!", val_back);
117     /// }
118     /// ```
119     #[stable(feature = "ptr_as_ref", since = "1.9.0")]
120     #[inline]
121     pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
122         // SAFETY: the caller must guarantee that `self` is valid for a
123         // reference if it isn't null.
124         if self.is_null() { None } else { unsafe { Some(&*self) } }
125     }
126
127     /// Returns `None` if the pointer is null, or else returns a shared reference to
128     /// the value wrapped in `Some`. In contrast to [`as_ref`], this does not require
129     /// that the value has to be initialized.
130     ///
131     /// For the mutable counterpart see [`as_uninit_mut`].
132     ///
133     /// [`as_ref`]: #method.as_ref-1
134     /// [`as_uninit_mut`]: #method.as_uninit_mut
135     ///
136     /// # Safety
137     ///
138     /// When calling this method, you have to ensure that *either* the pointer is null *or*
139     /// all of the following is true:
140     ///
141     /// * The pointer must be properly aligned.
142     ///
143     /// * It must be "dereferencable" in the sense defined in [the module documentation].
144     ///
145     /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
146     ///   arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
147     ///   In particular, for the duration of this lifetime, the memory the pointer points to must
148     ///   not get mutated (except inside `UnsafeCell`).
149     ///
150     /// This applies even if the result of this method is unused!
151     ///
152     /// [the module documentation]: crate::ptr#safety
153     ///
154     /// # Examples
155     ///
156     /// Basic usage:
157     ///
158     /// ```
159     /// #![feature(ptr_as_uninit)]
160     ///
161     /// let ptr: *mut u8 = &mut 10u8 as *mut u8;
162     ///
163     /// unsafe {
164     ///     if let Some(val_back) = ptr.as_uninit_ref() {
165     ///         println!("We got back the value: {}!", val_back.assume_init());
166     ///     }
167     /// }
168     /// ```
169     #[inline]
170     #[unstable(feature = "ptr_as_uninit", issue = "75402")]
171     pub unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>>
172     where
173         T: Sized,
174     {
175         // SAFETY: the caller must guarantee that `self` meets all the
176         // requirements for a reference.
177         if self.is_null() { None } else { Some(unsafe { &*(self as *const MaybeUninit<T>) }) }
178     }
179
180     /// Calculates the offset from a pointer.
181     ///
182     /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
183     /// offset of `3 * size_of::<T>()` bytes.
184     ///
185     /// # Safety
186     ///
187     /// If any of the following conditions are violated, the result is Undefined
188     /// Behavior:
189     ///
190     /// * Both the starting and resulting pointer must be either in bounds or one
191     ///   byte past the end of the same [allocated object].
192     ///
193     /// * The computed offset, **in bytes**, cannot overflow an `isize`.
194     ///
195     /// * The offset being in bounds cannot rely on "wrapping around" the address
196     ///   space. That is, the infinite-precision sum, **in bytes** must fit in a usize.
197     ///
198     /// The compiler and standard library generally tries to ensure allocations
199     /// never reach a size where an offset is a concern. For instance, `Vec`
200     /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
201     /// `vec.as_ptr().add(vec.len())` is always safe.
202     ///
203     /// Most platforms fundamentally can't even construct such an allocation.
204     /// For instance, no known 64-bit platform can ever serve a request
205     /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
206     /// However, some 32-bit and 16-bit platforms may successfully serve a request for
207     /// more than `isize::MAX` bytes with things like Physical Address
208     /// Extension. As such, memory acquired directly from allocators or memory
209     /// mapped files *may* be too large to handle with this function.
210     ///
211     /// Consider using [`wrapping_offset`] instead if these constraints are
212     /// difficult to satisfy. The only advantage of this method is that it
213     /// enables more aggressive compiler optimizations.
214     ///
215     /// [`wrapping_offset`]: #method.wrapping_offset
216     /// [allocated object]: crate::ptr#allocated-object
217     ///
218     /// # Examples
219     ///
220     /// Basic usage:
221     ///
222     /// ```
223     /// let mut s = [1, 2, 3];
224     /// let ptr: *mut u32 = s.as_mut_ptr();
225     ///
226     /// unsafe {
227     ///     println!("{}", *ptr.offset(1));
228     ///     println!("{}", *ptr.offset(2));
229     /// }
230     /// ```
231     #[stable(feature = "rust1", since = "1.0.0")]
232     #[must_use = "returns a new pointer rather than modifying its argument"]
233     #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
234     #[inline(always)]
235     pub const unsafe fn offset(self, count: isize) -> *mut T
236     where
237         T: Sized,
238     {
239         // SAFETY: the caller must uphold the safety contract for `offset`.
240         // The obtained pointer is valid for writes since the caller must
241         // guarantee that it points to the same allocated object as `self`.
242         unsafe { intrinsics::offset(self, count) as *mut T }
243     }
244
245     /// Calculates the offset from a pointer using wrapping arithmetic.
246     /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
247     /// offset of `3 * size_of::<T>()` bytes.
248     ///
249     /// # Safety
250     ///
251     /// This operation itself is always safe, but using the resulting pointer is not.
252     ///
253     /// The resulting pointer "remembers" the [allocated object] that `self` points to; it may not
254     /// be used to read or write other allocated objects.
255     ///
256     /// In other words, `let z = x.wrapping_offset((y as isize) - (x as isize))` does *not* make `z`
257     /// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
258     /// attached to the object `x` is attached to, and dereferencing it is Undefined Behavior unless
259     /// `x` and `y` point into the same allocated object.
260     ///
261     /// Compared to [`offset`], this method basically delays the requirement of staying within the
262     /// same allocated object: [`offset`] is immediate Undefined Behavior when crossing object
263     /// boundaries; `wrapping_offset` produces a pointer but still leads to Undefined Behavior if a
264     /// pointer is dereferenced when it is out-of-bounds of the object it is attached to. [`offset`]
265     /// can be optimized better and is thus preferable in performance-sensitive code.
266     ///
267     /// The delayed check only considers the value of the pointer that was dereferenced, not the
268     /// intermediate values used during the computation of the final result. For example,
269     /// `x.wrapping_offset(o).wrapping_offset(o.wrapping_neg())` is always the same as `x`. In other
270     /// words, leaving the allocated object and then re-entering it later is permitted.
271     ///
272     /// [`offset`]: #method.offset
273     /// [allocated object]: crate::ptr#allocated-object
274     ///
275     /// # Examples
276     ///
277     /// Basic usage:
278     ///
279     /// ```
280     /// // Iterate using a raw pointer in increments of two elements
281     /// let mut data = [1u8, 2, 3, 4, 5];
282     /// let mut ptr: *mut u8 = data.as_mut_ptr();
283     /// let step = 2;
284     /// let end_rounded_up = ptr.wrapping_offset(6);
285     ///
286     /// while ptr != end_rounded_up {
287     ///     unsafe {
288     ///         *ptr = 0;
289     ///     }
290     ///     ptr = ptr.wrapping_offset(step);
291     /// }
292     /// assert_eq!(&data, &[0, 2, 0, 4, 0]);
293     /// ```
294     #[stable(feature = "ptr_wrapping_offset", since = "1.16.0")]
295     #[must_use = "returns a new pointer rather than modifying its argument"]
296     #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
297     #[inline(always)]
298     pub const fn wrapping_offset(self, count: isize) -> *mut T
299     where
300         T: Sized,
301     {
302         // SAFETY: the `arith_offset` intrinsic has no prerequisites to be called.
303         unsafe { intrinsics::arith_offset(self, count) as *mut T }
304     }
305
306     /// Returns `None` if the pointer is null, or else returns a unique reference to
307     /// the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_mut`]
308     /// must be used instead.
309     ///
310     /// For the shared counterpart see [`as_ref`].
311     ///
312     /// [`as_uninit_mut`]: #method.as_uninit_mut
313     /// [`as_ref`]: #method.as_ref-1
314     ///
315     /// # Safety
316     ///
317     /// When calling this method, you have to ensure that *either* the pointer is null *or*
318     /// all of the following is true:
319     ///
320     /// * The pointer must be properly aligned.
321     ///
322     /// * It must be "dereferencable" in the sense defined in [the module documentation].
323     ///
324     /// * The pointer must point to an initialized instance of `T`.
325     ///
326     /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
327     ///   arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
328     ///   In particular, for the duration of this lifetime, the memory the pointer points to must
329     ///   not get accessed (read or written) through any other pointer.
330     ///
331     /// This applies even if the result of this method is unused!
332     /// (The part about being initialized is not yet fully decided, but until
333     /// it is, the only safe approach is to ensure that they are indeed initialized.)
334     ///
335     /// [the module documentation]: crate::ptr#safety
336     ///
337     /// # Examples
338     ///
339     /// Basic usage:
340     ///
341     /// ```
342     /// let mut s = [1, 2, 3];
343     /// let ptr: *mut u32 = s.as_mut_ptr();
344     /// let first_value = unsafe { ptr.as_mut().unwrap() };
345     /// *first_value = 4;
346     /// # assert_eq!(s, [4, 2, 3]);
347     /// println!("{:?}", s); // It'll print: "[4, 2, 3]".
348     /// ```
349     ///
350     /// # Null-unchecked version
351     ///
352     /// If you are sure the pointer can never be null and are looking for some kind of
353     /// `as_mut_unchecked` that returns the `&mut T` instead of `Option<&mut T>`, know that
354     /// you can dereference the pointer directly.
355     ///
356     /// ```
357     /// let mut s = [1, 2, 3];
358     /// let ptr: *mut u32 = s.as_mut_ptr();
359     /// let first_value = unsafe { &mut *ptr };
360     /// *first_value = 4;
361     /// # assert_eq!(s, [4, 2, 3]);
362     /// println!("{:?}", s); // It'll print: "[4, 2, 3]".
363     /// ```
364     #[stable(feature = "ptr_as_ref", since = "1.9.0")]
365     #[inline]
366     pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {
367         // SAFETY: the caller must guarantee that `self` is be valid for
368         // a mutable reference if it isn't null.
369         if self.is_null() { None } else { unsafe { Some(&mut *self) } }
370     }
371
372     /// Returns `None` if the pointer is null, or else returns a unique reference to
373     /// the value wrapped in `Some`. In contrast to [`as_mut`], this does not require
374     /// that the value has to be initialized.
375     ///
376     /// For the shared counterpart see [`as_uninit_ref`].
377     ///
378     /// [`as_mut`]: #method.as_mut
379     /// [`as_uninit_ref`]: #method.as_uninit_ref-1
380     ///
381     /// # Safety
382     ///
383     /// When calling this method, you have to ensure that *either* the pointer is null *or*
384     /// all of the following is true:
385     ///
386     /// * The pointer must be properly aligned.
387     ///
388     /// * It must be "dereferencable" in the sense defined in [the module documentation].
389     ///
390     /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
391     ///   arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
392     ///   In particular, for the duration of this lifetime, the memory the pointer points to must
393     ///   not get accessed (read or written) through any other pointer.
394     ///
395     /// This applies even if the result of this method is unused!
396     ///
397     /// [the module documentation]: crate::ptr#safety
398     #[inline]
399     #[unstable(feature = "ptr_as_uninit", issue = "75402")]
400     pub unsafe fn as_uninit_mut<'a>(self) -> Option<&'a mut MaybeUninit<T>>
401     where
402         T: Sized,
403     {
404         // SAFETY: the caller must guarantee that `self` meets all the
405         // requirements for a reference.
406         if self.is_null() { None } else { Some(unsafe { &mut *(self as *mut MaybeUninit<T>) }) }
407     }
408
409     /// Returns whether two pointers are guaranteed to be equal.
410     ///
411     /// At runtime this function behaves like `self == other`.
412     /// However, in some contexts (e.g., compile-time evaluation),
413     /// it is not always possible to determine equality of two pointers, so this function may
414     /// spuriously return `false` for pointers that later actually turn out to be equal.
415     /// But when it returns `true`, the pointers are guaranteed to be equal.
416     ///
417     /// This function is the mirror of [`guaranteed_ne`], but not its inverse. There are pointer
418     /// comparisons for which both functions return `false`.
419     ///
420     /// [`guaranteed_ne`]: #method.guaranteed_ne
421     ///
422     /// The return value may change depending on the compiler version and unsafe code may not
423     /// rely on the result of this function for soundness. It is suggested to only use this function
424     /// for performance optimizations where spurious `false` return values by this function do not
425     /// affect the outcome, but just the performance.
426     /// The consequences of using this method to make runtime and compile-time code behave
427     /// differently have not been explored. This method should not be used to introduce such
428     /// differences, and it should also not be stabilized before we have a better understanding
429     /// of this issue.
430     #[unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
431     #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
432     #[inline]
433     pub const fn guaranteed_eq(self, other: *mut T) -> bool
434     where
435         T: Sized,
436     {
437         intrinsics::ptr_guaranteed_eq(self as *const _, other as *const _)
438     }
439
440     /// Returns whether two pointers are guaranteed to be unequal.
441     ///
442     /// At runtime this function behaves like `self != other`.
443     /// However, in some contexts (e.g., compile-time evaluation),
444     /// it is not always possible to determine the inequality of two pointers, so this function may
445     /// spuriously return `false` for pointers that later actually turn out to be unequal.
446     /// But when it returns `true`, the pointers are guaranteed to be unequal.
447     ///
448     /// This function is the mirror of [`guaranteed_eq`], but not its inverse. There are pointer
449     /// comparisons for which both functions return `false`.
450     ///
451     /// [`guaranteed_eq`]: #method.guaranteed_eq
452     ///
453     /// The return value may change depending on the compiler version and unsafe code may not
454     /// rely on the result of this function for soundness. It is suggested to only use this function
455     /// for performance optimizations where spurious `false` return values by this function do not
456     /// affect the outcome, but just the performance.
457     /// The consequences of using this method to make runtime and compile-time code behave
458     /// differently have not been explored. This method should not be used to introduce such
459     /// differences, and it should also not be stabilized before we have a better understanding
460     /// of this issue.
461     #[unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
462     #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
463     #[inline]
464     pub const unsafe fn guaranteed_ne(self, other: *mut T) -> bool
465     where
466         T: Sized,
467     {
468         intrinsics::ptr_guaranteed_ne(self as *const _, other as *const _)
469     }
470
471     /// Calculates the distance between two pointers. The returned value is in
472     /// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
473     ///
474     /// This function is the inverse of [`offset`].
475     ///
476     /// [`offset`]: #method.offset-1
477     ///
478     /// # Safety
479     ///
480     /// If any of the following conditions are violated, the result is Undefined
481     /// Behavior:
482     ///
483     /// * Both the starting and other pointer must be either in bounds or one
484     ///   byte past the end of the same [allocated object].
485     ///
486     /// * Both pointers must be *derived from* a pointer to the same object.
487     ///   (See below for an example.)
488     ///
489     /// * The distance between the pointers, in bytes, must be an exact multiple
490     ///   of the size of `T`.
491     ///
492     /// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
493     ///
494     /// * The distance being in bounds cannot rely on "wrapping around" the address space.
495     ///
496     /// Rust types are never larger than `isize::MAX` and Rust allocations never wrap around the
497     /// address space, so two pointers within some value of any Rust type `T` will always satisfy
498     /// the last two conditions. The standard library also generally ensures that allocations
499     /// never reach a size where an offset is a concern. For instance, `Vec` and `Box` ensure they
500     /// never allocate more than `isize::MAX` bytes, so `ptr_into_vec.offset_from(vec.as_ptr())`
501     /// always satisfies the last two conditions.
502     ///
503     /// Most platforms fundamentally can't even construct such a large allocation.
504     /// For instance, no known 64-bit platform can ever serve a request
505     /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
506     /// However, some 32-bit and 16-bit platforms may successfully serve a request for
507     /// more than `isize::MAX` bytes with things like Physical Address
508     /// Extension. As such, memory acquired directly from allocators or memory
509     /// mapped files *may* be too large to handle with this function.
510     /// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
511     /// such large allocations either.)
512     ///
513     /// [`add`]: #method.add
514     /// [allocated object]: crate::ptr#allocated-object
515     ///
516     /// # Panics
517     ///
518     /// This function panics if `T` is a Zero-Sized Type ("ZST").
519     ///
520     /// # Examples
521     ///
522     /// Basic usage:
523     ///
524     /// ```
525     /// let mut a = [0; 5];
526     /// let ptr1: *mut i32 = &mut a[1];
527     /// let ptr2: *mut i32 = &mut a[3];
528     /// unsafe {
529     ///     assert_eq!(ptr2.offset_from(ptr1), 2);
530     ///     assert_eq!(ptr1.offset_from(ptr2), -2);
531     ///     assert_eq!(ptr1.offset(2), ptr2);
532     ///     assert_eq!(ptr2.offset(-2), ptr1);
533     /// }
534     /// ```
535     ///
536     /// *Incorrect* usage:
537     ///
538     /// ```rust,no_run
539     /// let ptr1 = Box::into_raw(Box::new(0u8));
540     /// let ptr2 = Box::into_raw(Box::new(1u8));
541     /// let diff = (ptr2 as isize).wrapping_sub(ptr1 as isize);
542     /// // Make ptr2_other an "alias" of ptr2, but derived from ptr1.
543     /// let ptr2_other = (ptr1 as *mut u8).wrapping_offset(diff);
544     /// assert_eq!(ptr2 as usize, ptr2_other as usize);
545     /// // Since ptr2_other and ptr2 are derived from pointers to different objects,
546     /// // computing their offset is undefined behavior, even though
547     /// // they point to the same address!
548     /// unsafe {
549     ///     let zero = ptr2_other.offset_from(ptr2); // Undefined Behavior
550     /// }
551     /// ```
552     #[stable(feature = "ptr_offset_from", since = "1.47.0")]
553     #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "41079")]
554     #[inline(always)]
555     pub const unsafe fn offset_from(self, origin: *const T) -> isize
556     where
557         T: Sized,
558     {
559         // SAFETY: the caller must uphold the safety contract for `offset_from`.
560         unsafe { (self as *const T).offset_from(origin) }
561     }
562
563     /// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
564     ///
565     /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
566     /// offset of `3 * size_of::<T>()` bytes.
567     ///
568     /// # Safety
569     ///
570     /// If any of the following conditions are violated, the result is Undefined
571     /// Behavior:
572     ///
573     /// * Both the starting and resulting pointer must be either in bounds or one
574     ///   byte past the end of the same [allocated object].
575     ///
576     /// * The computed offset, **in bytes**, cannot overflow an `isize`.
577     ///
578     /// * The offset being in bounds cannot rely on "wrapping around" the address
579     ///   space. That is, the infinite-precision sum must fit in a `usize`.
580     ///
581     /// The compiler and standard library generally tries to ensure allocations
582     /// never reach a size where an offset is a concern. For instance, `Vec`
583     /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
584     /// `vec.as_ptr().add(vec.len())` is always safe.
585     ///
586     /// Most platforms fundamentally can't even construct such an allocation.
587     /// For instance, no known 64-bit platform can ever serve a request
588     /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
589     /// However, some 32-bit and 16-bit platforms may successfully serve a request for
590     /// more than `isize::MAX` bytes with things like Physical Address
591     /// Extension. As such, memory acquired directly from allocators or memory
592     /// mapped files *may* be too large to handle with this function.
593     ///
594     /// Consider using [`wrapping_add`] instead if these constraints are
595     /// difficult to satisfy. The only advantage of this method is that it
596     /// enables more aggressive compiler optimizations.
597     ///
598     /// [`wrapping_add`]: #method.wrapping_add
599     ///
600     /// # Examples
601     ///
602     /// Basic usage:
603     ///
604     /// ```
605     /// let s: &str = "123";
606     /// let ptr: *const u8 = s.as_ptr();
607     ///
608     /// unsafe {
609     ///     println!("{}", *ptr.add(1) as char);
610     ///     println!("{}", *ptr.add(2) as char);
611     /// }
612     /// ```
613     #[stable(feature = "pointer_methods", since = "1.26.0")]
614     #[must_use = "returns a new pointer rather than modifying its argument"]
615     #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
616     #[inline(always)]
617     pub const unsafe fn add(self, count: usize) -> Self
618     where
619         T: Sized,
620     {
621         // SAFETY: the caller must uphold the safety contract for `offset`.
622         unsafe { self.offset(count as isize) }
623     }
624
625     /// Calculates the offset from a pointer (convenience for
626     /// `.offset((count as isize).wrapping_neg())`).
627     ///
628     /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
629     /// offset of `3 * size_of::<T>()` bytes.
630     ///
631     /// # Safety
632     ///
633     /// If any of the following conditions are violated, the result is Undefined
634     /// Behavior:
635     ///
636     /// * Both the starting and resulting pointer must be either in bounds or one
637     ///   byte past the end of the same [allocated object].
638     ///
639     /// * The computed offset cannot exceed `isize::MAX` **bytes**.
640     ///
641     /// * The offset being in bounds cannot rely on "wrapping around" the address
642     ///   space. That is, the infinite-precision sum must fit in a usize.
643     ///
644     /// The compiler and standard library generally tries to ensure allocations
645     /// never reach a size where an offset is a concern. For instance, `Vec`
646     /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
647     /// `vec.as_ptr().add(vec.len()).sub(vec.len())` is always safe.
648     ///
649     /// Most platforms fundamentally can't even construct such an allocation.
650     /// For instance, no known 64-bit platform can ever serve a request
651     /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
652     /// However, some 32-bit and 16-bit platforms may successfully serve a request for
653     /// more than `isize::MAX` bytes with things like Physical Address
654     /// Extension. As such, memory acquired directly from allocators or memory
655     /// mapped files *may* be too large to handle with this function.
656     ///
657     /// Consider using [`wrapping_sub`] instead if these constraints are
658     /// difficult to satisfy. The only advantage of this method is that it
659     /// enables more aggressive compiler optimizations.
660     ///
661     /// [`wrapping_sub`]: #method.wrapping_sub
662     /// [allocated object]: crate::ptr#allocated-object
663     ///
664     /// # Examples
665     ///
666     /// Basic usage:
667     ///
668     /// ```
669     /// let s: &str = "123";
670     ///
671     /// unsafe {
672     ///     let end: *const u8 = s.as_ptr().add(3);
673     ///     println!("{}", *end.sub(1) as char);
674     ///     println!("{}", *end.sub(2) as char);
675     /// }
676     /// ```
677     #[stable(feature = "pointer_methods", since = "1.26.0")]
678     #[must_use = "returns a new pointer rather than modifying its argument"]
679     #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
680     #[inline]
681     pub const unsafe fn sub(self, count: usize) -> Self
682     where
683         T: Sized,
684     {
685         // SAFETY: the caller must uphold the safety contract for `offset`.
686         unsafe { self.offset((count as isize).wrapping_neg()) }
687     }
688
689     /// Calculates the offset from a pointer using wrapping arithmetic.
690     /// (convenience for `.wrapping_offset(count as isize)`)
691     ///
692     /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
693     /// offset of `3 * size_of::<T>()` bytes.
694     ///
695     /// # Safety
696     ///
697     /// This operation itself is always safe, but using the resulting pointer is not.
698     ///
699     /// The resulting pointer "remembers" the [allocated object] that `self` points to; it may not
700     /// be used to read or write other allocated objects.
701     ///
702     /// In other words, `let z = x.wrapping_add((y as usize) - (x as usize))` does *not* make `z`
703     /// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
704     /// attached to the object `x` is attached to, and dereferencing it is Undefined Behavior unless
705     /// `x` and `y` point into the same allocated object.
706     ///
707     /// Compared to [`add`], this method basically delays the requirement of staying within the
708     /// same allocated object: [`add`] is immediate Undefined Behavior when crossing object
709     /// boundaries; `wrapping_add` produces a pointer but still leads to Undefined Behavior if a
710     /// pointer is dereferenced when it is out-of-bounds of the object it is attached to. [`add`]
711     /// can be optimized better and is thus preferable in performance-sensitive code.
712     ///
713     /// The delayed check only considers the value of the pointer that was dereferenced, not the
714     /// intermediate values used during the computation of the final result. For example,
715     /// `x.wrapping_add(o).wrapping_sub(o)` is always the same as `x`. In other words, leaving the
716     /// allocated object and then re-entering it later is permitted.
717     ///
718     /// [`add`]: #method.add
719     /// [allocated object]: crate::ptr#allocated-object
720     ///
721     /// # Examples
722     ///
723     /// Basic usage:
724     ///
725     /// ```
726     /// // Iterate using a raw pointer in increments of two elements
727     /// let data = [1u8, 2, 3, 4, 5];
728     /// let mut ptr: *const u8 = data.as_ptr();
729     /// let step = 2;
730     /// let end_rounded_up = ptr.wrapping_add(6);
731     ///
732     /// // This loop prints "1, 3, 5, "
733     /// while ptr != end_rounded_up {
734     ///     unsafe {
735     ///         print!("{}, ", *ptr);
736     ///     }
737     ///     ptr = ptr.wrapping_add(step);
738     /// }
739     /// ```
740     #[stable(feature = "pointer_methods", since = "1.26.0")]
741     #[must_use = "returns a new pointer rather than modifying its argument"]
742     #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
743     #[inline(always)]
744     pub const fn wrapping_add(self, count: usize) -> Self
745     where
746         T: Sized,
747     {
748         self.wrapping_offset(count as isize)
749     }
750
751     /// Calculates the offset from a pointer using wrapping arithmetic.
752     /// (convenience for `.wrapping_offset((count as isize).wrapping_neg())`)
753     ///
754     /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
755     /// offset of `3 * size_of::<T>()` bytes.
756     ///
757     /// # Safety
758     ///
759     /// This operation itself is always safe, but using the resulting pointer is not.
760     ///
761     /// The resulting pointer "remembers" the [allocated object] that `self` points to; it may not
762     /// be used to read or write other allocated objects.
763     ///
764     /// In other words, `let z = x.wrapping_sub((x as usize) - (y as usize))` does *not* make `z`
765     /// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
766     /// attached to the object `x` is attached to, and dereferencing it is Undefined Behavior unless
767     /// `x` and `y` point into the same allocated object.
768     ///
769     /// Compared to [`sub`], this method basically delays the requirement of staying within the
770     /// same allocated object: [`sub`] is immediate Undefined Behavior when crossing object
771     /// boundaries; `wrapping_sub` produces a pointer but still leads to Undefined Behavior if a
772     /// pointer is dereferenced when it is out-of-bounds of the object it is attached to. [`sub`]
773     /// can be optimized better and is thus preferable in performance-sensitive code.
774     ///
775     /// The delayed check only considers the value of the pointer that was dereferenced, not the
776     /// intermediate values used during the computation of the final result. For example,
777     /// `x.wrapping_add(o).wrapping_sub(o)` is always the same as `x`. In other words, leaving the
778     /// allocated object and then re-entering it later is permitted.
779     ///
780     /// [`sub`]: #method.sub
781     /// [allocated object]: crate::ptr#allocated-object
782     ///
783     /// # Examples
784     ///
785     /// Basic usage:
786     ///
787     /// ```
788     /// // Iterate using a raw pointer in increments of two elements (backwards)
789     /// let data = [1u8, 2, 3, 4, 5];
790     /// let mut ptr: *const u8 = data.as_ptr();
791     /// let start_rounded_down = ptr.wrapping_sub(2);
792     /// ptr = ptr.wrapping_add(4);
793     /// let step = 2;
794     /// // This loop prints "5, 3, 1, "
795     /// while ptr != start_rounded_down {
796     ///     unsafe {
797     ///         print!("{}, ", *ptr);
798     ///     }
799     ///     ptr = ptr.wrapping_sub(step);
800     /// }
801     /// ```
802     #[stable(feature = "pointer_methods", since = "1.26.0")]
803     #[must_use = "returns a new pointer rather than modifying its argument"]
804     #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
805     #[inline]
806     pub const fn wrapping_sub(self, count: usize) -> Self
807     where
808         T: Sized,
809     {
810         self.wrapping_offset((count as isize).wrapping_neg())
811     }
812
813     /// Sets the pointer value to `ptr`.
814     ///
815     /// In case `self` is a (fat) pointer to an unsized type, this operation
816     /// will only affect the pointer part, whereas for (thin) pointers to
817     /// sized types, this has the same effect as a simple assignment.
818     ///
819     /// The resulting pointer will have provenance of `val`, i.e., for a fat
820     /// pointer, this operation is semantically the same as creating a new
821     /// fat pointer with the data pointer value of `val` but the metadata of
822     /// `self`.
823     ///
824     /// # Examples
825     ///
826     /// This function is primarily useful for allowing byte-wise pointer
827     /// arithmetic on potentially fat pointers:
828     ///
829     /// ```
830     /// #![feature(set_ptr_value)]
831     /// # use core::fmt::Debug;
832     /// let mut arr: [i32; 3] = [1, 2, 3];
833     /// let mut ptr = arr.as_mut_ptr() as *mut dyn Debug;
834     /// let thin = ptr as *mut u8;
835     /// unsafe {
836     ///     ptr = ptr.set_ptr_value(thin.add(8));
837     ///     # assert_eq!(*(ptr as *mut i32), 3);
838     ///     println!("{:?}", &*ptr); // will print "3"
839     /// }
840     /// ```
841     #[unstable(feature = "set_ptr_value", issue = "75091")]
842     #[must_use = "returns a new pointer rather than modifying its argument"]
843     #[inline]
844     pub fn set_ptr_value(mut self, val: *mut u8) -> Self {
845         let thin = &mut self as *mut *mut T as *mut *mut u8;
846         // SAFETY: In case of a thin pointer, this operations is identical
847         // to a simple assignment. In case of a fat pointer, with the current
848         // fat pointer layout implementation, the first field of such a
849         // pointer is always the data pointer, which is likewise assigned.
850         unsafe { *thin = val };
851         self
852     }
853
854     /// Reads the value from `self` without moving it. This leaves the
855     /// memory in `self` unchanged.
856     ///
857     /// See [`ptr::read`] for safety concerns and examples.
858     ///
859     /// [`ptr::read`]: crate::ptr::read()
860     #[stable(feature = "pointer_methods", since = "1.26.0")]
861     #[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
862     #[inline(always)]
863     pub const unsafe fn read(self) -> T
864     where
865         T: Sized,
866     {
867         // SAFETY: the caller must uphold the safety contract for ``.
868         unsafe { read(self) }
869     }
870
871     /// Performs a volatile read of the value from `self` without moving it. This
872     /// leaves the memory in `self` unchanged.
873     ///
874     /// Volatile operations are intended to act on I/O memory, and are guaranteed
875     /// to not be elided or reordered by the compiler across other volatile
876     /// operations.
877     ///
878     /// See [`ptr::read_volatile`] for safety concerns and examples.
879     ///
880     /// [`ptr::read_volatile`]: crate::ptr::read_volatile()
881     #[stable(feature = "pointer_methods", since = "1.26.0")]
882     #[inline(always)]
883     pub unsafe fn read_volatile(self) -> T
884     where
885         T: Sized,
886     {
887         // SAFETY: the caller must uphold the safety contract for `read_volatile`.
888         unsafe { read_volatile(self) }
889     }
890
891     /// Reads the value from `self` without moving it. This leaves the
892     /// memory in `self` unchanged.
893     ///
894     /// Unlike `read`, the pointer may be unaligned.
895     ///
896     /// See [`ptr::read_unaligned`] for safety concerns and examples.
897     ///
898     /// [`ptr::read_unaligned`]: crate::ptr::read_unaligned()
899     #[stable(feature = "pointer_methods", since = "1.26.0")]
900     #[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
901     #[inline(always)]
902     pub const unsafe fn read_unaligned(self) -> T
903     where
904         T: Sized,
905     {
906         // SAFETY: the caller must uphold the safety contract for `read_unaligned`.
907         unsafe { read_unaligned(self) }
908     }
909
910     /// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
911     /// and destination may overlap.
912     ///
913     /// NOTE: this has the *same* argument order as [`ptr::copy`].
914     ///
915     /// See [`ptr::copy`] for safety concerns and examples.
916     ///
917     /// [`ptr::copy`]: crate::ptr::copy()
918     #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
919     #[stable(feature = "pointer_methods", since = "1.26.0")]
920     #[inline(always)]
921     pub const unsafe fn copy_to(self, dest: *mut T, count: usize)
922     where
923         T: Sized,
924     {
925         // SAFETY: the caller must uphold the safety contract for `copy`.
926         unsafe { copy(self, dest, count) }
927     }
928
929     /// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
930     /// and destination may *not* overlap.
931     ///
932     /// NOTE: this has the *same* argument order as [`ptr::copy_nonoverlapping`].
933     ///
934     /// See [`ptr::copy_nonoverlapping`] for safety concerns and examples.
935     ///
936     /// [`ptr::copy_nonoverlapping`]: crate::ptr::copy_nonoverlapping()
937     #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
938     #[stable(feature = "pointer_methods", since = "1.26.0")]
939     #[inline(always)]
940     pub const unsafe fn copy_to_nonoverlapping(self, dest: *mut T, count: usize)
941     where
942         T: Sized,
943     {
944         // SAFETY: the caller must uphold the safety contract for `copy_nonoverlapping`.
945         unsafe { copy_nonoverlapping(self, dest, count) }
946     }
947
948     /// Copies `count * size_of<T>` bytes from `src` to `self`. The source
949     /// and destination may overlap.
950     ///
951     /// NOTE: this has the *opposite* argument order of [`ptr::copy`].
952     ///
953     /// See [`ptr::copy`] for safety concerns and examples.
954     ///
955     /// [`ptr::copy`]: crate::ptr::copy()
956     #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
957     #[stable(feature = "pointer_methods", since = "1.26.0")]
958     #[inline(always)]
959     pub const unsafe fn copy_from(self, src: *const T, count: usize)
960     where
961         T: Sized,
962     {
963         // SAFETY: the caller must uphold the safety contract for `copy`.
964         unsafe { copy(src, self, count) }
965     }
966
967     /// Copies `count * size_of<T>` bytes from `src` to `self`. The source
968     /// and destination may *not* overlap.
969     ///
970     /// NOTE: this has the *opposite* argument order of [`ptr::copy_nonoverlapping`].
971     ///
972     /// See [`ptr::copy_nonoverlapping`] for safety concerns and examples.
973     ///
974     /// [`ptr::copy_nonoverlapping`]: crate::ptr::copy_nonoverlapping()
975     #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
976     #[stable(feature = "pointer_methods", since = "1.26.0")]
977     #[inline(always)]
978     pub const unsafe fn copy_from_nonoverlapping(self, src: *const T, count: usize)
979     where
980         T: Sized,
981     {
982         // SAFETY: the caller must uphold the safety contract for `copy_nonoverlapping`.
983         unsafe { copy_nonoverlapping(src, self, count) }
984     }
985
986     /// Executes the destructor (if any) of the pointed-to value.
987     ///
988     /// See [`ptr::drop_in_place`] for safety concerns and examples.
989     ///
990     /// [`ptr::drop_in_place`]: crate::ptr::drop_in_place()
991     #[stable(feature = "pointer_methods", since = "1.26.0")]
992     #[inline(always)]
993     pub unsafe fn drop_in_place(self) {
994         // SAFETY: the caller must uphold the safety contract for `drop_in_place`.
995         unsafe { drop_in_place(self) }
996     }
997
998     /// Overwrites a memory location with the given value without reading or
999     /// dropping the old value.
1000     ///
1001     /// See [`ptr::write`] for safety concerns and examples.
1002     ///
1003     /// [`ptr::write`]: crate::ptr::write()
1004     #[stable(feature = "pointer_methods", since = "1.26.0")]
1005     #[inline(always)]
1006     pub unsafe fn write(self, val: T)
1007     where
1008         T: Sized,
1009     {
1010         // SAFETY: the caller must uphold the safety contract for `write`.
1011         unsafe { write(self, val) }
1012     }
1013
1014     /// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
1015     /// bytes of memory starting at `self` to `val`.
1016     ///
1017     /// See [`ptr::write_bytes`] for safety concerns and examples.
1018     ///
1019     /// [`ptr::write_bytes`]: crate::ptr::write_bytes()
1020     #[stable(feature = "pointer_methods", since = "1.26.0")]
1021     #[inline(always)]
1022     pub unsafe fn write_bytes(self, val: u8, count: usize)
1023     where
1024         T: Sized,
1025     {
1026         // SAFETY: the caller must uphold the safety contract for `write_bytes`.
1027         unsafe { write_bytes(self, val, count) }
1028     }
1029
1030     /// Performs a volatile write of a memory location with the given value without
1031     /// reading or dropping the old value.
1032     ///
1033     /// Volatile operations are intended to act on I/O memory, and are guaranteed
1034     /// to not be elided or reordered by the compiler across other volatile
1035     /// operations.
1036     ///
1037     /// See [`ptr::write_volatile`] for safety concerns and examples.
1038     ///
1039     /// [`ptr::write_volatile`]: crate::ptr::write_volatile()
1040     #[stable(feature = "pointer_methods", since = "1.26.0")]
1041     #[inline(always)]
1042     pub unsafe fn write_volatile(self, val: T)
1043     where
1044         T: Sized,
1045     {
1046         // SAFETY: the caller must uphold the safety contract for `write_volatile`.
1047         unsafe { write_volatile(self, val) }
1048     }
1049
1050     /// Overwrites a memory location with the given value without reading or
1051     /// dropping the old value.
1052     ///
1053     /// Unlike `write`, the pointer may be unaligned.
1054     ///
1055     /// See [`ptr::write_unaligned`] for safety concerns and examples.
1056     ///
1057     /// [`ptr::write_unaligned`]: crate::ptr::write_unaligned()
1058     #[stable(feature = "pointer_methods", since = "1.26.0")]
1059     #[rustc_const_unstable(feature = "const_ptr_write", issue = "none")]
1060     #[inline(always)]
1061     pub const unsafe fn write_unaligned(self, val: T)
1062     where
1063         T: Sized,
1064     {
1065         // SAFETY: the caller must uphold the safety contract for `write_unaligned`.
1066         unsafe { write_unaligned(self, val) }
1067     }
1068
1069     /// Replaces the value at `self` with `src`, returning the old
1070     /// value, without dropping either.
1071     ///
1072     /// See [`ptr::replace`] for safety concerns and examples.
1073     ///
1074     /// [`ptr::replace`]: crate::ptr::replace()
1075     #[stable(feature = "pointer_methods", since = "1.26.0")]
1076     #[inline(always)]
1077     pub unsafe fn replace(self, src: T) -> T
1078     where
1079         T: Sized,
1080     {
1081         // SAFETY: the caller must uphold the safety contract for `replace`.
1082         unsafe { replace(self, src) }
1083     }
1084
1085     /// Swaps the values at two mutable locations of the same type, without
1086     /// deinitializing either. They may overlap, unlike `mem::swap` which is
1087     /// otherwise equivalent.
1088     ///
1089     /// See [`ptr::swap`] for safety concerns and examples.
1090     ///
1091     /// [`ptr::swap`]: crate::ptr::swap()
1092     #[stable(feature = "pointer_methods", since = "1.26.0")]
1093     #[inline(always)]
1094     pub unsafe fn swap(self, with: *mut T)
1095     where
1096         T: Sized,
1097     {
1098         // SAFETY: the caller must uphold the safety contract for `swap`.
1099         unsafe { swap(self, with) }
1100     }
1101
1102     /// Computes the offset that needs to be applied to the pointer in order to make it aligned to
1103     /// `align`.
1104     ///
1105     /// If it is not possible to align the pointer, the implementation returns
1106     /// `usize::MAX`. It is permissible for the implementation to *always*
1107     /// return `usize::MAX`. Only your algorithm's performance can depend
1108     /// on getting a usable offset here, not its correctness.
1109     ///
1110     /// The offset is expressed in number of `T` elements, and not bytes. The value returned can be
1111     /// used with the `wrapping_add` method.
1112     ///
1113     /// There are no guarantees whatsoever that offsetting the pointer will not overflow or go
1114     /// beyond the allocation that the pointer points into. It is up to the caller to ensure that
1115     /// the returned offset is correct in all terms other than alignment.
1116     ///
1117     /// # Panics
1118     ///
1119     /// The function panics if `align` is not a power-of-two.
1120     ///
1121     /// # Examples
1122     ///
1123     /// Accessing adjacent `u8` as `u16`
1124     ///
1125     /// ```
1126     /// # fn foo(n: usize) {
1127     /// # use std::mem::align_of;
1128     /// # unsafe {
1129     /// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
1130     /// let ptr = x.as_ptr().add(n) as *const u8;
1131     /// let offset = ptr.align_offset(align_of::<u16>());
1132     /// if offset < x.len() - n - 1 {
1133     ///     let u16_ptr = ptr.add(offset) as *const u16;
1134     ///     assert_ne!(*u16_ptr, 500);
1135     /// } else {
1136     ///     // while the pointer can be aligned via `offset`, it would point
1137     ///     // outside the allocation
1138     /// }
1139     /// # } }
1140     /// ```
1141     #[stable(feature = "align_offset", since = "1.36.0")]
1142     pub fn align_offset(self, align: usize) -> usize
1143     where
1144         T: Sized,
1145     {
1146         if !align.is_power_of_two() {
1147             panic!("align_offset: align is not a power-of-two");
1148         }
1149         // SAFETY: `align` has been checked to be a power of 2 above
1150         unsafe { align_offset(self, align) }
1151     }
1152 }
1153
1154 #[lang = "mut_slice_ptr"]
1155 impl<T> *mut [T] {
1156     /// Returns the length of a raw slice.
1157     ///
1158     /// The returned value is the number of **elements**, not the number of bytes.
1159     ///
1160     /// This function is safe, even when the raw slice cannot be cast to a slice
1161     /// reference because the pointer is null or unaligned.
1162     ///
1163     /// # Examples
1164     ///
1165     /// ```rust
1166     /// #![feature(slice_ptr_len)]
1167     /// use std::ptr;
1168     ///
1169     /// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3);
1170     /// assert_eq!(slice.len(), 3);
1171     /// ```
1172     #[inline(always)]
1173     #[unstable(feature = "slice_ptr_len", issue = "71146")]
1174     #[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
1175     pub const fn len(self) -> usize {
1176         metadata(self)
1177     }
1178
1179     /// Returns a raw pointer to the slice's buffer.
1180     ///
1181     /// This is equivalent to casting `self` to `*mut T`, but more type-safe.
1182     ///
1183     /// # Examples
1184     ///
1185     /// ```rust
1186     /// #![feature(slice_ptr_get)]
1187     /// use std::ptr;
1188     ///
1189     /// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3);
1190     /// assert_eq!(slice.as_mut_ptr(), 0 as *mut i8);
1191     /// ```
1192     #[inline(always)]
1193     #[unstable(feature = "slice_ptr_get", issue = "74265")]
1194     #[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")]
1195     pub const fn as_mut_ptr(self) -> *mut T {
1196         self as *mut T
1197     }
1198
1199     /// Returns a raw pointer to an element or subslice, without doing bounds
1200     /// checking.
1201     ///
1202     /// Calling this method with an out-of-bounds index or when `self` is not dereferencable
1203     /// is *[undefined behavior]* even if the resulting pointer is not used.
1204     ///
1205     /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
1206     ///
1207     /// # Examples
1208     ///
1209     /// ```
1210     /// #![feature(slice_ptr_get)]
1211     ///
1212     /// let x = &mut [1, 2, 4] as *mut [i32];
1213     ///
1214     /// unsafe {
1215     ///     assert_eq!(x.get_unchecked_mut(1), x.as_mut_ptr().add(1));
1216     /// }
1217     /// ```
1218     #[unstable(feature = "slice_ptr_get", issue = "74265")]
1219     #[inline(always)]
1220     pub unsafe fn get_unchecked_mut<I>(self, index: I) -> *mut I::Output
1221     where
1222         I: SliceIndex<[T]>,
1223     {
1224         // SAFETY: the caller ensures that `self` is dereferencable and `index` in-bounds.
1225         unsafe { index.get_unchecked_mut(self) }
1226     }
1227
1228     /// Returns `None` if the pointer is null, or else returns a shared slice to
1229     /// the value wrapped in `Some`. In contrast to [`as_ref`], this does not require
1230     /// that the value has to be initialized.
1231     ///
1232     /// For the mutable counterpart see [`as_uninit_slice_mut`].
1233     ///
1234     /// [`as_ref`]: #method.as_ref-1
1235     /// [`as_uninit_slice_mut`]: #method.as_uninit_slice_mut
1236     ///
1237     /// # Safety
1238     ///
1239     /// When calling this method, you have to ensure that *either* the pointer is null *or*
1240     /// all of the following is true:
1241     ///
1242     /// * The pointer must be [valid] for reads for `ptr.len() * mem::size_of::<T>()` many bytes,
1243     ///   and it must be properly aligned. This means in particular:
1244     ///
1245     ///     * The entire memory range of this slice must be contained within a single [allocated object]!
1246     ///       Slices can never span across multiple allocated objects.
1247     ///
1248     ///     * The pointer must be aligned even for zero-length slices. One
1249     ///       reason for this is that enum layout optimizations may rely on references
1250     ///       (including slices of any length) being aligned and non-null to distinguish
1251     ///       them from other data. You can obtain a pointer that is usable as `data`
1252     ///       for zero-length slices using [`NonNull::dangling()`].
1253     ///
1254     /// * The total size `ptr.len() * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`.
1255     ///   See the safety documentation of [`pointer::offset`].
1256     ///
1257     /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
1258     ///   arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
1259     ///   In particular, for the duration of this lifetime, the memory the pointer points to must
1260     ///   not get mutated (except inside `UnsafeCell`).
1261     ///
1262     /// This applies even if the result of this method is unused!
1263     ///
1264     /// See also [`slice::from_raw_parts`][].
1265     ///
1266     /// [valid]: crate::ptr#safety
1267     /// [allocated object]: crate::ptr#allocated-object
1268     #[inline]
1269     #[unstable(feature = "ptr_as_uninit", issue = "75402")]
1270     pub unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
1271         if self.is_null() {
1272             None
1273         } else {
1274             // SAFETY: the caller must uphold the safety contract for `as_uninit_slice`.
1275             Some(unsafe { slice::from_raw_parts(self as *const MaybeUninit<T>, self.len()) })
1276         }
1277     }
1278
1279     /// Returns `None` if the pointer is null, or else returns a unique slice to
1280     /// the value wrapped in `Some`. In contrast to [`as_mut`], this does not require
1281     /// that the value has to be initialized.
1282     ///
1283     /// For the shared counterpart see [`as_uninit_slice`].
1284     ///
1285     /// [`as_mut`]: #method.as_mut
1286     /// [`as_uninit_slice`]: #method.as_uninit_slice-1
1287     ///
1288     /// # Safety
1289     ///
1290     /// When calling this method, you have to ensure that *either* the pointer is null *or*
1291     /// all of the following is true:
1292     ///
1293     /// * The pointer must be [valid] for reads and writes for `ptr.len() * mem::size_of::<T>()`
1294     ///   many bytes, and it must be properly aligned. This means in particular:
1295     ///
1296     ///     * The entire memory range of this slice must be contained within a single [allocated object]!
1297     ///       Slices can never span across multiple allocated objects.
1298     ///
1299     ///     * The pointer must be aligned even for zero-length slices. One
1300     ///       reason for this is that enum layout optimizations may rely on references
1301     ///       (including slices of any length) being aligned and non-null to distinguish
1302     ///       them from other data. You can obtain a pointer that is usable as `data`
1303     ///       for zero-length slices using [`NonNull::dangling()`].
1304     ///
1305     /// * The total size `ptr.len() * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`.
1306     ///   See the safety documentation of [`pointer::offset`].
1307     ///
1308     /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
1309     ///   arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
1310     ///   In particular, for the duration of this lifetime, the memory the pointer points to must
1311     ///   not get accessed (read or written) through any other pointer.
1312     ///
1313     /// This applies even if the result of this method is unused!
1314     ///
1315     /// See also [`slice::from_raw_parts_mut`][].
1316     ///
1317     /// [valid]: crate::ptr#safety
1318     /// [allocated object]: crate::ptr#allocated-object
1319     #[inline]
1320     #[unstable(feature = "ptr_as_uninit", issue = "75402")]
1321     pub unsafe fn as_uninit_slice_mut<'a>(self) -> Option<&'a mut [MaybeUninit<T>]> {
1322         if self.is_null() {
1323             None
1324         } else {
1325             // SAFETY: the caller must uphold the safety contract for `as_uninit_slice_mut`.
1326             Some(unsafe { slice::from_raw_parts_mut(self as *mut MaybeUninit<T>, self.len()) })
1327         }
1328     }
1329 }
1330
1331 // Equality for pointers
1332 #[stable(feature = "rust1", since = "1.0.0")]
1333 impl<T: ?Sized> PartialEq for *mut T {
1334     #[inline(always)]
1335     fn eq(&self, other: &*mut T) -> bool {
1336         *self == *other
1337     }
1338 }
1339
1340 #[stable(feature = "rust1", since = "1.0.0")]
1341 impl<T: ?Sized> Eq for *mut T {}
1342
1343 #[stable(feature = "rust1", since = "1.0.0")]
1344 impl<T: ?Sized> Ord for *mut T {
1345     #[inline]
1346     fn cmp(&self, other: &*mut T) -> Ordering {
1347         if self < other {
1348             Less
1349         } else if self == other {
1350             Equal
1351         } else {
1352             Greater
1353         }
1354     }
1355 }
1356
1357 #[stable(feature = "rust1", since = "1.0.0")]
1358 impl<T: ?Sized> PartialOrd for *mut T {
1359     #[inline(always)]
1360     fn partial_cmp(&self, other: &*mut T) -> Option<Ordering> {
1361         Some(self.cmp(other))
1362     }
1363
1364     #[inline(always)]
1365     fn lt(&self, other: &*mut T) -> bool {
1366         *self < *other
1367     }
1368
1369     #[inline(always)]
1370     fn le(&self, other: &*mut T) -> bool {
1371         *self <= *other
1372     }
1373
1374     #[inline(always)]
1375     fn gt(&self, other: &*mut T) -> bool {
1376         *self > *other
1377     }
1378
1379     #[inline(always)]
1380     fn ge(&self, other: &*mut T) -> bool {
1381         *self >= *other
1382     }
1383 }