#![cfg(feature = "alloc")] #![allow(missing_docs)] use crate::rust_string::RustString; use alloc::string::String; use alloc::vec::Vec; use core::ffi::c_void; use core::marker::PhantomData; use core::mem::{self, ManuallyDrop, MaybeUninit}; use core::ptr; // ABI compatible with C++ rust::Vec (not necessarily alloc::vec::Vec). #[repr(C)] pub struct RustVec { repr: [MaybeUninit; mem::size_of::>() / mem::size_of::()], marker: PhantomData>, } impl RustVec { pub fn new() -> Self { Self::from(Vec::new()) } pub fn from(v: Vec) -> Self { unsafe { mem::transmute::, RustVec>(v) } } pub fn from_ref(v: &Vec) -> &Self { unsafe { &*(v as *const Vec as *const RustVec) } } pub fn from_mut(v: &mut Vec) -> &mut Self { unsafe { &mut *(v as *mut Vec as *mut RustVec) } } pub fn into_vec(self) -> Vec { unsafe { mem::transmute::, Vec>(self) } } pub fn as_vec(&self) -> &Vec { unsafe { &*(self as *const RustVec as *const Vec) } } pub fn as_mut_vec(&mut self) -> &mut Vec { unsafe { &mut *(self as *mut RustVec as *mut Vec) } } pub fn len(&self) -> usize { self.as_vec().len() } pub fn capacity(&self) -> usize { self.as_vec().capacity() } pub fn as_ptr(&self) -> *const T { self.as_vec().as_ptr() } pub fn reserve_total(&mut self, new_cap: usize) { let vec = self.as_mut_vec(); if new_cap > vec.capacity() { let additional = new_cap - vec.len(); vec.reserve(additional); } } pub unsafe fn set_len(&mut self, len: usize) { unsafe { self.as_mut_vec().set_len(len) } } pub fn truncate(&mut self, len: usize) { self.as_mut_vec().truncate(len); } } impl RustVec { pub fn from_vec_string(v: Vec) -> Self { let mut v = ManuallyDrop::new(v); let ptr = v.as_mut_ptr().cast::(); let len = v.len(); let cap = v.capacity(); Self::from(unsafe { Vec::from_raw_parts(ptr, len, cap) }) } pub fn from_ref_vec_string(v: &Vec) -> &Self { Self::from_ref(unsafe { &*(v as *const Vec as *const Vec) }) } pub fn from_mut_vec_string(v: &mut Vec) -> &mut Self { Self::from_mut(unsafe { &mut *(v as *mut Vec as *mut Vec) }) } pub fn into_vec_string(self) -> Vec { let mut v = ManuallyDrop::new(self.into_vec()); let ptr = v.as_mut_ptr().cast::(); let len = v.len(); let cap = v.capacity(); unsafe { Vec::from_raw_parts(ptr, len, cap) } } pub fn as_vec_string(&self) -> &Vec { unsafe { &*(self as *const RustVec as *const Vec) } } pub fn as_mut_vec_string(&mut self) -> &mut Vec { unsafe { &mut *(self as *mut RustVec as *mut Vec) } } } impl Drop for RustVec { fn drop(&mut self) { unsafe { ptr::drop_in_place(self.as_mut_vec()) } } }