• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![cfg(feature = "alloc")]
2 #![allow(missing_docs)]
3 
4 use crate::rust_string::RustString;
5 use alloc::string::String;
6 use alloc::vec::Vec;
7 use core::ffi::c_void;
8 use core::marker::PhantomData;
9 use core::mem::{self, ManuallyDrop, MaybeUninit};
10 use core::ptr;
11 
12 // ABI compatible with C++ rust::Vec<T> (not necessarily alloc::vec::Vec<T>).
13 #[repr(C)]
14 pub struct RustVec<T> {
15     repr: [MaybeUninit<usize>; mem::size_of::<Vec<c_void>>() / mem::size_of::<usize>()],
16     marker: PhantomData<Vec<T>>,
17 }
18 
19 impl<T> RustVec<T> {
new() -> Self20     pub fn new() -> Self {
21         Self::from(Vec::new())
22     }
23 
from(v: Vec<T>) -> Self24     pub fn from(v: Vec<T>) -> Self {
25         unsafe { mem::transmute::<Vec<T>, RustVec<T>>(v) }
26     }
27 
from_ref(v: &Vec<T>) -> &Self28     pub fn from_ref(v: &Vec<T>) -> &Self {
29         unsafe { &*(v as *const Vec<T> as *const RustVec<T>) }
30     }
31 
from_mut(v: &mut Vec<T>) -> &mut Self32     pub fn from_mut(v: &mut Vec<T>) -> &mut Self {
33         unsafe { &mut *(v as *mut Vec<T> as *mut RustVec<T>) }
34     }
35 
into_vec(self) -> Vec<T>36     pub fn into_vec(self) -> Vec<T> {
37         unsafe { mem::transmute::<RustVec<T>, Vec<T>>(self) }
38     }
39 
as_vec(&self) -> &Vec<T>40     pub fn as_vec(&self) -> &Vec<T> {
41         unsafe { &*(self as *const RustVec<T> as *const Vec<T>) }
42     }
43 
as_mut_vec(&mut self) -> &mut Vec<T>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 
len(&self) -> usize48     pub fn len(&self) -> usize {
49         self.as_vec().len()
50     }
51 
capacity(&self) -> usize52     pub fn capacity(&self) -> usize {
53         self.as_vec().capacity()
54     }
55 
as_ptr(&self) -> *const T56     pub fn as_ptr(&self) -> *const T {
57         self.as_vec().as_ptr()
58     }
59 
reserve_total(&mut self, new_cap: usize)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 
set_len(&mut self, len: usize)68     pub unsafe fn set_len(&mut self, len: usize) {
69         unsafe { self.as_mut_vec().set_len(len) }
70     }
71 
truncate(&mut self, len: usize)72     pub fn truncate(&mut self, len: usize) {
73         self.as_mut_vec().truncate(len);
74     }
75 }
76 
77 impl RustVec<RustString> {
from_vec_string(v: Vec<String>) -> Self78     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 
from_ref_vec_string(v: &Vec<String>) -> &Self86     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 
from_mut_vec_string(v: &mut Vec<String>) -> &mut Self90     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 
into_vec_string(self) -> Vec<String>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 
as_vec_string(&self) -> &Vec<String>102     pub fn as_vec_string(&self) -> &Vec<String> {
103         unsafe { &*(self as *const RustVec<RustString> as *const Vec<String>) }
104     }
105 
as_mut_vec_string(&mut self) -> &mut Vec<String>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 
111 impl<T> Drop for RustVec<T> {
drop(&mut self)112     fn drop(&mut self) {
113         unsafe { ptr::drop_in_place(self.as_mut_vec()) }
114     }
115 }
116