| 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> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 7 | pub fn from(v: Vec<T>) -> Self { |
| 8 | RustVec { repr: v } |
| 9 | } |
| 10 | |
| 11 | pub fn from_ref(v: &Vec<T>) -> &Self { |
| David Tolnay | fac8b25 | 2020-04-24 11:37:39 -0700 | [diff] [blame] | 12 | unsafe { &*(v as *const Vec<T> as *const RustVec<T>) } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | pub fn into_vec(self) -> Vec<T> { |
| 16 | self.repr |
| 17 | } |
| 18 | |
| 19 | pub fn as_vec(&self) -> &Vec<T> { |
| 20 | &self.repr |
| 21 | } |
| 22 | |
| 23 | pub fn as_mut_vec(&mut self) -> &mut Vec<T> { |
| 24 | &mut self.repr |
| 25 | } |
| 26 | |
| 27 | pub fn len(&self) -> usize { |
| 28 | self.repr.len() |
| 29 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 30 | } |