1 #![cfg(feature = "alloc")] 2 3 use crate::c_char::c_char; 4 use crate::rust_string::RustString; 5 use crate::rust_vec::RustVec; 6 use alloc::vec::Vec; 7 use core::mem; 8 use core::ptr; 9 10 macro_rules! rust_vec_shims { 11 ($segment:expr, $ty:ty) => { 12 const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<RustVec<$ty>>()); 13 const_assert_eq!(mem::size_of::<Vec<$ty>>(), mem::size_of::<RustVec<$ty>>()); 14 const_assert_eq!(mem::align_of::<Vec<$ty>>(), mem::align_of::<RustVec<$ty>>()); 15 16 const _: () = { 17 attr! { 18 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$new")] 19 unsafe extern "C" fn __new(this: *mut RustVec<$ty>) { 20 unsafe { ptr::write(this, RustVec::new()) } 21 } 22 } 23 attr! { 24 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$drop")] 25 unsafe extern "C" fn __drop(this: *mut RustVec<$ty>) { 26 unsafe { ptr::drop_in_place(this) } 27 } 28 } 29 attr! { 30 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$len")] 31 unsafe extern "C" fn __len(this: *const RustVec<$ty>) -> usize { 32 unsafe { &*this }.len() 33 } 34 } 35 attr! { 36 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$capacity")] 37 unsafe extern "C" fn __capacity(this: *const RustVec<$ty>) -> usize { 38 unsafe { &*this }.capacity() 39 } 40 } 41 attr! { 42 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$data")] 43 unsafe extern "C" fn __data(this: *const RustVec<$ty>) -> *const $ty { 44 unsafe { &*this }.as_ptr() 45 } 46 } 47 attr! { 48 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$reserve_total")] 49 unsafe extern "C" fn __reserve_total(this: *mut RustVec<$ty>, new_cap: usize) { 50 unsafe { &mut *this }.reserve_total(new_cap); 51 } 52 } 53 attr! { 54 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$set_len")] 55 unsafe extern "C" fn __set_len(this: *mut RustVec<$ty>, len: usize) { 56 unsafe { (*this).set_len(len) } 57 } 58 } 59 attr! { 60 #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$truncate")] 61 unsafe extern "C" fn __truncate(this: *mut RustVec<$ty>, len: usize) { 62 unsafe { (*this).truncate(len) } 63 } 64 } 65 }; 66 }; 67 } 68 69 macro_rules! rust_vec_shims_for_primitive { 70 ($ty:ident) => { 71 rust_vec_shims!(stringify!($ty), $ty); 72 }; 73 } 74 75 rust_vec_shims_for_primitive!(bool); 76 rust_vec_shims_for_primitive!(u8); 77 rust_vec_shims_for_primitive!(u16); 78 rust_vec_shims_for_primitive!(u32); 79 rust_vec_shims_for_primitive!(u64); 80 rust_vec_shims_for_primitive!(usize); 81 rust_vec_shims_for_primitive!(i8); 82 rust_vec_shims_for_primitive!(i16); 83 rust_vec_shims_for_primitive!(i32); 84 rust_vec_shims_for_primitive!(i64); 85 rust_vec_shims_for_primitive!(isize); 86 rust_vec_shims_for_primitive!(f32); 87 rust_vec_shims_for_primitive!(f64); 88 89 rust_vec_shims!("char", c_char); 90 rust_vec_shims!("string", RustString); 91 rust_vec_shims!("str", &str); 92