| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame] | 1 | use crate::rust_string::RustString; |
| David Tolnay | c5a52f9 | 2020-09-14 00:43:29 -0400 | [diff] [blame] | 2 | use alloc::string::String; |
| 3 | use alloc::vec::Vec; |
| David Tolnay | 3384c14 | 2020-09-14 00:26:47 -0400 | [diff] [blame] | 4 | use core::mem::ManuallyDrop; |
| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame] | 5 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 6 | #[repr(C)] |
| David Tolnay | 3a8ae09 | 2020-04-24 11:55:46 -0700 | [diff] [blame] | 7 | pub struct RustVec<T> { |
| David Tolnay | 9f6c075 | 2020-09-07 22:26:46 -0700 | [diff] [blame] | 8 | pub(crate) repr: Vec<T>, |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 9 | } |
| 10 | |
| David Tolnay | 3a8ae09 | 2020-04-24 11:55:46 -0700 | [diff] [blame] | 11 | impl<T> RustVec<T> { |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 12 | pub fn new() -> Self { |
| 13 | RustVec { repr: Vec::new() } |
| 14 | } |
| 15 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 16 | pub fn from(v: Vec<T>) -> Self { |
| 17 | RustVec { repr: v } |
| 18 | } |
| 19 | |
| 20 | pub fn from_ref(v: &Vec<T>) -> &Self { |
| David Tolnay | fac8b25 | 2020-04-24 11:37:39 -0700 | [diff] [blame] | 21 | unsafe { &*(v as *const Vec<T> as *const RustVec<T>) } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 22 | } |
| 23 | |
| David Tolnay | f1c7f32 | 2020-08-27 00:46:01 -0700 | [diff] [blame] | 24 | 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 Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 28 | 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 Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 43 | |
| David Tolnay | dc62d71 | 2020-12-11 13:51:53 -0800 | [diff] [blame^] | 44 | pub fn capacity(&self) -> usize { |
| 45 | self.repr.capacity() |
| 46 | } |
| 47 | |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 48 | pub fn as_ptr(&self) -> *const T { |
| 49 | self.repr.as_ptr() |
| 50 | } |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 51 | |
| 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 Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 62 | } |
| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame] | 63 | |
| 64 | impl 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 | } |