| 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 | |
| 44 | pub fn as_ptr(&self) -> *const T { |
| 45 | self.repr.as_ptr() |
| 46 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 47 | } |
| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame] | 48 | |
| 49 | impl RustVec<RustString> { |
| 50 | pub fn from_vec_string(v: Vec<String>) -> Self { |
| 51 | let mut v = ManuallyDrop::new(v); |
| 52 | let ptr = v.as_mut_ptr().cast::<RustString>(); |
| 53 | let len = v.len(); |
| 54 | let cap = v.capacity(); |
| 55 | Self::from(unsafe { Vec::from_raw_parts(ptr, len, cap) }) |
| 56 | } |
| 57 | |
| 58 | pub fn from_ref_vec_string(v: &Vec<String>) -> &Self { |
| 59 | Self::from_ref(unsafe { &*(v as *const Vec<String> as *const Vec<RustString>) }) |
| 60 | } |
| 61 | |
| 62 | pub fn from_mut_vec_string(v: &mut Vec<String>) -> &mut Self { |
| 63 | Self::from_mut(unsafe { &mut *(v as *mut Vec<String> as *mut Vec<RustString>) }) |
| 64 | } |
| 65 | |
| 66 | pub fn into_vec_string(self) -> Vec<String> { |
| 67 | let mut v = ManuallyDrop::new(self.repr); |
| 68 | let ptr = v.as_mut_ptr().cast::<String>(); |
| 69 | let len = v.len(); |
| 70 | let cap = v.capacity(); |
| 71 | unsafe { Vec::from_raw_parts(ptr, len, cap) } |
| 72 | } |
| 73 | |
| 74 | pub fn as_vec_string(&self) -> &Vec<String> { |
| 75 | unsafe { &*(&self.repr as *const Vec<RustString> as *const Vec<String>) } |
| 76 | } |
| 77 | |
| 78 | pub fn as_mut_vec_string(&mut self) -> &mut Vec<String> { |
| 79 | unsafe { &mut *(&mut self.repr as *mut Vec<RustString> as *mut Vec<String>) } |
| 80 | } |
| 81 | } |