• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::rust_string::RustString;
2 use alloc::string::String;
3 use alloc::vec::Vec;
4 use core::mem::ManuallyDrop;
5 
6 #[repr(C)]
7 pub struct RustVec<T> {
8     pub(crate) repr: Vec<T>,
9 }
10 
11 impl<T> RustVec<T> {
new() -> Self12     pub fn new() -> Self {
13         RustVec { repr: Vec::new() }
14     }
15 
from(v: Vec<T>) -> Self16     pub fn from(v: Vec<T>) -> Self {
17         RustVec { repr: v }
18     }
19 
from_ref(v: &Vec<T>) -> &Self20     pub fn from_ref(v: &Vec<T>) -> &Self {
21         unsafe { &*(v as *const Vec<T> as *const RustVec<T>) }
22     }
23 
from_mut(v: &mut Vec<T>) -> &mut Self24     pub fn from_mut(v: &mut Vec<T>) -> &mut Self {
25         unsafe { &mut *(v as *mut Vec<T> as *mut RustVec<T>) }
26     }
27 
into_vec(self) -> Vec<T>28     pub fn into_vec(self) -> Vec<T> {
29         self.repr
30     }
31 
as_vec(&self) -> &Vec<T>32     pub fn as_vec(&self) -> &Vec<T> {
33         &self.repr
34     }
35 
as_mut_vec(&mut self) -> &mut Vec<T>36     pub fn as_mut_vec(&mut self) -> &mut Vec<T> {
37         &mut self.repr
38     }
39 
len(&self) -> usize40     pub fn len(&self) -> usize {
41         self.repr.len()
42     }
43 
capacity(&self) -> usize44     pub fn capacity(&self) -> usize {
45         self.repr.capacity()
46     }
47 
as_ptr(&self) -> *const T48     pub fn as_ptr(&self) -> *const T {
49         self.repr.as_ptr()
50     }
51 
reserve_total(&mut self, cap: usize)52     pub fn reserve_total(&mut self, cap: usize) {
53         let len = self.repr.len();
54         if cap > len {
55             self.repr.reserve(cap - len);
56         }
57     }
58 
set_len(&mut self, len: usize)59     pub unsafe fn set_len(&mut self, len: usize) {
60         self.repr.set_len(len);
61     }
62 }
63 
64 impl RustVec<RustString> {
from_vec_string(v: Vec<String>) -> Self65     pub fn from_vec_string(v: Vec<String>) -> Self {
66         let mut v = ManuallyDrop::new(v);
67         let ptr = v.as_mut_ptr().cast::<RustString>();
68         let len = v.len();
69         let cap = v.capacity();
70         Self::from(unsafe { Vec::from_raw_parts(ptr, len, cap) })
71     }
72 
from_ref_vec_string(v: &Vec<String>) -> &Self73     pub fn from_ref_vec_string(v: &Vec<String>) -> &Self {
74         Self::from_ref(unsafe { &*(v as *const Vec<String> as *const Vec<RustString>) })
75     }
76 
from_mut_vec_string(v: &mut Vec<String>) -> &mut Self77     pub fn from_mut_vec_string(v: &mut Vec<String>) -> &mut Self {
78         Self::from_mut(unsafe { &mut *(v as *mut Vec<String> as *mut Vec<RustString>) })
79     }
80 
into_vec_string(self) -> Vec<String>81     pub fn into_vec_string(self) -> Vec<String> {
82         let mut v = ManuallyDrop::new(self.repr);
83         let ptr = v.as_mut_ptr().cast::<String>();
84         let len = v.len();
85         let cap = v.capacity();
86         unsafe { Vec::from_raw_parts(ptr, len, cap) }
87     }
88 
as_vec_string(&self) -> &Vec<String>89     pub fn as_vec_string(&self) -> &Vec<String> {
90         unsafe { &*(&self.repr as *const Vec<RustString> as *const Vec<String>) }
91     }
92 
as_mut_vec_string(&mut self) -> &mut Vec<String>93     pub fn as_mut_vec_string(&mut self) -> &mut Vec<String> {
94         unsafe { &mut *(&mut self.repr as *mut Vec<RustString> as *mut Vec<String>) }
95     }
96 }
97