use std::{mem::ManuallyDrop, ptr::NonNull};
pub(crate) struct VecParts<T> {
ptr: NonNull<T>,
len: usize,
cap: usize,
}
unsafe impl<T> Send for VecParts<T> where Vec<T>: Send {}
impl<T> VecParts<T> {
#[inline]
pub fn ptr(&self) -> *mut T {
self.ptr.as_ptr()
}
#[inline]
pub fn len(&self) -> usize {
self.len
}
}
impl<T> From<Vec<T>> for VecParts<T> {
#[inline]
fn from(v: Vec<T>) -> Self {
let mut v = ManuallyDrop::new(v);
let ptr = unsafe { NonNull::new_unchecked(v.as_mut_ptr()) };
Self {
ptr,
len: v.len(),
cap: v.capacity(),
}
}
}
impl<T> From<VecParts<T>> for Vec<T> {
#[inline]
fn from(p: VecParts<T>) -> Self {
let p = ManuallyDrop::new(p);
unsafe { Vec::from_raw_parts(p.ptr.as_ptr(), p.len, p.cap) }
}
}
impl<T> Drop for VecParts<T> {
#[inline]
fn drop(&mut self) {
unsafe {
drop(Vec::from_raw_parts(self.ptr.as_ptr(), self.len, self.cap));
}
}
}