cxx/
rust_vec.rs

1#![cfg(feature = "alloc")]
2#![allow(missing_docs)]
3
4use crate::rust_string::RustString;
5use alloc::string::String;
6use alloc::vec::Vec;
7use core::ffi::c_void;
8use core::marker::PhantomData;
9use core::mem::{self, ManuallyDrop, MaybeUninit};
10use core::ptr;
11
12// ABI compatible with C++ rust::Vec<T> (not necessarily alloc::vec::Vec<T>).
13#[repr(C)]
14pub struct RustVec<T> {
15    repr: [MaybeUninit<usize>; mem::size_of::<Vec<c_void>>() / mem::size_of::<usize>()],
16    marker: PhantomData<Vec<T>>,
17}
18
19impl<T> RustVec<T> {
20    pub fn new() -> Self {
21        Self::from(Vec::new())
22    }
23
24    pub fn from(v: Vec<T>) -> Self {
25        unsafe { mem::transmute::<Vec<T>, RustVec<T>>(v) }
26    }
27
28    pub fn from_ref(v: &Vec<T>) -> &Self {
29        unsafe { &*(v as *const Vec<T> as *const RustVec<T>) }
30    }
31
32    pub fn from_mut(v: &mut Vec<T>) -> &mut Self {
33        unsafe { &mut *(v as *mut Vec<T> as *mut RustVec<T>) }
34    }
35
36    pub fn into_vec(self) -> Vec<T> {
37        unsafe { mem::transmute::<RustVec<T>, Vec<T>>(self) }
38    }
39
40    pub fn as_vec(&self) -> &Vec<T> {
41        unsafe { &*(self as *const RustVec<T> as *const Vec<T>) }
42    }
43
44    pub fn as_mut_vec(&mut self) -> &mut Vec<T> {
45        unsafe { &mut *(self as *mut RustVec<T> as *mut Vec<T>) }
46    }
47
48    pub fn len(&self) -> usize {
49        self.as_vec().len()
50    }
51
52    pub fn capacity(&self) -> usize {
53        self.as_vec().capacity()
54    }
55
56    pub fn as_ptr(&self) -> *const T {
57        self.as_vec().as_ptr()
58    }
59
60    pub fn reserve_total(&mut self, new_cap: usize) {
61        let vec = self.as_mut_vec();
62        if new_cap > vec.capacity() {
63            let additional = new_cap - vec.len();
64            vec.reserve(additional);
65        }
66    }
67
68    pub unsafe fn set_len(&mut self, len: usize) {
69        unsafe { self.as_mut_vec().set_len(len) }
70    }
71
72    pub fn truncate(&mut self, len: usize) {
73        self.as_mut_vec().truncate(len);
74    }
75}
76
77impl RustVec<RustString> {
78    pub fn from_vec_string(v: Vec<String>) -> Self {
79        let mut v = ManuallyDrop::new(v);
80        let ptr = v.as_mut_ptr().cast::<RustString>();
81        let len = v.len();
82        let cap = v.capacity();
83        Self::from(unsafe { Vec::from_raw_parts(ptr, len, cap) })
84    }
85
86    pub fn from_ref_vec_string(v: &Vec<String>) -> &Self {
87        Self::from_ref(unsafe { &*(v as *const Vec<String> as *const Vec<RustString>) })
88    }
89
90    pub fn from_mut_vec_string(v: &mut Vec<String>) -> &mut Self {
91        Self::from_mut(unsafe { &mut *(v as *mut Vec<String> as *mut Vec<RustString>) })
92    }
93
94    pub fn into_vec_string(self) -> Vec<String> {
95        let mut v = ManuallyDrop::new(self.into_vec());
96        let ptr = v.as_mut_ptr().cast::<String>();
97        let len = v.len();
98        let cap = v.capacity();
99        unsafe { Vec::from_raw_parts(ptr, len, cap) }
100    }
101
102    pub fn as_vec_string(&self) -> &Vec<String> {
103        unsafe { &*(self as *const RustVec<RustString> as *const Vec<String>) }
104    }
105
106    pub fn as_mut_vec_string(&mut self) -> &mut Vec<String> {
107        unsafe { &mut *(self as *mut RustVec<RustString> as *mut Vec<String>) }
108    }
109}
110
111impl<T> Drop for RustVec<T> {
112    fn drop(&mut self) {
113        unsafe { ptr::drop_in_place(self.as_mut_vec()) }
114    }
115}