diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2021-12-13 00:20:10 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-13 00:20:10 +0100 |
| commit | 9e662d0c0322e2f82fbd9b9e5216a9284f2b124f (patch) | |
| tree | 716b9e9f038c4125a3ed040706d5fe78b8dec11b | |
| parent | 3b79d4f0b7d0c9ef9a018809a26be03818adff06 (diff) | |
| parent | 7f5dc0f6090eaad75fadf75b1cf2449294a15f52 (diff) | |
Rollup merge of #91824 - woppopo:const_ptr_write_bytes, r=oli-obk
Make `(*mut T)::write_bytes` `const` Tracking issue: #86302
| -rw-r--r-- | library/core/src/ptr/mut_ptr.rs | 3 | ||||
| -rw-r--r-- | library/core/tests/ptr.rs | 15 |
2 files changed, 17 insertions, 1 deletions
diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index 15ef64bc73f..f3655edb3d0 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -1069,8 +1069,9 @@ impl<T: ?Sized> *mut T { /// /// [`ptr::write_bytes`]: crate::ptr::write_bytes() #[stable(feature = "pointer_methods", since = "1.26.0")] + #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")] #[inline(always)] - pub unsafe fn write_bytes(self, val: u8, count: usize) + pub const unsafe fn write_bytes(self, val: u8, count: usize) where T: Sized, { diff --git a/library/core/tests/ptr.rs b/library/core/tests/ptr.rs index 11af8090c3a..b9c0d75b702 100644 --- a/library/core/tests/ptr.rs +++ b/library/core/tests/ptr.rs @@ -251,6 +251,21 @@ fn test_set_memory() { } #[test] +#[cfg(not(bootstrap))] +fn test_set_memory_const() { + const XS: [u8; 20] = { + let mut xs = [0u8; 20]; + let ptr = xs.as_mut_ptr(); + unsafe { + ptr.write_bytes(5u8, xs.len()); + } + xs + }; + + assert!(XS == [5u8; 20]); +} + +#[test] fn test_unsized_nonnull() { let xs: &[i32] = &[1, 2, 3]; let ptr = unsafe { NonNull::new_unchecked(xs as *const [i32] as *mut [i32]) }; |
