2 use crate::cmp::Ordering::{self, Equal, Greater, Less};
5 // ignore-tidy-undocumented-unsafe
8 impl<T: ?Sized> *mut 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 mut s = [1, 2, 3];
22 /// let ptr: *mut u32 = s.as_mut_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 *mut u8) == null_mut()
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) -> *mut 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 if the pointer is
52 /// non-NULL, then it is properly aligned, dereferenceable (for the whole
53 /// size of `T`) and points to an initialized instance of `T`. This applies
54 /// even if the result of this method is unused!
55 /// (The part about being initialized is not yet fully decided, but until
56 /// it is, the only safe approach is to ensure that they are indeed initialized.)
58 /// Additionally, the lifetime `'a` returned is arbitrarily chosen and does
59 /// not necessarily reflect the actual lifetime of the data. It is up to the
60 /// caller to ensure that for the duration of this lifetime, the memory this
61 /// pointer points to does not get written to outside of `UnsafeCell<U>`.
68 /// let ptr: *mut u8 = &mut 10u8 as *mut u8;
71 /// if let Some(val_back) = ptr.as_ref() {
72 /// println!("We got back the value: {}!", val_back);
77 /// # Null-unchecked version
79 /// If you are sure the pointer can never be null and are looking for some kind of
80 /// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>`, know that you can
81 /// dereference the pointer directly.
84 /// let ptr: *mut u8 = &mut 10u8 as *mut u8;
87 /// let val_back = &*ptr;
88 /// println!("We got back the value: {}!", val_back);
91 #[stable(feature = "ptr_as_ref", since = "1.9.0")]
93 pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
94 if self.is_null() { None } else { Some(&*self) }
97 /// Calculates the offset from a pointer.
99 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
100 /// offset of `3 * size_of::<T>()` bytes.
104 /// If any of the following conditions are violated, the result is Undefined
107 /// * Both the starting and resulting pointer must be either in bounds or one
108 /// byte past the end of the same allocated object. Note that in Rust,
109 /// every (stack-allocated) variable is considered a separate allocated object.
111 /// * The computed offset, **in bytes**, cannot overflow an `isize`.
113 /// * The offset being in bounds cannot rely on "wrapping around" the address
114 /// space. That is, the infinite-precision sum, **in bytes** must fit in a usize.
116 /// The compiler and standard library generally tries to ensure allocations
117 /// never reach a size where an offset is a concern. For instance, `Vec`
118 /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
119 /// `vec.as_ptr().add(vec.len())` is always safe.
121 /// Most platforms fundamentally can't even construct such an allocation.
122 /// For instance, no known 64-bit platform can ever serve a request
123 /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
124 /// However, some 32-bit and 16-bit platforms may successfully serve a request for
125 /// more than `isize::MAX` bytes with things like Physical Address
126 /// Extension. As such, memory acquired directly from allocators or memory
127 /// mapped files *may* be too large to handle with this function.
129 /// Consider using [`wrapping_offset`] instead if these constraints are
130 /// difficult to satisfy. The only advantage of this method is that it
131 /// enables more aggressive compiler optimizations.
133 /// [`wrapping_offset`]: #method.wrapping_offset
140 /// let mut s = [1, 2, 3];
141 /// let ptr: *mut u32 = s.as_mut_ptr();
144 /// println!("{}", *ptr.offset(1));
145 /// println!("{}", *ptr.offset(2));
148 #[stable(feature = "rust1", since = "1.0.0")]
150 pub unsafe fn offset(self, count: isize) -> *mut T
154 intrinsics::offset(self, count) as *mut T
157 /// Calculates the offset from a pointer using wrapping arithmetic.
158 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
159 /// offset of `3 * size_of::<T>()` bytes.
163 /// The resulting pointer does not need to be in bounds, but it is
164 /// potentially hazardous to dereference (which requires `unsafe`).
166 /// In particular, the resulting pointer remains attached to the same allocated
167 /// object that `self` points to. It may *not* be used to access a
168 /// different allocated object. Note that in Rust,
169 /// every (stack-allocated) variable is considered a separate allocated object.
171 /// In other words, `x.wrapping_offset(y.wrapping_offset_from(x))` is
172 /// *not* the same as `y`, and dereferencing it is undefined behavior
173 /// unless `x` and `y` point into the same allocated object.
175 /// Compared to [`offset`], this method basically delays the requirement of staying
176 /// within the same allocated object: [`offset`] is immediate Undefined Behavior when
177 /// crossing object boundaries; `wrapping_offset` produces a pointer but still leads
178 /// to Undefined Behavior if that pointer is dereferenced. [`offset`] can be optimized
179 /// better and is thus preferable in performance-sensitive code.
181 /// If you need to cross object boundaries, cast the pointer to an integer and
182 /// do the arithmetic there.
184 /// [`offset`]: #method.offset
191 /// // Iterate using a raw pointer in increments of two elements
192 /// let mut data = [1u8, 2, 3, 4, 5];
193 /// let mut ptr: *mut u8 = data.as_mut_ptr();
195 /// let end_rounded_up = ptr.wrapping_offset(6);
197 /// while ptr != end_rounded_up {
201 /// ptr = ptr.wrapping_offset(step);
203 /// assert_eq!(&data, &[0, 2, 0, 4, 0]);
205 #[stable(feature = "ptr_wrapping_offset", since = "1.16.0")]
207 pub fn wrapping_offset(self, count: isize) -> *mut T
211 unsafe { intrinsics::arith_offset(self, count) as *mut T }
214 /// Returns `None` if the pointer is null, or else returns a mutable
215 /// reference to the value wrapped in `Some`.
219 /// As with [`as_ref`], this is unsafe because it cannot verify the validity
220 /// of the returned pointer, nor can it ensure that the lifetime `'a`
221 /// returned is indeed a valid lifetime for the contained data.
223 /// When calling this method, you have to ensure that *either* the pointer is NULL *or*
224 /// all of the following is true:
225 /// - it is properly aligned
226 /// - it must point to an initialized instance of T; in particular, the pointer must be
227 /// "dereferenceable" in the sense defined [here].
229 /// This applies even if the result of this method is unused!
230 /// (The part about being initialized is not yet fully decided, but until
231 /// it is the only safe approach is to ensure that they are indeed initialized.)
233 /// Additionally, the lifetime `'a` returned is arbitrarily chosen and does
234 /// not necessarily reflect the actual lifetime of the data. *You* must enforce
235 /// Rust's aliasing rules. In particular, for the duration of this lifetime,
236 /// the memory this pointer points to must not get accessed (read or written)
237 /// through any other pointer.
239 /// [here]: crate::ptr#safety
240 /// [`as_ref`]: #method.as_ref
247 /// let mut s = [1, 2, 3];
248 /// let ptr: *mut u32 = s.as_mut_ptr();
249 /// let first_value = unsafe { ptr.as_mut().unwrap() };
250 /// *first_value = 4;
251 /// println!("{:?}", s); // It'll print: "[4, 2, 3]".
254 /// # Null-unchecked version
256 /// If you are sure the pointer can never be null and are looking for some kind of
257 /// `as_mut_unchecked` that returns the `&mut T` instead of `Option<&mut T>`, know that
258 /// you can dereference the pointer directly.
261 /// let mut s = [1, 2, 3];
262 /// let ptr: *mut u32 = s.as_mut_ptr();
263 /// let first_value = unsafe { &mut *ptr };
264 /// *first_value = 4;
265 /// println!("{:?}", s); // It'll print: "[4, 2, 3]".
267 #[stable(feature = "ptr_as_ref", since = "1.9.0")]
269 pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {
270 if self.is_null() { None } else { Some(&mut *self) }
273 /// Calculates the distance between two pointers. The returned value is in
274 /// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
276 /// This function is the inverse of [`offset`].
278 /// [`offset`]: #method.offset-1
279 /// [`wrapping_offset_from`]: #method.wrapping_offset_from-1
283 /// If any of the following conditions are violated, the result is Undefined
286 /// * Both the starting and other pointer must be either in bounds or one
287 /// byte past the end of the same allocated object. Note that in Rust,
288 /// every (stack-allocated) variable is considered a separate allocated object.
290 /// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
292 /// * The distance between the pointers, in bytes, must be an exact multiple
293 /// of the size of `T`.
295 /// * The distance being in bounds cannot rely on "wrapping around" the address space.
297 /// The compiler and standard library generally try to ensure allocations
298 /// never reach a size where an offset is a concern. For instance, `Vec`
299 /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
300 /// `ptr_into_vec.offset_from(vec.as_ptr())` is always safe.
302 /// Most platforms fundamentally can't even construct such an allocation.
303 /// For instance, no known 64-bit platform can ever serve a request
304 /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
305 /// However, some 32-bit and 16-bit platforms may successfully serve a request for
306 /// more than `isize::MAX` bytes with things like Physical Address
307 /// Extension. As such, memory acquired directly from allocators or memory
308 /// mapped files *may* be too large to handle with this function.
310 /// Consider using [`wrapping_offset_from`] instead if these constraints are
311 /// difficult to satisfy. The only advantage of this method is that it
312 /// enables more aggressive compiler optimizations.
316 /// This function panics if `T` is a Zero-Sized Type ("ZST").
323 /// #![feature(ptr_offset_from)]
325 /// let mut a = [0; 5];
326 /// let ptr1: *mut i32 = &mut a[1];
327 /// let ptr2: *mut i32 = &mut a[3];
329 /// assert_eq!(ptr2.offset_from(ptr1), 2);
330 /// assert_eq!(ptr1.offset_from(ptr2), -2);
331 /// assert_eq!(ptr1.offset(2), ptr2);
332 /// assert_eq!(ptr2.offset(-2), ptr1);
335 #[unstable(feature = "ptr_offset_from", issue = "41079")]
336 #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "41079")]
338 pub const unsafe fn offset_from(self, origin: *const T) -> isize
342 (self as *const T).offset_from(origin)
345 /// Calculates the distance between two pointers. The returned value is in
346 /// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
348 /// If the address different between the two pointers is not a multiple of
349 /// `mem::size_of::<T>()` then the result of the division is rounded towards
352 /// Though this method is safe for any two pointers, note that its result
353 /// will be mostly useless if the two pointers aren't into the same allocated
354 /// object, for example if they point to two different local variables.
358 /// This function panics if `T` is a zero-sized type.
365 /// #![feature(ptr_wrapping_offset_from)]
367 /// let mut a = [0; 5];
368 /// let ptr1: *mut i32 = &mut a[1];
369 /// let ptr2: *mut i32 = &mut a[3];
370 /// assert_eq!(ptr2.wrapping_offset_from(ptr1), 2);
371 /// assert_eq!(ptr1.wrapping_offset_from(ptr2), -2);
372 /// assert_eq!(ptr1.wrapping_offset(2), ptr2);
373 /// assert_eq!(ptr2.wrapping_offset(-2), ptr1);
375 /// let ptr1: *mut i32 = 3 as _;
376 /// let ptr2: *mut i32 = 13 as _;
377 /// assert_eq!(ptr2.wrapping_offset_from(ptr1), 2);
379 #[unstable(feature = "ptr_wrapping_offset_from", issue = "41079")]
381 pub fn wrapping_offset_from(self, origin: *const T) -> isize
385 (self as *const T).wrapping_offset_from(origin)
388 /// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
390 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
391 /// offset of `3 * size_of::<T>()` bytes.
395 /// If any of the following conditions are violated, the result is Undefined
398 /// * Both the starting and resulting pointer must be either in bounds or one
399 /// byte past the end of the same allocated object. Note that in Rust,
400 /// every (stack-allocated) variable is considered a separate allocated object.
402 /// * The computed offset, **in bytes**, cannot overflow an `isize`.
404 /// * The offset being in bounds cannot rely on "wrapping around" the address
405 /// space. That is, the infinite-precision sum must fit in a `usize`.
407 /// The compiler and standard library generally tries to ensure allocations
408 /// never reach a size where an offset is a concern. For instance, `Vec`
409 /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
410 /// `vec.as_ptr().add(vec.len())` is always safe.
412 /// Most platforms fundamentally can't even construct such an allocation.
413 /// For instance, no known 64-bit platform can ever serve a request
414 /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
415 /// However, some 32-bit and 16-bit platforms may successfully serve a request for
416 /// more than `isize::MAX` bytes with things like Physical Address
417 /// Extension. As such, memory acquired directly from allocators or memory
418 /// mapped files *may* be too large to handle with this function.
420 /// Consider using [`wrapping_add`] instead if these constraints are
421 /// difficult to satisfy. The only advantage of this method is that it
422 /// enables more aggressive compiler optimizations.
424 /// [`wrapping_add`]: #method.wrapping_add
431 /// let s: &str = "123";
432 /// let ptr: *const u8 = s.as_ptr();
435 /// println!("{}", *ptr.add(1) as char);
436 /// println!("{}", *ptr.add(2) as char);
439 #[stable(feature = "pointer_methods", since = "1.26.0")]
441 pub unsafe fn add(self, count: usize) -> Self
445 self.offset(count as isize)
448 /// Calculates the offset from a pointer (convenience for
449 /// `.offset((count as isize).wrapping_neg())`).
451 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
452 /// offset of `3 * size_of::<T>()` bytes.
456 /// If any of the following conditions are violated, the result is Undefined
459 /// * Both the starting and resulting pointer must be either in bounds or one
460 /// byte past the end of the same allocated object. Note that in Rust,
461 /// every (stack-allocated) variable is considered a separate allocated object.
463 /// * The computed offset cannot exceed `isize::MAX` **bytes**.
465 /// * The offset being in bounds cannot rely on "wrapping around" the address
466 /// space. That is, the infinite-precision sum must fit in a usize.
468 /// The compiler and standard library generally tries to ensure allocations
469 /// never reach a size where an offset is a concern. For instance, `Vec`
470 /// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
471 /// `vec.as_ptr().add(vec.len()).sub(vec.len())` is always safe.
473 /// Most platforms fundamentally can't even construct such an allocation.
474 /// For instance, no known 64-bit platform can ever serve a request
475 /// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
476 /// However, some 32-bit and 16-bit platforms may successfully serve a request for
477 /// more than `isize::MAX` bytes with things like Physical Address
478 /// Extension. As such, memory acquired directly from allocators or memory
479 /// mapped files *may* be too large to handle with this function.
481 /// Consider using [`wrapping_sub`] instead if these constraints are
482 /// difficult to satisfy. The only advantage of this method is that it
483 /// enables more aggressive compiler optimizations.
485 /// [`wrapping_sub`]: #method.wrapping_sub
492 /// let s: &str = "123";
495 /// let end: *const u8 = s.as_ptr().add(3);
496 /// println!("{}", *end.sub(1) as char);
497 /// println!("{}", *end.sub(2) as char);
500 #[stable(feature = "pointer_methods", since = "1.26.0")]
502 pub unsafe fn sub(self, count: usize) -> Self
506 self.offset((count as isize).wrapping_neg())
509 /// Calculates the offset from a pointer using wrapping arithmetic.
510 /// (convenience for `.wrapping_offset(count as isize)`)
512 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
513 /// offset of `3 * size_of::<T>()` bytes.
517 /// The resulting pointer does not need to be in bounds, but it is
518 /// potentially hazardous to dereference (which requires `unsafe`).
520 /// In particular, the resulting pointer remains attached to the same allocated
521 /// object that `self` points to. It may *not* be used to access a
522 /// different allocated object. Note that in Rust,
523 /// every (stack-allocated) variable is considered a separate allocated object.
525 /// Compared to [`add`], this method basically delays the requirement of staying
526 /// within the same allocated object: [`add`] is immediate Undefined Behavior when
527 /// crossing object boundaries; `wrapping_add` produces a pointer but still leads
528 /// to Undefined Behavior if that pointer is dereferenced. [`add`] can be optimized
529 /// better and is thus preferable in performance-sensitive code.
531 /// If you need to cross object boundaries, cast the pointer to an integer and
532 /// do the arithmetic there.
534 /// [`add`]: #method.add
541 /// // Iterate using a raw pointer in increments of two elements
542 /// let data = [1u8, 2, 3, 4, 5];
543 /// let mut ptr: *const u8 = data.as_ptr();
545 /// let end_rounded_up = ptr.wrapping_add(6);
547 /// // This loop prints "1, 3, 5, "
548 /// while ptr != end_rounded_up {
550 /// print!("{}, ", *ptr);
552 /// ptr = ptr.wrapping_add(step);
555 #[stable(feature = "pointer_methods", since = "1.26.0")]
557 pub fn wrapping_add(self, count: usize) -> Self
561 self.wrapping_offset(count as isize)
564 /// Calculates the offset from a pointer using wrapping arithmetic.
565 /// (convenience for `.wrapping_offset((count as isize).wrapping_sub())`)
567 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
568 /// offset of `3 * size_of::<T>()` bytes.
572 /// The resulting pointer does not need to be in bounds, but it is
573 /// potentially hazardous to dereference (which requires `unsafe`).
575 /// In particular, the resulting pointer remains attached to the same allocated
576 /// object that `self` points to. It may *not* be used to access a
577 /// different allocated object. Note that in Rust,
578 /// every (stack-allocated) variable is considered a separate allocated object.
580 /// Compared to [`sub`], this method basically delays the requirement of staying
581 /// within the same allocated object: [`sub`] is immediate Undefined Behavior when
582 /// crossing object boundaries; `wrapping_sub` produces a pointer but still leads
583 /// to Undefined Behavior if that pointer is dereferenced. [`sub`] can be optimized
584 /// better and is thus preferable in performance-sensitive code.
586 /// If you need to cross object boundaries, cast the pointer to an integer and
587 /// do the arithmetic there.
589 /// [`sub`]: #method.sub
596 /// // Iterate using a raw pointer in increments of two elements (backwards)
597 /// let data = [1u8, 2, 3, 4, 5];
598 /// let mut ptr: *const u8 = data.as_ptr();
599 /// let start_rounded_down = ptr.wrapping_sub(2);
600 /// ptr = ptr.wrapping_add(4);
602 /// // This loop prints "5, 3, 1, "
603 /// while ptr != start_rounded_down {
605 /// print!("{}, ", *ptr);
607 /// ptr = ptr.wrapping_sub(step);
610 #[stable(feature = "pointer_methods", since = "1.26.0")]
612 pub fn wrapping_sub(self, count: usize) -> Self
616 self.wrapping_offset((count as isize).wrapping_neg())
619 /// Reads the value from `self` without moving it. This leaves the
620 /// memory in `self` unchanged.
622 /// See [`ptr::read`] for safety concerns and examples.
624 /// [`ptr::read`]: ./ptr/fn.read.html
625 #[stable(feature = "pointer_methods", since = "1.26.0")]
627 pub unsafe fn read(self) -> T
634 /// Performs a volatile read of the value from `self` without moving it. This
635 /// leaves the memory in `self` unchanged.
637 /// Volatile operations are intended to act on I/O memory, and are guaranteed
638 /// to not be elided or reordered by the compiler across other volatile
641 /// See [`ptr::read_volatile`] for safety concerns and examples.
643 /// [`ptr::read_volatile`]: ./ptr/fn.read_volatile.html
644 #[stable(feature = "pointer_methods", since = "1.26.0")]
646 pub unsafe fn read_volatile(self) -> T
653 /// Reads the value from `self` without moving it. This leaves the
654 /// memory in `self` unchanged.
656 /// Unlike `read`, the pointer may be unaligned.
658 /// See [`ptr::read_unaligned`] for safety concerns and examples.
660 /// [`ptr::read_unaligned`]: ./ptr/fn.read_unaligned.html
661 #[stable(feature = "pointer_methods", since = "1.26.0")]
663 pub unsafe fn read_unaligned(self) -> T
670 /// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
671 /// and destination may overlap.
673 /// NOTE: this has the *same* argument order as [`ptr::copy`].
675 /// See [`ptr::copy`] for safety concerns and examples.
677 /// [`ptr::copy`]: ./ptr/fn.copy.html
678 #[stable(feature = "pointer_methods", since = "1.26.0")]
680 pub unsafe fn copy_to(self, dest: *mut T, count: usize)
684 copy(self, dest, count)
687 /// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
688 /// and destination may *not* overlap.
690 /// NOTE: this has the *same* argument order as [`ptr::copy_nonoverlapping`].
692 /// See [`ptr::copy_nonoverlapping`] for safety concerns and examples.
694 /// [`ptr::copy_nonoverlapping`]: ./ptr/fn.copy_nonoverlapping.html
695 #[stable(feature = "pointer_methods", since = "1.26.0")]
697 pub unsafe fn copy_to_nonoverlapping(self, dest: *mut T, count: usize)
701 copy_nonoverlapping(self, dest, count)
704 /// Copies `count * size_of<T>` bytes from `src` to `self`. The source
705 /// and destination may overlap.
707 /// NOTE: this has the *opposite* argument order of [`ptr::copy`].
709 /// See [`ptr::copy`] for safety concerns and examples.
711 /// [`ptr::copy`]: ./ptr/fn.copy.html
712 #[stable(feature = "pointer_methods", since = "1.26.0")]
714 pub unsafe fn copy_from(self, src: *const T, count: usize)
718 copy(src, self, count)
721 /// Copies `count * size_of<T>` bytes from `src` to `self`. The source
722 /// and destination may *not* overlap.
724 /// NOTE: this has the *opposite* argument order of [`ptr::copy_nonoverlapping`].
726 /// See [`ptr::copy_nonoverlapping`] for safety concerns and examples.
728 /// [`ptr::copy_nonoverlapping`]: ./ptr/fn.copy_nonoverlapping.html
729 #[stable(feature = "pointer_methods", since = "1.26.0")]
731 pub unsafe fn copy_from_nonoverlapping(self, src: *const T, count: usize)
735 copy_nonoverlapping(src, self, count)
738 /// Executes the destructor (if any) of the pointed-to value.
740 /// See [`ptr::drop_in_place`] for safety concerns and examples.
742 /// [`ptr::drop_in_place`]: ./ptr/fn.drop_in_place.html
743 #[stable(feature = "pointer_methods", since = "1.26.0")]
745 pub unsafe fn drop_in_place(self) {
749 /// Overwrites a memory location with the given value without reading or
750 /// dropping the old value.
752 /// See [`ptr::write`] for safety concerns and examples.
754 /// [`ptr::write`]: ./ptr/fn.write.html
755 #[stable(feature = "pointer_methods", since = "1.26.0")]
757 pub unsafe fn write(self, val: T)
764 /// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
765 /// bytes of memory starting at `self` to `val`.
767 /// See [`ptr::write_bytes`] for safety concerns and examples.
769 /// [`ptr::write_bytes`]: ./ptr/fn.write_bytes.html
770 #[stable(feature = "pointer_methods", since = "1.26.0")]
772 pub unsafe fn write_bytes(self, val: u8, count: usize)
776 write_bytes(self, val, count)
779 /// Performs a volatile write of a memory location with the given value without
780 /// reading or dropping the old value.
782 /// Volatile operations are intended to act on I/O memory, and are guaranteed
783 /// to not be elided or reordered by the compiler across other volatile
786 /// See [`ptr::write_volatile`] for safety concerns and examples.
788 /// [`ptr::write_volatile`]: ./ptr/fn.write_volatile.html
789 #[stable(feature = "pointer_methods", since = "1.26.0")]
791 pub unsafe fn write_volatile(self, val: T)
795 write_volatile(self, val)
798 /// Overwrites a memory location with the given value without reading or
799 /// dropping the old value.
801 /// Unlike `write`, the pointer may be unaligned.
803 /// See [`ptr::write_unaligned`] for safety concerns and examples.
805 /// [`ptr::write_unaligned`]: ./ptr/fn.write_unaligned.html
806 #[stable(feature = "pointer_methods", since = "1.26.0")]
808 pub unsafe fn write_unaligned(self, val: T)
812 write_unaligned(self, val)
815 /// Replaces the value at `self` with `src`, returning the old
816 /// value, without dropping either.
818 /// See [`ptr::replace`] for safety concerns and examples.
820 /// [`ptr::replace`]: ./ptr/fn.replace.html
821 #[stable(feature = "pointer_methods", since = "1.26.0")]
823 pub unsafe fn replace(self, src: T) -> T
830 /// Swaps the values at two mutable locations of the same type, without
831 /// deinitializing either. They may overlap, unlike `mem::swap` which is
832 /// otherwise equivalent.
834 /// See [`ptr::swap`] for safety concerns and examples.
836 /// [`ptr::swap`]: ./ptr/fn.swap.html
837 #[stable(feature = "pointer_methods", since = "1.26.0")]
839 pub unsafe fn swap(self, with: *mut T)
846 /// Computes the offset that needs to be applied to the pointer in order to make it aligned to
849 /// If it is not possible to align the pointer, the implementation returns
850 /// `usize::max_value()`. It is permissible for the implementation to *always*
851 /// return `usize::max_value()`. Only your algorithm's performance can depend
852 /// on getting a usable offset here, not its correctness.
854 /// The offset is expressed in number of `T` elements, and not bytes. The value returned can be
855 /// used with the `wrapping_add` method.
857 /// There are no guarantees whatsoever that offsetting the pointer will not overflow or go
858 /// beyond the allocation that the pointer points into. It is up to the caller to ensure that
859 /// the returned offset is correct in all terms other than alignment.
863 /// The function panics if `align` is not a power-of-two.
867 /// Accessing adjacent `u8` as `u16`
870 /// # fn foo(n: usize) {
871 /// # use std::mem::align_of;
873 /// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
874 /// let ptr = &x[n] as *const u8;
875 /// let offset = ptr.align_offset(align_of::<u16>());
876 /// if offset < x.len() - n - 1 {
877 /// let u16_ptr = ptr.add(offset) as *const u16;
878 /// assert_ne!(*u16_ptr, 500);
880 /// // while the pointer can be aligned via `offset`, it would point
881 /// // outside the allocation
885 #[stable(feature = "align_offset", since = "1.36.0")]
886 pub fn align_offset(self, align: usize) -> usize
890 if !align.is_power_of_two() {
891 panic!("align_offset: align is not a power-of-two");
893 unsafe { align_offset(self, align) }
897 // Equality for pointers
898 #[stable(feature = "rust1", since = "1.0.0")]
899 impl<T: ?Sized> PartialEq for *mut T {
901 fn eq(&self, other: &*mut T) -> bool {
906 #[stable(feature = "rust1", since = "1.0.0")]
907 impl<T: ?Sized> Eq for *mut T {}
909 #[stable(feature = "rust1", since = "1.0.0")]
910 impl<T: ?Sized> Ord for *mut T {
912 fn cmp(&self, other: &*mut T) -> Ordering {
915 } else if self == other {
923 #[stable(feature = "rust1", since = "1.0.0")]
924 impl<T: ?Sized> PartialOrd for *mut T {
926 fn partial_cmp(&self, other: &*mut T) -> Option<Ordering> {
927 Some(self.cmp(other))
931 fn lt(&self, other: &*mut T) -> bool {
936 fn le(&self, other: &*mut T) -> bool {
941 fn gt(&self, other: &*mut T) -> bool {
946 fn ge(&self, other: &*mut T) -> bool {