@@ -358,6 +358,44 @@ impl<T> Vec<T> {
358358 }
359359 }
360360
361+ /// Decomposes a `Vec<T>` into its raw components.
362+ ///
363+ /// Returns the raw pointer to the underlying data, the length of
364+ /// the vector (in elements), and the allocated capacity of the
365+ /// data (in elements). These are the same arguments in the same
366+ /// order as the arguments to [`from_raw_parts`].
367+ ///
368+ /// After calling this function, the caller is responsible for the
369+ /// memory previously managed by the `Vec`. The only way to do
370+ /// this is to convert the raw pointer, length, and capacity back
371+ /// into a `Vec` with the [`from_raw_parts`] function, allowing
372+ /// the destructor to perform the cleanup.
373+ ///
374+ /// [`from_raw_parts`]: #method.from_raw_parts
375+ ///
376+ /// # Examples
377+ ///
378+ /// ```
379+ /// #![feature(vec_into_raw_parts)]
380+ /// let v: Vec<i32> = vec![-1, 0, 1];
381+ ///
382+ /// let (ptr, len, cap) = v.into_raw_parts();
383+ ///
384+ /// let rebuilt = unsafe {
385+ /// // We can now make changes to the components, such as
386+ /// // transmuting the raw pointer to a compatible type.
387+ /// let ptr = ptr as *mut u32;
388+ ///
389+ /// Vec::from_raw_parts(ptr, len, cap)
390+ /// };
391+ /// assert_eq!(rebuilt, [4294967295, 0, 1]);
392+ /// ```
393+ #[ unstable( feature = "vec_into_raw_parts" , reason = "new API" , issue = "65816" ) ]
394+ pub fn into_raw_parts ( self ) -> ( * mut T , usize , usize ) {
395+ let mut me = mem:: ManuallyDrop :: new ( self ) ;
396+ ( me. as_mut_ptr ( ) , me. len ( ) , me. capacity ( ) )
397+ }
398+
361399 /// Creates a `Vec<T>` directly from the raw components of another vector.
362400 ///
363401 /// # Safety
@@ -391,6 +429,7 @@ impl<T> Vec<T> {
391429 ///
392430 /// let v = vec![1, 2, 3];
393431 ///
432+ // FIXME Update this when vec_into_raw_parts is stabilized
394433 /// // Prevent running `v`'s destructor so we are in complete control
395434 /// // of the allocation.
396435 /// let mut v = mem::ManuallyDrop::new(v);
0 commit comments