]> git.lizzy.rs Git - rust.git/commitdiff
Make `(*mut T)::write_bytes` `const`
authorwoppopo <woppopo@protonmail.com>
Sun, 12 Dec 2021 05:02:53 +0000 (14:02 +0900)
committerwoppopo <woppopo@protonmail.com>
Sun, 12 Dec 2021 05:02:53 +0000 (14:02 +0900)
library/core/src/ptr/mut_ptr.rs
library/core/tests/ptr.rs

index 5d4e37641ee84fc719f5abcca5ccfbd9da601fdd..c48f4f9f2090d736bc4c4c37992825089bc95a55 100644 (file)
@@ -1069,8 +1069,9 @@ pub unsafe fn drop_in_place(self) {
     ///
     /// [`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,
     {
index 11af8090c3a4f26034ee98a22548ef55fa6f6a32..b9c0d75b702e5ffb8d284a284fa80a19617659ac 100644 (file)
@@ -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];