| David Tolnay | 83f7115 | 2020-09-06 19:31:09 -0700 | [diff] [blame] | 1 | use crate::rust_string::RustString; |
| David Tolnay | 9f6c075 | 2020-09-07 22:26:46 -0700 | [diff] [blame] | 2 | use crate::rust_vec::RustVec; |
| David Tolnay | c5a52f9 | 2020-09-14 00:43:29 -0400 | [diff] [blame] | 3 | use alloc::vec::Vec; |
| David Tolnay | 3384c14 | 2020-09-14 00:26:47 -0400 | [diff] [blame] | 4 | use core::mem; |
| 5 | use core::ptr; |
| David Tolnay | 93e71d0 | 2020-11-25 20:16:52 -0800 | [diff] [blame] | 6 | use std::os::raw::c_char; |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 7 | |
| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame] | 8 | macro_rules! rust_vec_shims { |
| 9 | ($segment:expr, $ty:ty) => { |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 10 | 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 Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 15 | #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$new")] |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 16 | unsafe extern "C" fn __new(this: *mut RustVec<$ty>) { |
| 17 | ptr::write(this, RustVec { repr: Vec::new() }); |
| 18 | } |
| 19 | } |
| 20 | attr! { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 21 | #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$drop")] |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 22 | unsafe extern "C" fn __drop(this: *mut RustVec<$ty>) { |
| 23 | ptr::drop_in_place(this); |
| 24 | } |
| 25 | } |
| 26 | attr! { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 27 | #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$len")] |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 28 | unsafe extern "C" fn __len(this: *const RustVec<$ty>) -> usize { |
| 29 | (*this).repr.len() |
| 30 | } |
| 31 | } |
| 32 | attr! { |
| David Tolnay | dc62d71 | 2020-12-11 13:51:53 -0800 | [diff] [blame] | 33 | #[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 Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 39 | #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$data")] |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 40 | unsafe extern "C" fn __data(this: *const RustVec<$ty>) -> *const $ty { |
| 41 | (*this).repr.as_ptr() |
| 42 | } |
| 43 | } |
| 44 | attr! { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 45 | #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$reserve_total")] |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 46 | unsafe extern "C" fn __reserve_total(this: *mut RustVec<$ty>, cap: usize) { |
| 47 | (*this).reserve_total(cap); |
| 48 | } |
| 49 | } |
| 50 | attr! { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 51 | #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$set_len")] |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 52 | unsafe extern "C" fn __set_len(this: *mut RustVec<$ty>, len: usize) { |
| 53 | (*this).repr.set_len(len); |
| 54 | } |
| 55 | } |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 56 | }; |
| 57 | }; |
| 58 | } |
| 59 | |
| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame] | 60 | macro_rules! rust_vec_shims_for_primitive { |
| 61 | ($ty:ident) => { |
| 62 | rust_vec_shims!(stringify!($ty), $ty); |
| 63 | }; |
| 64 | } |
| 65 | |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 66 | rust_vec_shims_for_primitive!(bool); |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 67 | rust_vec_shims_for_primitive!(u8); |
| 68 | rust_vec_shims_for_primitive!(u16); |
| 69 | rust_vec_shims_for_primitive!(u32); |
| 70 | rust_vec_shims_for_primitive!(u64); |
| 71 | rust_vec_shims_for_primitive!(i8); |
| 72 | rust_vec_shims_for_primitive!(i16); |
| 73 | rust_vec_shims_for_primitive!(i32); |
| 74 | rust_vec_shims_for_primitive!(i64); |
| 75 | rust_vec_shims_for_primitive!(f32); |
| 76 | rust_vec_shims_for_primitive!(f64); |
| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame] | 77 | |
| David Tolnay | 93e71d0 | 2020-11-25 20:16:52 -0800 | [diff] [blame] | 78 | rust_vec_shims!("char", c_char); |
| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame] | 79 | rust_vec_shims!("string", RustString); |