Extract Vec parts without unstable API
This commit is contained in:
+8
-2
@@ -1,3 +1,4 @@
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::ptr::NonNull;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -8,8 +9,13 @@ pub(super) struct VecParts {
|
||||
}
|
||||
|
||||
impl VecParts {
|
||||
pub(super) fn from_vec<T>(vec: Vec<T>) -> Self {
|
||||
let (raw_ptr, len, cap) = vec.into_raw_parts();
|
||||
pub(super) fn from_vec<T>(values: Vec<T>) -> Self {
|
||||
// Vec::into_raw_parts is newer than the crate's Rust 1.85 MSRV, so
|
||||
// extract the same fields while ManuallyDrop suppresses Vec::drop.
|
||||
let mut vec_owner = ManuallyDrop::new(values);
|
||||
let raw_ptr = vec_owner.as_mut_ptr();
|
||||
let len = vec_owner.len();
|
||||
let cap = vec_owner.capacity();
|
||||
|
||||
// SAFETY: Vec guarantees its underlying pointer is non-null.
|
||||
let ptr = unsafe { NonNull::new_unchecked(raw_ptr.cast::<u8>()) };
|
||||
|
||||
Reference in New Issue
Block a user