| David Tolnay | 9c6bf2d | 2020-04-24 15:27:07 -0700 | [diff] [blame] | 1 | use std::mem; |
| David Tolnay | a006bca | 2020-04-25 11:28:13 -0700 | [diff] [blame^] | 2 | use std::ptr; |
| David Tolnay | 9c6bf2d | 2020-04-24 15:27:07 -0700 | [diff] [blame] | 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> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 10 | pub fn from(v: Vec<T>) -> Self { |
| 11 | RustVec { repr: v } |
| 12 | } |
| 13 | |
| 14 | pub fn from_ref(v: &Vec<T>) -> &Self { |
| David Tolnay | fac8b25 | 2020-04-24 11:37:39 -0700 | [diff] [blame] | 15 | unsafe { &*(v as *const Vec<T> as *const RustVec<T>) } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | pub fn into_vec(self) -> Vec<T> { |
| 19 | self.repr |
| 20 | } |
| 21 | |
| 22 | pub fn as_vec(&self) -> &Vec<T> { |
| 23 | &self.repr |
| 24 | } |
| 25 | |
| 26 | pub fn as_mut_vec(&mut self) -> &mut Vec<T> { |
| 27 | &mut self.repr |
| 28 | } |
| 29 | |
| 30 | pub fn len(&self) -> usize { |
| 31 | self.repr.len() |
| 32 | } |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 33 | |
| 34 | pub fn as_ptr(&self) -> *const T { |
| 35 | self.repr.as_ptr() |
| 36 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 37 | } |
| David Tolnay | 9c6bf2d | 2020-04-24 15:27:07 -0700 | [diff] [blame] | 38 | |
| David Tolnay | a006bca | 2020-04-25 11:28:13 -0700 | [diff] [blame^] | 39 | macro_rules! rust_vec_shims_for_primitive { |
| 40 | ($ty:ident) => { |
| 41 | const _: () = { |
| 42 | attr! { |
| 43 | #[export_name = concat!("cxxbridge02$rust_vec$", stringify!($ty), "$drop")] |
| 44 | unsafe extern "C" fn __drop(this: *mut RustVec<$ty>) { |
| 45 | ptr::drop_in_place(this); |
| 46 | } |
| 47 | } |
| 48 | attr! { |
| 49 | #[export_name = concat!("cxxbridge02$rust_vec$", stringify!($ty), "$len")] |
| 50 | unsafe extern "C" fn __len(this: *const RustVec<$ty>) -> usize { |
| 51 | (*this).len() |
| 52 | } |
| 53 | } |
| 54 | attr! { |
| 55 | #[export_name = concat!("cxxbridge02$rust_vec$", stringify!($ty), "$data")] |
| 56 | unsafe extern "C" fn __data(this: *const RustVec<$ty>) -> *const $ty { |
| 57 | (*this).as_ptr() |
| 58 | } |
| 59 | } |
| 60 | attr! { |
| 61 | #[export_name = concat!("cxxbridge02$rust_vec$", stringify!($ty), "$stride")] |
| 62 | unsafe extern "C" fn __stride() -> usize { |
| 63 | mem::size_of::<$ty>() |
| 64 | } |
| 65 | } |
| 66 | }; |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | rust_vec_shims_for_primitive!(u8); |
| 71 | rust_vec_shims_for_primitive!(u16); |
| 72 | rust_vec_shims_for_primitive!(u32); |
| 73 | rust_vec_shims_for_primitive!(u64); |
| 74 | rust_vec_shims_for_primitive!(i8); |
| 75 | rust_vec_shims_for_primitive!(i16); |
| 76 | rust_vec_shims_for_primitive!(i32); |
| 77 | rust_vec_shims_for_primitive!(i64); |
| 78 | rust_vec_shims_for_primitive!(f32); |
| 79 | rust_vec_shims_for_primitive!(f64); |
| 80 | |
| David Tolnay | 9c6bf2d | 2020-04-24 15:27:07 -0700 | [diff] [blame] | 81 | const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<Vec<i32>>()); |
| 82 | const_assert_eq!(mem::align_of::<usize>(), mem::align_of::<Vec<i32>>()); |