Function with_capacity_safe::vec_into_raw_parts[][src]

pub fn vec_into_raw_parts<T>(vec: Vec<T>) -> (*mut T, usize, usize)
Expand description

A copy of Vec::into_raw_parts which is currently unstable. The documentation below is also copy-pasted.

Decomposes a Vec<T> into its raw components.

Returns the raw pointer to the underlying data, the length of the vector (in elements), and the allocated capacity of the data (in elements). These are the same arguments in the same order as the arguments to from_raw_parts.

After calling this function, the caller is responsible for the memory previously managed by the Vec. The only way to do this is to convert the raw pointer, length, and capacity back into a Vec with the from_raw_parts function, allowing the destructor to perform the cleanup.

Examples

use with_capacity_safe::vec_into_raw_parts;
let v: Vec<i32> = vec![-1, 0, 1];

let (ptr, len, cap) = vec_into_raw_parts(v);

let rebuilt = unsafe {
    // We can now make changes to the components, such as
    // transmuting the raw pointer to a compatible type.
    let ptr = ptr as *mut u32;

    Vec::from_raw_parts(ptr, len, cap)
};
assert_eq!(rebuilt, [4294967295, 0, 1]);