From ac9bb13267fd120280ba18460e014491b5a4a352 Mon Sep 17 00:00:00 2001 From: Max Siling Date: Tue, 11 Nov 2025 19:34:29 +0300 Subject: [PATCH] Stabilize vec_into_raw_parts --- library/alloc/src/string.rs | 25 +++-------- library/alloc/src/vec/mod.rs | 66 ++++++------------------------ library/core/src/intrinsics/mod.rs | 8 +--- library/std/src/lib.rs | 1 - 4 files changed, 19 insertions(+), 81 deletions(-) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 31743b0e35b24..4a2689e01ff17 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -265,18 +265,11 @@ use crate::vec::{self, Vec}; /// You can look at these with the [`as_ptr`], [`len`], and [`capacity`] /// methods: /// -// FIXME Update this when vec_into_raw_parts is stabilized /// ``` -/// use std::mem; -/// /// let story = String::from("Once upon a time..."); /// -/// // Prevent automatically dropping the String's data -/// let mut story = mem::ManuallyDrop::new(story); -/// -/// let ptr = story.as_mut_ptr(); -/// let len = story.len(); -/// let capacity = story.capacity(); +/// // Deconstruct the String into parts. +/// let (ptr, len, capacity) = story.into_raw_parts(); /// /// // story has nineteen bytes /// assert_eq!(19, len); @@ -932,7 +925,6 @@ impl String { /// # Examples /// /// ``` - /// #![feature(vec_into_raw_parts)] /// let s = String::from("hello"); /// /// let (ptr, len, cap) = s.into_raw_parts(); @@ -941,7 +933,7 @@ impl String { /// assert_eq!(rebuilt, "hello"); /// ``` #[must_use = "losing the pointer will leak memory"] - #[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")] + #[stable(feature = "vec_into_raw_parts", since = "CURRENT_RUSTC_VERSION")] pub fn into_raw_parts(self) -> (*mut u8, usize, usize) { self.vec.into_raw_parts() } @@ -970,19 +962,12 @@ impl String { /// /// # Examples /// - // FIXME Update this when vec_into_raw_parts is stabilized /// ``` - /// use std::mem; - /// /// unsafe { /// let s = String::from("hello"); /// - /// // Prevent automatically dropping the String's data - /// let mut s = mem::ManuallyDrop::new(s); - /// - /// let ptr = s.as_mut_ptr(); - /// let len = s.len(); - /// let capacity = s.capacity(); + /// // Deconstruct the String into parts. + /// let (ptr, len, capacity) = s.into_raw_parts(); /// /// let s = String::from_raw_parts(ptr, len, capacity); /// diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 43a68ff203738..f8e14d7e54c0e 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -592,21 +592,13 @@ impl Vec { /// /// # Examples /// - // FIXME Update this when vec_into_raw_parts is stabilized /// ``` /// use std::ptr; - /// use std::mem; /// /// let v = vec![1, 2, 3]; /// - /// // Prevent running `v`'s destructor so we are in complete control - /// // of the allocation. - /// let mut v = mem::ManuallyDrop::new(v); - /// - /// // Pull out the various important pieces of information about `v` - /// let p = v.as_mut_ptr(); - /// let len = v.len(); - /// let cap = v.capacity(); + /// // Deconstruct the vector into parts. + /// let (p, len, cap) = v.into_raw_parts(); /// /// unsafe { /// // Overwrite memory with 4, 5, 6 @@ -700,23 +692,13 @@ impl Vec { /// /// # Examples /// - // FIXME Update this when vec_into_raw_parts is stabilized /// ``` /// #![feature(box_vec_non_null)] /// - /// use std::ptr::NonNull; - /// use std::mem; - /// /// let v = vec![1, 2, 3]; /// - /// // Prevent running `v`'s destructor so we are in complete control - /// // of the allocation. - /// let mut v = mem::ManuallyDrop::new(v); - /// - /// // Pull out the various important pieces of information about `v` - /// let p = unsafe { NonNull::new_unchecked(v.as_mut_ptr()) }; - /// let len = v.len(); - /// let cap = v.capacity(); + /// // Deconstruct the vector into parts. + /// let (p, len, cap) = v.into_parts(); /// /// unsafe { /// // Overwrite memory with 4, 5, 6 @@ -783,7 +765,6 @@ impl Vec { /// # Examples /// /// ``` - /// #![feature(vec_into_raw_parts)] /// let v: Vec = vec![-1, 0, 1]; /// /// let (ptr, len, cap) = v.into_raw_parts(); @@ -798,7 +779,7 @@ impl Vec { /// assert_eq!(rebuilt, [4294967295, 0, 1]); /// ``` #[must_use = "losing the pointer will leak memory"] - #[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")] + #[stable(feature = "vec_into_raw_parts", since = "CURRENT_RUSTC_VERSION")] pub fn into_raw_parts(self) -> (*mut T, usize, usize) { let mut me = ManuallyDrop::new(self); (me.as_mut_ptr(), me.len(), me.capacity()) @@ -823,7 +804,7 @@ impl Vec { /// # Examples /// /// ``` - /// #![feature(vec_into_raw_parts, box_vec_non_null)] + /// #![feature(box_vec_non_null)] /// /// let v: Vec = vec![-1, 0, 1]; /// @@ -840,7 +821,6 @@ impl Vec { /// ``` #[must_use = "losing the pointer will leak memory"] #[unstable(feature = "box_vec_non_null", reason = "new API", issue = "130364")] - // #[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")] pub fn into_parts(self) -> (NonNull, usize, usize) { let (ptr, len, capacity) = self.into_raw_parts(); // SAFETY: A `Vec` always has a non-null pointer. @@ -996,29 +976,20 @@ impl Vec { /// /// # Examples /// - // FIXME Update this when vec_into_raw_parts is stabilized /// ``` /// #![feature(allocator_api)] /// /// use std::alloc::System; /// /// use std::ptr; - /// use std::mem; /// /// let mut v = Vec::with_capacity_in(3, System); /// v.push(1); /// v.push(2); /// v.push(3); /// - /// // Prevent running `v`'s destructor so we are in complete control - /// // of the allocation. - /// let mut v = mem::ManuallyDrop::new(v); - /// - /// // Pull out the various important pieces of information about `v` - /// let p = v.as_mut_ptr(); - /// let len = v.len(); - /// let cap = v.capacity(); - /// let alloc = v.allocator(); + /// // Deconstruct the vector into parts. + /// let (p, len, cap, alloc) = v.into_raw_parts_with_alloc(); /// /// unsafe { /// // Overwrite memory with 4, 5, 6 @@ -1116,29 +1087,18 @@ impl Vec { /// /// # Examples /// - // FIXME Update this when vec_into_raw_parts is stabilized /// ``` /// #![feature(allocator_api, box_vec_non_null)] /// /// use std::alloc::System; /// - /// use std::ptr::NonNull; - /// use std::mem; - /// /// let mut v = Vec::with_capacity_in(3, System); /// v.push(1); /// v.push(2); /// v.push(3); /// - /// // Prevent running `v`'s destructor so we are in complete control - /// // of the allocation. - /// let mut v = mem::ManuallyDrop::new(v); - /// - /// // Pull out the various important pieces of information about `v` - /// let p = unsafe { NonNull::new_unchecked(v.as_mut_ptr()) }; - /// let len = v.len(); - /// let cap = v.capacity(); - /// let alloc = v.allocator(); + /// // Deconstruct the vector into parts. + /// let (p, len, cap, alloc) = v.into_parts_with_alloc(); /// /// unsafe { /// // Overwrite memory with 4, 5, 6 @@ -1206,7 +1166,7 @@ impl Vec { /// # Examples /// /// ``` - /// #![feature(allocator_api, vec_into_raw_parts)] + /// #![feature(allocator_api)] /// /// use std::alloc::System; /// @@ -1228,7 +1188,6 @@ impl Vec { /// ``` #[must_use = "losing the pointer will leak memory"] #[unstable(feature = "allocator_api", issue = "32838")] - // #[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")] pub fn into_raw_parts_with_alloc(self) -> (*mut T, usize, usize, A) { let mut me = ManuallyDrop::new(self); let len = me.len(); @@ -1256,7 +1215,7 @@ impl Vec { /// # Examples /// /// ``` - /// #![feature(allocator_api, vec_into_raw_parts, box_vec_non_null)] + /// #![feature(allocator_api, box_vec_non_null)] /// /// use std::alloc::System; /// @@ -1279,7 +1238,6 @@ impl Vec { #[must_use = "losing the pointer will leak memory"] #[unstable(feature = "allocator_api", issue = "32838")] // #[unstable(feature = "box_vec_non_null", reason = "new API", issue = "130364")] - // #[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")] pub fn into_parts_with_alloc(self) -> (NonNull, usize, usize, A) { let (ptr, len, capacity, alloc) = self.into_raw_parts_with_alloc(); // SAFETY: A `Vec` always has a non-null pointer. diff --git a/library/core/src/intrinsics/mod.rs b/library/core/src/intrinsics/mod.rs index 6b13b4c4f56cc..82cb489ccf402 100644 --- a/library/core/src/intrinsics/mod.rs +++ b/library/core/src/intrinsics/mod.rs @@ -769,13 +769,9 @@ pub const fn forget(_: T); /// // in terms of converting the original inner type (`&i32`) to the new one (`Option<&i32>`), /// // this has all the same caveats. Besides the information provided above, also consult the /// // [`from_raw_parts`] documentation. +/// let (ptr, len, capacity) = v_clone.into_raw_parts(); /// let v_from_raw = unsafe { -// FIXME Update this when vec_into_raw_parts is stabilized -/// // Ensure the original vector is not dropped. -/// let mut v_clone = std::mem::ManuallyDrop::new(v_clone); -/// Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>, -/// v_clone.len(), -/// v_clone.capacity()) +/// Vec::from_raw_parts(ptr.cast::<*mut Option<&i32>>(), len, capacity) /// }; /// ``` /// diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 7b6cfbfe0f259..9099322708e0f 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -381,7 +381,6 @@ #![feature(try_reserve_kind)] #![feature(try_with_capacity)] #![feature(unique_rc_arc)] -#![feature(vec_into_raw_parts)] #![feature(wtf8_internals)] // tidy-alphabetical-end //