blob: 1c0c64f2e996d89d72d714dfa7b8249c0a011369 [file] [log] [blame]
David Tolnay9c6bf2d2020-04-24 15:27:07 -07001use std::mem;
2
Myron Ahneba35cf2020-02-05 19:41:51 +07003#[repr(C)]
David Tolnay3a8ae092020-04-24 11:55:46 -07004pub struct RustVec<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +07005 repr: Vec<T>,
6}
7
David Tolnay3a8ae092020-04-24 11:55:46 -07008impl<T> RustVec<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +07009 pub fn from(v: Vec<T>) -> Self {
10 RustVec { repr: v }
11 }
12
13 pub fn from_ref(v: &Vec<T>) -> &Self {
David Tolnayfac8b252020-04-24 11:37:39 -070014 unsafe { &*(v as *const Vec<T> as *const RustVec<T>) }
Myron Ahneba35cf2020-02-05 19:41:51 +070015 }
16
17 pub fn into_vec(self) -> Vec<T> {
18 self.repr
19 }
20
21 pub fn as_vec(&self) -> &Vec<T> {
22 &self.repr
23 }
24
25 pub fn as_mut_vec(&mut self) -> &mut Vec<T> {
26 &mut self.repr
27 }
28
29 pub fn len(&self) -> usize {
30 self.repr.len()
31 }
Myron Ahneba35cf2020-02-05 19:41:51 +070032}
David Tolnay9c6bf2d2020-04-24 15:27:07 -070033
34const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<Vec<i32>>());
35const_assert_eq!(mem::align_of::<usize>(), mem::align_of::<Vec<i32>>());