Skip to content

Commit 7f5dc0f

Browse files
committed
Make (*mut T)::write_bytes const
1 parent e70e4d4 commit 7f5dc0f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

library/core/src/ptr/mut_ptr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1069,8 +1069,9 @@ impl<T: ?Sized> *mut T {
10691069
///
10701070
/// [`ptr::write_bytes`]: crate::ptr::write_bytes()
10711071
#[stable(feature = "pointer_methods", since = "1.26.0")]
1072+
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
10721073
#[inline(always)]
1073-
pub unsafe fn write_bytes(self, val: u8, count: usize)
1074+
pub const unsafe fn write_bytes(self, val: u8, count: usize)
10741075
where
10751076
T: Sized,
10761077
{

library/core/tests/ptr.rs

+15
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,21 @@ fn test_set_memory() {
250250
assert!(xs == [5u8; 20]);
251251
}
252252

253+
#[test]
254+
#[cfg(not(bootstrap))]
255+
fn test_set_memory_const() {
256+
const XS: [u8; 20] = {
257+
let mut xs = [0u8; 20];
258+
let ptr = xs.as_mut_ptr();
259+
unsafe {
260+
ptr.write_bytes(5u8, xs.len());
261+
}
262+
xs
263+
};
264+
265+
assert!(XS == [5u8; 20]);
266+
}
267+
253268
#[test]
254269
fn test_unsized_nonnull() {
255270
let xs: &[i32] = &[1, 2, 3];

0 commit comments

Comments
 (0)