core/ptr/mut_ptr.rs
1#[cfg(not(feature = "ferrocene_certified"))]
2use super::*;
3#[cfg(not(feature = "ferrocene_certified"))]
4use crate::cmp::Ordering::{Equal, Greater, Less};
5#[cfg(not(feature = "ferrocene_certified"))]
6use crate::intrinsics::const_eval_select;
7use crate::marker::PointeeSized;
8#[cfg(not(feature = "ferrocene_certified"))]
9use crate::mem::{self, SizedTypeProperties};
10#[cfg(not(feature = "ferrocene_certified"))]
11use crate::slice::{self, SliceIndex};
12
13impl<T: PointeeSized> *mut T {
14 #[doc = include_str!("docs/is_null.md")]
15 ///
16 /// # Examples
17 ///
18 /// ```
19 /// let mut s = [1, 2, 3];
20 /// let ptr: *mut u32 = s.as_mut_ptr();
21 /// assert!(!ptr.is_null());
22 /// ```
23 #[stable(feature = "rust1", since = "1.0.0")]
24 #[rustc_const_stable(feature = "const_ptr_is_null", since = "1.84.0")]
25 #[rustc_diagnostic_item = "ptr_is_null"]
26 #[inline]
27 #[cfg(not(feature = "ferrocene_certified"))]
28 pub const fn is_null(self) -> bool {
29 self.cast_const().is_null()
30 }
31
32 /// Casts to a pointer of another type.
33 #[stable(feature = "ptr_cast", since = "1.38.0")]
34 #[rustc_const_stable(feature = "const_ptr_cast", since = "1.38.0")]
35 #[rustc_diagnostic_item = "ptr_cast"]
36 #[inline(always)]
37 pub const fn cast<U>(self) -> *mut U {
38 self as _
39 }
40
41 /// Try to cast to a pointer of another type by checking alignment.
42 ///
43 /// If the pointer is properly aligned to the target type, it will be
44 /// cast to the target type. Otherwise, `None` is returned.
45 ///
46 /// # Examples
47 ///
48 /// ```rust
49 /// #![feature(pointer_try_cast_aligned)]
50 ///
51 /// let mut x = 0u64;
52 ///
53 /// let aligned: *mut u64 = &mut x;
54 /// let unaligned = unsafe { aligned.byte_add(1) };
55 ///
56 /// assert!(aligned.try_cast_aligned::<u32>().is_some());
57 /// assert!(unaligned.try_cast_aligned::<u32>().is_none());
58 /// ```
59 #[unstable(feature = "pointer_try_cast_aligned", issue = "141221")]
60 #[must_use = "this returns the result of the operation, \
61 without modifying the original"]
62 #[inline]
63 #[cfg(not(feature = "ferrocene_certified"))]
64 pub fn try_cast_aligned<U>(self) -> Option<*mut U> {
65 if self.is_aligned_to(align_of::<U>()) { Some(self.cast()) } else { None }
66 }
67
68 /// Uses the address value in a new pointer of another type.
69 ///
70 /// This operation will ignore the address part of its `meta` operand and discard existing
71 /// metadata of `self`. For pointers to a sized types (thin pointers), this has the same effect
72 /// as a simple cast. For pointers to an unsized type (fat pointers) this recombines the address
73 /// with new metadata such as slice lengths or `dyn`-vtable.
74 ///
75 /// The resulting pointer will have provenance of `self`. This operation is semantically the
76 /// same as creating a new pointer with the data pointer value of `self` but the metadata of
77 /// `meta`, being fat or thin depending on the `meta` operand.
78 ///
79 /// # Examples
80 ///
81 /// This function is primarily useful for enabling pointer arithmetic on potentially fat
82 /// pointers. The pointer is cast to a sized pointee to utilize offset operations and then
83 /// recombined with its own original metadata.
84 ///
85 /// ```
86 /// #![feature(set_ptr_value)]
87 /// # use core::fmt::Debug;
88 /// let mut arr: [i32; 3] = [1, 2, 3];
89 /// let mut ptr = arr.as_mut_ptr() as *mut dyn Debug;
90 /// let thin = ptr as *mut u8;
91 /// unsafe {
92 /// ptr = thin.add(8).with_metadata_of(ptr);
93 /// # assert_eq!(*(ptr as *mut i32), 3);
94 /// println!("{:?}", &*ptr); // will print "3"
95 /// }
96 /// ```
97 ///
98 /// # *Incorrect* usage
99 ///
100 /// The provenance from pointers is *not* combined. The result must only be used to refer to the
101 /// address allowed by `self`.
102 ///
103 /// ```rust,no_run
104 /// #![feature(set_ptr_value)]
105 /// let mut x = 0u32;
106 /// let mut y = 1u32;
107 ///
108 /// let x = (&mut x) as *mut u32;
109 /// let y = (&mut y) as *mut u32;
110 ///
111 /// let offset = (x as usize - y as usize) / 4;
112 /// let bad = x.wrapping_add(offset).with_metadata_of(y);
113 ///
114 /// // This dereference is UB. The pointer only has provenance for `x` but points to `y`.
115 /// println!("{:?}", unsafe { &*bad });
116 #[unstable(feature = "set_ptr_value", issue = "75091")]
117 #[must_use = "returns a new pointer rather than modifying its argument"]
118 #[inline]
119 #[cfg(not(feature = "ferrocene_certified"))]
120 pub const fn with_metadata_of<U>(self, meta: *const U) -> *mut U
121 where
122 U: PointeeSized,
123 {
124 from_raw_parts_mut::<U>(self as *mut (), metadata(meta))
125 }
126
127 /// Changes constness without changing the type.
128 ///
129 /// This is a bit safer than `as` because it wouldn't silently change the type if the code is
130 /// refactored.
131 ///
132 /// While not strictly required (`*mut T` coerces to `*const T`), this is provided for symmetry
133 /// with [`cast_mut`] on `*const T` and may have documentation value if used instead of implicit
134 /// coercion.
135 ///
136 /// [`cast_mut`]: pointer::cast_mut
137 #[stable(feature = "ptr_const_cast", since = "1.65.0")]
138 #[rustc_const_stable(feature = "ptr_const_cast", since = "1.65.0")]
139 #[rustc_diagnostic_item = "ptr_cast_const"]
140 #[inline(always)]
141 #[cfg(not(feature = "ferrocene_certified"))]
142 pub const fn cast_const(self) -> *const T {
143 self as _
144 }
145
146 /// Gets the "address" portion of the pointer.
147 ///
148 /// This is similar to `self as usize`, except that the [provenance][crate::ptr#provenance] of
149 /// the pointer is discarded and not [exposed][crate::ptr#exposed-provenance]. This means that
150 /// casting the returned address back to a pointer yields a [pointer without
151 /// provenance][without_provenance_mut], which is undefined behavior to dereference. To properly
152 /// restore the lost information and obtain a dereferenceable pointer, use
153 /// [`with_addr`][pointer::with_addr] or [`map_addr`][pointer::map_addr].
154 ///
155 /// If using those APIs is not possible because there is no way to preserve a pointer with the
156 /// required provenance, then Strict Provenance might not be for you. Use pointer-integer casts
157 /// or [`expose_provenance`][pointer::expose_provenance] and [`with_exposed_provenance`][with_exposed_provenance]
158 /// instead. However, note that this makes your code less portable and less amenable to tools
159 /// that check for compliance with the Rust memory model.
160 ///
161 /// On most platforms this will produce a value with the same bytes as the original
162 /// pointer, because all the bytes are dedicated to describing the address.
163 /// Platforms which need to store additional information in the pointer may
164 /// perform a change of representation to produce a value containing only the address
165 /// portion of the pointer. What that means is up to the platform to define.
166 ///
167 /// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
168 #[must_use]
169 #[inline(always)]
170 #[stable(feature = "strict_provenance", since = "1.84.0")]
171 #[cfg(not(feature = "ferrocene_certified"))]
172 pub fn addr(self) -> usize {
173 // A pointer-to-integer transmute currently has exactly the right semantics: it returns the
174 // address without exposing the provenance. Note that this is *not* a stable guarantee about
175 // transmute semantics, it relies on sysroot crates having special status.
176 // SAFETY: Pointer-to-integer transmutes are valid (if you are okay with losing the
177 // provenance).
178 unsafe { mem::transmute(self.cast::<()>()) }
179 }
180
181 /// Exposes the ["provenance"][crate::ptr#provenance] part of the pointer for future use in
182 /// [`with_exposed_provenance_mut`] and returns the "address" portion.
183 ///
184 /// This is equivalent to `self as usize`, which semantically discards provenance information.
185 /// Furthermore, this (like the `as` cast) has the implicit side-effect of marking the
186 /// provenance as 'exposed', so on platforms that support it you can later call
187 /// [`with_exposed_provenance_mut`] to reconstitute the original pointer including its provenance.
188 ///
189 /// Due to its inherent ambiguity, [`with_exposed_provenance_mut`] may not be supported by tools
190 /// that help you to stay conformant with the Rust memory model. It is recommended to use
191 /// [Strict Provenance][crate::ptr#strict-provenance] APIs such as [`with_addr`][pointer::with_addr]
192 /// wherever possible, in which case [`addr`][pointer::addr] should be used instead of `expose_provenance`.
193 ///
194 /// On most platforms this will produce a value with the same bytes as the original pointer,
195 /// because all the bytes are dedicated to describing the address. Platforms which need to store
196 /// additional information in the pointer may not support this operation, since the 'expose'
197 /// side-effect which is required for [`with_exposed_provenance_mut`] to work is typically not
198 /// available.
199 ///
200 /// This is an [Exposed Provenance][crate::ptr#exposed-provenance] API.
201 ///
202 /// [`with_exposed_provenance_mut`]: with_exposed_provenance_mut
203 #[inline(always)]
204 #[stable(feature = "exposed_provenance", since = "1.84.0")]
205 #[cfg(not(feature = "ferrocene_certified"))]
206 pub fn expose_provenance(self) -> usize {
207 self.cast::<()>() as usize
208 }
209
210 /// Creates a new pointer with the given address and the [provenance][crate::ptr#provenance] of
211 /// `self`.
212 ///
213 /// This is similar to a `addr as *mut T` cast, but copies
214 /// the *provenance* of `self` to the new pointer.
215 /// This avoids the inherent ambiguity of the unary cast.
216 ///
217 /// This is equivalent to using [`wrapping_offset`][pointer::wrapping_offset] to offset
218 /// `self` to the given address, and therefore has all the same capabilities and restrictions.
219 ///
220 /// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
221 #[must_use]
222 #[inline]
223 #[stable(feature = "strict_provenance", since = "1.84.0")]
224 #[cfg(not(feature = "ferrocene_certified"))]
225 pub fn with_addr(self, addr: usize) -> Self {
226 // This should probably be an intrinsic to avoid doing any sort of arithmetic, but
227 // meanwhile, we can implement it with `wrapping_offset`, which preserves the pointer's
228 // provenance.
229 let self_addr = self.addr() as isize;
230 let dest_addr = addr as isize;
231 let offset = dest_addr.wrapping_sub(self_addr);
232 self.wrapping_byte_offset(offset)
233 }
234
235 /// Creates a new pointer by mapping `self`'s address to a new one, preserving the original
236 /// pointer's [provenance][crate::ptr#provenance].
237 ///
238 /// This is a convenience for [`with_addr`][pointer::with_addr], see that method for details.
239 ///
240 /// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
241 #[must_use]
242 #[inline]
243 #[stable(feature = "strict_provenance", since = "1.84.0")]
244 #[cfg(not(feature = "ferrocene_certified"))]
245 pub fn map_addr(self, f: impl FnOnce(usize) -> usize) -> Self {
246 self.with_addr(f(self.addr()))
247 }
248
249 /// Decompose a (possibly wide) pointer into its data pointer and metadata components.
250 ///
251 /// The pointer can be later reconstructed with [`from_raw_parts_mut`].
252 #[unstable(feature = "ptr_metadata", issue = "81513")]
253 #[inline]
254 #[cfg(not(feature = "ferrocene_certified"))]
255 pub const fn to_raw_parts(self) -> (*mut (), <T as super::Pointee>::Metadata) {
256 (self.cast(), super::metadata(self))
257 }
258
259 /// Returns `None` if the pointer is null, or else returns a shared reference to
260 /// the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_ref`]
261 /// must be used instead.
262 ///
263 /// For the mutable counterpart see [`as_mut`].
264 ///
265 /// [`as_uninit_ref`]: pointer#method.as_uninit_ref-1
266 /// [`as_mut`]: #method.as_mut
267 ///
268 /// # Safety
269 ///
270 /// When calling this method, you have to ensure that *either* the pointer is null *or*
271 /// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
272 ///
273 /// # Panics during const evaluation
274 ///
275 /// This method will panic during const evaluation if the pointer cannot be
276 /// determined to be null or not. See [`is_null`] for more information.
277 ///
278 /// [`is_null`]: #method.is_null-1
279 ///
280 /// # Examples
281 ///
282 /// ```
283 /// let ptr: *mut u8 = &mut 10u8 as *mut u8;
284 ///
285 /// unsafe {
286 /// if let Some(val_back) = ptr.as_ref() {
287 /// println!("We got back the value: {val_back}!");
288 /// }
289 /// }
290 /// ```
291 ///
292 /// # Null-unchecked version
293 ///
294 /// If you are sure the pointer can never be null and are looking for some kind of
295 /// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>`, know that you can
296 /// dereference the pointer directly.
297 ///
298 /// ```
299 /// let ptr: *mut u8 = &mut 10u8 as *mut u8;
300 ///
301 /// unsafe {
302 /// let val_back = &*ptr;
303 /// println!("We got back the value: {val_back}!");
304 /// }
305 /// ```
306 #[stable(feature = "ptr_as_ref", since = "1.9.0")]
307 #[rustc_const_stable(feature = "const_ptr_is_null", since = "1.84.0")]
308 #[inline]
309 #[cfg(not(feature = "ferrocene_certified"))]
310 pub const unsafe fn as_ref<'a>(self) -> Option<&'a T> {
311 // SAFETY: the caller must guarantee that `self` is valid for a
312 // reference if it isn't null.
313 if self.is_null() { None } else { unsafe { Some(&*self) } }
314 }
315
316 /// Returns a shared reference to the value behind the pointer.
317 /// If the pointer may be null or the value may be uninitialized, [`as_uninit_ref`] must be used instead.
318 /// If the pointer may be null, but the value is known to have been initialized, [`as_ref`] must be used instead.
319 ///
320 /// For the mutable counterpart see [`as_mut_unchecked`].
321 ///
322 /// [`as_ref`]: #method.as_ref
323 /// [`as_uninit_ref`]: #method.as_uninit_ref
324 /// [`as_mut_unchecked`]: #method.as_mut_unchecked
325 ///
326 /// # Safety
327 ///
328 /// When calling this method, you have to ensure that the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
329 ///
330 /// # Examples
331 ///
332 /// ```
333 /// #![feature(ptr_as_ref_unchecked)]
334 /// let ptr: *mut u8 = &mut 10u8 as *mut u8;
335 ///
336 /// unsafe {
337 /// println!("We got back the value: {}!", ptr.as_ref_unchecked());
338 /// }
339 /// ```
340 // FIXME: mention it in the docs for `as_ref` and `as_uninit_ref` once stabilized.
341 #[unstable(feature = "ptr_as_ref_unchecked", issue = "122034")]
342 #[inline]
343 #[must_use]
344 #[cfg(not(feature = "ferrocene_certified"))]
345 pub const unsafe fn as_ref_unchecked<'a>(self) -> &'a T {
346 // SAFETY: the caller must guarantee that `self` is valid for a reference
347 unsafe { &*self }
348 }
349
350 /// Returns `None` if the pointer is null, or else returns a shared reference to
351 /// the value wrapped in `Some`. In contrast to [`as_ref`], this does not require
352 /// that the value has to be initialized.
353 ///
354 /// For the mutable counterpart see [`as_uninit_mut`].
355 ///
356 /// [`as_ref`]: pointer#method.as_ref-1
357 /// [`as_uninit_mut`]: #method.as_uninit_mut
358 ///
359 /// # Safety
360 ///
361 /// When calling this method, you have to ensure that *either* the pointer is null *or*
362 /// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
363 /// Note that because the created reference is to `MaybeUninit<T>`, the
364 /// source pointer can point to uninitialized memory.
365 ///
366 /// # Panics during const evaluation
367 ///
368 /// This method will panic during const evaluation if the pointer cannot be
369 /// determined to be null or not. See [`is_null`] for more information.
370 ///
371 /// [`is_null`]: #method.is_null-1
372 ///
373 /// # Examples
374 ///
375 /// ```
376 /// #![feature(ptr_as_uninit)]
377 ///
378 /// let ptr: *mut u8 = &mut 10u8 as *mut u8;
379 ///
380 /// unsafe {
381 /// if let Some(val_back) = ptr.as_uninit_ref() {
382 /// println!("We got back the value: {}!", val_back.assume_init());
383 /// }
384 /// }
385 /// ```
386 #[inline]
387 #[unstable(feature = "ptr_as_uninit", issue = "75402")]
388 #[cfg(not(feature = "ferrocene_certified"))]
389 pub const unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>>
390 where
391 T: Sized,
392 {
393 // SAFETY: the caller must guarantee that `self` meets all the
394 // requirements for a reference.
395 if self.is_null() { None } else { Some(unsafe { &*(self as *const MaybeUninit<T>) }) }
396 }
397
398 #[doc = include_str!("./docs/offset.md")]
399 ///
400 /// # Examples
401 ///
402 /// ```
403 /// let mut s = [1, 2, 3];
404 /// let ptr: *mut u32 = s.as_mut_ptr();
405 ///
406 /// unsafe {
407 /// assert_eq!(2, *ptr.offset(1));
408 /// assert_eq!(3, *ptr.offset(2));
409 /// }
410 /// ```
411 #[stable(feature = "rust1", since = "1.0.0")]
412 #[must_use = "returns a new pointer rather than modifying its argument"]
413 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
414 #[inline(always)]
415 #[track_caller]
416 #[cfg(not(feature = "ferrocene_certified"))]
417 pub const unsafe fn offset(self, count: isize) -> *mut T
418 where
419 T: Sized,
420 {
421 #[inline]
422 #[rustc_allow_const_fn_unstable(const_eval_select)]
423 const fn runtime_offset_nowrap(this: *const (), count: isize, size: usize) -> bool {
424 // We can use const_eval_select here because this is only for UB checks.
425 const_eval_select!(
426 @capture { this: *const (), count: isize, size: usize } -> bool:
427 if const {
428 true
429 } else {
430 // `size` is the size of a Rust type, so we know that
431 // `size <= isize::MAX` and thus `as` cast here is not lossy.
432 let Some(byte_offset) = count.checked_mul(size as isize) else {
433 return false;
434 };
435 let (_, overflow) = this.addr().overflowing_add_signed(byte_offset);
436 !overflow
437 }
438 )
439 }
440
441 ub_checks::assert_unsafe_precondition!(
442 check_language_ub,
443 "ptr::offset requires the address calculation to not overflow",
444 (
445 this: *const () = self as *const (),
446 count: isize = count,
447 size: usize = size_of::<T>(),
448 ) => runtime_offset_nowrap(this, count, size)
449 );
450
451 // SAFETY: the caller must uphold the safety contract for `offset`.
452 // The obtained pointer is valid for writes since the caller must
453 // guarantee that it points to the same allocation as `self`.
454 unsafe { intrinsics::offset(self, count) }
455 }
456
457 /// Adds a signed offset in bytes to a pointer.
458 ///
459 /// `count` is in units of **bytes**.
460 ///
461 /// This is purely a convenience for casting to a `u8` pointer and
462 /// using [offset][pointer::offset] on it. See that method for documentation
463 /// and safety requirements.
464 ///
465 /// For non-`Sized` pointees this operation changes only the data pointer,
466 /// leaving the metadata untouched.
467 #[must_use]
468 #[inline(always)]
469 #[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
470 #[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
471 #[track_caller]
472 #[cfg(not(feature = "ferrocene_certified"))]
473 pub const unsafe fn byte_offset(self, count: isize) -> Self {
474 // SAFETY: the caller must uphold the safety contract for `offset`.
475 unsafe { self.cast::<u8>().offset(count).with_metadata_of(self) }
476 }
477
478 /// Adds a signed offset to a pointer using wrapping arithmetic.
479 ///
480 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
481 /// offset of `3 * size_of::<T>()` bytes.
482 ///
483 /// # Safety
484 ///
485 /// This operation itself is always safe, but using the resulting pointer is not.
486 ///
487 /// The resulting pointer "remembers" the [allocation] that `self` points to
488 /// (this is called "[Provenance](ptr/index.html#provenance)").
489 /// The pointer must not be used to read or write other allocations.
490 ///
491 /// In other words, `let z = x.wrapping_offset((y as isize) - (x as isize))` does *not* make `z`
492 /// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
493 /// attached to the object `x` is attached to, and dereferencing it is Undefined Behavior unless
494 /// `x` and `y` point into the same allocation.
495 ///
496 /// Compared to [`offset`], this method basically delays the requirement of staying within the
497 /// same allocation: [`offset`] is immediate Undefined Behavior when crossing object
498 /// boundaries; `wrapping_offset` produces a pointer but still leads to Undefined Behavior if a
499 /// pointer is dereferenced when it is out-of-bounds of the object it is attached to. [`offset`]
500 /// can be optimized better and is thus preferable in performance-sensitive code.
501 ///
502 /// The delayed check only considers the value of the pointer that was dereferenced, not the
503 /// intermediate values used during the computation of the final result. For example,
504 /// `x.wrapping_offset(o).wrapping_offset(o.wrapping_neg())` is always the same as `x`. In other
505 /// words, leaving the allocation and then re-entering it later is permitted.
506 ///
507 /// [`offset`]: #method.offset
508 /// [allocation]: crate::ptr#allocation
509 ///
510 /// # Examples
511 ///
512 /// ```
513 /// // Iterate using a raw pointer in increments of two elements
514 /// let mut data = [1u8, 2, 3, 4, 5];
515 /// let mut ptr: *mut u8 = data.as_mut_ptr();
516 /// let step = 2;
517 /// let end_rounded_up = ptr.wrapping_offset(6);
518 ///
519 /// while ptr != end_rounded_up {
520 /// unsafe {
521 /// *ptr = 0;
522 /// }
523 /// ptr = ptr.wrapping_offset(step);
524 /// }
525 /// assert_eq!(&data, &[0, 2, 0, 4, 0]);
526 /// ```
527 #[stable(feature = "ptr_wrapping_offset", since = "1.16.0")]
528 #[must_use = "returns a new pointer rather than modifying its argument"]
529 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
530 #[inline(always)]
531 #[cfg(not(feature = "ferrocene_certified"))]
532 pub const fn wrapping_offset(self, count: isize) -> *mut T
533 where
534 T: Sized,
535 {
536 // SAFETY: the `arith_offset` intrinsic has no prerequisites to be called.
537 unsafe { intrinsics::arith_offset(self, count) as *mut T }
538 }
539
540 /// Adds a signed offset in bytes to a pointer using wrapping arithmetic.
541 ///
542 /// `count` is in units of **bytes**.
543 ///
544 /// This is purely a convenience for casting to a `u8` pointer and
545 /// using [wrapping_offset][pointer::wrapping_offset] on it. See that method
546 /// for documentation.
547 ///
548 /// For non-`Sized` pointees this operation changes only the data pointer,
549 /// leaving the metadata untouched.
550 #[must_use]
551 #[inline(always)]
552 #[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
553 #[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
554 #[cfg(not(feature = "ferrocene_certified"))]
555 pub const fn wrapping_byte_offset(self, count: isize) -> Self {
556 self.cast::<u8>().wrapping_offset(count).with_metadata_of(self)
557 }
558
559 /// Masks out bits of the pointer according to a mask.
560 ///
561 /// This is convenience for `ptr.map_addr(|a| a & mask)`.
562 ///
563 /// For non-`Sized` pointees this operation changes only the data pointer,
564 /// leaving the metadata untouched.
565 ///
566 /// ## Examples
567 ///
568 /// ```
569 /// #![feature(ptr_mask)]
570 /// let mut v = 17_u32;
571 /// let ptr: *mut u32 = &mut v;
572 ///
573 /// // `u32` is 4 bytes aligned,
574 /// // which means that lower 2 bits are always 0.
575 /// let tag_mask = 0b11;
576 /// let ptr_mask = !tag_mask;
577 ///
578 /// // We can store something in these lower bits
579 /// let tagged_ptr = ptr.map_addr(|a| a | 0b10);
580 ///
581 /// // Get the "tag" back
582 /// let tag = tagged_ptr.addr() & tag_mask;
583 /// assert_eq!(tag, 0b10);
584 ///
585 /// // Note that `tagged_ptr` is unaligned, it's UB to read from/write to it.
586 /// // To get original pointer `mask` can be used:
587 /// let masked_ptr = tagged_ptr.mask(ptr_mask);
588 /// assert_eq!(unsafe { *masked_ptr }, 17);
589 ///
590 /// unsafe { *masked_ptr = 0 };
591 /// assert_eq!(v, 0);
592 /// ```
593 #[unstable(feature = "ptr_mask", issue = "98290")]
594 #[must_use = "returns a new pointer rather than modifying its argument"]
595 #[inline(always)]
596 #[cfg(not(feature = "ferrocene_certified"))]
597 pub fn mask(self, mask: usize) -> *mut T {
598 intrinsics::ptr_mask(self.cast::<()>(), mask).cast_mut().with_metadata_of(self)
599 }
600
601 /// Returns `None` if the pointer is null, or else returns a unique reference to
602 /// the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_mut`]
603 /// must be used instead.
604 ///
605 /// For the shared counterpart see [`as_ref`].
606 ///
607 /// [`as_uninit_mut`]: #method.as_uninit_mut
608 /// [`as_ref`]: pointer#method.as_ref-1
609 ///
610 /// # Safety
611 ///
612 /// When calling this method, you have to ensure that *either*
613 /// the pointer is null *or*
614 /// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
615 ///
616 /// # Panics during const evaluation
617 ///
618 /// This method will panic during const evaluation if the pointer cannot be
619 /// determined to be null or not. See [`is_null`] for more information.
620 ///
621 /// [`is_null`]: #method.is_null-1
622 ///
623 /// # Examples
624 ///
625 /// ```
626 /// let mut s = [1, 2, 3];
627 /// let ptr: *mut u32 = s.as_mut_ptr();
628 /// let first_value = unsafe { ptr.as_mut().unwrap() };
629 /// *first_value = 4;
630 /// # assert_eq!(s, [4, 2, 3]);
631 /// println!("{s:?}"); // It'll print: "[4, 2, 3]".
632 /// ```
633 ///
634 /// # Null-unchecked version
635 ///
636 /// If you are sure the pointer can never be null and are looking for some kind of
637 /// `as_mut_unchecked` that returns the `&mut T` instead of `Option<&mut T>`, know that
638 /// you can dereference the pointer directly.
639 ///
640 /// ```
641 /// let mut s = [1, 2, 3];
642 /// let ptr: *mut u32 = s.as_mut_ptr();
643 /// let first_value = unsafe { &mut *ptr };
644 /// *first_value = 4;
645 /// # assert_eq!(s, [4, 2, 3]);
646 /// println!("{s:?}"); // It'll print: "[4, 2, 3]".
647 /// ```
648 #[stable(feature = "ptr_as_ref", since = "1.9.0")]
649 #[rustc_const_stable(feature = "const_ptr_is_null", since = "1.84.0")]
650 #[inline]
651 #[cfg(not(feature = "ferrocene_certified"))]
652 pub const unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {
653 // SAFETY: the caller must guarantee that `self` is be valid for
654 // a mutable reference if it isn't null.
655 if self.is_null() { None } else { unsafe { Some(&mut *self) } }
656 }
657
658 /// Returns a unique reference to the value behind the pointer.
659 /// If the pointer may be null or the value may be uninitialized, [`as_uninit_mut`] must be used instead.
660 /// If the pointer may be null, but the value is known to have been initialized, [`as_mut`] must be used instead.
661 ///
662 /// For the shared counterpart see [`as_ref_unchecked`].
663 ///
664 /// [`as_mut`]: #method.as_mut
665 /// [`as_uninit_mut`]: #method.as_uninit_mut
666 /// [`as_ref_unchecked`]: #method.as_mut_unchecked
667 ///
668 /// # Safety
669 ///
670 /// When calling this method, you have to ensure that
671 /// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
672 ///
673 /// # Examples
674 ///
675 /// ```
676 /// #![feature(ptr_as_ref_unchecked)]
677 /// let mut s = [1, 2, 3];
678 /// let ptr: *mut u32 = s.as_mut_ptr();
679 /// let first_value = unsafe { ptr.as_mut_unchecked() };
680 /// *first_value = 4;
681 /// # assert_eq!(s, [4, 2, 3]);
682 /// println!("{s:?}"); // It'll print: "[4, 2, 3]".
683 /// ```
684 // FIXME: mention it in the docs for `as_mut` and `as_uninit_mut` once stabilized.
685 #[unstable(feature = "ptr_as_ref_unchecked", issue = "122034")]
686 #[inline]
687 #[must_use]
688 #[cfg(not(feature = "ferrocene_certified"))]
689 pub const unsafe fn as_mut_unchecked<'a>(self) -> &'a mut T {
690 // SAFETY: the caller must guarantee that `self` is valid for a reference
691 unsafe { &mut *self }
692 }
693
694 /// Returns `None` if the pointer is null, or else returns a unique reference to
695 /// the value wrapped in `Some`. In contrast to [`as_mut`], this does not require
696 /// that the value has to be initialized.
697 ///
698 /// For the shared counterpart see [`as_uninit_ref`].
699 ///
700 /// [`as_mut`]: #method.as_mut
701 /// [`as_uninit_ref`]: pointer#method.as_uninit_ref-1
702 ///
703 /// # Safety
704 ///
705 /// When calling this method, you have to ensure that *either* the pointer is null *or*
706 /// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
707 ///
708 /// # Panics during const evaluation
709 ///
710 /// This method will panic during const evaluation if the pointer cannot be
711 /// determined to be null or not. See [`is_null`] for more information.
712 ///
713 /// [`is_null`]: #method.is_null-1
714 #[inline]
715 #[unstable(feature = "ptr_as_uninit", issue = "75402")]
716 #[cfg(not(feature = "ferrocene_certified"))]
717 pub const unsafe fn as_uninit_mut<'a>(self) -> Option<&'a mut MaybeUninit<T>>
718 where
719 T: Sized,
720 {
721 // SAFETY: the caller must guarantee that `self` meets all the
722 // requirements for a reference.
723 if self.is_null() { None } else { Some(unsafe { &mut *(self as *mut MaybeUninit<T>) }) }
724 }
725
726 /// Returns whether two pointers are guaranteed to be equal.
727 ///
728 /// At runtime this function behaves like `Some(self == other)`.
729 /// However, in some contexts (e.g., compile-time evaluation),
730 /// it is not always possible to determine equality of two pointers, so this function may
731 /// spuriously return `None` for pointers that later actually turn out to have its equality known.
732 /// But when it returns `Some`, the pointers' equality is guaranteed to be known.
733 ///
734 /// The return value may change from `Some` to `None` and vice versa depending on the compiler
735 /// version and unsafe code must not
736 /// rely on the result of this function for soundness. It is suggested to only use this function
737 /// for performance optimizations where spurious `None` return values by this function do not
738 /// affect the outcome, but just the performance.
739 /// The consequences of using this method to make runtime and compile-time code behave
740 /// differently have not been explored. This method should not be used to introduce such
741 /// differences, and it should also not be stabilized before we have a better understanding
742 /// of this issue.
743 #[unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
744 #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
745 #[inline]
746 #[cfg(not(feature = "ferrocene_certified"))]
747 pub const fn guaranteed_eq(self, other: *mut T) -> Option<bool>
748 where
749 T: Sized,
750 {
751 (self as *const T).guaranteed_eq(other as _)
752 }
753
754 /// Returns whether two pointers are guaranteed to be inequal.
755 ///
756 /// At runtime this function behaves like `Some(self != other)`.
757 /// However, in some contexts (e.g., compile-time evaluation),
758 /// it is not always possible to determine inequality of two pointers, so this function may
759 /// spuriously return `None` for pointers that later actually turn out to have its inequality known.
760 /// But when it returns `Some`, the pointers' inequality is guaranteed to be known.
761 ///
762 /// The return value may change from `Some` to `None` and vice versa depending on the compiler
763 /// version and unsafe code must not
764 /// rely on the result of this function for soundness. It is suggested to only use this function
765 /// for performance optimizations where spurious `None` return values by this function do not
766 /// affect the outcome, but just the performance.
767 /// The consequences of using this method to make runtime and compile-time code behave
768 /// differently have not been explored. This method should not be used to introduce such
769 /// differences, and it should also not be stabilized before we have a better understanding
770 /// of this issue.
771 #[unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
772 #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
773 #[inline]
774 #[cfg(not(feature = "ferrocene_certified"))]
775 pub const fn guaranteed_ne(self, other: *mut T) -> Option<bool>
776 where
777 T: Sized,
778 {
779 (self as *const T).guaranteed_ne(other as _)
780 }
781
782 /// Calculates the distance between two pointers within the same allocation. The returned value is in
783 /// units of T: the distance in bytes divided by `size_of::<T>()`.
784 ///
785 /// This is equivalent to `(self as isize - origin as isize) / (size_of::<T>() as isize)`,
786 /// except that it has a lot more opportunities for UB, in exchange for the compiler
787 /// better understanding what you are doing.
788 ///
789 /// The primary motivation of this method is for computing the `len` of an array/slice
790 /// of `T` that you are currently representing as a "start" and "end" pointer
791 /// (and "end" is "one past the end" of the array).
792 /// In that case, `end.offset_from(start)` gets you the length of the array.
793 ///
794 /// All of the following safety requirements are trivially satisfied for this usecase.
795 ///
796 /// [`offset`]: pointer#method.offset-1
797 ///
798 /// # Safety
799 ///
800 /// If any of the following conditions are violated, the result is Undefined Behavior:
801 ///
802 /// * `self` and `origin` must either
803 ///
804 /// * point to the same address, or
805 /// * both be [derived from][crate::ptr#provenance] a pointer to the same [allocation], and the memory range between
806 /// the two pointers must be in bounds of that object. (See below for an example.)
807 ///
808 /// * The distance between the pointers, in bytes, must be an exact multiple
809 /// of the size of `T`.
810 ///
811 /// As a consequence, the absolute distance between the pointers, in bytes, computed on
812 /// mathematical integers (without "wrapping around"), cannot overflow an `isize`. This is
813 /// implied by the in-bounds requirement, and the fact that no allocation can be larger
814 /// than `isize::MAX` bytes.
815 ///
816 /// The requirement for pointers to be derived from the same allocation is primarily
817 /// needed for `const`-compatibility: the distance between pointers into *different* allocated
818 /// objects is not known at compile-time. However, the requirement also exists at
819 /// runtime and may be exploited by optimizations. If you wish to compute the difference between
820 /// pointers that are not guaranteed to be from the same allocation, use `(self as isize -
821 /// origin as isize) / size_of::<T>()`.
822 // FIXME: recommend `addr()` instead of `as usize` once that is stable.
823 ///
824 /// [`add`]: #method.add
825 /// [allocation]: crate::ptr#allocation
826 ///
827 /// # Panics
828 ///
829 /// This function panics if `T` is a Zero-Sized Type ("ZST").
830 ///
831 /// # Examples
832 ///
833 /// Basic usage:
834 ///
835 /// ```
836 /// let mut a = [0; 5];
837 /// let ptr1: *mut i32 = &mut a[1];
838 /// let ptr2: *mut i32 = &mut a[3];
839 /// unsafe {
840 /// assert_eq!(ptr2.offset_from(ptr1), 2);
841 /// assert_eq!(ptr1.offset_from(ptr2), -2);
842 /// assert_eq!(ptr1.offset(2), ptr2);
843 /// assert_eq!(ptr2.offset(-2), ptr1);
844 /// }
845 /// ```
846 ///
847 /// *Incorrect* usage:
848 ///
849 /// ```rust,no_run
850 /// let ptr1 = Box::into_raw(Box::new(0u8));
851 /// let ptr2 = Box::into_raw(Box::new(1u8));
852 /// let diff = (ptr2 as isize).wrapping_sub(ptr1 as isize);
853 /// // Make ptr2_other an "alias" of ptr2.add(1), but derived from ptr1.
854 /// let ptr2_other = (ptr1 as *mut u8).wrapping_offset(diff).wrapping_offset(1);
855 /// assert_eq!(ptr2 as usize, ptr2_other as usize);
856 /// // Since ptr2_other and ptr2 are derived from pointers to different objects,
857 /// // computing their offset is undefined behavior, even though
858 /// // they point to addresses that are in-bounds of the same object!
859 /// unsafe {
860 /// let one = ptr2_other.offset_from(ptr2); // Undefined Behavior! ⚠️
861 /// }
862 /// ```
863 #[stable(feature = "ptr_offset_from", since = "1.47.0")]
864 #[rustc_const_stable(feature = "const_ptr_offset_from", since = "1.65.0")]
865 #[inline(always)]
866 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
867 #[cfg(not(feature = "ferrocene_certified"))]
868 pub const unsafe fn offset_from(self, origin: *const T) -> isize
869 where
870 T: Sized,
871 {
872 // SAFETY: the caller must uphold the safety contract for `offset_from`.
873 unsafe { (self as *const T).offset_from(origin) }
874 }
875
876 /// Calculates the distance between two pointers within the same allocation. The returned value is in
877 /// units of **bytes**.
878 ///
879 /// This is purely a convenience for casting to a `u8` pointer and
880 /// using [`offset_from`][pointer::offset_from] on it. See that method for
881 /// documentation and safety requirements.
882 ///
883 /// For non-`Sized` pointees this operation considers only the data pointers,
884 /// ignoring the metadata.
885 #[inline(always)]
886 #[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
887 #[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
888 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
889 #[cfg(not(feature = "ferrocene_certified"))]
890 pub const unsafe fn byte_offset_from<U: ?Sized>(self, origin: *const U) -> isize {
891 // SAFETY: the caller must uphold the safety contract for `offset_from`.
892 unsafe { self.cast::<u8>().offset_from(origin.cast::<u8>()) }
893 }
894
895 /// Calculates the distance between two pointers within the same allocation, *where it's known that
896 /// `self` is equal to or greater than `origin`*. The returned value is in
897 /// units of T: the distance in bytes is divided by `size_of::<T>()`.
898 ///
899 /// This computes the same value that [`offset_from`](#method.offset_from)
900 /// would compute, but with the added precondition that the offset is
901 /// guaranteed to be non-negative. This method is equivalent to
902 /// `usize::try_from(self.offset_from(origin)).unwrap_unchecked()`,
903 /// but it provides slightly more information to the optimizer, which can
904 /// sometimes allow it to optimize slightly better with some backends.
905 ///
906 /// This method can be thought of as recovering the `count` that was passed
907 /// to [`add`](#method.add) (or, with the parameters in the other order,
908 /// to [`sub`](#method.sub)). The following are all equivalent, assuming
909 /// that their safety preconditions are met:
910 /// ```rust
911 /// # unsafe fn blah(ptr: *mut i32, origin: *mut i32, count: usize) -> bool { unsafe {
912 /// ptr.offset_from_unsigned(origin) == count
913 /// # &&
914 /// origin.add(count) == ptr
915 /// # &&
916 /// ptr.sub(count) == origin
917 /// # } }
918 /// ```
919 ///
920 /// # Safety
921 ///
922 /// - The distance between the pointers must be non-negative (`self >= origin`)
923 ///
924 /// - *All* the safety conditions of [`offset_from`](#method.offset_from)
925 /// apply to this method as well; see it for the full details.
926 ///
927 /// Importantly, despite the return type of this method being able to represent
928 /// a larger offset, it's still *not permitted* to pass pointers which differ
929 /// by more than `isize::MAX` *bytes*. As such, the result of this method will
930 /// always be less than or equal to `isize::MAX as usize`.
931 ///
932 /// # Panics
933 ///
934 /// This function panics if `T` is a Zero-Sized Type ("ZST").
935 ///
936 /// # Examples
937 ///
938 /// ```
939 /// let mut a = [0; 5];
940 /// let p: *mut i32 = a.as_mut_ptr();
941 /// unsafe {
942 /// let ptr1: *mut i32 = p.add(1);
943 /// let ptr2: *mut i32 = p.add(3);
944 ///
945 /// assert_eq!(ptr2.offset_from_unsigned(ptr1), 2);
946 /// assert_eq!(ptr1.add(2), ptr2);
947 /// assert_eq!(ptr2.sub(2), ptr1);
948 /// assert_eq!(ptr2.offset_from_unsigned(ptr2), 0);
949 /// }
950 ///
951 /// // This would be incorrect, as the pointers are not correctly ordered:
952 /// // ptr1.offset_from(ptr2)
953 /// ```
954 #[stable(feature = "ptr_sub_ptr", since = "1.87.0")]
955 #[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "1.87.0")]
956 #[inline]
957 #[track_caller]
958 #[cfg(not(feature = "ferrocene_certified"))]
959 pub const unsafe fn offset_from_unsigned(self, origin: *const T) -> usize
960 where
961 T: Sized,
962 {
963 // SAFETY: the caller must uphold the safety contract for `offset_from_unsigned`.
964 unsafe { (self as *const T).offset_from_unsigned(origin) }
965 }
966
967 /// Calculates the distance between two pointers within the same allocation, *where it's known that
968 /// `self` is equal to or greater than `origin`*. The returned value is in
969 /// units of **bytes**.
970 ///
971 /// This is purely a convenience for casting to a `u8` pointer and
972 /// using [`offset_from_unsigned`][pointer::offset_from_unsigned] on it.
973 /// See that method for documentation and safety requirements.
974 ///
975 /// For non-`Sized` pointees this operation considers only the data pointers,
976 /// ignoring the metadata.
977 #[stable(feature = "ptr_sub_ptr", since = "1.87.0")]
978 #[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "1.87.0")]
979 #[inline]
980 #[track_caller]
981 #[cfg(not(feature = "ferrocene_certified"))]
982 pub const unsafe fn byte_offset_from_unsigned<U: ?Sized>(self, origin: *mut U) -> usize {
983 // SAFETY: the caller must uphold the safety contract for `byte_offset_from_unsigned`.
984 unsafe { (self as *const T).byte_offset_from_unsigned(origin) }
985 }
986
987 #[doc = include_str!("./docs/add.md")]
988 ///
989 /// # Examples
990 ///
991 /// ```
992 /// let mut s: String = "123".to_string();
993 /// let ptr: *mut u8 = s.as_mut_ptr();
994 ///
995 /// unsafe {
996 /// assert_eq!('2', *ptr.add(1) as char);
997 /// assert_eq!('3', *ptr.add(2) as char);
998 /// }
999 /// ```
1000 #[stable(feature = "pointer_methods", since = "1.26.0")]
1001 #[must_use = "returns a new pointer rather than modifying its argument"]
1002 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1003 #[inline(always)]
1004 #[track_caller]
1005 #[cfg(not(feature = "ferrocene_certified"))]
1006 pub const unsafe fn add(self, count: usize) -> Self
1007 where
1008 T: Sized,
1009 {
1010 #[cfg(debug_assertions)]
1011 #[inline]
1012 #[rustc_allow_const_fn_unstable(const_eval_select)]
1013 const fn runtime_add_nowrap(this: *const (), count: usize, size: usize) -> bool {
1014 const_eval_select!(
1015 @capture { this: *const (), count: usize, size: usize } -> bool:
1016 if const {
1017 true
1018 } else {
1019 let Some(byte_offset) = count.checked_mul(size) else {
1020 return false;
1021 };
1022 let (_, overflow) = this.addr().overflowing_add(byte_offset);
1023 byte_offset <= (isize::MAX as usize) && !overflow
1024 }
1025 )
1026 }
1027
1028 #[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild.
1029 ub_checks::assert_unsafe_precondition!(
1030 check_language_ub,
1031 "ptr::add requires that the address calculation does not overflow",
1032 (
1033 this: *const () = self as *const (),
1034 count: usize = count,
1035 size: usize = size_of::<T>(),
1036 ) => runtime_add_nowrap(this, count, size)
1037 );
1038
1039 // SAFETY: the caller must uphold the safety contract for `offset`.
1040 unsafe { intrinsics::offset(self, count) }
1041 }
1042
1043 /// Adds an unsigned offset in bytes to a pointer.
1044 ///
1045 /// `count` is in units of bytes.
1046 ///
1047 /// This is purely a convenience for casting to a `u8` pointer and
1048 /// using [add][pointer::add] on it. See that method for documentation
1049 /// and safety requirements.
1050 ///
1051 /// For non-`Sized` pointees this operation changes only the data pointer,
1052 /// leaving the metadata untouched.
1053 #[must_use]
1054 #[inline(always)]
1055 #[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
1056 #[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
1057 #[track_caller]
1058 #[cfg(not(feature = "ferrocene_certified"))]
1059 pub const unsafe fn byte_add(self, count: usize) -> Self {
1060 // SAFETY: the caller must uphold the safety contract for `add`.
1061 unsafe { self.cast::<u8>().add(count).with_metadata_of(self) }
1062 }
1063
1064 /// Subtracts an unsigned offset from a pointer.
1065 ///
1066 /// This can only move the pointer backward (or not move it). If you need to move forward or
1067 /// backward depending on the value, then you might want [`offset`](#method.offset) instead
1068 /// which takes a signed offset.
1069 ///
1070 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
1071 /// offset of `3 * size_of::<T>()` bytes.
1072 ///
1073 /// # Safety
1074 ///
1075 /// If any of the following conditions are violated, the result is Undefined Behavior:
1076 ///
1077 /// * The offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without
1078 /// "wrapping around"), must fit in an `isize`.
1079 ///
1080 /// * If the computed offset is non-zero, then `self` must be [derived from][crate::ptr#provenance] a pointer to some
1081 /// [allocation], and the entire memory range between `self` and the result must be in
1082 /// bounds of that allocation. In particular, this range must not "wrap around" the edge
1083 /// of the address space.
1084 ///
1085 /// Allocations can never be larger than `isize::MAX` bytes, so if the computed offset
1086 /// stays in bounds of the allocation, it is guaranteed to satisfy the first requirement.
1087 /// This implies, for instance, that `vec.as_ptr().add(vec.len())` (for `vec: Vec<T>`) is always
1088 /// safe.
1089 ///
1090 /// Consider using [`wrapping_sub`] instead if these constraints are
1091 /// difficult to satisfy. The only advantage of this method is that it
1092 /// enables more aggressive compiler optimizations.
1093 ///
1094 /// [`wrapping_sub`]: #method.wrapping_sub
1095 /// [allocation]: crate::ptr#allocation
1096 ///
1097 /// # Examples
1098 ///
1099 /// ```
1100 /// let s: &str = "123";
1101 ///
1102 /// unsafe {
1103 /// let end: *const u8 = s.as_ptr().add(3);
1104 /// assert_eq!('3', *end.sub(1) as char);
1105 /// assert_eq!('2', *end.sub(2) as char);
1106 /// }
1107 /// ```
1108 #[stable(feature = "pointer_methods", since = "1.26.0")]
1109 #[must_use = "returns a new pointer rather than modifying its argument"]
1110 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1111 #[inline(always)]
1112 #[track_caller]
1113 #[cfg(not(feature = "ferrocene_certified"))]
1114 pub const unsafe fn sub(self, count: usize) -> Self
1115 where
1116 T: Sized,
1117 {
1118 #[cfg(debug_assertions)]
1119 #[inline]
1120 #[rustc_allow_const_fn_unstable(const_eval_select)]
1121 const fn runtime_sub_nowrap(this: *const (), count: usize, size: usize) -> bool {
1122 const_eval_select!(
1123 @capture { this: *const (), count: usize, size: usize } -> bool:
1124 if const {
1125 true
1126 } else {
1127 let Some(byte_offset) = count.checked_mul(size) else {
1128 return false;
1129 };
1130 byte_offset <= (isize::MAX as usize) && this.addr() >= byte_offset
1131 }
1132 )
1133 }
1134
1135 #[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild.
1136 ub_checks::assert_unsafe_precondition!(
1137 check_language_ub,
1138 "ptr::sub requires that the address calculation does not overflow",
1139 (
1140 this: *const () = self as *const (),
1141 count: usize = count,
1142 size: usize = size_of::<T>(),
1143 ) => runtime_sub_nowrap(this, count, size)
1144 );
1145
1146 if T::IS_ZST {
1147 // Pointer arithmetic does nothing when the pointee is a ZST.
1148 self
1149 } else {
1150 // SAFETY: the caller must uphold the safety contract for `offset`.
1151 // Because the pointee is *not* a ZST, that means that `count` is
1152 // at most `isize::MAX`, and thus the negation cannot overflow.
1153 unsafe { intrinsics::offset(self, intrinsics::unchecked_sub(0, count as isize)) }
1154 }
1155 }
1156
1157 /// Subtracts an unsigned offset in bytes from a pointer.
1158 ///
1159 /// `count` is in units of bytes.
1160 ///
1161 /// This is purely a convenience for casting to a `u8` pointer and
1162 /// using [sub][pointer::sub] on it. See that method for documentation
1163 /// and safety requirements.
1164 ///
1165 /// For non-`Sized` pointees this operation changes only the data pointer,
1166 /// leaving the metadata untouched.
1167 #[must_use]
1168 #[inline(always)]
1169 #[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
1170 #[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
1171 #[track_caller]
1172 #[cfg(not(feature = "ferrocene_certified"))]
1173 pub const unsafe fn byte_sub(self, count: usize) -> Self {
1174 // SAFETY: the caller must uphold the safety contract for `sub`.
1175 unsafe { self.cast::<u8>().sub(count).with_metadata_of(self) }
1176 }
1177
1178 /// Adds an unsigned offset to a pointer using wrapping arithmetic.
1179 ///
1180 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
1181 /// offset of `3 * size_of::<T>()` bytes.
1182 ///
1183 /// # Safety
1184 ///
1185 /// This operation itself is always safe, but using the resulting pointer is not.
1186 ///
1187 /// The resulting pointer "remembers" the [allocation] that `self` points to; it must not
1188 /// be used to read or write other allocations.
1189 ///
1190 /// In other words, `let z = x.wrapping_add((y as usize) - (x as usize))` does *not* make `z`
1191 /// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
1192 /// attached to the object `x` is attached to, and dereferencing it is Undefined Behavior unless
1193 /// `x` and `y` point into the same allocation.
1194 ///
1195 /// Compared to [`add`], this method basically delays the requirement of staying within the
1196 /// same allocation: [`add`] is immediate Undefined Behavior when crossing object
1197 /// boundaries; `wrapping_add` produces a pointer but still leads to Undefined Behavior if a
1198 /// pointer is dereferenced when it is out-of-bounds of the object it is attached to. [`add`]
1199 /// can be optimized better and is thus preferable in performance-sensitive code.
1200 ///
1201 /// The delayed check only considers the value of the pointer that was dereferenced, not the
1202 /// intermediate values used during the computation of the final result. For example,
1203 /// `x.wrapping_add(o).wrapping_sub(o)` is always the same as `x`. In other words, leaving the
1204 /// allocation and then re-entering it later is permitted.
1205 ///
1206 /// [`add`]: #method.add
1207 /// [allocation]: crate::ptr#allocation
1208 ///
1209 /// # Examples
1210 ///
1211 /// ```
1212 /// // Iterate using a raw pointer in increments of two elements
1213 /// let data = [1u8, 2, 3, 4, 5];
1214 /// let mut ptr: *const u8 = data.as_ptr();
1215 /// let step = 2;
1216 /// let end_rounded_up = ptr.wrapping_add(6);
1217 ///
1218 /// // This loop prints "1, 3, 5, "
1219 /// while ptr != end_rounded_up {
1220 /// unsafe {
1221 /// print!("{}, ", *ptr);
1222 /// }
1223 /// ptr = ptr.wrapping_add(step);
1224 /// }
1225 /// ```
1226 #[stable(feature = "pointer_methods", since = "1.26.0")]
1227 #[must_use = "returns a new pointer rather than modifying its argument"]
1228 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1229 #[inline(always)]
1230 #[cfg(not(feature = "ferrocene_certified"))]
1231 pub const fn wrapping_add(self, count: usize) -> Self
1232 where
1233 T: Sized,
1234 {
1235 self.wrapping_offset(count as isize)
1236 }
1237
1238 /// Adds an unsigned offset in bytes to a pointer using wrapping arithmetic.
1239 ///
1240 /// `count` is in units of bytes.
1241 ///
1242 /// This is purely a convenience for casting to a `u8` pointer and
1243 /// using [wrapping_add][pointer::wrapping_add] on it. See that method for documentation.
1244 ///
1245 /// For non-`Sized` pointees this operation changes only the data pointer,
1246 /// leaving the metadata untouched.
1247 #[must_use]
1248 #[inline(always)]
1249 #[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
1250 #[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
1251 #[cfg(not(feature = "ferrocene_certified"))]
1252 pub const fn wrapping_byte_add(self, count: usize) -> Self {
1253 self.cast::<u8>().wrapping_add(count).with_metadata_of(self)
1254 }
1255
1256 /// Subtracts an unsigned offset from a pointer using wrapping arithmetic.
1257 ///
1258 /// `count` is in units of T; e.g., a `count` of 3 represents a pointer
1259 /// offset of `3 * size_of::<T>()` bytes.
1260 ///
1261 /// # Safety
1262 ///
1263 /// This operation itself is always safe, but using the resulting pointer is not.
1264 ///
1265 /// The resulting pointer "remembers" the [allocation] that `self` points to; it must not
1266 /// be used to read or write other allocations.
1267 ///
1268 /// In other words, `let z = x.wrapping_sub((x as usize) - (y as usize))` does *not* make `z`
1269 /// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
1270 /// attached to the object `x` is attached to, and dereferencing it is Undefined Behavior unless
1271 /// `x` and `y` point into the same allocation.
1272 ///
1273 /// Compared to [`sub`], this method basically delays the requirement of staying within the
1274 /// same allocation: [`sub`] is immediate Undefined Behavior when crossing object
1275 /// boundaries; `wrapping_sub` produces a pointer but still leads to Undefined Behavior if a
1276 /// pointer is dereferenced when it is out-of-bounds of the object it is attached to. [`sub`]
1277 /// can be optimized better and is thus preferable in performance-sensitive code.
1278 ///
1279 /// The delayed check only considers the value of the pointer that was dereferenced, not the
1280 /// intermediate values used during the computation of the final result. For example,
1281 /// `x.wrapping_add(o).wrapping_sub(o)` is always the same as `x`. In other words, leaving the
1282 /// allocation and then re-entering it later is permitted.
1283 ///
1284 /// [`sub`]: #method.sub
1285 /// [allocation]: crate::ptr#allocation
1286 ///
1287 /// # Examples
1288 ///
1289 /// ```
1290 /// // Iterate using a raw pointer in increments of two elements (backwards)
1291 /// let data = [1u8, 2, 3, 4, 5];
1292 /// let mut ptr: *const u8 = data.as_ptr();
1293 /// let start_rounded_down = ptr.wrapping_sub(2);
1294 /// ptr = ptr.wrapping_add(4);
1295 /// let step = 2;
1296 /// // This loop prints "5, 3, 1, "
1297 /// while ptr != start_rounded_down {
1298 /// unsafe {
1299 /// print!("{}, ", *ptr);
1300 /// }
1301 /// ptr = ptr.wrapping_sub(step);
1302 /// }
1303 /// ```
1304 #[stable(feature = "pointer_methods", since = "1.26.0")]
1305 #[must_use = "returns a new pointer rather than modifying its argument"]
1306 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1307 #[inline(always)]
1308 #[cfg(not(feature = "ferrocene_certified"))]
1309 pub const fn wrapping_sub(self, count: usize) -> Self
1310 where
1311 T: Sized,
1312 {
1313 self.wrapping_offset((count as isize).wrapping_neg())
1314 }
1315
1316 /// Subtracts an unsigned offset in bytes from a pointer using wrapping arithmetic.
1317 ///
1318 /// `count` is in units of bytes.
1319 ///
1320 /// This is purely a convenience for casting to a `u8` pointer and
1321 /// using [wrapping_sub][pointer::wrapping_sub] on it. See that method for documentation.
1322 ///
1323 /// For non-`Sized` pointees this operation changes only the data pointer,
1324 /// leaving the metadata untouched.
1325 #[must_use]
1326 #[inline(always)]
1327 #[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
1328 #[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
1329 #[cfg(not(feature = "ferrocene_certified"))]
1330 pub const fn wrapping_byte_sub(self, count: usize) -> Self {
1331 self.cast::<u8>().wrapping_sub(count).with_metadata_of(self)
1332 }
1333
1334 /// Reads the value from `self` without moving it. This leaves the
1335 /// memory in `self` unchanged.
1336 ///
1337 /// See [`ptr::read`] for safety concerns and examples.
1338 ///
1339 /// [`ptr::read`]: crate::ptr::read()
1340 #[stable(feature = "pointer_methods", since = "1.26.0")]
1341 #[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
1342 #[inline(always)]
1343 #[track_caller]
1344 #[cfg(not(feature = "ferrocene_certified"))]
1345 pub const unsafe fn read(self) -> T
1346 where
1347 T: Sized,
1348 {
1349 // SAFETY: the caller must uphold the safety contract for ``.
1350 unsafe { read(self) }
1351 }
1352
1353 /// Performs a volatile read of the value from `self` without moving it. This
1354 /// leaves the memory in `self` unchanged.
1355 ///
1356 /// Volatile operations are intended to act on I/O memory, and are guaranteed
1357 /// to not be elided or reordered by the compiler across other volatile
1358 /// operations.
1359 ///
1360 /// See [`ptr::read_volatile`] for safety concerns and examples.
1361 ///
1362 /// [`ptr::read_volatile`]: crate::ptr::read_volatile()
1363 #[stable(feature = "pointer_methods", since = "1.26.0")]
1364 #[inline(always)]
1365 #[track_caller]
1366 #[cfg(not(feature = "ferrocene_certified"))]
1367 pub unsafe fn read_volatile(self) -> T
1368 where
1369 T: Sized,
1370 {
1371 // SAFETY: the caller must uphold the safety contract for `read_volatile`.
1372 unsafe { read_volatile(self) }
1373 }
1374
1375 /// Reads the value from `self` without moving it. This leaves the
1376 /// memory in `self` unchanged.
1377 ///
1378 /// Unlike `read`, the pointer may be unaligned.
1379 ///
1380 /// See [`ptr::read_unaligned`] for safety concerns and examples.
1381 ///
1382 /// [`ptr::read_unaligned`]: crate::ptr::read_unaligned()
1383 #[stable(feature = "pointer_methods", since = "1.26.0")]
1384 #[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
1385 #[inline(always)]
1386 #[track_caller]
1387 #[cfg(not(feature = "ferrocene_certified"))]
1388 pub const unsafe fn read_unaligned(self) -> T
1389 where
1390 T: Sized,
1391 {
1392 // SAFETY: the caller must uphold the safety contract for `read_unaligned`.
1393 unsafe { read_unaligned(self) }
1394 }
1395
1396 /// Copies `count * size_of::<T>()` bytes from `self` to `dest`. The source
1397 /// and destination may overlap.
1398 ///
1399 /// NOTE: this has the *same* argument order as [`ptr::copy`].
1400 ///
1401 /// See [`ptr::copy`] for safety concerns and examples.
1402 ///
1403 /// [`ptr::copy`]: crate::ptr::copy()
1404 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
1405 #[stable(feature = "pointer_methods", since = "1.26.0")]
1406 #[inline(always)]
1407 #[track_caller]
1408 #[cfg(not(feature = "ferrocene_certified"))]
1409 pub const unsafe fn copy_to(self, dest: *mut T, count: usize)
1410 where
1411 T: Sized,
1412 {
1413 // SAFETY: the caller must uphold the safety contract for `copy`.
1414 unsafe { copy(self, dest, count) }
1415 }
1416
1417 /// Copies `count * size_of::<T>()` bytes from `self` to `dest`. The source
1418 /// and destination may *not* overlap.
1419 ///
1420 /// NOTE: this has the *same* argument order as [`ptr::copy_nonoverlapping`].
1421 ///
1422 /// See [`ptr::copy_nonoverlapping`] for safety concerns and examples.
1423 ///
1424 /// [`ptr::copy_nonoverlapping`]: crate::ptr::copy_nonoverlapping()
1425 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
1426 #[stable(feature = "pointer_methods", since = "1.26.0")]
1427 #[inline(always)]
1428 #[track_caller]
1429 #[cfg(not(feature = "ferrocene_certified"))]
1430 pub const unsafe fn copy_to_nonoverlapping(self, dest: *mut T, count: usize)
1431 where
1432 T: Sized,
1433 {
1434 // SAFETY: the caller must uphold the safety contract for `copy_nonoverlapping`.
1435 unsafe { copy_nonoverlapping(self, dest, count) }
1436 }
1437
1438 /// Copies `count * size_of::<T>()` bytes from `src` to `self`. The source
1439 /// and destination may overlap.
1440 ///
1441 /// NOTE: this has the *opposite* argument order of [`ptr::copy`].
1442 ///
1443 /// See [`ptr::copy`] for safety concerns and examples.
1444 ///
1445 /// [`ptr::copy`]: crate::ptr::copy()
1446 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
1447 #[stable(feature = "pointer_methods", since = "1.26.0")]
1448 #[inline(always)]
1449 #[track_caller]
1450 #[cfg(not(feature = "ferrocene_certified"))]
1451 pub const unsafe fn copy_from(self, src: *const T, count: usize)
1452 where
1453 T: Sized,
1454 {
1455 // SAFETY: the caller must uphold the safety contract for `copy`.
1456 unsafe { copy(src, self, count) }
1457 }
1458
1459 /// Copies `count * size_of::<T>()` bytes from `src` to `self`. The source
1460 /// and destination may *not* overlap.
1461 ///
1462 /// NOTE: this has the *opposite* argument order of [`ptr::copy_nonoverlapping`].
1463 ///
1464 /// See [`ptr::copy_nonoverlapping`] for safety concerns and examples.
1465 ///
1466 /// [`ptr::copy_nonoverlapping`]: crate::ptr::copy_nonoverlapping()
1467 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
1468 #[stable(feature = "pointer_methods", since = "1.26.0")]
1469 #[inline(always)]
1470 #[track_caller]
1471 #[cfg(not(feature = "ferrocene_certified"))]
1472 pub const unsafe fn copy_from_nonoverlapping(self, src: *const T, count: usize)
1473 where
1474 T: Sized,
1475 {
1476 // SAFETY: the caller must uphold the safety contract for `copy_nonoverlapping`.
1477 unsafe { copy_nonoverlapping(src, self, count) }
1478 }
1479
1480 /// Executes the destructor (if any) of the pointed-to value.
1481 ///
1482 /// See [`ptr::drop_in_place`] for safety concerns and examples.
1483 ///
1484 /// [`ptr::drop_in_place`]: crate::ptr::drop_in_place()
1485 #[stable(feature = "pointer_methods", since = "1.26.0")]
1486 #[inline(always)]
1487 #[cfg(not(feature = "ferrocene_certified"))]
1488 pub unsafe fn drop_in_place(self) {
1489 // SAFETY: the caller must uphold the safety contract for `drop_in_place`.
1490 unsafe { drop_in_place(self) }
1491 }
1492
1493 /// Overwrites a memory location with the given value without reading or
1494 /// dropping the old value.
1495 ///
1496 /// See [`ptr::write`] for safety concerns and examples.
1497 ///
1498 /// [`ptr::write`]: crate::ptr::write()
1499 #[stable(feature = "pointer_methods", since = "1.26.0")]
1500 #[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
1501 #[inline(always)]
1502 #[track_caller]
1503 #[cfg(not(feature = "ferrocene_certified"))]
1504 pub const unsafe fn write(self, val: T)
1505 where
1506 T: Sized,
1507 {
1508 // SAFETY: the caller must uphold the safety contract for `write`.
1509 unsafe { write(self, val) }
1510 }
1511
1512 /// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
1513 /// bytes of memory starting at `self` to `val`.
1514 ///
1515 /// See [`ptr::write_bytes`] for safety concerns and examples.
1516 ///
1517 /// [`ptr::write_bytes`]: crate::ptr::write_bytes()
1518 #[doc(alias = "memset")]
1519 #[stable(feature = "pointer_methods", since = "1.26.0")]
1520 #[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
1521 #[inline(always)]
1522 #[track_caller]
1523 #[cfg(not(feature = "ferrocene_certified"))]
1524 pub const unsafe fn write_bytes(self, val: u8, count: usize)
1525 where
1526 T: Sized,
1527 {
1528 // SAFETY: the caller must uphold the safety contract for `write_bytes`.
1529 unsafe { write_bytes(self, val, count) }
1530 }
1531
1532 /// Performs a volatile write of a memory location with the given value without
1533 /// reading or dropping the old value.
1534 ///
1535 /// Volatile operations are intended to act on I/O memory, and are guaranteed
1536 /// to not be elided or reordered by the compiler across other volatile
1537 /// operations.
1538 ///
1539 /// See [`ptr::write_volatile`] for safety concerns and examples.
1540 ///
1541 /// [`ptr::write_volatile`]: crate::ptr::write_volatile()
1542 #[stable(feature = "pointer_methods", since = "1.26.0")]
1543 #[inline(always)]
1544 #[track_caller]
1545 #[cfg(not(feature = "ferrocene_certified"))]
1546 pub unsafe fn write_volatile(self, val: T)
1547 where
1548 T: Sized,
1549 {
1550 // SAFETY: the caller must uphold the safety contract for `write_volatile`.
1551 unsafe { write_volatile(self, val) }
1552 }
1553
1554 /// Overwrites a memory location with the given value without reading or
1555 /// dropping the old value.
1556 ///
1557 /// Unlike `write`, the pointer may be unaligned.
1558 ///
1559 /// See [`ptr::write_unaligned`] for safety concerns and examples.
1560 ///
1561 /// [`ptr::write_unaligned`]: crate::ptr::write_unaligned()
1562 #[stable(feature = "pointer_methods", since = "1.26.0")]
1563 #[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
1564 #[inline(always)]
1565 #[track_caller]
1566 #[cfg(not(feature = "ferrocene_certified"))]
1567 pub const unsafe fn write_unaligned(self, val: T)
1568 where
1569 T: Sized,
1570 {
1571 // SAFETY: the caller must uphold the safety contract for `write_unaligned`.
1572 unsafe { write_unaligned(self, val) }
1573 }
1574
1575 /// Replaces the value at `self` with `src`, returning the old
1576 /// value, without dropping either.
1577 ///
1578 /// See [`ptr::replace`] for safety concerns and examples.
1579 ///
1580 /// [`ptr::replace`]: crate::ptr::replace()
1581 #[stable(feature = "pointer_methods", since = "1.26.0")]
1582 #[rustc_const_stable(feature = "const_inherent_ptr_replace", since = "1.88.0")]
1583 #[inline(always)]
1584 #[cfg(not(feature = "ferrocene_certified"))]
1585 pub const unsafe fn replace(self, src: T) -> T
1586 where
1587 T: Sized,
1588 {
1589 // SAFETY: the caller must uphold the safety contract for `replace`.
1590 unsafe { replace(self, src) }
1591 }
1592
1593 /// Swaps the values at two mutable locations of the same type, without
1594 /// deinitializing either. They may overlap, unlike `mem::swap` which is
1595 /// otherwise equivalent.
1596 ///
1597 /// See [`ptr::swap`] for safety concerns and examples.
1598 ///
1599 /// [`ptr::swap`]: crate::ptr::swap()
1600 #[stable(feature = "pointer_methods", since = "1.26.0")]
1601 #[rustc_const_stable(feature = "const_swap", since = "1.85.0")]
1602 #[inline(always)]
1603 #[cfg(not(feature = "ferrocene_certified"))]
1604 pub const unsafe fn swap(self, with: *mut T)
1605 where
1606 T: Sized,
1607 {
1608 // SAFETY: the caller must uphold the safety contract for `swap`.
1609 unsafe { swap(self, with) }
1610 }
1611
1612 /// Computes the offset that needs to be applied to the pointer in order to make it aligned to
1613 /// `align`.
1614 ///
1615 /// If it is not possible to align the pointer, the implementation returns
1616 /// `usize::MAX`.
1617 ///
1618 /// The offset is expressed in number of `T` elements, and not bytes. The value returned can be
1619 /// used with the `wrapping_add` method.
1620 ///
1621 /// There are no guarantees whatsoever that offsetting the pointer will not overflow or go
1622 /// beyond the allocation that the pointer points into. It is up to the caller to ensure that
1623 /// the returned offset is correct in all terms other than alignment.
1624 ///
1625 /// # Panics
1626 ///
1627 /// The function panics if `align` is not a power-of-two.
1628 ///
1629 /// # Examples
1630 ///
1631 /// Accessing adjacent `u8` as `u16`
1632 ///
1633 /// ```
1634 /// # unsafe {
1635 /// let mut x = [5_u8, 6, 7, 8, 9];
1636 /// let ptr = x.as_mut_ptr();
1637 /// let offset = ptr.align_offset(align_of::<u16>());
1638 ///
1639 /// if offset < x.len() - 1 {
1640 /// let u16_ptr = ptr.add(offset).cast::<u16>();
1641 /// *u16_ptr = 0;
1642 ///
1643 /// assert!(x == [0, 0, 7, 8, 9] || x == [5, 0, 0, 8, 9]);
1644 /// } else {
1645 /// // while the pointer can be aligned via `offset`, it would point
1646 /// // outside the allocation
1647 /// }
1648 /// # }
1649 /// ```
1650 #[must_use]
1651 #[inline]
1652 #[stable(feature = "align_offset", since = "1.36.0")]
1653 #[cfg(not(feature = "ferrocene_certified"))]
1654 pub fn align_offset(self, align: usize) -> usize
1655 where
1656 T: Sized,
1657 {
1658 if !align.is_power_of_two() {
1659 panic!("align_offset: align is not a power-of-two");
1660 }
1661
1662 // SAFETY: `align` has been checked to be a power of 2 above
1663 let ret = unsafe { align_offset(self, align) };
1664
1665 // Inform Miri that we want to consider the resulting pointer to be suitably aligned.
1666 #[cfg(miri)]
1667 if ret != usize::MAX {
1668 intrinsics::miri_promise_symbolic_alignment(
1669 self.wrapping_add(ret).cast_const().cast(),
1670 align,
1671 );
1672 }
1673
1674 ret
1675 }
1676
1677 /// Returns whether the pointer is properly aligned for `T`.
1678 ///
1679 /// # Examples
1680 ///
1681 /// ```
1682 /// // On some platforms, the alignment of i32 is less than 4.
1683 /// #[repr(align(4))]
1684 /// struct AlignedI32(i32);
1685 ///
1686 /// let mut data = AlignedI32(42);
1687 /// let ptr = &mut data as *mut AlignedI32;
1688 ///
1689 /// assert!(ptr.is_aligned());
1690 /// assert!(!ptr.wrapping_byte_add(1).is_aligned());
1691 /// ```
1692 #[must_use]
1693 #[inline]
1694 #[stable(feature = "pointer_is_aligned", since = "1.79.0")]
1695 #[cfg(not(feature = "ferrocene_certified"))]
1696 pub fn is_aligned(self) -> bool
1697 where
1698 T: Sized,
1699 {
1700 self.is_aligned_to(align_of::<T>())
1701 }
1702
1703 /// Returns whether the pointer is aligned to `align`.
1704 ///
1705 /// For non-`Sized` pointees this operation considers only the data pointer,
1706 /// ignoring the metadata.
1707 ///
1708 /// # Panics
1709 ///
1710 /// The function panics if `align` is not a power-of-two (this includes 0).
1711 ///
1712 /// # Examples
1713 ///
1714 /// ```
1715 /// #![feature(pointer_is_aligned_to)]
1716 ///
1717 /// // On some platforms, the alignment of i32 is less than 4.
1718 /// #[repr(align(4))]
1719 /// struct AlignedI32(i32);
1720 ///
1721 /// let mut data = AlignedI32(42);
1722 /// let ptr = &mut data as *mut AlignedI32;
1723 ///
1724 /// assert!(ptr.is_aligned_to(1));
1725 /// assert!(ptr.is_aligned_to(2));
1726 /// assert!(ptr.is_aligned_to(4));
1727 ///
1728 /// assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
1729 /// assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
1730 ///
1731 /// assert_ne!(ptr.is_aligned_to(8), ptr.wrapping_add(1).is_aligned_to(8));
1732 /// ```
1733 #[must_use]
1734 #[inline]
1735 #[unstable(feature = "pointer_is_aligned_to", issue = "96284")]
1736 #[cfg(not(feature = "ferrocene_certified"))]
1737 pub fn is_aligned_to(self, align: usize) -> bool {
1738 if !align.is_power_of_two() {
1739 panic!("is_aligned_to: align is not a power-of-two");
1740 }
1741
1742 self.addr() & (align - 1) == 0
1743 }
1744}
1745
1746#[cfg(not(feature = "ferrocene_certified"))]
1747impl<T> *mut [T] {
1748 /// Returns the length of a raw slice.
1749 ///
1750 /// The returned value is the number of **elements**, not the number of bytes.
1751 ///
1752 /// This function is safe, even when the raw slice cannot be cast to a slice
1753 /// reference because the pointer is null or unaligned.
1754 ///
1755 /// # Examples
1756 ///
1757 /// ```rust
1758 /// use std::ptr;
1759 ///
1760 /// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3);
1761 /// assert_eq!(slice.len(), 3);
1762 /// ```
1763 #[inline(always)]
1764 #[stable(feature = "slice_ptr_len", since = "1.79.0")]
1765 #[rustc_const_stable(feature = "const_slice_ptr_len", since = "1.79.0")]
1766 pub const fn len(self) -> usize {
1767 metadata(self)
1768 }
1769
1770 /// Returns `true` if the raw slice has a length of 0.
1771 ///
1772 /// # Examples
1773 ///
1774 /// ```
1775 /// use std::ptr;
1776 ///
1777 /// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3);
1778 /// assert!(!slice.is_empty());
1779 /// ```
1780 #[inline(always)]
1781 #[stable(feature = "slice_ptr_len", since = "1.79.0")]
1782 #[rustc_const_stable(feature = "const_slice_ptr_len", since = "1.79.0")]
1783 pub const fn is_empty(self) -> bool {
1784 self.len() == 0
1785 }
1786
1787 /// Gets a raw, mutable pointer to the underlying array.
1788 ///
1789 /// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
1790 #[unstable(feature = "slice_as_array", issue = "133508")]
1791 #[inline]
1792 #[must_use]
1793 pub const fn as_mut_array<const N: usize>(self) -> Option<*mut [T; N]> {
1794 if self.len() == N {
1795 let me = self.as_mut_ptr() as *mut [T; N];
1796 Some(me)
1797 } else {
1798 None
1799 }
1800 }
1801
1802 /// Divides one mutable raw slice into two at an index.
1803 ///
1804 /// The first will contain all indices from `[0, mid)` (excluding
1805 /// the index `mid` itself) and the second will contain all
1806 /// indices from `[mid, len)` (excluding the index `len` itself).
1807 ///
1808 /// # Panics
1809 ///
1810 /// Panics if `mid > len`.
1811 ///
1812 /// # Safety
1813 ///
1814 /// `mid` must be [in-bounds] of the underlying [allocation].
1815 /// Which means `self` must be dereferenceable and span a single allocation
1816 /// that is at least `mid * size_of::<T>()` bytes long. Not upholding these
1817 /// requirements is *[undefined behavior]* even if the resulting pointers are not used.
1818 ///
1819 /// Since `len` being in-bounds it is not a safety invariant of `*mut [T]` the
1820 /// safety requirements of this method are the same as for [`split_at_mut_unchecked`].
1821 /// The explicit bounds check is only as useful as `len` is correct.
1822 ///
1823 /// [`split_at_mut_unchecked`]: #method.split_at_mut_unchecked
1824 /// [in-bounds]: #method.add
1825 /// [allocation]: crate::ptr#allocation
1826 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
1827 ///
1828 /// # Examples
1829 ///
1830 /// ```
1831 /// #![feature(raw_slice_split)]
1832 /// #![feature(slice_ptr_get)]
1833 ///
1834 /// let mut v = [1, 0, 3, 0, 5, 6];
1835 /// let ptr = &mut v as *mut [_];
1836 /// unsafe {
1837 /// let (left, right) = ptr.split_at_mut(2);
1838 /// assert_eq!(&*left, [1, 0]);
1839 /// assert_eq!(&*right, [3, 0, 5, 6]);
1840 /// }
1841 /// ```
1842 #[inline(always)]
1843 #[track_caller]
1844 #[unstable(feature = "raw_slice_split", issue = "95595")]
1845 pub unsafe fn split_at_mut(self, mid: usize) -> (*mut [T], *mut [T]) {
1846 assert!(mid <= self.len());
1847 // SAFETY: The assert above is only a safety-net as long as `self.len()` is correct
1848 // The actual safety requirements of this function are the same as for `split_at_mut_unchecked`
1849 unsafe { self.split_at_mut_unchecked(mid) }
1850 }
1851
1852 /// Divides one mutable raw slice into two at an index, without doing bounds checking.
1853 ///
1854 /// The first will contain all indices from `[0, mid)` (excluding
1855 /// the index `mid` itself) and the second will contain all
1856 /// indices from `[mid, len)` (excluding the index `len` itself).
1857 ///
1858 /// # Safety
1859 ///
1860 /// `mid` must be [in-bounds] of the underlying [allocation].
1861 /// Which means `self` must be dereferenceable and span a single allocation
1862 /// that is at least `mid * size_of::<T>()` bytes long. Not upholding these
1863 /// requirements is *[undefined behavior]* even if the resulting pointers are not used.
1864 ///
1865 /// [in-bounds]: #method.add
1866 /// [out-of-bounds index]: #method.add
1867 /// [allocation]: crate::ptr#allocation
1868 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
1869 ///
1870 /// # Examples
1871 ///
1872 /// ```
1873 /// #![feature(raw_slice_split)]
1874 ///
1875 /// let mut v = [1, 0, 3, 0, 5, 6];
1876 /// // scoped to restrict the lifetime of the borrows
1877 /// unsafe {
1878 /// let ptr = &mut v as *mut [_];
1879 /// let (left, right) = ptr.split_at_mut_unchecked(2);
1880 /// assert_eq!(&*left, [1, 0]);
1881 /// assert_eq!(&*right, [3, 0, 5, 6]);
1882 /// (&mut *left)[1] = 2;
1883 /// (&mut *right)[1] = 4;
1884 /// }
1885 /// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
1886 /// ```
1887 #[inline(always)]
1888 #[unstable(feature = "raw_slice_split", issue = "95595")]
1889 pub unsafe fn split_at_mut_unchecked(self, mid: usize) -> (*mut [T], *mut [T]) {
1890 let len = self.len();
1891 let ptr = self.as_mut_ptr();
1892
1893 // SAFETY: Caller must pass a valid pointer and an index that is in-bounds.
1894 let tail = unsafe { ptr.add(mid) };
1895 (
1896 crate::ptr::slice_from_raw_parts_mut(ptr, mid),
1897 crate::ptr::slice_from_raw_parts_mut(tail, len - mid),
1898 )
1899 }
1900
1901 /// Returns a raw pointer to the slice's buffer.
1902 ///
1903 /// This is equivalent to casting `self` to `*mut T`, but more type-safe.
1904 ///
1905 /// # Examples
1906 ///
1907 /// ```rust
1908 /// #![feature(slice_ptr_get)]
1909 /// use std::ptr;
1910 ///
1911 /// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3);
1912 /// assert_eq!(slice.as_mut_ptr(), ptr::null_mut());
1913 /// ```
1914 #[inline(always)]
1915 #[unstable(feature = "slice_ptr_get", issue = "74265")]
1916 pub const fn as_mut_ptr(self) -> *mut T {
1917 self as *mut T
1918 }
1919
1920 /// Returns a raw pointer to an element or subslice, without doing bounds
1921 /// checking.
1922 ///
1923 /// Calling this method with an [out-of-bounds index] or when `self` is not dereferenceable
1924 /// is *[undefined behavior]* even if the resulting pointer is not used.
1925 ///
1926 /// [out-of-bounds index]: #method.add
1927 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
1928 ///
1929 /// # Examples
1930 ///
1931 /// ```
1932 /// #![feature(slice_ptr_get)]
1933 ///
1934 /// let x = &mut [1, 2, 4] as *mut [i32];
1935 ///
1936 /// unsafe {
1937 /// assert_eq!(x.get_unchecked_mut(1), x.as_mut_ptr().add(1));
1938 /// }
1939 /// ```
1940 #[unstable(feature = "slice_ptr_get", issue = "74265")]
1941 #[rustc_const_unstable(feature = "const_index", issue = "143775")]
1942 #[inline(always)]
1943 pub const unsafe fn get_unchecked_mut<I>(self, index: I) -> *mut I::Output
1944 where
1945 I: ~const SliceIndex<[T]>,
1946 {
1947 // SAFETY: the caller ensures that `self` is dereferenceable and `index` in-bounds.
1948 unsafe { index.get_unchecked_mut(self) }
1949 }
1950
1951 #[doc = include_str!("docs/as_uninit_slice.md")]
1952 ///
1953 /// # See Also
1954 /// For the mutable counterpart see [`as_uninit_slice_mut`](pointer::as_uninit_slice_mut).
1955 #[inline]
1956 #[unstable(feature = "ptr_as_uninit", issue = "75402")]
1957 pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
1958 if self.is_null() {
1959 None
1960 } else {
1961 // SAFETY: the caller must uphold the safety contract for `as_uninit_slice`.
1962 Some(unsafe { slice::from_raw_parts(self as *const MaybeUninit<T>, self.len()) })
1963 }
1964 }
1965
1966 /// Returns `None` if the pointer is null, or else returns a unique slice to
1967 /// the value wrapped in `Some`. In contrast to [`as_mut`], this does not require
1968 /// that the value has to be initialized.
1969 ///
1970 /// For the shared counterpart see [`as_uninit_slice`].
1971 ///
1972 /// [`as_mut`]: #method.as_mut
1973 /// [`as_uninit_slice`]: #method.as_uninit_slice-1
1974 ///
1975 /// # Safety
1976 ///
1977 /// When calling this method, you have to ensure that *either* the pointer is null *or*
1978 /// all of the following is true:
1979 ///
1980 /// * The pointer must be [valid] for reads and writes for `ptr.len() * size_of::<T>()`
1981 /// many bytes, and it must be properly aligned. This means in particular:
1982 ///
1983 /// * The entire memory range of this slice must be contained within a single [allocation]!
1984 /// Slices can never span across multiple allocations.
1985 ///
1986 /// * The pointer must be aligned even for zero-length slices. One
1987 /// reason for this is that enum layout optimizations may rely on references
1988 /// (including slices of any length) being aligned and non-null to distinguish
1989 /// them from other data. You can obtain a pointer that is usable as `data`
1990 /// for zero-length slices using [`NonNull::dangling()`].
1991 ///
1992 /// * The total size `ptr.len() * size_of::<T>()` of the slice must be no larger than `isize::MAX`.
1993 /// See the safety documentation of [`pointer::offset`].
1994 ///
1995 /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
1996 /// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
1997 /// In particular, while this reference exists, the memory the pointer points to must
1998 /// not get accessed (read or written) through any other pointer.
1999 ///
2000 /// This applies even if the result of this method is unused!
2001 ///
2002 /// See also [`slice::from_raw_parts_mut`][].
2003 ///
2004 /// [valid]: crate::ptr#safety
2005 /// [allocation]: crate::ptr#allocation
2006 ///
2007 /// # Panics during const evaluation
2008 ///
2009 /// This method will panic during const evaluation if the pointer cannot be
2010 /// determined to be null or not. See [`is_null`] for more information.
2011 ///
2012 /// [`is_null`]: #method.is_null-1
2013 #[inline]
2014 #[unstable(feature = "ptr_as_uninit", issue = "75402")]
2015 pub const unsafe fn as_uninit_slice_mut<'a>(self) -> Option<&'a mut [MaybeUninit<T>]> {
2016 if self.is_null() {
2017 None
2018 } else {
2019 // SAFETY: the caller must uphold the safety contract for `as_uninit_slice_mut`.
2020 Some(unsafe { slice::from_raw_parts_mut(self as *mut MaybeUninit<T>, self.len()) })
2021 }
2022 }
2023}
2024
2025#[cfg(not(feature = "ferrocene_certified"))]
2026impl<T, const N: usize> *mut [T; N] {
2027 /// Returns a raw pointer to the array's buffer.
2028 ///
2029 /// This is equivalent to casting `self` to `*mut T`, but more type-safe.
2030 ///
2031 /// # Examples
2032 ///
2033 /// ```rust
2034 /// #![feature(array_ptr_get)]
2035 /// use std::ptr;
2036 ///
2037 /// let arr: *mut [i8; 3] = ptr::null_mut();
2038 /// assert_eq!(arr.as_mut_ptr(), ptr::null_mut());
2039 /// ```
2040 #[inline]
2041 #[unstable(feature = "array_ptr_get", issue = "119834")]
2042 pub const fn as_mut_ptr(self) -> *mut T {
2043 self as *mut T
2044 }
2045
2046 /// Returns a raw pointer to a mutable slice containing the entire array.
2047 ///
2048 /// # Examples
2049 ///
2050 /// ```
2051 /// #![feature(array_ptr_get)]
2052 ///
2053 /// let mut arr = [1, 2, 5];
2054 /// let ptr: *mut [i32; 3] = &mut arr;
2055 /// unsafe {
2056 /// (&mut *ptr.as_mut_slice())[..2].copy_from_slice(&[3, 4]);
2057 /// }
2058 /// assert_eq!(arr, [3, 4, 5]);
2059 /// ```
2060 #[inline]
2061 #[unstable(feature = "array_ptr_get", issue = "119834")]
2062 pub const fn as_mut_slice(self) -> *mut [T] {
2063 self
2064 }
2065}
2066
2067/// Pointer equality is by address, as produced by the [`<*mut T>::addr`](pointer::addr) method.
2068#[stable(feature = "rust1", since = "1.0.0")]
2069#[cfg(not(feature = "ferrocene_certified"))]
2070impl<T: PointeeSized> PartialEq for *mut T {
2071 #[inline(always)]
2072 #[allow(ambiguous_wide_pointer_comparisons)]
2073 fn eq(&self, other: &*mut T) -> bool {
2074 *self == *other
2075 }
2076}
2077
2078/// Pointer equality is an equivalence relation.
2079#[stable(feature = "rust1", since = "1.0.0")]
2080#[cfg(not(feature = "ferrocene_certified"))]
2081impl<T: PointeeSized> Eq for *mut T {}
2082
2083/// Pointer comparison is by address, as produced by the [`<*mut T>::addr`](pointer::addr) method.
2084#[stable(feature = "rust1", since = "1.0.0")]
2085#[cfg(not(feature = "ferrocene_certified"))]
2086impl<T: PointeeSized> Ord for *mut T {
2087 #[inline]
2088 #[allow(ambiguous_wide_pointer_comparisons)]
2089 fn cmp(&self, other: &*mut T) -> Ordering {
2090 if self < other {
2091 Less
2092 } else if self == other {
2093 Equal
2094 } else {
2095 Greater
2096 }
2097 }
2098}
2099
2100/// Pointer comparison is by address, as produced by the [`<*mut T>::addr`](pointer::addr) method.
2101#[stable(feature = "rust1", since = "1.0.0")]
2102#[cfg(not(feature = "ferrocene_certified"))]
2103impl<T: PointeeSized> PartialOrd for *mut T {
2104 #[inline(always)]
2105 #[allow(ambiguous_wide_pointer_comparisons)]
2106 fn partial_cmp(&self, other: &*mut T) -> Option<Ordering> {
2107 Some(self.cmp(other))
2108 }
2109
2110 #[inline(always)]
2111 #[allow(ambiguous_wide_pointer_comparisons)]
2112 fn lt(&self, other: &*mut T) -> bool {
2113 *self < *other
2114 }
2115
2116 #[inline(always)]
2117 #[allow(ambiguous_wide_pointer_comparisons)]
2118 fn le(&self, other: &*mut T) -> bool {
2119 *self <= *other
2120 }
2121
2122 #[inline(always)]
2123 #[allow(ambiguous_wide_pointer_comparisons)]
2124 fn gt(&self, other: &*mut T) -> bool {
2125 *self > *other
2126 }
2127
2128 #[inline(always)]
2129 #[allow(ambiguous_wide_pointer_comparisons)]
2130 fn ge(&self, other: &*mut T) -> bool {
2131 *self >= *other
2132 }
2133}
2134
2135#[stable(feature = "raw_ptr_default", since = "1.88.0")]
2136#[cfg(not(feature = "ferrocene_certified"))]
2137impl<T: ?Sized + Thin> Default for *mut T {
2138 /// Returns the default value of [`null_mut()`][crate::ptr::null_mut].
2139 fn default() -> Self {
2140 crate::ptr::null_mut()
2141 }
2142}