blob: d5de489dc0883a59c2778799d65925531a392322 [file] [log] [blame]
David Tolnay9c6bf2d2020-04-24 15:27:07 -07001use std::mem;
David Tolnaya006bca2020-04-25 11:28:13 -07002use std::ptr;
David Tolnay9c6bf2d2020-04-24 15:27:07 -07003
Myron Ahneba35cf2020-02-05 19:41:51 +07004#[repr(C)]
David Tolnay3a8ae092020-04-24 11:55:46 -07005pub struct RustVec<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +07006 repr: Vec<T>,
7}
8
David Tolnay3a8ae092020-04-24 11:55:46 -07009impl<T> RustVec<T> {
David Tolnayf97c2d52020-04-25 16:37:48 -070010 pub fn new() -> Self {
11 RustVec { repr: Vec::new() }
12 }
13
Myron Ahneba35cf2020-02-05 19:41:51 +070014 pub fn from(v: Vec<T>) -> Self {
15 RustVec { repr: v }
16 }
17
18 pub fn from_ref(v: &Vec<T>) -> &Self {
David Tolnayfac8b252020-04-24 11:37:39 -070019 unsafe { &*(v as *const Vec<T> as *const RustVec<T>) }
Myron Ahneba35cf2020-02-05 19:41:51 +070020 }
21
22 pub fn into_vec(self) -> Vec<T> {
23 self.repr
24 }
25
26 pub fn as_vec(&self) -> &Vec<T> {
27 &self.repr
28 }
29
30 pub fn as_mut_vec(&mut self) -> &mut Vec<T> {
31 &mut self.repr
32 }
33
34 pub fn len(&self) -> usize {
35 self.repr.len()
36 }
David Tolnay219c0792020-04-24 20:31:37 -070037
38 pub fn as_ptr(&self) -> *const T {
39 self.repr.as_ptr()
40 }
Myron Ahneba35cf2020-02-05 19:41:51 +070041}
David Tolnay9c6bf2d2020-04-24 15:27:07 -070042
David Tolnaya006bca2020-04-25 11:28:13 -070043macro_rules! rust_vec_shims_for_primitive {
44 ($ty:ident) => {
David Tolnayf0446632020-04-25 11:29:26 -070045 const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<Vec<$ty>>());
46 const_assert_eq!(mem::align_of::<usize>(), mem::align_of::<Vec<$ty>>());
47
David Tolnaya006bca2020-04-25 11:28:13 -070048 const _: () = {
49 attr! {
David Tolnayf97c2d52020-04-25 16:37:48 -070050 #[export_name = concat!("cxxbridge02$rust_vec$", stringify!($ty), "$new")]
51 unsafe extern "C" fn __new(this: *mut RustVec<$ty>) {
52 ptr::write(this, RustVec::new());
53 }
54 }
55 attr! {
David Tolnaya006bca2020-04-25 11:28:13 -070056 #[export_name = concat!("cxxbridge02$rust_vec$", stringify!($ty), "$drop")]
57 unsafe extern "C" fn __drop(this: *mut RustVec<$ty>) {
58 ptr::drop_in_place(this);
59 }
60 }
61 attr! {
62 #[export_name = concat!("cxxbridge02$rust_vec$", stringify!($ty), "$len")]
63 unsafe extern "C" fn __len(this: *const RustVec<$ty>) -> usize {
64 (*this).len()
65 }
66 }
67 attr! {
68 #[export_name = concat!("cxxbridge02$rust_vec$", stringify!($ty), "$data")]
69 unsafe extern "C" fn __data(this: *const RustVec<$ty>) -> *const $ty {
70 (*this).as_ptr()
71 }
72 }
73 attr! {
74 #[export_name = concat!("cxxbridge02$rust_vec$", stringify!($ty), "$stride")]
75 unsafe extern "C" fn __stride() -> usize {
76 mem::size_of::<$ty>()
77 }
78 }
79 };
80 };
81}
82
83rust_vec_shims_for_primitive!(u8);
84rust_vec_shims_for_primitive!(u16);
85rust_vec_shims_for_primitive!(u32);
86rust_vec_shims_for_primitive!(u64);
87rust_vec_shims_for_primitive!(i8);
88rust_vec_shims_for_primitive!(i16);
89rust_vec_shims_for_primitive!(i32);
90rust_vec_shims_for_primitive!(i64);
91rust_vec_shims_for_primitive!(f32);
92rust_vec_shims_for_primitive!(f64);