blob: 60b9cfa54a2e56943c9386d5646d899ebd271d0a [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 Tolnay93e71d02020-11-25 20:16:52 -08006use std::os::raw::c_char;
David Tolnay3c90cd22020-04-30 07:28:21 -07007
David Tolnay33f56ad2020-08-27 17:06:35 -07008macro_rules! rust_vec_shims {
9 ($segment:expr, $ty:ty) => {
David Tolnay3c90cd22020-04-30 07:28:21 -070010 const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<Vec<$ty>>());
11 const_assert_eq!(mem::align_of::<usize>(), mem::align_of::<Vec<$ty>>());
12
13 const _: () = {
14 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -080015 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$new")]
David Tolnay3c90cd22020-04-30 07:28:21 -070016 unsafe extern "C" fn __new(this: *mut RustVec<$ty>) {
17 ptr::write(this, RustVec { repr: Vec::new() });
18 }
19 }
20 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -080021 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$drop")]
David Tolnay3c90cd22020-04-30 07:28:21 -070022 unsafe extern "C" fn __drop(this: *mut RustVec<$ty>) {
23 ptr::drop_in_place(this);
24 }
25 }
26 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -080027 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$len")]
David Tolnay3c90cd22020-04-30 07:28:21 -070028 unsafe extern "C" fn __len(this: *const RustVec<$ty>) -> usize {
29 (*this).repr.len()
30 }
31 }
32 attr! {
David Tolnaydc62d712020-12-11 13:51:53 -080033 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$capacity")]
34 unsafe extern "C" fn __capacity(this: *const RustVec<$ty>) -> usize {
35 (*this).repr.capacity()
36 }
37 }
38 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -080039 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$data")]
David Tolnay3c90cd22020-04-30 07:28:21 -070040 unsafe extern "C" fn __data(this: *const RustVec<$ty>) -> *const $ty {
41 (*this).repr.as_ptr()
42 }
43 }
44 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -080045 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$reserve_total")]
David Tolnayfb6b73c2020-11-10 14:32:16 -080046 unsafe extern "C" fn __reserve_total(this: *mut RustVec<$ty>, cap: usize) {
47 (*this).reserve_total(cap);
48 }
49 }
50 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -080051 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$set_len")]
David Tolnayfb6b73c2020-11-10 14:32:16 -080052 unsafe extern "C" fn __set_len(this: *mut RustVec<$ty>, len: usize) {
53 (*this).repr.set_len(len);
54 }
55 }
David Tolnay3c90cd22020-04-30 07:28:21 -070056 };
57 };
58}
59
David Tolnay33f56ad2020-08-27 17:06:35 -070060macro_rules! rust_vec_shims_for_primitive {
61 ($ty:ident) => {
62 rust_vec_shims!(stringify!($ty), $ty);
63 };
64}
65
David Tolnayf336b3b2020-04-30 08:45:54 -070066rust_vec_shims_for_primitive!(bool);
David Tolnay3c90cd22020-04-30 07:28:21 -070067rust_vec_shims_for_primitive!(u8);
68rust_vec_shims_for_primitive!(u16);
69rust_vec_shims_for_primitive!(u32);
70rust_vec_shims_for_primitive!(u64);
71rust_vec_shims_for_primitive!(i8);
72rust_vec_shims_for_primitive!(i16);
73rust_vec_shims_for_primitive!(i32);
74rust_vec_shims_for_primitive!(i64);
75rust_vec_shims_for_primitive!(f32);
76rust_vec_shims_for_primitive!(f64);
David Tolnay33f56ad2020-08-27 17:06:35 -070077
David Tolnay93e71d02020-11-25 20:16:52 -080078rust_vec_shims!("char", c_char);
David Tolnay33f56ad2020-08-27 17:06:35 -070079rust_vec_shims!("string", RustString);