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             #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$new")]
18             unsafe extern "C" fn __new(this: *mut RustVec<$ty>) {
19                 unsafe { ptr::write(this, RustVec::new()) }
20             }
21             #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$drop")]
22             unsafe extern "C" fn __drop(this: *mut RustVec<$ty>) {
23                 unsafe { ptr::drop_in_place(this) }
24             }
25             #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$len")]
26             unsafe extern "C" fn __len(this: *const RustVec<$ty>) -> usize {
27                 unsafe { &*this }.len()
28             }
29             #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$capacity")]
30             unsafe extern "C" fn __capacity(this: *const RustVec<$ty>) -> usize {
31                 unsafe { &*this }.capacity()
32             }
33             #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$data")]
34             unsafe extern "C" fn __data(this: *const RustVec<$ty>) -> *const $ty {
35                 unsafe { &*this }.as_ptr()
36             }
37             #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$reserve_total")]
38             unsafe extern "C" fn __reserve_total(this: *mut RustVec<$ty>, new_cap: usize) {
39                 unsafe { &mut *this }.reserve_total(new_cap);
40             }
41             #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$set_len")]
42             unsafe extern "C" fn __set_len(this: *mut RustVec<$ty>, len: usize) {
43                 unsafe { (*this).set_len(len) }
44             }
45             #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$truncate")]
46             unsafe extern "C" fn __truncate(this: *mut RustVec<$ty>, len: usize) {
47                 unsafe { (*this).truncate(len) }
48             }
49         };
50     };
51 }
52 
53 macro_rules! rust_vec_shims_for_primitive {
54     ($ty:ident) => {
55         rust_vec_shims!(stringify!($ty), $ty);
56     };
57 }
58 
59 rust_vec_shims_for_primitive!(bool);
60 rust_vec_shims_for_primitive!(u8);
61 rust_vec_shims_for_primitive!(u16);
62 rust_vec_shims_for_primitive!(u32);
63 rust_vec_shims_for_primitive!(u64);
64 rust_vec_shims_for_primitive!(usize);
65 rust_vec_shims_for_primitive!(i8);
66 rust_vec_shims_for_primitive!(i16);
67 rust_vec_shims_for_primitive!(i32);
68 rust_vec_shims_for_primitive!(i64);
69 rust_vec_shims_for_primitive!(isize);
70 rust_vec_shims_for_primitive!(f32);
71 rust_vec_shims_for_primitive!(f64);
72 
73 rust_vec_shims!("char", c_char);
74 rust_vec_shims!("string", RustString);
75 rust_vec_shims!("str", &str);
76