1#![cfg(feature = "alloc")]
2
3use crate::c_char::c_char;
4use crate::rust_string::RustString;
5use crate::rust_vec::RustVec;
6use alloc::vec::Vec;
7use core::mem;
8use core::ptr;
9
10macro_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
69macro_rules! rust_vec_shims_for_primitive {
70    ($ty:ident) => {
71        rust_vec_shims!(stringify!($ty), $ty);
72    };
73}
74
75rust_vec_shims_for_primitive!(bool);
76rust_vec_shims_for_primitive!(u8);
77rust_vec_shims_for_primitive!(u16);
78rust_vec_shims_for_primitive!(u32);
79rust_vec_shims_for_primitive!(u64);
80rust_vec_shims_for_primitive!(usize);
81rust_vec_shims_for_primitive!(i8);
82rust_vec_shims_for_primitive!(i16);
83rust_vec_shims_for_primitive!(i32);
84rust_vec_shims_for_primitive!(i64);
85rust_vec_shims_for_primitive!(isize);
86rust_vec_shims_for_primitive!(f32);
87rust_vec_shims_for_primitive!(f64);
88
89rust_vec_shims!("char", c_char);
90rust_vec_shims!("string", RustString);
91rust_vec_shims!("str", &str);
92