| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame^] | 1 | use crate::rust_string::RustString; |
| 2 | use std::mem::ManuallyDrop; |
| 3 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 4 | #[repr(C)] |
| David Tolnay | 3a8ae09 | 2020-04-24 11:55:46 -0700 | [diff] [blame] | 5 | pub struct RustVec<T> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 6 | repr: Vec<T>, |
| 7 | } |
| 8 | |
| David Tolnay | 3a8ae09 | 2020-04-24 11:55:46 -0700 | [diff] [blame] | 9 | impl<T> RustVec<T> { |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 10 | pub fn new() -> Self { |
| 11 | RustVec { repr: Vec::new() } |
| 12 | } |
| 13 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 14 | pub fn from(v: Vec<T>) -> Self { |
| 15 | RustVec { repr: v } |
| 16 | } |
| 17 | |
| 18 | pub fn from_ref(v: &Vec<T>) -> &Self { |
| David Tolnay | fac8b25 | 2020-04-24 11:37:39 -0700 | [diff] [blame] | 19 | unsafe { &*(v as *const Vec<T> as *const RustVec<T>) } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 20 | } |
| 21 | |
| David Tolnay | f1c7f32 | 2020-08-27 00:46:01 -0700 | [diff] [blame] | 22 | pub fn from_mut(v: &mut Vec<T>) -> &mut Self { |
| 23 | unsafe { &mut *(v as *mut Vec<T> as *mut RustVec<T>) } |
| 24 | } |
| 25 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 26 | pub fn into_vec(self) -> Vec<T> { |
| 27 | self.repr |
| 28 | } |
| 29 | |
| 30 | pub fn as_vec(&self) -> &Vec<T> { |
| 31 | &self.repr |
| 32 | } |
| 33 | |
| 34 | pub fn as_mut_vec(&mut self) -> &mut Vec<T> { |
| 35 | &mut self.repr |
| 36 | } |
| 37 | |
| 38 | pub fn len(&self) -> usize { |
| 39 | self.repr.len() |
| 40 | } |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 41 | |
| 42 | pub fn as_ptr(&self) -> *const T { |
| 43 | self.repr.as_ptr() |
| 44 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 45 | } |
| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame^] | 46 | |
| 47 | impl RustVec<RustString> { |
| 48 | pub fn from_vec_string(v: Vec<String>) -> Self { |
| 49 | let mut v = ManuallyDrop::new(v); |
| 50 | let ptr = v.as_mut_ptr().cast::<RustString>(); |
| 51 | let len = v.len(); |
| 52 | let cap = v.capacity(); |
| 53 | Self::from(unsafe { Vec::from_raw_parts(ptr, len, cap) }) |
| 54 | } |
| 55 | |
| 56 | pub fn from_ref_vec_string(v: &Vec<String>) -> &Self { |
| 57 | Self::from_ref(unsafe { &*(v as *const Vec<String> as *const Vec<RustString>) }) |
| 58 | } |
| 59 | |
| 60 | pub fn from_mut_vec_string(v: &mut Vec<String>) -> &mut Self { |
| 61 | Self::from_mut(unsafe { &mut *(v as *mut Vec<String> as *mut Vec<RustString>) }) |
| 62 | } |
| 63 | |
| 64 | pub fn into_vec_string(self) -> Vec<String> { |
| 65 | let mut v = ManuallyDrop::new(self.repr); |
| 66 | let ptr = v.as_mut_ptr().cast::<String>(); |
| 67 | let len = v.len(); |
| 68 | let cap = v.capacity(); |
| 69 | unsafe { Vec::from_raw_parts(ptr, len, cap) } |
| 70 | } |
| 71 | |
| 72 | pub fn as_vec_string(&self) -> &Vec<String> { |
| 73 | unsafe { &*(&self.repr as *const Vec<RustString> as *const Vec<String>) } |
| 74 | } |
| 75 | |
| 76 | pub fn as_mut_vec_string(&mut self) -> &mut Vec<String> { |
| 77 | unsafe { &mut *(&mut self.repr as *mut Vec<RustString> as *mut Vec<String>) } |
| 78 | } |
| 79 | } |