Skip to content

Commit 51eed00

Browse files
authoredAug 12, 2022
Rollup merge of #100030 - WaffleLapkin:nice_pointer_sis, r=scottmcm
cleanup code w/ pointers in std a little Use pointer methods (`byte_add`, `null_mut`, etc) to make code in std a little nicer.
2 parents a8c799a + d52ed82 commit 51eed00

File tree

8 files changed

+33
-26
lines changed

8 files changed

+33
-26
lines changed
 

‎library/core/src/alloc/global.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use crate::ptr;
7474
/// {
7575
/// return null_mut();
7676
/// };
77-
/// (self.arena.get() as *mut u8).add(allocated)
77+
/// self.arena.get().cast::<u8>().add(allocated)
7878
/// }
7979
/// unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
8080
/// }

‎library/core/src/ptr/const_ptr.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -1267,20 +1267,21 @@ impl<T: ?Sized> *const T {
12671267
/// Accessing adjacent `u8` as `u16`
12681268
///
12691269
/// ```
1270-
/// # fn foo(n: usize) {
1271-
/// # use std::mem::align_of;
1270+
/// use std::mem::align_of;
1271+
///
12721272
/// # unsafe {
1273-
/// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
1274-
/// let ptr = x.as_ptr().add(n) as *const u8;
1273+
/// let x = [5_u8, 6, 7, 8, 9];
1274+
/// let ptr = x.as_ptr();
12751275
/// let offset = ptr.align_offset(align_of::<u16>());
1276-
/// if offset < x.len() - n - 1 {
1277-
/// let u16_ptr = ptr.add(offset) as *const u16;
1278-
/// assert_ne!(*u16_ptr, 500);
1276+
///
1277+
/// if offset < x.len() - 1 {
1278+
/// let u16_ptr = ptr.add(offset).cast::<u16>();
1279+
/// assert!(*u16_ptr == u16::from_ne_bytes([5, 6]) || *u16_ptr == u16::from_ne_bytes([6, 7]));
12791280
/// } else {
12801281
/// // while the pointer can be aligned via `offset`, it would point
12811282
/// // outside the allocation
12821283
/// }
1283-
/// # } }
1284+
/// # }
12841285
/// ```
12851286
#[stable(feature = "align_offset", since = "1.36.0")]
12861287
#[rustc_const_unstable(feature = "const_align_offset", issue = "90962")]

‎library/core/src/ptr/mut_ptr.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -1545,20 +1545,23 @@ impl<T: ?Sized> *mut T {
15451545
/// Accessing adjacent `u8` as `u16`
15461546
///
15471547
/// ```
1548-
/// # fn foo(n: usize) {
1549-
/// # use std::mem::align_of;
1548+
/// use std::mem::align_of;
1549+
///
15501550
/// # unsafe {
1551-
/// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
1552-
/// let ptr = x.as_ptr().add(n) as *const u8;
1551+
/// let mut x = [5_u8, 6, 7, 8, 9];
1552+
/// let ptr = x.as_mut_ptr();
15531553
/// let offset = ptr.align_offset(align_of::<u16>());
1554-
/// if offset < x.len() - n - 1 {
1555-
/// let u16_ptr = ptr.add(offset) as *const u16;
1556-
/// assert_ne!(*u16_ptr, 500);
1554+
///
1555+
/// if offset < x.len() - 1 {
1556+
/// let u16_ptr = ptr.add(offset).cast::<u16>();
1557+
/// *u16_ptr = 0;
1558+
///
1559+
/// assert!(x == [0, 0, 7, 8, 9] || x == [5, 0, 0, 8, 9]);
15571560
/// } else {
15581561
/// // while the pointer can be aligned via `offset`, it would point
15591562
/// // outside the allocation
15601563
/// }
1561-
/// # } }
1564+
/// # }
15621565
/// ```
15631566
#[stable(feature = "align_offset", since = "1.36.0")]
15641567
#[rustc_const_unstable(feature = "const_align_offset", issue = "90962")]

‎library/core/src/slice/iter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<'a, T> Iter<'a, T> {
9292
assume(!ptr.is_null());
9393

9494
let end = if mem::size_of::<T>() == 0 {
95-
(ptr as *const u8).wrapping_add(slice.len()) as *const T
95+
ptr.wrapping_byte_add(slice.len())
9696
} else {
9797
ptr.add(slice.len())
9898
};
@@ -228,7 +228,7 @@ impl<'a, T> IterMut<'a, T> {
228228
assume(!ptr.is_null());
229229

230230
let end = if mem::size_of::<T>() == 0 {
231-
(ptr as *mut u8).wrapping_add(slice.len()) as *mut T
231+
ptr.wrapping_byte_add(slice.len())
232232
} else {
233233
ptr.add(slice.len())
234234
};

‎library/core/tests/const_ptr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const DATA: [u16; 2] = [u16::from_ne_bytes([0x01, 0x23]), u16::from_ne_bytes([0x
33

44
const fn unaligned_ptr() -> *const u16 {
55
// Since DATA.as_ptr() is aligned to two bytes, adding 1 byte to that produces an unaligned *const u16
6-
unsafe { (DATA.as_ptr() as *const u8).add(1) as *const u16 }
6+
unsafe { DATA.as_ptr().byte_add(1) }
77
}
88

99
#[test]
@@ -67,7 +67,7 @@ fn write() {
6767
const fn write_unaligned() -> [u16; 2] {
6868
let mut two_aligned = [0u16; 2];
6969
unsafe {
70-
let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16;
70+
let unaligned_ptr = two_aligned.as_mut_ptr().byte_add(1);
7171
ptr::write_unaligned(unaligned_ptr, u16::from_ne_bytes([0x23, 0x45]));
7272
}
7373
two_aligned
@@ -91,7 +91,7 @@ fn mut_ptr_write() {
9191
const fn write_unaligned() -> [u16; 2] {
9292
let mut two_aligned = [0u16; 2];
9393
unsafe {
94-
let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16;
94+
let unaligned_ptr = two_aligned.as_mut_ptr().byte_add(1);
9595
unaligned_ptr.write_unaligned(u16::from_ne_bytes([0x23, 0x45]));
9696
}
9797
two_aligned

‎library/core/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(const_maybe_uninit_assume_init_read)]
1515
#![feature(const_nonnull_new)]
1616
#![feature(const_num_from_num)]
17+
#![feature(const_pointer_byte_offsets)]
1718
#![feature(const_ptr_as_ref)]
1819
#![feature(const_ptr_read)]
1920
#![feature(const_ptr_write)]
@@ -74,6 +75,7 @@
7475
#![feature(never_type)]
7576
#![feature(unwrap_infallible)]
7677
#![feature(result_into_ok_or_err)]
78+
#![feature(pointer_byte_offsets)]
7779
#![feature(portable_simd)]
7880
#![feature(ptr_metadata)]
7981
#![feature(once_cell)]

‎library/std/src/sys/sgx/abi/usercalls/alloc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub unsafe trait UserSafe {
115115
/// * the pointer is null.
116116
/// * the pointed-to range is not in user memory.
117117
unsafe fn check_ptr(ptr: *const Self) {
118-
let is_aligned = |p| -> bool { 0 == (p as usize) & (Self::align_of() - 1) };
118+
let is_aligned = |p: *const u8| -> bool { 0 == p.addr() & (Self::align_of() - 1) };
119119

120120
assert!(is_aligned(ptr as *const u8));
121121
assert!(is_user_range(ptr as _, mem::size_of_val(unsafe { &*ptr })));
+4-3
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
use crate::alloc::{GlobalAlloc, Layout, System};
2+
use crate::ptr::null_mut;
23

34
#[stable(feature = "alloc_system_type", since = "1.28.0")]
45
unsafe impl GlobalAlloc for System {
56
#[inline]
67
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
7-
0 as *mut u8
8+
null_mut()
89
}
910

1011
#[inline]
1112
unsafe fn alloc_zeroed(&self, _layout: Layout) -> *mut u8 {
12-
0 as *mut u8
13+
null_mut()
1314
}
1415

1516
#[inline]
1617
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
1718

1819
#[inline]
1920
unsafe fn realloc(&self, _ptr: *mut u8, _layout: Layout, _new_size: usize) -> *mut u8 {
20-
0 as *mut u8
21+
null_mut()
2122
}
2223
}

0 commit comments

Comments
 (0)