Skip to content

Commit 9011912

Browse files
authored
Rollup merge of #100821 - WaffleLapkin:ptr_add_docs, r=scottmcm
Make some docs nicer wrt pointer offsets This PR replaces `pointer::offset` with `pointer::add` and similarly `.cast().wrapping_add().cast()` with `.wrapping_byte_add()` **in docs**. r? ````@scottmcm```` _split off from #100746_
2 parents cf29d18 + b2625e2 commit 9011912

File tree

4 files changed

+15
-17
lines changed

4 files changed

+15
-17
lines changed

library/alloc/src/ffi/c_str.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,9 @@ impl CString {
436436
///
437437
/// unsafe {
438438
/// assert_eq!(b'f', *ptr as u8);
439-
/// assert_eq!(b'o', *ptr.offset(1) as u8);
440-
/// assert_eq!(b'o', *ptr.offset(2) as u8);
441-
/// assert_eq!(b'\0', *ptr.offset(3) as u8);
439+
/// assert_eq!(b'o', *ptr.add(1) as u8);
440+
/// assert_eq!(b'o', *ptr.add(2) as u8);
441+
/// assert_eq!(b'\0', *ptr.add(3) as u8);
442442
///
443443
/// // retake pointer to free memory
444444
/// let _ = CString::from_raw(ptr);

library/alloc/src/vec/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ impl<T> Vec<T> {
542542
///
543543
/// unsafe {
544544
/// // Overwrite memory with 4, 5, 6
545-
/// for i in 0..len as isize {
546-
/// ptr::write(p.offset(i), 4 + i);
545+
/// for i in 0..len {
546+
/// ptr::write(p.add(i), 4 + i);
547547
/// }
548548
///
549549
/// // Put everything back together into a Vec
@@ -702,8 +702,8 @@ impl<T, A: Allocator> Vec<T, A> {
702702
///
703703
/// unsafe {
704704
/// // Overwrite memory with 4, 5, 6
705-
/// for i in 0..len as isize {
706-
/// ptr::write(p.offset(i), 4 + i);
705+
/// for i in 0..len {
706+
/// ptr::write(p.add(i), 4 + i);
707707
/// }
708708
///
709709
/// // Put everything back together into a Vec

library/core/src/intrinsics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2209,9 +2209,9 @@ pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -
22092209
/// dst.reserve(src_len);
22102210
///
22112211
/// unsafe {
2212-
/// // The call to offset is always safe because `Vec` will never
2212+
/// // The call to add is always safe because `Vec` will never
22132213
/// // allocate more than `isize::MAX` bytes.
2214-
/// let dst_ptr = dst.as_mut_ptr().offset(dst_len as isize);
2214+
/// let dst_ptr = dst.as_mut_ptr().add(dst_len);
22152215
/// let src_ptr = src.as_ptr();
22162216
///
22172217
/// // Truncate `src` without dropping its contents. We do this first,

library/core/src/sync/atomic.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1554,8 +1554,8 @@ impl<T> AtomicPtr<T> {
15541554
/// Offsets the pointer's address by adding `val` *bytes*, returning the
15551555
/// previous pointer.
15561556
///
1557-
/// This is equivalent to using [`wrapping_add`] and [`cast`] to atomically
1558-
/// perform `ptr = ptr.cast::<u8>().wrapping_add(val).cast::<T>()`.
1557+
/// This is equivalent to using [`wrapping_byte_add`] to atomically
1558+
/// perform `ptr = ptr.wrapping_byte_add(val)`.
15591559
///
15601560
/// `fetch_byte_add` takes an [`Ordering`] argument which describes the
15611561
/// memory ordering of this operation. All ordering modes are possible. Note
@@ -1565,8 +1565,7 @@ impl<T> AtomicPtr<T> {
15651565
/// **Note**: This method is only available on platforms that support atomic
15661566
/// operations on [`AtomicPtr`].
15671567
///
1568-
/// [`wrapping_add`]: pointer::wrapping_add
1569-
/// [`cast`]: pointer::cast
1568+
/// [`wrapping_byte_add`]: pointer::wrapping_byte_add
15701569
///
15711570
/// # Examples
15721571
///
@@ -1591,8 +1590,8 @@ impl<T> AtomicPtr<T> {
15911590
/// Offsets the pointer's address by subtracting `val` *bytes*, returning the
15921591
/// previous pointer.
15931592
///
1594-
/// This is equivalent to using [`wrapping_sub`] and [`cast`] to atomically
1595-
/// perform `ptr = ptr.cast::<u8>().wrapping_sub(val).cast::<T>()`.
1593+
/// This is equivalent to using [`wrapping_byte_sub`] to atomically
1594+
/// perform `ptr = ptr.wrapping_byte_sub(val)`.
15961595
///
15971596
/// `fetch_byte_sub` takes an [`Ordering`] argument which describes the
15981597
/// memory ordering of this operation. All ordering modes are possible. Note
@@ -1602,8 +1601,7 @@ impl<T> AtomicPtr<T> {
16021601
/// **Note**: This method is only available on platforms that support atomic
16031602
/// operations on [`AtomicPtr`].
16041603
///
1605-
/// [`wrapping_sub`]: pointer::wrapping_sub
1606-
/// [`cast`]: pointer::cast
1604+
/// [`wrapping_byte_sub`]: pointer::wrapping_byte_sub
16071605
///
16081606
/// # Examples
16091607
///

0 commit comments

Comments
 (0)