Extract Vec parts without unstable API

This commit is contained in:
2026-07-31 04:06:57 +02:00
parent e883282c80
commit e7f53d41de
+8 -2
View File
@@ -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>()) };