diff options
| author | Maybe Waffle <waffle.lapkin@gmail.com> | 2022-08-01 21:50:21 +0400 |
|---|---|---|
| committer | Maybe Waffle <waffle.lapkin@gmail.com> | 2022-08-05 16:47:49 +0400 |
| commit | 127b6c4c18998695edfafc195c682bf3f42e203e (patch) | |
| tree | b392b581fa91941caf441b81f00e2c3cb5547514 /library/core | |
| parent | 1f5d8d49eb6111931091f700d07518cd2b80bc18 (diff) | |
| download | rust-127b6c4c18998695edfafc195c682bf3f42e203e.tar.gz rust-127b6c4c18998695edfafc195c682bf3f42e203e.zip | |
cleanup code w/ pointers in std a little
Diffstat (limited to 'library/core')
| -rw-r--r-- | library/core/src/alloc/global.rs | 2 | ||||
| -rw-r--r-- | library/core/src/ptr/const_ptr.rs | 4 | ||||
| -rw-r--r-- | library/core/src/ptr/mut_ptr.rs | 7 | ||||
| -rw-r--r-- | library/core/src/slice/iter.rs | 4 | ||||
| -rw-r--r-- | library/core/tests/const_ptr.rs | 6 | ||||
| -rw-r--r-- | library/core/tests/lib.rs | 2 |
6 files changed, 14 insertions, 11 deletions
diff --git a/library/core/src/alloc/global.rs b/library/core/src/alloc/global.rs index 887246c6001..6756eecd0e0 100644 --- a/library/core/src/alloc/global.rs +++ b/library/core/src/alloc/global.rs @@ -74,7 +74,7 @@ use crate::ptr; /// { /// return null_mut(); /// }; -/// (self.arena.get() as *mut u8).add(allocated) +/// self.arena.get().cast::<u8>().add(allocated) /// } /// unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} /// } diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index e0655d68d2c..2f2d8329baa 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -1270,8 +1270,8 @@ impl<T: ?Sized> *const T { /// # fn foo(n: usize) { /// # use std::mem::align_of; /// # unsafe { - /// let x = [5u8, 6u8, 7u8, 8u8, 9u8]; - /// let ptr = x.as_ptr().add(n) as *const u8; + /// let x = [5u8, 6, 7, 8, 9]; + /// let ptr = x.as_ptr().add(n); /// let offset = ptr.align_offset(align_of::<u16>()); /// if offset < x.len() - n - 1 { /// let u16_ptr = ptr.add(offset) as *const u16; diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index fc3dd2a9b25..e932c72ad98 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -1548,12 +1548,13 @@ impl<T: ?Sized> *mut T { /// # fn foo(n: usize) { /// # use std::mem::align_of; /// # unsafe { - /// let x = [5u8, 6u8, 7u8, 8u8, 9u8]; - /// let ptr = x.as_ptr().add(n) as *const u8; + /// let mut x = [5u8, 6, 7, 8, 9]; + /// let ptr = x.as_mut_ptr().add(n); /// let offset = ptr.align_offset(align_of::<u16>()); /// if offset < x.len() - n - 1 { - /// let u16_ptr = ptr.add(offset) as *const u16; + /// let u16_ptr = ptr.add(offset) as *mut u16; /// assert_ne!(*u16_ptr, 500); + /// *u16_ptr = 0; /// } else { /// // while the pointer can be aligned via `offset`, it would point /// // outside the allocation diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index 76b4a534e5d..c310ffe091d 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -92,7 +92,7 @@ impl<'a, T> Iter<'a, T> { assume(!ptr.is_null()); let end = if mem::size_of::<T>() == 0 { - (ptr as *const u8).wrapping_add(slice.len()) as *const T + ptr.wrapping_byte_add(slice.len()) } else { ptr.add(slice.len()) }; @@ -228,7 +228,7 @@ impl<'a, T> IterMut<'a, T> { assume(!ptr.is_null()); let end = if mem::size_of::<T>() == 0 { - (ptr as *mut u8).wrapping_add(slice.len()) as *mut T + ptr.wrapping_byte_add(slice.len()) } else { ptr.add(slice.len()) }; diff --git a/library/core/tests/const_ptr.rs b/library/core/tests/const_ptr.rs index 152fed803ec..d874f08317f 100644 --- a/library/core/tests/const_ptr.rs +++ b/library/core/tests/const_ptr.rs @@ -3,7 +3,7 @@ const DATA: [u16; 2] = [u16::from_ne_bytes([0x01, 0x23]), u16::from_ne_bytes([0x const fn unaligned_ptr() -> *const u16 { // Since DATA.as_ptr() is aligned to two bytes, adding 1 byte to that produces an unaligned *const u16 - unsafe { (DATA.as_ptr() as *const u8).add(1) as *const u16 } + unsafe { DATA.as_ptr().byte_add(1) } } #[test] @@ -67,7 +67,7 @@ fn write() { const fn write_unaligned() -> [u16; 2] { let mut two_aligned = [0u16; 2]; unsafe { - let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16; + let unaligned_ptr = two_aligned.as_mut_ptr().byte_add(1); ptr::write_unaligned(unaligned_ptr, u16::from_ne_bytes([0x23, 0x45])); } two_aligned @@ -91,7 +91,7 @@ fn mut_ptr_write() { const fn write_unaligned() -> [u16; 2] { let mut two_aligned = [0u16; 2]; unsafe { - let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16; + let unaligned_ptr = two_aligned.as_mut_ptr().byte_add(1); unaligned_ptr.write_unaligned(u16::from_ne_bytes([0x23, 0x45])); } two_aligned diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index db94368f6e0..df9b1073a09 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -14,6 +14,7 @@ #![feature(const_maybe_uninit_assume_init_read)] #![feature(const_nonnull_new)] #![feature(const_num_from_num)] +#![feature(const_pointer_byte_offsets)] #![feature(const_ptr_as_ref)] #![feature(const_ptr_read)] #![feature(const_ptr_write)] @@ -74,6 +75,7 @@ #![feature(never_type)] #![feature(unwrap_infallible)] #![feature(result_into_ok_or_err)] +#![feature(pointer_byte_offsets)] #![feature(portable_simd)] #![feature(ptr_metadata)] #![feature(once_cell)] |
