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