| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1 | #[repr(C)] |
| David Tolnay | 3a8ae09 | 2020-04-24 11:55:46 -0700 | [diff] [blame] | 2 | pub struct RustVec<T> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 3 | repr: Vec<T>, |
| 4 | } |
| 5 | |
| David Tolnay | 3a8ae09 | 2020-04-24 11:55:46 -0700 | [diff] [blame] | 6 | impl<T> RustVec<T> { |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 7 | pub fn new() -> Self { |
| 8 | RustVec { repr: Vec::new() } |
| 9 | } |
| 10 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 11 | pub fn from(v: Vec<T>) -> Self { |
| 12 | RustVec { repr: v } |
| 13 | } |
| 14 | |
| 15 | pub fn from_ref(v: &Vec<T>) -> &Self { |
| David Tolnay | fac8b25 | 2020-04-24 11:37:39 -0700 | [diff] [blame] | 16 | unsafe { &*(v as *const Vec<T> as *const RustVec<T>) } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 17 | } |
| 18 | |
| David Tolnay | f1c7f32 | 2020-08-27 00:46:01 -0700 | [diff] [blame^] | 19 | pub fn from_mut(v: &mut Vec<T>) -> &mut Self { |
| 20 | unsafe { &mut *(v as *mut Vec<T> as *mut RustVec<T>) } |
| 21 | } |
| 22 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 23 | pub fn into_vec(self) -> Vec<T> { |
| 24 | self.repr |
| 25 | } |
| 26 | |
| 27 | pub fn as_vec(&self) -> &Vec<T> { |
| 28 | &self.repr |
| 29 | } |
| 30 | |
| 31 | pub fn as_mut_vec(&mut self) -> &mut Vec<T> { |
| 32 | &mut self.repr |
| 33 | } |
| 34 | |
| 35 | pub fn len(&self) -> usize { |
| 36 | self.repr.len() |
| 37 | } |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 38 | |
| 39 | pub fn as_ptr(&self) -> *const T { |
| 40 | self.repr.as_ptr() |
| 41 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 42 | } |