blob: 9ff4bbf205c004e69331623cdb268f393a0edebc [file] [log] [blame]
Myron Ahneba35cf2020-02-05 19:41:51 +07001#[repr(C)]
David Tolnay3a8ae092020-04-24 11:55:46 -07002pub struct RustVec<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +07003 repr: Vec<T>,
4}
5
David Tolnay3a8ae092020-04-24 11:55:46 -07006impl<T> RustVec<T> {
David Tolnayf97c2d52020-04-25 16:37:48 -07007 pub fn new() -> Self {
8 RustVec { repr: Vec::new() }
9 }
10
Myron Ahneba35cf2020-02-05 19:41:51 +070011 pub fn from(v: Vec<T>) -> Self {
12 RustVec { repr: v }
13 }
14
15 pub fn from_ref(v: &Vec<T>) -> &Self {
David Tolnayfac8b252020-04-24 11:37:39 -070016 unsafe { &*(v as *const Vec<T> as *const RustVec<T>) }
Myron Ahneba35cf2020-02-05 19:41:51 +070017 }
18
David Tolnayf1c7f322020-08-27 00:46:01 -070019 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 Ahneba35cf2020-02-05 19:41:51 +070023 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 Tolnay219c0792020-04-24 20:31:37 -070038
39 pub fn as_ptr(&self) -> *const T {
40 self.repr.as_ptr()
41 }
Myron Ahneba35cf2020-02-05 19:41:51 +070042}