Skip to content

Commit

Permalink
Rollup merge of #91824 - woppopo:const_ptr_write_bytes, r=oli-obk
Browse files Browse the repository at this point in the history
Make `(*mut T)::write_bytes` `const`

Tracking issue: #86302
  • Loading branch information
matthiaskrgr authored Dec 12, 2021
2 parents 3b79d4f + 7f5dc0f commit 9e662d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand Down
15 changes: 15 additions & 0 deletions library/core/tests/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,21 @@ fn test_set_memory() {
assert!(xs == [5u8; 20]);
}

#[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];
Expand Down

0 comments on commit 9e662d0

Please sign in to comment.