blob: 126fdbf9a77b22e3af341778775a69af60e4d988 [file] [log] [blame]
David Tolnay33f56ad2020-08-27 17:06:35 -07001use crate::rust_string::RustString;
David Tolnayc5a52f92020-09-14 00:43:29 -04002use alloc::string::String;
3use alloc::vec::Vec;
David Tolnay3384c142020-09-14 00:26:47 -04004use core::mem::ManuallyDrop;
David Tolnay33f56ad2020-08-27 17:06:35 -07005
Myron Ahneba35cf2020-02-05 19:41:51 +07006#[repr(C)]
David Tolnay3a8ae092020-04-24 11:55:46 -07007pub struct RustVec<T> {
David Tolnay9f6c0752020-09-07 22:26:46 -07008 pub(crate) repr: Vec<T>,
Myron Ahneba35cf2020-02-05 19:41:51 +07009}
10
David Tolnay3a8ae092020-04-24 11:55:46 -070011impl<T> RustVec<T> {
David Tolnayf97c2d52020-04-25 16:37:48 -070012 pub fn new() -> Self {
13 RustVec { repr: Vec::new() }
14 }
15
Myron Ahneba35cf2020-02-05 19:41:51 +070016 pub fn from(v: Vec<T>) -> Self {
17 RustVec { repr: v }
18 }
19
20 pub fn from_ref(v: &Vec<T>) -> &Self {
David Tolnayfac8b252020-04-24 11:37:39 -070021 unsafe { &*(v as *const Vec<T> as *const RustVec<T>) }
Myron Ahneba35cf2020-02-05 19:41:51 +070022 }
23
David Tolnayf1c7f322020-08-27 00:46:01 -070024 pub fn from_mut(v: &mut Vec<T>) -> &mut Self {
25 unsafe { &mut *(v as *mut Vec<T> as *mut RustVec<T>) }
26 }
27
Myron Ahneba35cf2020-02-05 19:41:51 +070028 pub fn into_vec(self) -> Vec<T> {
29 self.repr
30 }
31
32 pub fn as_vec(&self) -> &Vec<T> {
33 &self.repr
34 }
35
36 pub fn as_mut_vec(&mut self) -> &mut Vec<T> {
37 &mut self.repr
38 }
39
40 pub fn len(&self) -> usize {
41 self.repr.len()
42 }
David Tolnay219c0792020-04-24 20:31:37 -070043
David Tolnaydc62d712020-12-11 13:51:53 -080044 pub fn capacity(&self) -> usize {
45 self.repr.capacity()
46 }
47
David Tolnay219c0792020-04-24 20:31:37 -070048 pub fn as_ptr(&self) -> *const T {
49 self.repr.as_ptr()
50 }
David Tolnayfb6b73c2020-11-10 14:32:16 -080051
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
59 pub unsafe fn set_len(&mut self, len: usize) {
60 self.repr.set_len(len);
61 }
Myron Ahneba35cf2020-02-05 19:41:51 +070062}
David Tolnay33f56ad2020-08-27 17:06:35 -070063
64impl RustVec<RustString> {
65 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
73 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
77 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
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
89 pub fn as_vec_string(&self) -> &Vec<String> {
90 unsafe { &*(&self.repr as *const Vec<RustString> as *const Vec<String>) }
91 }
92
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}