2 use crate::cmp::Ordering::{self, Equal, Greater, Less};
5 use crate::slice::SliceIndex;
8 impl<T: ?Sized> *const T {
9 /// Returns `true` if the pointer is null.
11 /// Note that unsized types have many possible null pointers, as only the
12 /// raw data pointer is considered, not their length, vtable, etc.
13 /// Therefore, two pointers that are null may still not compare equal to
21 /// let s: &str = "Follow the rabbit";
22 /// let ptr: *const u8 = s.as_ptr();
23 /// assert!(!ptr.is_null());
25 #[stable(feature = "rust1", since = "1.0.0")]
27 pub fn is_null(self) -> bool {
28 // Compare via a cast to a thin pointer, so fat pointers are only
29 // considering their "data" part for null-ness.
30 (self as *const u8) == null()
33 /// Casts to a pointer of another type.
34 #[stable(feature = "ptr_cast", since = "1.38.0")]
35 #[rustc_const_stable(feature = "const_ptr_cast", since = "1.38.0")]
37 pub const fn cast<U>(self) -> *const U {
41 /// Returns `None` if the pointer is null, or else returns a reference to
42 /// the value wrapped in `Some`.
46 /// While this method and its mutable counterpart are useful for
47 /// null-safety, it is important to note that this is still an unsafe
48 /// operation because the returned value could be pointing to invalid
51 /// When calling this method, you have to ensure that *either* the pointer is NULL *or*
52 /// all of the following is true:
53 /// - it is properly aligned
54 /// - it must point to an initialized instance of T; in particular, the pointer must be
55 /// "dereferenceable" in the sense defined [here].
57 /// This applies even if the result of this method is unused!
58 /// (The part about being initialized is not yet fully decided, but until
59 /// it is, the only safe approach is to ensure that they are indeed initialized.)
61 /// Additionally, the lifetime `'a` returned is arbitrarily chosen and does
62 /// not necessarily reflect the actual lifetime of the data. *You* must enforce
63 /// Rust's aliasing rules. In particular, for the duration of this lifetime,
64 /// the memory the pointer points to must not get mutated (except inside `UnsafeCell`).
66 /// [here]: crate::ptr#safety
73 /// let ptr: *const u8 = &10u8 as *const u8;
76 /// if let Some(val_back) = ptr.as_ref() {
77 /// println!("We got back the value: {}!", val_back);
82 /// # Null-unchecked version
84 /// If you are sure the pointer can never be null and are looking for some kind of
85 /// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>`, know that you can
86 /// dereference the pointer directly.
89 /// let ptr: *const u8 = &10u8 as *const u8;
92 /// let val_back = &*ptr;
93 /// println!("We got back the value: {}!", val_back);
96 #[stable(feature = "ptr_as_ref", since = "1.9.0")]
98 pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
99 // SAFETY: the caller must guarantee that `self` is valid
100 // for a reference if it isn't null.
101 if self.is_null() { None } else { unsafe { Some(&*self) } }
104 /// Calculates the offset from a pointer.
106 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
107 /// offset of `3 * size_of::<T>()` bytes.
111 /// If any of the following conditions are violated, the result is Undefined
114 /// * Both the starting and resulting pointer must be either in bounds or one
115 /// byte past the end of the same allocated object. Note that in Rust,
116 /// every (stack-allocated) variable is considered a separate allocated object.
118 /// * The computed offset, **in bytes**, cannot overflow an `isize`.
120 /// * The offset being in bounds cannot rely on "wrapping around" the address
121 /// space. That is, the infinite-precision sum, **in bytes** must fit in a usize.
123 /// The compiler and standard library generally tries to ensure allocations
124 /// never reach a size where an offset is a concern. For instance, `Vec`
125 /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
126 /// `vec.as_ptr().add(vec.len())` is always safe.
128 /// Most platforms fundamentally can't even construct such an allocation.
129 /// For instance, no known 64-bit platform can ever serve a request
130 /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
131 /// However, some 32-bit and 16-bit platforms may successfully serve a request for
132 /// more than `isize::MAX` bytes with things like Physical Address
133 /// Extension. As such, memory acquired directly from allocators or memory
134 /// mapped files *may* be too large to handle with this function.
136 /// Consider using [`wrapping_offset`] instead if these constraints are
137 /// difficult to satisfy. The only advantage of this method is that it
138 /// enables more aggressive compiler optimizations.
140 /// [`wrapping_offset`]: #method.wrapping_offset
147 /// let s: &str = "123";
148 /// let ptr: *const u8 = s.as_ptr();
151 /// println!("{}", *ptr.offset(1) as char);
152 /// println!("{}", *ptr.offset(2) as char);
155 #[stable(feature = "rust1", since = "1.0.0")]
156 #[must_use = "returns a new pointer rather than modifying its argument"]
157 #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
159 pub const unsafe fn offset(self, count: isize) -> *const T
163 // SAFETY: the caller must uphold the safety contract for `offset`.
164 unsafe { intrinsics::offset(self, count) }
167 /// Calculates the offset from a pointer using wrapping arithmetic.
169 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
170 /// offset of `3 * size_of::<T>()` bytes.
174 /// The resulting pointer does not need to be in bounds, but it is
175 /// potentially hazardous to dereference (which requires `unsafe`).
177 /// In particular, the resulting pointer remains attached to the same allocated
178 /// object that `self` points to. It may *not* be used to access a
179 /// different allocated object. Note that in Rust,
180 /// every (stack-allocated) variable is considered a separate allocated object.
182 /// In other words, `x.wrapping_offset(y.wrapping_offset_from(x))` is
183 /// *not* the same as `y`, and dereferencing it is undefined behavior
184 /// unless `x` and `y` point into the same allocated object.
186 /// Compared to [`offset`], this method basically delays the requirement of staying
187 /// within the same allocated object: [`offset`] is immediate Undefined Behavior when
188 /// crossing object boundaries; `wrapping_offset` produces a pointer but still leads
189 /// to Undefined Behavior if that pointer is dereferenced. [`offset`] can be optimized
190 /// better and is thus preferable in performance-sensitive code.
192 /// If you need to cross object boundaries, cast the pointer to an integer and
193 /// do the arithmetic there.
195 /// [`offset`]: #method.offset
202 /// // Iterate using a raw pointer in increments of two elements
203 /// let data = [1u8, 2, 3, 4, 5];
204 /// let mut ptr: *const u8 = data.as_ptr();
206 /// let end_rounded_up = ptr.wrapping_offset(6);
208 /// // This loop prints "1, 3, 5, "
209 /// while ptr != end_rounded_up {
211 /// print!("{}, ", *ptr);
213 /// ptr = ptr.wrapping_offset(step);
216 #[stable(feature = "ptr_wrapping_offset", since = "1.16.0")]
217 #[must_use = "returns a new pointer rather than modifying its argument"]
218 #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
220 pub const fn wrapping_offset(self, count: isize) -> *const T
224 // SAFETY: the `arith_offset` intrinsic has no prerequisites to be called.
225 unsafe { intrinsics::arith_offset(self, count) }
228 /// Calculates the distance between two pointers. The returned value is in
229 /// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
231 /// This function is the inverse of [`offset`].
233 /// [`offset`]: #method.offset
234 /// [`wrapping_offset_from`]: #method.wrapping_offset_from
238 /// If any of the following conditions are violated, the result is Undefined
241 /// * Both the starting and other pointer must be either in bounds or one
242 /// byte past the end of the same allocated object. Note that in Rust,
243 /// every (stack-allocated) variable is considered a separate allocated object.
245 /// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
247 /// * The distance between the pointers, in bytes, must be an exact multiple
248 /// of the size of `T`.
250 /// * The distance being in bounds cannot rely on "wrapping around" the address space.
252 /// The compiler and standard library generally try to ensure allocations
253 /// never reach a size where an offset is a concern. For instance, `Vec`
254 /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
255 /// `ptr_into_vec.offset_from(vec.as_ptr())` is always safe.
257 /// Most platforms fundamentally can't even construct such an allocation.
258 /// For instance, no known 64-bit platform can ever serve a request
259 /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
260 /// However, some 32-bit and 16-bit platforms may successfully serve a request for
261 /// more than `isize::MAX` bytes with things like Physical Address
262 /// Extension. As such, memory acquired directly from allocators or memory
263 /// mapped files *may* be too large to handle with this function.
265 /// Consider using [`wrapping_offset_from`] instead if these constraints are
266 /// difficult to satisfy. The only advantage of this method is that it
267 /// enables more aggressive compiler optimizations.
271 /// This function panics if `T` is a Zero-Sized Type ("ZST").
278 /// #![feature(ptr_offset_from)]
281 /// let ptr1: *const i32 = &a[1];
282 /// let ptr2: *const i32 = &a[3];
284 /// assert_eq!(ptr2.offset_from(ptr1), 2);
285 /// assert_eq!(ptr1.offset_from(ptr2), -2);
286 /// assert_eq!(ptr1.offset(2), ptr2);
287 /// assert_eq!(ptr2.offset(-2), ptr1);
290 #[unstable(feature = "ptr_offset_from", issue = "41079")]
291 #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "41079")]
293 pub const unsafe fn offset_from(self, origin: *const T) -> isize
297 let pointee_size = mem::size_of::<T>();
298 assert!(0 < pointee_size && pointee_size <= isize::MAX as usize);
299 // SAFETY: the caller must uphold the safety contract for `ptr_offset_from`.
300 unsafe { intrinsics::ptr_offset_from(self, origin) }
303 /// Returns whether two pointers are guaranteed to be equal.
305 /// At runtime this function behaves like `self == other`.
306 /// However, in some contexts (e.g., compile-time evaluation),
307 /// it is not always possible to determine equality of two pointers, so this function may
308 /// spuriously return `false` for pointers that later actually turn out to be equal.
309 /// But when it returns `true`, the pointers are guaranteed to be equal.
311 /// This function is the mirror of [`guaranteed_ne`], but not its inverse. There are pointer
312 /// comparisons for which both functions return `false`.
314 /// [`guaranteed_ne`]: #method.guaranteed_ne
316 /// The return value may change depending on the compiler version and unsafe code may not
317 /// rely on the result of this function for soundness. It is suggested to only use this function
318 /// for performance optimizations where spurious `false` return values by this function do not
319 /// affect the outcome, but just the performance.
320 /// The consequences of using this method to make runtime and compile-time code behave
321 /// differently have not been explored. This method should not be used to introduce such
322 /// differences, and it should also not be stabilized before we have a better understanding
324 #[unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
325 #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
327 #[cfg(not(bootstrap))]
328 pub const fn guaranteed_eq(self, other: *const T) -> bool
332 intrinsics::ptr_guaranteed_eq(self, other)
335 /// Returns whether two pointers are guaranteed to be inequal.
337 /// At runtime this function behaves like `self != other`.
338 /// However, in some contexts (e.g., compile-time evaluation),
339 /// it is not always possible to determine the inequality of two pointers, so this function may
340 /// spuriously return `false` for pointers that later actually turn out to be inequal.
341 /// But when it returns `true`, the pointers are guaranteed to be inequal.
343 /// This function is the mirror of [`guaranteed_eq`], but not its inverse. There are pointer
344 /// comparisons for which both functions return `false`.
346 /// [`guaranteed_eq`]: #method.guaranteed_eq
348 /// The return value may change depending on the compiler version and unsafe code may not
349 /// rely on the result of this function for soundness. It is suggested to only use this function
350 /// for performance optimizations where spurious `false` return values by this function do not
351 /// affect the outcome, but just the performance.
352 /// The consequences of using this method to make runtime and compile-time code behave
353 /// differently have not been explored. This method should not be used to introduce such
354 /// differences, and it should also not be stabilized before we have a better understanding
356 #[unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
357 #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
359 #[cfg(not(bootstrap))]
360 pub const fn guaranteed_ne(self, other: *const T) -> bool
364 intrinsics::ptr_guaranteed_ne(self, other)
367 /// Calculates the distance between two pointers. The returned value is in
368 /// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
370 /// If the address different between the two pointers is not a multiple of
371 /// `mem::size_of::<T>()` then the result of the division is rounded towards
374 /// Though this method is safe for any two pointers, note that its result
375 /// will be mostly useless if the two pointers aren't into the same allocated
376 /// object, for example if they point to two different local variables.
380 /// This function panics if `T` is a zero-sized type.
387 /// #![feature(ptr_wrapping_offset_from)]
390 /// let ptr1: *const i32 = &a[1];
391 /// let ptr2: *const i32 = &a[3];
392 /// assert_eq!(ptr2.wrapping_offset_from(ptr1), 2);
393 /// assert_eq!(ptr1.wrapping_offset_from(ptr2), -2);
394 /// assert_eq!(ptr1.wrapping_offset(2), ptr2);
395 /// assert_eq!(ptr2.wrapping_offset(-2), ptr1);
397 /// let ptr1: *const i32 = 3 as _;
398 /// let ptr2: *const i32 = 13 as _;
399 /// assert_eq!(ptr2.wrapping_offset_from(ptr1), 2);
401 #[unstable(feature = "ptr_wrapping_offset_from", issue = "41079")]
404 reason = "Pointer distances across allocation \
405 boundaries are not typically meaningful. \
406 Use integer subtraction if you really need this."
409 pub fn wrapping_offset_from(self, origin: *const T) -> isize
413 let pointee_size = mem::size_of::<T>();
414 assert!(0 < pointee_size && pointee_size <= isize::MAX as usize);
416 let d = isize::wrapping_sub(self as _, origin as _);
417 d.wrapping_div(pointee_size as _)
420 /// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
422 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
423 /// offset of `3 * size_of::<T>()` bytes.
427 /// If any of the following conditions are violated, the result is Undefined
430 /// * Both the starting and resulting pointer must be either in bounds or one
431 /// byte past the end of the same allocated object. Note that in Rust,
432 /// every (stack-allocated) variable is considered a separate allocated object.
434 /// * The computed offset, **in bytes**, cannot overflow an `isize`.
436 /// * The offset being in bounds cannot rely on "wrapping around" the address
437 /// space. That is, the infinite-precision sum must fit in a `usize`.
439 /// The compiler and standard library generally tries to ensure allocations
440 /// never reach a size where an offset is a concern. For instance, `Vec`
441 /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
442 /// `vec.as_ptr().add(vec.len())` is always safe.
444 /// Most platforms fundamentally can't even construct such an allocation.
445 /// For instance, no known 64-bit platform can ever serve a request
446 /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
447 /// However, some 32-bit and 16-bit platforms may successfully serve a request for
448 /// more than `isize::MAX` bytes with things like Physical Address
449 /// Extension. As such, memory acquired directly from allocators or memory
450 /// mapped files *may* be too large to handle with this function.
452 /// Consider using [`wrapping_add`] instead if these constraints are
453 /// difficult to satisfy. The only advantage of this method is that it
454 /// enables more aggressive compiler optimizations.
456 /// [`wrapping_add`]: #method.wrapping_add
463 /// let s: &str = "123";
464 /// let ptr: *const u8 = s.as_ptr();
467 /// println!("{}", *ptr.add(1) as char);
468 /// println!("{}", *ptr.add(2) as char);
471 #[stable(feature = "pointer_methods", since = "1.26.0")]
472 #[must_use = "returns a new pointer rather than modifying its argument"]
473 #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
475 pub const unsafe fn add(self, count: usize) -> Self
479 // SAFETY: the caller must uphold the safety contract for `offset`.
480 unsafe { self.offset(count as isize) }
483 /// Calculates the offset from a pointer (convenience for
484 /// `.offset((count as isize).wrapping_neg())`).
486 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
487 /// offset of `3 * size_of::<T>()` bytes.
491 /// If any of the following conditions are violated, the result is Undefined
494 /// * Both the starting and resulting pointer must be either in bounds or one
495 /// byte past the end of the same allocated object. Note that in Rust,
496 /// every (stack-allocated) variable is considered a separate allocated object.
498 /// * The computed offset cannot exceed `isize::MAX` **bytes**.
500 /// * The offset being in bounds cannot rely on "wrapping around" the address
501 /// space. That is, the infinite-precision sum must fit in a usize.
503 /// The compiler and standard library generally tries to ensure allocations
504 /// never reach a size where an offset is a concern. For instance, `Vec`
505 /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
506 /// `vec.as_ptr().add(vec.len()).sub(vec.len())` is always safe.
508 /// Most platforms fundamentally can't even construct such an allocation.
509 /// For instance, no known 64-bit platform can ever serve a request
510 /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
511 /// However, some 32-bit and 16-bit platforms may successfully serve a request for
512 /// more than `isize::MAX` bytes with things like Physical Address
513 /// Extension. As such, memory acquired directly from allocators or memory
514 /// mapped files *may* be too large to handle with this function.
516 /// Consider using [`wrapping_sub`] instead if these constraints are
517 /// difficult to satisfy. The only advantage of this method is that it
518 /// enables more aggressive compiler optimizations.
520 /// [`wrapping_sub`]: #method.wrapping_sub
527 /// let s: &str = "123";
530 /// let end: *const u8 = s.as_ptr().add(3);
531 /// println!("{}", *end.sub(1) as char);
532 /// println!("{}", *end.sub(2) as char);
535 #[stable(feature = "pointer_methods", since = "1.26.0")]
536 #[must_use = "returns a new pointer rather than modifying its argument"]
537 #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
539 pub const unsafe fn sub(self, count: usize) -> Self
543 // SAFETY: the caller must uphold the safety contract for `offset`.
544 unsafe { self.offset((count as isize).wrapping_neg()) }
547 /// Calculates the offset from a pointer using wrapping arithmetic.
548 /// (convenience for `.wrapping_offset(count as isize)`)
550 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
551 /// offset of `3 * size_of::<T>()` bytes.
555 /// The resulting pointer does not need to be in bounds, but it is
556 /// potentially hazardous to dereference (which requires `unsafe`).
558 /// In particular, the resulting pointer remains attached to the same allocated
559 /// object that `self` points to. It may *not* be used to access a
560 /// different allocated object. Note that in Rust,
561 /// every (stack-allocated) variable is considered a separate allocated object.
563 /// Compared to [`add`], this method basically delays the requirement of staying
564 /// within the same allocated object: [`add`] is immediate Undefined Behavior when
565 /// crossing object boundaries; `wrapping_add` produces a pointer but still leads
566 /// to Undefined Behavior if that pointer is dereferenced. [`add`] can be optimized
567 /// better and is thus preferable in performance-sensitive code.
569 /// If you need to cross object boundaries, cast the pointer to an integer and
570 /// do the arithmetic there.
572 /// [`add`]: #method.add
579 /// // Iterate using a raw pointer in increments of two elements
580 /// let data = [1u8, 2, 3, 4, 5];
581 /// let mut ptr: *const u8 = data.as_ptr();
583 /// let end_rounded_up = ptr.wrapping_add(6);
585 /// // This loop prints "1, 3, 5, "
586 /// while ptr != end_rounded_up {
588 /// print!("{}, ", *ptr);
590 /// ptr = ptr.wrapping_add(step);
593 #[stable(feature = "pointer_methods", since = "1.26.0")]
594 #[must_use = "returns a new pointer rather than modifying its argument"]
595 #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
597 pub const fn wrapping_add(self, count: usize) -> Self
601 self.wrapping_offset(count as isize)
604 /// Calculates the offset from a pointer using wrapping arithmetic.
605 /// (convenience for `.wrapping_offset((count as isize).wrapping_sub())`)
607 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
608 /// offset of `3 * size_of::<T>()` bytes.
612 /// The resulting pointer does not need to be in bounds, but it is
613 /// potentially hazardous to dereference (which requires `unsafe`).
615 /// In particular, the resulting pointer remains attached to the same allocated
616 /// object that `self` points to. It may *not* be used to access a
617 /// different allocated object. Note that in Rust,
618 /// every (stack-allocated) variable is considered a separate allocated object.
620 /// Compared to [`sub`], this method basically delays the requirement of staying
621 /// within the same allocated object: [`sub`] is immediate Undefined Behavior when
622 /// crossing object boundaries; `wrapping_sub` produces a pointer but still leads
623 /// to Undefined Behavior if that pointer is dereferenced. [`sub`] can be optimized
624 /// better and is thus preferable in performance-sensitive code.
626 /// If you need to cross object boundaries, cast the pointer to an integer and
627 /// do the arithmetic there.
629 /// [`sub`]: #method.sub
636 /// // Iterate using a raw pointer in increments of two elements (backwards)
637 /// let data = [1u8, 2, 3, 4, 5];
638 /// let mut ptr: *const u8 = data.as_ptr();
639 /// let start_rounded_down = ptr.wrapping_sub(2);
640 /// ptr = ptr.wrapping_add(4);
642 /// // This loop prints "5, 3, 1, "
643 /// while ptr != start_rounded_down {
645 /// print!("{}, ", *ptr);
647 /// ptr = ptr.wrapping_sub(step);
650 #[stable(feature = "pointer_methods", since = "1.26.0")]
651 #[must_use = "returns a new pointer rather than modifying its argument"]
652 #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
654 pub const fn wrapping_sub(self, count: usize) -> Self
658 self.wrapping_offset((count as isize).wrapping_neg())
661 /// Reads the value from `self` without moving it. This leaves the
662 /// memory in `self` unchanged.
664 /// See [`ptr::read`] for safety concerns and examples.
666 /// [`ptr::read`]: ./ptr/fn.read.html
667 #[stable(feature = "pointer_methods", since = "1.26.0")]
669 pub unsafe fn read(self) -> T
673 // SAFETY: the caller must uphold the safety contract for `read`.
674 unsafe { read(self) }
677 /// Performs a volatile read of the value from `self` without moving it. This
678 /// leaves the memory in `self` unchanged.
680 /// Volatile operations are intended to act on I/O memory, and are guaranteed
681 /// to not be elided or reordered by the compiler across other volatile
684 /// See [`ptr::read_volatile`] for safety concerns and examples.
686 /// [`ptr::read_volatile`]: ./ptr/fn.read_volatile.html
687 #[stable(feature = "pointer_methods", since = "1.26.0")]
689 pub unsafe fn read_volatile(self) -> T
693 // SAFETY: the caller must uphold the safety contract for `read_volatile`.
694 unsafe { read_volatile(self) }
697 /// Reads the value from `self` without moving it. This leaves the
698 /// memory in `self` unchanged.
700 /// Unlike `read`, the pointer may be unaligned.
702 /// See [`ptr::read_unaligned`] for safety concerns and examples.
704 /// [`ptr::read_unaligned`]: ./ptr/fn.read_unaligned.html
705 #[stable(feature = "pointer_methods", since = "1.26.0")]
707 pub unsafe fn read_unaligned(self) -> T
711 // SAFETY: the caller must uphold the safety contract for `read_unaligned`.
712 unsafe { read_unaligned(self) }
715 /// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
716 /// and destination may overlap.
718 /// NOTE: this has the *same* argument order as [`ptr::copy`].
720 /// See [`ptr::copy`] for safety concerns and examples.
722 /// [`ptr::copy`]: ./ptr/fn.copy.html
723 #[stable(feature = "pointer_methods", since = "1.26.0")]
725 pub unsafe fn copy_to(self, dest: *mut T, count: usize)
729 // SAFETY: the caller must uphold the safety contract for `copy`.
730 unsafe { copy(self, dest, count) }
733 /// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
734 /// and destination may *not* overlap.
736 /// NOTE: this has the *same* argument order as [`ptr::copy_nonoverlapping`].
738 /// See [`ptr::copy_nonoverlapping`] for safety concerns and examples.
740 /// [`ptr::copy_nonoverlapping`]: ./ptr/fn.copy_nonoverlapping.html
741 #[stable(feature = "pointer_methods", since = "1.26.0")]
743 pub unsafe fn copy_to_nonoverlapping(self, dest: *mut T, count: usize)
747 // SAFETY: the caller must uphold the safety contract for `copy_nonoverlapping`.
748 unsafe { copy_nonoverlapping(self, dest, count) }
751 /// Computes the offset that needs to be applied to the pointer in order to make it aligned to
754 /// If it is not possible to align the pointer, the implementation returns
755 /// `usize::MAX`. It is permissible for the implementation to *always*
756 /// return `usize::MAX`. Only your algorithm's performance can depend
757 /// on getting a usable offset here, not its correctness.
759 /// The offset is expressed in number of `T` elements, and not bytes. The value returned can be
760 /// used with the `wrapping_add` method.
762 /// There are no guarantees whatsoever that offsetting the pointer will not overflow or go
763 /// beyond the allocation that the pointer points into. It is up to the caller to ensure that
764 /// the returned offset is correct in all terms other than alignment.
768 /// The function panics if `align` is not a power-of-two.
772 /// Accessing adjacent `u8` as `u16`
775 /// # fn foo(n: usize) {
776 /// # use std::mem::align_of;
778 /// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
779 /// let ptr = &x[n] as *const u8;
780 /// let offset = ptr.align_offset(align_of::<u16>());
781 /// if offset < x.len() - n - 1 {
782 /// let u16_ptr = ptr.add(offset) as *const u16;
783 /// assert_ne!(*u16_ptr, 500);
785 /// // while the pointer can be aligned via `offset`, it would point
786 /// // outside the allocation
790 #[stable(feature = "align_offset", since = "1.36.0")]
791 pub fn align_offset(self, align: usize) -> usize
795 if !align.is_power_of_two() {
796 panic!("align_offset: align is not a power-of-two");
798 // SAFETY: `align` has been checked to be a power of 2 above
799 unsafe { align_offset(self, align) }
803 #[lang = "const_slice_ptr"]
805 /// Returns the length of a raw slice.
807 /// The returned value is the number of **elements**, not the number of bytes.
809 /// This function is safe, even when the raw slice cannot be cast to a slice
810 /// reference because the pointer is null or unaligned.
815 /// #![feature(slice_ptr_len)]
819 /// let slice: *const [i8] = ptr::slice_from_raw_parts(ptr::null(), 3);
820 /// assert_eq!(slice.len(), 3);
823 #[unstable(feature = "slice_ptr_len", issue = "71146")]
824 #[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
825 pub const fn len(self) -> usize {
826 // SAFETY: this is safe because `*const [T]` and `FatPtr<T>` have the same layout.
827 // Only `std` can make this guarantee.
828 unsafe { Repr { rust: self }.raw }.len
831 /// Returns a raw pointer to the slice's buffer.
833 /// This is equivalent to casting `self` to `*const T`, but more type-safe.
838 /// #![feature(slice_ptr_get)]
841 /// let slice: *const [i8] = ptr::slice_from_raw_parts(ptr::null(), 3);
842 /// assert_eq!(slice.as_ptr(), 0 as *const i8);
845 #[unstable(feature = "slice_ptr_get", issue = "none")]
846 #[rustc_const_unstable(feature = "slice_ptr_get", issue = "none")]
847 pub const fn as_ptr(self) -> *const T {
851 /// Returns a raw pointer to an element or subslice, without doing bounds
854 /// Calling this method with an out-of-bounds index or when `self` is not dereferencable
855 /// is *[undefined behavior]* even if the resulting pointer is not used.
857 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
862 /// #![feature(slice_ptr_get)]
864 /// let x = &[1, 2, 4] as *const [i32];
867 /// assert_eq!(x.get_unchecked(1), x.as_ptr().add(1));
870 #[unstable(feature = "slice_ptr_get", issue = "none")]
872 pub unsafe fn get_unchecked<I>(self, index: I) -> *const I::Output
876 // SAFETY: the caller ensures that `self` is dereferencable and `index` in-bounds.
877 unsafe { index.get_unchecked(self) }
881 // Equality for pointers
882 #[stable(feature = "rust1", since = "1.0.0")]
883 impl<T: ?Sized> PartialEq for *const T {
885 fn eq(&self, other: &*const T) -> bool {
890 #[stable(feature = "rust1", since = "1.0.0")]
891 impl<T: ?Sized> Eq for *const T {}
893 // Comparison for pointers
894 #[stable(feature = "rust1", since = "1.0.0")]
895 impl<T: ?Sized> Ord for *const T {
897 fn cmp(&self, other: &*const T) -> Ordering {
900 } else if self == other {
908 #[stable(feature = "rust1", since = "1.0.0")]
909 impl<T: ?Sized> PartialOrd for *const T {
911 fn partial_cmp(&self, other: &*const T) -> Option<Ordering> {
912 Some(self.cmp(other))
916 fn lt(&self, other: &*const T) -> bool {
921 fn le(&self, other: &*const T) -> bool {
926 fn gt(&self, other: &*const T) -> bool {
931 fn ge(&self, other: &*const T) -> bool {