blob: df6dec6de6220036c37d70eb1997e4004ecfff98 [file] [log] [blame]
David Tolnay83f71152020-09-06 19:31:09 -07001use crate::rust_string::RustString;
David Tolnay9f6c0752020-09-07 22:26:46 -07002use crate::rust_vec::RustVec;
David Tolnayc5a52f92020-09-14 00:43:29 -04003use alloc::vec::Vec;
David Tolnay3384c142020-09-14 00:26:47 -04004use core::mem;
5use core::ptr;
David Tolnay3c90cd22020-04-30 07:28:21 -07006
David Tolnay33f56ad2020-08-27 17:06:35 -07007macro_rules! rust_vec_shims {
8 ($segment:expr, $ty:ty) => {
David Tolnay3c90cd22020-04-30 07:28:21 -07009 const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<Vec<$ty>>());
10 const_assert_eq!(mem::align_of::<usize>(), mem::align_of::<Vec<$ty>>());
11
12 const _: () = {
13 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -080014 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$new")]
David Tolnay3c90cd22020-04-30 07:28:21 -070015 unsafe extern "C" fn __new(this: *mut RustVec<$ty>) {
16 ptr::write(this, RustVec { repr: Vec::new() });
17 }
18 }
19 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -080020 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$drop")]
David Tolnay3c90cd22020-04-30 07:28:21 -070021 unsafe extern "C" fn __drop(this: *mut RustVec<$ty>) {
22 ptr::drop_in_place(this);
23 }
24 }
25 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -080026 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$len")]
David Tolnay3c90cd22020-04-30 07:28:21 -070027 unsafe extern "C" fn __len(this: *const RustVec<$ty>) -> usize {
28 (*this).repr.len()
29 }
30 }
31 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -080032 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$data")]
David Tolnay3c90cd22020-04-30 07:28:21 -070033 unsafe extern "C" fn __data(this: *const RustVec<$ty>) -> *const $ty {
34 (*this).repr.as_ptr()
35 }
36 }
37 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -080038 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$reserve_total")]
David Tolnayfb6b73c2020-11-10 14:32:16 -080039 unsafe extern "C" fn __reserve_total(this: *mut RustVec<$ty>, cap: usize) {
40 (*this).reserve_total(cap);
41 }
42 }
43 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -080044 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$set_len")]
David Tolnayfb6b73c2020-11-10 14:32:16 -080045 unsafe extern "C" fn __set_len(this: *mut RustVec<$ty>, len: usize) {
46 (*this).repr.set_len(len);
47 }
48 }
49 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -080050 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$stride")]
David Tolnay3c90cd22020-04-30 07:28:21 -070051 unsafe extern "C" fn __stride() -> usize {
52 mem::size_of::<$ty>()
53 }
54 }
55 };
56 };
57}
58
David Tolnay33f56ad2020-08-27 17:06:35 -070059macro_rules! rust_vec_shims_for_primitive {
60 ($ty:ident) => {
61 rust_vec_shims!(stringify!($ty), $ty);
62 };
63}
64
David Tolnayf336b3b2020-04-30 08:45:54 -070065rust_vec_shims_for_primitive!(bool);
David Tolnay3c90cd22020-04-30 07:28:21 -070066rust_vec_shims_for_primitive!(u8);
67rust_vec_shims_for_primitive!(u16);
68rust_vec_shims_for_primitive!(u32);
69rust_vec_shims_for_primitive!(u64);
70rust_vec_shims_for_primitive!(i8);
71rust_vec_shims_for_primitive!(i16);
72rust_vec_shims_for_primitive!(i32);
73rust_vec_shims_for_primitive!(i64);
74rust_vec_shims_for_primitive!(f32);
75rust_vec_shims_for_primitive!(f64);
David Tolnay33f56ad2020-08-27 17:06:35 -070076
77rust_vec_shims!("string", RustString);