2 use crate::cmp
::Ordering
::{self, Equal, Greater, Less}
;
4 use crate::slice
::{self, SliceIndex}
;
7 impl<T
: ?Sized
> *mut T
{
8 /// Returns `true` if the pointer is null.
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
15 /// ## Behavior during const evaluation
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
29 /// let mut s = [1, 2, 3];
30 /// let ptr: *mut u32 = s.as_mut_ptr();
31 /// assert!(!ptr.is_null());
33 #[stable(feature = "rust1", since = "1.0.0")]
34 #[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
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())
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")]
46 pub const fn cast
<U
>(self) -> *mut U
{
50 /// Decompose a (possibly wide) pointer into is address and metadata components.
52 /// The pointer can be later reconstructed with [`from_raw_parts_mut`].
53 #[cfg(not(bootstrap))]
54 #[unstable(feature = "ptr_metadata", issue = "81513")]
55 #[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
57 pub const fn to_raw_parts(self) -> (*mut (), <T
as super::Pointee
>::Metadata
) {
58 (self.cast(), super::metadata(self))
61 /// Returns `None` if the pointer is null, or else returns a shared reference to
62 /// the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_ref`]
63 /// must be used instead.
65 /// For the mutable counterpart see [`as_mut`].
67 /// [`as_uninit_ref`]: #method.as_uninit_ref-1
68 /// [`as_mut`]: #method.as_mut
72 /// When calling this method, you have to ensure that *either* the pointer is NULL *or*
73 /// all of the following is true:
75 /// * The pointer must be properly aligned.
77 /// * It must be "dereferencable" in the sense defined in [the module documentation].
79 /// * The pointer must point to an initialized instance of `T`.
81 /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
82 /// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
83 /// In particular, for the duration of this lifetime, the memory the pointer points to must
84 /// not get mutated (except inside `UnsafeCell`).
86 /// This applies even if the result of this method is unused!
87 /// (The part about being initialized is not yet fully decided, but until
88 /// it is, the only safe approach is to ensure that they are indeed initialized.)
90 /// [the module documentation]: crate::ptr#safety
97 /// let ptr: *mut u8 = &mut 10u8 as *mut u8;
100 /// if let Some(val_back) = ptr.as_ref() {
101 /// println!("We got back the value: {}!", val_back);
106 /// # Null-unchecked version
108 /// If you are sure the pointer can never be null and are looking for some kind of
109 /// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>`, know that you can
110 /// dereference the pointer directly.
113 /// let ptr: *mut u8 = &mut 10u8 as *mut u8;
116 /// let val_back = &*ptr;
117 /// println!("We got back the value: {}!", val_back);
120 #[stable(feature = "ptr_as_ref", since = "1.9.0")]
122 pub unsafe fn as_ref
<'a
>(self) -> Option
<&'a T
> {
123 // SAFETY: the caller must guarantee that `self` is valid for a
124 // reference if it isn't null.
125 if self.is_null() { None }
else { unsafe { Some(&*self) }
}
128 /// Returns `None` if the pointer is null, or else returns a shared reference to
129 /// the value wrapped in `Some`. In contrast to [`as_ref`], this does not require
130 /// that the value has to be initialized.
132 /// For the mutable counterpart see [`as_uninit_mut`].
134 /// [`as_ref`]: #method.as_ref-1
135 /// [`as_uninit_mut`]: #method.as_uninit_mut
139 /// When calling this method, you have to ensure that *either* the pointer is NULL *or*
140 /// all of the following is true:
142 /// * The pointer must be properly aligned.
144 /// * It must be "dereferencable" in the sense defined in [the module documentation].
146 /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
147 /// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
148 /// In particular, for the duration of this lifetime, the memory the pointer points to must
149 /// not get mutated (except inside `UnsafeCell`).
151 /// This applies even if the result of this method is unused!
153 /// [the module documentation]: crate::ptr#safety
160 /// #![feature(ptr_as_uninit)]
162 /// let ptr: *mut u8 = &mut 10u8 as *mut u8;
165 /// if let Some(val_back) = ptr.as_uninit_ref() {
166 /// println!("We got back the value: {}!", val_back.assume_init());
171 #[unstable(feature = "ptr_as_uninit", issue = "75402")]
172 pub unsafe fn as_uninit_ref
<'a
>(self) -> Option
<&'a MaybeUninit
<T
>>
176 // SAFETY: the caller must guarantee that `self` meets all the
177 // requirements for a reference.
178 if self.is_null() { None }
else { Some(unsafe { &*(self as *const MaybeUninit<T>) }
) }
181 /// Calculates the offset from a pointer.
183 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
184 /// offset of `3 * size_of::<T>()` bytes.
188 /// If any of the following conditions are violated, the result is Undefined
191 /// * Both the starting and resulting pointer must be either in bounds or one
192 /// byte past the end of the same allocated object. Note that in Rust,
193 /// every (stack-allocated) variable is considered a separate allocated object.
195 /// * The computed offset, **in bytes**, cannot overflow an `isize`.
197 /// * The offset being in bounds cannot rely on "wrapping around" the address
198 /// space. That is, the infinite-precision sum, **in bytes** must fit in a usize.
200 /// The compiler and standard library generally tries to ensure allocations
201 /// never reach a size where an offset is a concern. For instance, `Vec`
202 /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
203 /// `vec.as_ptr().add(vec.len())` is always safe.
205 /// Most platforms fundamentally can't even construct such an allocation.
206 /// For instance, no known 64-bit platform can ever serve a request
207 /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
208 /// However, some 32-bit and 16-bit platforms may successfully serve a request for
209 /// more than `isize::MAX` bytes with things like Physical Address
210 /// Extension. As such, memory acquired directly from allocators or memory
211 /// mapped files *may* be too large to handle with this function.
213 /// Consider using [`wrapping_offset`] instead if these constraints are
214 /// difficult to satisfy. The only advantage of this method is that it
215 /// enables more aggressive compiler optimizations.
217 /// [`wrapping_offset`]: #method.wrapping_offset
224 /// let mut s = [1, 2, 3];
225 /// let ptr: *mut u32 = s.as_mut_ptr();
228 /// println!("{}", *ptr.offset(1));
229 /// println!("{}", *ptr.offset(2));
232 #[stable(feature = "rust1", since = "1.0.0")]
233 #[must_use = "returns a new pointer rather than modifying its argument"]
234 #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
236 pub const unsafe fn offset(self, count
: isize) -> *mut T
240 // SAFETY: the caller must uphold the safety contract for `offset`.
241 // The obtained pointer is valid for writes since the caller must
242 // guarantee that it points to the same allocated object as `self`.
243 unsafe { intrinsics::offset(self, count) as *mut T }
246 /// Calculates the offset from a pointer using wrapping arithmetic.
247 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
248 /// offset of `3 * size_of::<T>()` bytes.
252 /// This operation itself is always safe, but using the resulting pointer is not.
254 /// The resulting pointer remains attached to the same allocated object that `self` points to.
255 /// It may *not* be used to access a different allocated object. Note that in Rust, every
256 /// (stack-allocated) variable is considered a separate allocated object.
258 /// In other words, `let z = x.wrapping_offset((y as isize) - (x as isize))` does *not* make `z`
259 /// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
260 /// attached to the object `x` is attached to, and dereferencing it is Undefined Behavior unless
261 /// `x` and `y` point into the same allocated object.
263 /// Compared to [`offset`], this method basically delays the requirement of staying within the
264 /// same allocated object: [`offset`] is immediate Undefined Behavior when crossing object
265 /// boundaries; `wrapping_offset` produces a pointer but still leads to Undefined Behavior if a
266 /// pointer is dereferenced when it is out-of-bounds of the object it is attached to. [`offset`]
267 /// can be optimized better and is thus preferable in performance-sensitive code.
269 /// The delayed check only considers the value of the pointer that was dereferenced, not the
270 /// intermediate values used during the computation of the final result. For example,
271 /// `x.wrapping_offset(o).wrapping_offset(o.wrapping_neg())` is always the same as `x`. In other
272 /// words, leaving the allocated object and then re-entering it later is permitted.
274 /// If you need to cross object boundaries, cast the pointer to an integer and
275 /// do the arithmetic there.
277 /// [`offset`]: #method.offset
284 /// // Iterate using a raw pointer in increments of two elements
285 /// let mut data = [1u8, 2, 3, 4, 5];
286 /// let mut ptr: *mut u8 = data.as_mut_ptr();
288 /// let end_rounded_up = ptr.wrapping_offset(6);
290 /// while ptr != end_rounded_up {
294 /// ptr = ptr.wrapping_offset(step);
296 /// assert_eq!(&data, &[0, 2, 0, 4, 0]);
298 #[stable(feature = "ptr_wrapping_offset", since = "1.16.0")]
299 #[must_use = "returns a new pointer rather than modifying its argument"]
300 #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
302 pub const fn wrapping_offset(self, count
: isize) -> *mut T
306 // SAFETY: the `arith_offset` intrinsic has no prerequisites to be called.
307 unsafe { intrinsics::arith_offset(self, count) as *mut T }
310 /// Returns `None` if the pointer is null, or else returns a unique reference to
311 /// the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_mut`]
312 /// must be used instead.
314 /// For the shared counterpart see [`as_ref`].
316 /// [`as_uninit_mut`]: #method.as_uninit_mut
317 /// [`as_ref`]: #method.as_ref-1
321 /// When calling this method, you have to ensure that *either* the pointer is NULL *or*
322 /// all of the following is true:
324 /// * The pointer must be properly aligned.
326 /// * It must be "dereferencable" in the sense defined in [the module documentation].
328 /// * The pointer must point to an initialized instance of `T`.
330 /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
331 /// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
332 /// In particular, for the duration of this lifetime, the memory the pointer points to must
333 /// not get accessed (read or written) through any other pointer.
335 /// This applies even if the result of this method is unused!
336 /// (The part about being initialized is not yet fully decided, but until
337 /// it is, the only safe approach is to ensure that they are indeed initialized.)
339 /// [the module documentation]: crate::ptr#safety
346 /// let mut s = [1, 2, 3];
347 /// let ptr: *mut u32 = s.as_mut_ptr();
348 /// let first_value = unsafe { ptr.as_mut().unwrap() };
349 /// *first_value = 4;
350 /// # assert_eq!(s, [4, 2, 3]);
351 /// println!("{:?}", s); // It'll print: "[4, 2, 3]".
354 /// # Null-unchecked version
356 /// If you are sure the pointer can never be null and are looking for some kind of
357 /// `as_mut_unchecked` that returns the `&mut T` instead of `Option<&mut T>`, know that
358 /// you can dereference the pointer directly.
361 /// let mut s = [1, 2, 3];
362 /// let ptr: *mut u32 = s.as_mut_ptr();
363 /// let first_value = unsafe { &mut *ptr };
364 /// *first_value = 4;
365 /// # assert_eq!(s, [4, 2, 3]);
366 /// println!("{:?}", s); // It'll print: "[4, 2, 3]".
368 #[stable(feature = "ptr_as_ref", since = "1.9.0")]
370 pub unsafe fn as_mut
<'a
>(self) -> Option
<&'a
mut T
> {
371 // SAFETY: the caller must guarantee that `self` is be valid for
372 // a mutable reference if it isn't null.
373 if self.is_null() { None }
else { unsafe { Some(&mut *self) }
}
376 /// Returns `None` if the pointer is null, or else returns a unique reference to
377 /// the value wrapped in `Some`. In contrast to [`as_mut`], this does not require
378 /// that the value has to be initialized.
380 /// For the shared counterpart see [`as_uninit_ref`].
382 /// [`as_mut`]: #method.as_mut
383 /// [`as_uninit_ref`]: #method.as_uninit_ref-1
387 /// When calling this method, you have to ensure that *either* the pointer is NULL *or*
388 /// all of the following is true:
390 /// * The pointer must be properly aligned.
392 /// * It must be "dereferencable" in the sense defined in [the module documentation].
394 /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
395 /// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
396 /// In particular, for the duration of this lifetime, the memory the pointer points to must
397 /// not get accessed (read or written) through any other pointer.
399 /// This applies even if the result of this method is unused!
401 /// [the module documentation]: crate::ptr#safety
403 #[unstable(feature = "ptr_as_uninit", issue = "75402")]
404 pub unsafe fn as_uninit_mut
<'a
>(self) -> Option
<&'a
mut MaybeUninit
<T
>>
408 // SAFETY: the caller must guarantee that `self` meets all the
409 // requirements for a reference.
410 if self.is_null() { None }
else { Some(unsafe { &mut *(self as *mut MaybeUninit<T>) }
) }
413 /// Returns whether two pointers are guaranteed to be equal.
415 /// At runtime this function behaves like `self == other`.
416 /// However, in some contexts (e.g., compile-time evaluation),
417 /// it is not always possible to determine equality of two pointers, so this function may
418 /// spuriously return `false` for pointers that later actually turn out to be equal.
419 /// But when it returns `true`, the pointers are guaranteed to be equal.
421 /// This function is the mirror of [`guaranteed_ne`], but not its inverse. There are pointer
422 /// comparisons for which both functions return `false`.
424 /// [`guaranteed_ne`]: #method.guaranteed_ne
426 /// The return value may change depending on the compiler version and unsafe code may not
427 /// rely on the result of this function for soundness. It is suggested to only use this function
428 /// for performance optimizations where spurious `false` return values by this function do not
429 /// affect the outcome, but just the performance.
430 /// The consequences of using this method to make runtime and compile-time code behave
431 /// differently have not been explored. This method should not be used to introduce such
432 /// differences, and it should also not be stabilized before we have a better understanding
434 #[unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
435 #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
437 pub const fn guaranteed_eq(self, other
: *mut T
) -> bool
441 intrinsics
::ptr_guaranteed_eq(self as *const _
, other
as *const _
)
444 /// Returns whether two pointers are guaranteed to be unequal.
446 /// At runtime this function behaves like `self != other`.
447 /// However, in some contexts (e.g., compile-time evaluation),
448 /// it is not always possible to determine the inequality of two pointers, so this function may
449 /// spuriously return `false` for pointers that later actually turn out to be unequal.
450 /// But when it returns `true`, the pointers are guaranteed to be unequal.
452 /// This function is the mirror of [`guaranteed_eq`], but not its inverse. There are pointer
453 /// comparisons for which both functions return `false`.
455 /// [`guaranteed_eq`]: #method.guaranteed_eq
457 /// The return value may change depending on the compiler version and unsafe code may not
458 /// rely on the result of this function for soundness. It is suggested to only use this function
459 /// for performance optimizations where spurious `false` return values by this function do not
460 /// affect the outcome, but just the performance.
461 /// The consequences of using this method to make runtime and compile-time code behave
462 /// differently have not been explored. This method should not be used to introduce such
463 /// differences, and it should also not be stabilized before we have a better understanding
465 #[unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
466 #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
468 pub const unsafe fn guaranteed_ne(self, other
: *mut T
) -> bool
472 intrinsics
::ptr_guaranteed_ne(self as *const _
, other
as *const _
)
475 /// Calculates the distance between two pointers. The returned value is in
476 /// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
478 /// This function is the inverse of [`offset`].
480 /// [`offset`]: #method.offset-1
484 /// If any of the following conditions are violated, the result is Undefined
487 /// * Both the starting and other pointer must be either in bounds or one
488 /// byte past the end of the same allocated object. Note that in Rust,
489 /// every (stack-allocated) variable is considered a separate allocated object.
491 /// * Both pointers must be *derived from* a pointer to the same object.
492 /// (See below for an example.)
494 /// * The distance between the pointers, in bytes, must be an exact multiple
495 /// of the size of `T`.
497 /// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
499 /// * The distance being in bounds cannot rely on "wrapping around" the address space.
501 /// Rust types are never larger than `isize::MAX` and Rust allocations never wrap around the
502 /// address space, so two pointers within some value of any Rust type `T` will always satisfy
503 /// the last two conditions. The standard library also generally ensures that allocations
504 /// never reach a size where an offset is a concern. For instance, `Vec` and `Box` ensure they
505 /// never allocate more than `isize::MAX` bytes, so `ptr_into_vec.offset_from(vec.as_ptr())`
506 /// always satisfies the last two conditions.
508 /// Most platforms fundamentally can't even construct such a large 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.
515 /// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
516 /// such large allocations either.)
518 /// [`add`]: #method.add
522 /// This function panics if `T` is a Zero-Sized Type ("ZST").
529 /// let mut a = [0; 5];
530 /// let ptr1: *mut i32 = &mut a[1];
531 /// let ptr2: *mut i32 = &mut a[3];
533 /// assert_eq!(ptr2.offset_from(ptr1), 2);
534 /// assert_eq!(ptr1.offset_from(ptr2), -2);
535 /// assert_eq!(ptr1.offset(2), ptr2);
536 /// assert_eq!(ptr2.offset(-2), ptr1);
540 /// *Incorrect* usage:
543 /// let ptr1 = Box::into_raw(Box::new(0u8));
544 /// let ptr2 = Box::into_raw(Box::new(1u8));
545 /// let diff = (ptr2 as isize).wrapping_sub(ptr1 as isize);
546 /// // Make ptr2_other an "alias" of ptr2, but derived from ptr1.
547 /// let ptr2_other = (ptr1 as *mut u8).wrapping_offset(diff);
548 /// assert_eq!(ptr2 as usize, ptr2_other as usize);
549 /// // Since ptr2_other and ptr2 are derived from pointers to different objects,
550 /// // computing their offset is undefined behavior, even though
551 /// // they point to the same address!
553 /// let zero = ptr2_other.offset_from(ptr2); // Undefined Behavior
556 #[stable(feature = "ptr_offset_from", since = "1.47.0")]
557 #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "41079")]
559 pub const unsafe fn offset_from(self, origin
: *const T
) -> isize
563 // SAFETY: the caller must uphold the safety contract for `offset_from`.
564 unsafe { (self as *const T).offset_from(origin) }
567 /// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
569 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
570 /// offset of `3 * size_of::<T>()` bytes.
574 /// If any of the following conditions are violated, the result is Undefined
577 /// * Both the starting and resulting pointer must be either in bounds or one
578 /// byte past the end of the same allocated object. Note that in Rust,
579 /// every (stack-allocated) variable is considered a separate allocated object.
581 /// * The computed offset, **in bytes**, cannot overflow an `isize`.
583 /// * The offset being in bounds cannot rely on "wrapping around" the address
584 /// space. That is, the infinite-precision sum must fit in a `usize`.
586 /// The compiler and standard library generally tries to ensure allocations
587 /// never reach a size where an offset is a concern. For instance, `Vec`
588 /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
589 /// `vec.as_ptr().add(vec.len())` is always safe.
591 /// Most platforms fundamentally can't even construct such an allocation.
592 /// For instance, no known 64-bit platform can ever serve a request
593 /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
594 /// However, some 32-bit and 16-bit platforms may successfully serve a request for
595 /// more than `isize::MAX` bytes with things like Physical Address
596 /// Extension. As such, memory acquired directly from allocators or memory
597 /// mapped files *may* be too large to handle with this function.
599 /// Consider using [`wrapping_add`] instead if these constraints are
600 /// difficult to satisfy. The only advantage of this method is that it
601 /// enables more aggressive compiler optimizations.
603 /// [`wrapping_add`]: #method.wrapping_add
610 /// let s: &str = "123";
611 /// let ptr: *const u8 = s.as_ptr();
614 /// println!("{}", *ptr.add(1) as char);
615 /// println!("{}", *ptr.add(2) as char);
618 #[stable(feature = "pointer_methods", since = "1.26.0")]
619 #[must_use = "returns a new pointer rather than modifying its argument"]
620 #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
622 pub const unsafe fn add(self, count
: usize) -> Self
626 // SAFETY: the caller must uphold the safety contract for `offset`.
627 unsafe { self.offset(count as isize) }
630 /// Calculates the offset from a pointer (convenience for
631 /// `.offset((count as isize).wrapping_neg())`).
633 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
634 /// offset of `3 * size_of::<T>()` bytes.
638 /// If any of the following conditions are violated, the result is Undefined
641 /// * Both the starting and resulting pointer must be either in bounds or one
642 /// byte past the end of the same allocated object. Note that in Rust,
643 /// every (stack-allocated) variable is considered a separate allocated object.
645 /// * The computed offset cannot exceed `isize::MAX` **bytes**.
647 /// * The offset being in bounds cannot rely on "wrapping around" the address
648 /// space. That is, the infinite-precision sum must fit in a usize.
650 /// The compiler and standard library generally tries to ensure allocations
651 /// never reach a size where an offset is a concern. For instance, `Vec`
652 /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
653 /// `vec.as_ptr().add(vec.len()).sub(vec.len())` is always safe.
655 /// Most platforms fundamentally can't even construct such an allocation.
656 /// For instance, no known 64-bit platform can ever serve a request
657 /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
658 /// However, some 32-bit and 16-bit platforms may successfully serve a request for
659 /// more than `isize::MAX` bytes with things like Physical Address
660 /// Extension. As such, memory acquired directly from allocators or memory
661 /// mapped files *may* be too large to handle with this function.
663 /// Consider using [`wrapping_sub`] instead if these constraints are
664 /// difficult to satisfy. The only advantage of this method is that it
665 /// enables more aggressive compiler optimizations.
667 /// [`wrapping_sub`]: #method.wrapping_sub
674 /// let s: &str = "123";
677 /// let end: *const u8 = s.as_ptr().add(3);
678 /// println!("{}", *end.sub(1) as char);
679 /// println!("{}", *end.sub(2) as char);
682 #[stable(feature = "pointer_methods", since = "1.26.0")]
683 #[must_use = "returns a new pointer rather than modifying its argument"]
684 #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
686 pub const unsafe fn sub(self, count
: usize) -> Self
690 // SAFETY: the caller must uphold the safety contract for `offset`.
691 unsafe { self.offset((count as isize).wrapping_neg()) }
694 /// Calculates the offset from a pointer using wrapping arithmetic.
695 /// (convenience for `.wrapping_offset(count as isize)`)
697 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
698 /// offset of `3 * size_of::<T>()` bytes.
702 /// This operation itself is always safe, but using the resulting pointer is not.
704 /// The resulting pointer remains attached to the same allocated object that `self` points to.
705 /// It may *not* be used to access a different allocated object. Note that in Rust, every
706 /// (stack-allocated) variable is considered a separate allocated object.
708 /// In other words, `let z = x.wrapping_add((y as usize) - (x as usize))` does *not* make `z`
709 /// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
710 /// attached to the object `x` is attached to, and dereferencing it is Undefined Behavior unless
711 /// `x` and `y` point into the same allocated object.
713 /// Compared to [`add`], this method basically delays the requirement of staying within the
714 /// same allocated object: [`add`] is immediate Undefined Behavior when crossing object
715 /// boundaries; `wrapping_add` produces a pointer but still leads to Undefined Behavior if a
716 /// pointer is dereferenced when it is out-of-bounds of the object it is attached to. [`add`]
717 /// can be optimized better and is thus preferable in performance-sensitive code.
719 /// The delayed check only considers the value of the pointer that was dereferenced, not the
720 /// intermediate values used during the computation of the final result. For example,
721 /// `x.wrapping_add(o).wrapping_sub(o)` is always the same as `x`. In other words, leaving the
722 /// allocated object and then re-entering it later is permitted.
724 /// If you need to cross object boundaries, cast the pointer to an integer and
725 /// do the arithmetic there.
727 /// [`add`]: #method.add
734 /// // Iterate using a raw pointer in increments of two elements
735 /// let data = [1u8, 2, 3, 4, 5];
736 /// let mut ptr: *const u8 = data.as_ptr();
738 /// let end_rounded_up = ptr.wrapping_add(6);
740 /// // This loop prints "1, 3, 5, "
741 /// while ptr != end_rounded_up {
743 /// print!("{}, ", *ptr);
745 /// ptr = ptr.wrapping_add(step);
748 #[stable(feature = "pointer_methods", since = "1.26.0")]
749 #[must_use = "returns a new pointer rather than modifying its argument"]
750 #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
752 pub const fn wrapping_add(self, count
: usize) -> Self
756 self.wrapping_offset(count
as isize)
759 /// Calculates the offset from a pointer using wrapping arithmetic.
760 /// (convenience for `.wrapping_offset((count as isize).wrapping_neg())`)
762 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
763 /// offset of `3 * size_of::<T>()` bytes.
767 /// This operation itself is always safe, but using the resulting pointer is not.
769 /// The resulting pointer remains attached to the same allocated object that `self` points to.
770 /// It may *not* be used to access a different allocated object. Note that in Rust, every
771 /// (stack-allocated) variable is considered a separate allocated object.
773 /// In other words, `let z = x.wrapping_sub((x as usize) - (y as usize))` does *not* make `z`
774 /// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
775 /// attached to the object `x` is attached to, and dereferencing it is Undefined Behavior unless
776 /// `x` and `y` point into the same allocated object.
778 /// Compared to [`sub`], this method basically delays the requirement of staying within the
779 /// same allocated object: [`sub`] is immediate Undefined Behavior when crossing object
780 /// boundaries; `wrapping_sub` produces a pointer but still leads to Undefined Behavior if a
781 /// pointer is dereferenced when it is out-of-bounds of the object it is attached to. [`sub`]
782 /// can be optimized better and is thus preferable in performance-sensitive code.
784 /// The delayed check only considers the value of the pointer that was dereferenced, not the
785 /// intermediate values used during the computation of the final result. For example,
786 /// `x.wrapping_add(o).wrapping_sub(o)` is always the same as `x`. In other words, leaving the
787 /// allocated object and then re-entering it later is permitted.
789 /// If you need to cross object boundaries, cast the pointer to an integer and
790 /// do the arithmetic there.
792 /// [`sub`]: #method.sub
799 /// // Iterate using a raw pointer in increments of two elements (backwards)
800 /// let data = [1u8, 2, 3, 4, 5];
801 /// let mut ptr: *const u8 = data.as_ptr();
802 /// let start_rounded_down = ptr.wrapping_sub(2);
803 /// ptr = ptr.wrapping_add(4);
805 /// // This loop prints "5, 3, 1, "
806 /// while ptr != start_rounded_down {
808 /// print!("{}, ", *ptr);
810 /// ptr = ptr.wrapping_sub(step);
813 #[stable(feature = "pointer_methods", since = "1.26.0")]
814 #[must_use = "returns a new pointer rather than modifying its argument"]
815 #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
817 pub const fn wrapping_sub(self, count
: usize) -> Self
821 self.wrapping_offset((count
as isize).wrapping_neg())
824 /// Sets the pointer value to `ptr`.
826 /// In case `self` is a (fat) pointer to an unsized type, this operation
827 /// will only affect the pointer part, whereas for (thin) pointers to
828 /// sized types, this has the same effect as a simple assignment.
830 /// The resulting pointer will have provenance of `val`, i.e., for a fat
831 /// pointer, this operation is semantically the same as creating a new
832 /// fat pointer with the data pointer value of `val` but the metadata of
837 /// This function is primarily useful for allowing byte-wise pointer
838 /// arithmetic on potentially fat pointers:
841 /// #![feature(set_ptr_value)]
842 /// # use core::fmt::Debug;
843 /// let mut arr: [i32; 3] = [1, 2, 3];
844 /// let mut ptr = &mut arr[0] as *mut dyn Debug;
845 /// let thin = ptr as *mut u8;
847 /// ptr = ptr.set_ptr_value(thin.add(8));
848 /// # assert_eq!(*(ptr as *mut i32), 3);
849 /// println!("{:?}", &*ptr); // will print "3"
852 #[unstable(feature = "set_ptr_value", issue = "75091")]
853 #[must_use = "returns a new pointer rather than modifying its argument"]
855 pub fn set_ptr_value(mut self, val
: *mut u8) -> Self {
856 let thin
= &mut self as *mut *mut T
as *mut *mut u8;
857 // SAFETY: In case of a thin pointer, this operations is identical
858 // to a simple assignment. In case of a fat pointer, with the current
859 // fat pointer layout implementation, the first field of such a
860 // pointer is always the data pointer, which is likewise assigned.
861 unsafe { *thin = val }
;
865 /// Reads the value from `self` without moving it. This leaves the
866 /// memory in `self` unchanged.
868 /// See [`ptr::read`] for safety concerns and examples.
870 /// [`ptr::read`]: crate::ptr::read()
871 #[stable(feature = "pointer_methods", since = "1.26.0")]
872 #[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
874 pub const unsafe fn read(self) -> T
878 // SAFETY: the caller must uphold the safety contract for ``.
879 unsafe { read(self) }
882 /// Performs a volatile read of the value from `self` without moving it. This
883 /// leaves the memory in `self` unchanged.
885 /// Volatile operations are intended to act on I/O memory, and are guaranteed
886 /// to not be elided or reordered by the compiler across other volatile
889 /// See [`ptr::read_volatile`] for safety concerns and examples.
891 /// [`ptr::read_volatile`]: crate::ptr::read_volatile()
892 #[stable(feature = "pointer_methods", since = "1.26.0")]
894 pub unsafe fn read_volatile(self) -> T
898 // SAFETY: the caller must uphold the safety contract for `read_volatile`.
899 unsafe { read_volatile(self) }
902 /// Reads the value from `self` without moving it. This leaves the
903 /// memory in `self` unchanged.
905 /// Unlike `read`, the pointer may be unaligned.
907 /// See [`ptr::read_unaligned`] for safety concerns and examples.
909 /// [`ptr::read_unaligned`]: crate::ptr::read_unaligned()
910 #[stable(feature = "pointer_methods", since = "1.26.0")]
911 #[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
913 pub const unsafe fn read_unaligned(self) -> T
917 // SAFETY: the caller must uphold the safety contract for `read_unaligned`.
918 unsafe { read_unaligned(self) }
921 /// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
922 /// and destination may overlap.
924 /// NOTE: this has the *same* argument order as [`ptr::copy`].
926 /// See [`ptr::copy`] for safety concerns and examples.
928 /// [`ptr::copy`]: crate::ptr::copy()
929 #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
930 #[stable(feature = "pointer_methods", since = "1.26.0")]
932 pub const unsafe fn copy_to(self, dest
: *mut T
, count
: usize)
936 // SAFETY: the caller must uphold the safety contract for `copy`.
937 unsafe { copy(self, dest, count) }
940 /// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
941 /// and destination may *not* overlap.
943 /// NOTE: this has the *same* argument order as [`ptr::copy_nonoverlapping`].
945 /// See [`ptr::copy_nonoverlapping`] for safety concerns and examples.
947 /// [`ptr::copy_nonoverlapping`]: crate::ptr::copy_nonoverlapping()
948 #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
949 #[stable(feature = "pointer_methods", since = "1.26.0")]
951 pub const unsafe fn copy_to_nonoverlapping(self, dest
: *mut T
, count
: usize)
955 // SAFETY: the caller must uphold the safety contract for `copy_nonoverlapping`.
956 unsafe { copy_nonoverlapping(self, dest, count) }
959 /// Copies `count * size_of<T>` bytes from `src` to `self`. The source
960 /// and destination may overlap.
962 /// NOTE: this has the *opposite* argument order of [`ptr::copy`].
964 /// See [`ptr::copy`] for safety concerns and examples.
966 /// [`ptr::copy`]: crate::ptr::copy()
967 #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
968 #[stable(feature = "pointer_methods", since = "1.26.0")]
970 pub const unsafe fn copy_from(self, src
: *const T
, count
: usize)
974 // SAFETY: the caller must uphold the safety contract for `copy`.
975 unsafe { copy(src, self, count) }
978 /// Copies `count * size_of<T>` bytes from `src` to `self`. The source
979 /// and destination may *not* overlap.
981 /// NOTE: this has the *opposite* argument order of [`ptr::copy_nonoverlapping`].
983 /// See [`ptr::copy_nonoverlapping`] for safety concerns and examples.
985 /// [`ptr::copy_nonoverlapping`]: crate::ptr::copy_nonoverlapping()
986 #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
987 #[stable(feature = "pointer_methods", since = "1.26.0")]
989 pub const unsafe fn copy_from_nonoverlapping(self, src
: *const T
, count
: usize)
993 // SAFETY: the caller must uphold the safety contract for `copy_nonoverlapping`.
994 unsafe { copy_nonoverlapping(src, self, count) }
997 /// Executes the destructor (if any) of the pointed-to value.
999 /// See [`ptr::drop_in_place`] for safety concerns and examples.
1001 /// [`ptr::drop_in_place`]: crate::ptr::drop_in_place()
1002 #[stable(feature = "pointer_methods", since = "1.26.0")]
1004 pub unsafe fn drop_in_place(self) {
1005 // SAFETY: the caller must uphold the safety contract for `drop_in_place`.
1006 unsafe { drop_in_place(self) }
1009 /// Overwrites a memory location with the given value without reading or
1010 /// dropping the old value.
1012 /// See [`ptr::write`] for safety concerns and examples.
1014 /// [`ptr::write`]: crate::ptr::write()
1015 #[stable(feature = "pointer_methods", since = "1.26.0")]
1017 pub unsafe fn write(self, val
: T
)
1021 // SAFETY: the caller must uphold the safety contract for `write`.
1022 unsafe { write(self, val) }
1025 /// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
1026 /// bytes of memory starting at `self` to `val`.
1028 /// See [`ptr::write_bytes`] for safety concerns and examples.
1030 /// [`ptr::write_bytes`]: crate::ptr::write_bytes()
1031 #[stable(feature = "pointer_methods", since = "1.26.0")]
1033 pub unsafe fn write_bytes(self, val
: u8, count
: usize)
1037 // SAFETY: the caller must uphold the safety contract for `write_bytes`.
1038 unsafe { write_bytes(self, val, count) }
1041 /// Performs a volatile write of a memory location with the given value without
1042 /// reading or dropping the old value.
1044 /// Volatile operations are intended to act on I/O memory, and are guaranteed
1045 /// to not be elided or reordered by the compiler across other volatile
1048 /// See [`ptr::write_volatile`] for safety concerns and examples.
1050 /// [`ptr::write_volatile`]: crate::ptr::write_volatile()
1051 #[stable(feature = "pointer_methods", since = "1.26.0")]
1053 pub unsafe fn write_volatile(self, val
: T
)
1057 // SAFETY: the caller must uphold the safety contract for `write_volatile`.
1058 unsafe { write_volatile(self, val) }
1061 /// Overwrites a memory location with the given value without reading or
1062 /// dropping the old value.
1064 /// Unlike `write`, the pointer may be unaligned.
1066 /// See [`ptr::write_unaligned`] for safety concerns and examples.
1068 /// [`ptr::write_unaligned`]: crate::ptr::write_unaligned()
1069 #[stable(feature = "pointer_methods", since = "1.26.0")]
1070 #[rustc_const_unstable(feature = "const_ptr_write", issue = "none")]
1072 pub const unsafe fn write_unaligned(self, val
: T
)
1076 // SAFETY: the caller must uphold the safety contract for `write_unaligned`.
1077 unsafe { write_unaligned(self, val) }
1080 /// Replaces the value at `self` with `src`, returning the old
1081 /// value, without dropping either.
1083 /// See [`ptr::replace`] for safety concerns and examples.
1085 /// [`ptr::replace`]: crate::ptr::replace()
1086 #[stable(feature = "pointer_methods", since = "1.26.0")]
1088 pub unsafe fn replace(self, src
: T
) -> T
1092 // SAFETY: the caller must uphold the safety contract for `replace`.
1093 unsafe { replace(self, src) }
1096 /// Swaps the values at two mutable locations of the same type, without
1097 /// deinitializing either. They may overlap, unlike `mem::swap` which is
1098 /// otherwise equivalent.
1100 /// See [`ptr::swap`] for safety concerns and examples.
1102 /// [`ptr::swap`]: crate::ptr::swap()
1103 #[stable(feature = "pointer_methods", since = "1.26.0")]
1105 pub unsafe fn swap(self, with
: *mut T
)
1109 // SAFETY: the caller must uphold the safety contract for `swap`.
1110 unsafe { swap(self, with) }
1113 /// Computes the offset that needs to be applied to the pointer in order to make it aligned to
1116 /// If it is not possible to align the pointer, the implementation returns
1117 /// `usize::MAX`. It is permissible for the implementation to *always*
1118 /// return `usize::MAX`. Only your algorithm's performance can depend
1119 /// on getting a usable offset here, not its correctness.
1121 /// The offset is expressed in number of `T` elements, and not bytes. The value returned can be
1122 /// used with the `wrapping_add` method.
1124 /// There are no guarantees whatsoever that offsetting the pointer will not overflow or go
1125 /// beyond the allocation that the pointer points into. It is up to the caller to ensure that
1126 /// the returned offset is correct in all terms other than alignment.
1130 /// The function panics if `align` is not a power-of-two.
1134 /// Accessing adjacent `u8` as `u16`
1137 /// # fn foo(n: usize) {
1138 /// # use std::mem::align_of;
1140 /// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
1141 /// let ptr = x.as_ptr().add(n) as *const u8;
1142 /// let offset = ptr.align_offset(align_of::<u16>());
1143 /// if offset < x.len() - n - 1 {
1144 /// let u16_ptr = ptr.add(offset) as *const u16;
1145 /// assert_ne!(*u16_ptr, 500);
1147 /// // while the pointer can be aligned via `offset`, it would point
1148 /// // outside the allocation
1152 #[stable(feature = "align_offset", since = "1.36.0")]
1153 pub fn align_offset(self, align
: usize) -> usize
1157 if !align
.is_power_of_two() {
1158 panic
!("align_offset: align is not a power-of-two");
1160 // SAFETY: `align` has been checked to be a power of 2 above
1161 unsafe { align_offset(self, align) }
1165 #[lang = "mut_slice_ptr"]
1167 /// Returns the length of a raw slice.
1169 /// The returned value is the number of **elements**, not the number of bytes.
1171 /// This function is safe, even when the raw slice cannot be cast to a slice
1172 /// reference because the pointer is null or unaligned.
1177 /// #![feature(slice_ptr_len)]
1180 /// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3);
1181 /// assert_eq!(slice.len(), 3);
1184 #[unstable(feature = "slice_ptr_len", issue = "71146")]
1185 #[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
1186 pub const fn len(self) -> usize {
1189 // SAFETY: this is safe because `*const [T]` and `FatPtr<T>` have the same layout.
1190 // Only `std` can make this guarantee.
1191 unsafe { Repr { rust_mut: self }
.raw
}.len
1193 #[cfg(not(bootstrap))]
1197 /// Returns a raw pointer to the slice's buffer.
1199 /// This is equivalent to casting `self` to `*mut T`, but more type-safe.
1204 /// #![feature(slice_ptr_get)]
1207 /// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3);
1208 /// assert_eq!(slice.as_mut_ptr(), 0 as *mut i8);
1211 #[unstable(feature = "slice_ptr_get", issue = "74265")]
1212 #[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")]
1213 pub const fn as_mut_ptr(self) -> *mut T
{
1217 /// Returns a raw pointer to an element or subslice, without doing bounds
1220 /// Calling this method with an out-of-bounds index or when `self` is not dereferencable
1221 /// is *[undefined behavior]* even if the resulting pointer is not used.
1223 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
1228 /// #![feature(slice_ptr_get)]
1230 /// let x = &mut [1, 2, 4] as *mut [i32];
1233 /// assert_eq!(x.get_unchecked_mut(1), x.as_mut_ptr().add(1));
1236 #[unstable(feature = "slice_ptr_get", issue = "74265")]
1238 pub unsafe fn get_unchecked_mut
<I
>(self, index
: I
) -> *mut I
::Output
1242 // SAFETY: the caller ensures that `self` is dereferencable and `index` in-bounds.
1243 unsafe { index.get_unchecked_mut(self) }
1246 /// Returns `None` if the pointer is null, or else returns a shared slice to
1247 /// the value wrapped in `Some`. In contrast to [`as_ref`], this does not require
1248 /// that the value has to be initialized.
1250 /// For the mutable counterpart see [`as_uninit_slice_mut`].
1252 /// [`as_ref`]: #method.as_ref-1
1253 /// [`as_uninit_slice_mut`]: #method.as_uninit_slice_mut
1257 /// When calling this method, you have to ensure that *either* the pointer is NULL *or*
1258 /// all of the following is true:
1260 /// * The pointer must be [valid] for reads for `ptr.len() * mem::size_of::<T>()` many bytes,
1261 /// and it must be properly aligned. This means in particular:
1263 /// * The entire memory range of this slice must be contained within a single allocated object!
1264 /// Slices can never span across multiple allocated objects.
1266 /// * The pointer must be aligned even for zero-length slices. One
1267 /// reason for this is that enum layout optimizations may rely on references
1268 /// (including slices of any length) being aligned and non-null to distinguish
1269 /// them from other data. You can obtain a pointer that is usable as `data`
1270 /// for zero-length slices using [`NonNull::dangling()`].
1272 /// * The total size `ptr.len() * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`.
1273 /// See the safety documentation of [`pointer::offset`].
1275 /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
1276 /// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
1277 /// In particular, for the duration of this lifetime, the memory the pointer points to must
1278 /// not get mutated (except inside `UnsafeCell`).
1280 /// This applies even if the result of this method is unused!
1282 /// See also [`slice::from_raw_parts`][].
1284 /// [valid]: crate::ptr#safety
1286 #[unstable(feature = "ptr_as_uninit", issue = "75402")]
1287 pub unsafe fn as_uninit_slice
<'a
>(self) -> Option
<&'a
[MaybeUninit
<T
>]> {
1291 // SAFETY: the caller must uphold the safety contract for `as_uninit_slice`.
1292 Some(unsafe { slice::from_raw_parts(self as *const MaybeUninit<T>, self.len()) }
)
1296 /// Returns `None` if the pointer is null, or else returns a unique slice to
1297 /// the value wrapped in `Some`. In contrast to [`as_mut`], this does not require
1298 /// that the value has to be initialized.
1300 /// For the shared counterpart see [`as_uninit_slice`].
1302 /// [`as_mut`]: #method.as_mut
1303 /// [`as_uninit_slice`]: #method.as_uninit_slice-1
1307 /// When calling this method, you have to ensure that *either* the pointer is NULL *or*
1308 /// all of the following is true:
1310 /// * The pointer must be [valid] for reads and writes for `ptr.len() * mem::size_of::<T>()`
1311 /// many bytes, and it must be properly aligned. This means in particular:
1313 /// * The entire memory range of this slice must be contained within a single allocated object!
1314 /// Slices can never span across multiple allocated objects.
1316 /// * The pointer must be aligned even for zero-length slices. One
1317 /// reason for this is that enum layout optimizations may rely on references
1318 /// (including slices of any length) being aligned and non-null to distinguish
1319 /// them from other data. You can obtain a pointer that is usable as `data`
1320 /// for zero-length slices using [`NonNull::dangling()`].
1322 /// * The total size `ptr.len() * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`.
1323 /// See the safety documentation of [`pointer::offset`].
1325 /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
1326 /// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
1327 /// In particular, for the duration of this lifetime, the memory the pointer points to must
1328 /// not get accessed (read or written) through any other pointer.
1330 /// This applies even if the result of this method is unused!
1332 /// See also [`slice::from_raw_parts_mut`][].
1334 /// [valid]: crate::ptr#safety
1336 #[unstable(feature = "ptr_as_uninit", issue = "75402")]
1337 pub unsafe fn as_uninit_slice_mut
<'a
>(self) -> Option
<&'a
mut [MaybeUninit
<T
>]> {
1341 // SAFETY: the caller must uphold the safety contract for `as_uninit_slice_mut`.
1342 Some(unsafe { slice::from_raw_parts_mut(self as *mut MaybeUninit<T>, self.len()) }
)
1347 // Equality for pointers
1348 #[stable(feature = "rust1", since = "1.0.0")]
1349 impl<T
: ?Sized
> PartialEq
for *mut T
{
1351 fn eq(&self, other
: &*mut T
) -> bool
{
1356 #[stable(feature = "rust1", since = "1.0.0")]
1357 impl<T
: ?Sized
> Eq
for *mut T {}
1359 #[stable(feature = "rust1", since = "1.0.0")]
1360 impl<T
: ?Sized
> Ord
for *mut T
{
1362 fn cmp(&self, other
: &*mut T
) -> Ordering
{
1365 } else if self == other
{
1373 #[stable(feature = "rust1", since = "1.0.0")]
1374 impl<T
: ?Sized
> PartialOrd
for *mut T
{
1376 fn partial_cmp(&self, other
: &*mut T
) -> Option
<Ordering
> {
1377 Some(self.cmp(other
))
1381 fn lt(&self, other
: &*mut T
) -> bool
{
1386 fn le(&self, other
: &*mut T
) -> bool
{
1391 fn gt(&self, other
: &*mut T
) -> bool
{
1396 fn ge(&self, other
: &*mut T
) -> bool
{