aboutsummaryrefslogtreecommitdiff
path: root/src/symbols/rust_vec.rs
blob: 712a9e8ce5d1fbe3a19f921b2ce6393e835abf79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::mem;
use std::ptr;

#[repr(C)]
pub struct RustVec<T> {
    repr: Vec<T>,
}

macro_rules! attr {
    (#[$name:ident = $value:expr] $($rest:tt)*) => {
        #[$name = $value]
        $($rest)*
    };
}

macro_rules! rust_vec_shims_for_primitive {
    ($ty:ident) => {
        const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<Vec<$ty>>());
        const_assert_eq!(mem::align_of::<usize>(), mem::align_of::<Vec<$ty>>());

        const _: () = {
            attr! {
                #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$new")]
                unsafe extern "C" fn __new(this: *mut RustVec<$ty>) {
                    ptr::write(this, RustVec { repr: Vec::new() });
                }
            }
            attr! {
                #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$drop")]
                unsafe extern "C" fn __drop(this: *mut RustVec<$ty>) {
                    ptr::drop_in_place(this);
                }
            }
            attr! {
                #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$len")]
                unsafe extern "C" fn __len(this: *const RustVec<$ty>) -> usize {
                    (*this).repr.len()
                }
            }
            attr! {
                #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$data")]
                unsafe extern "C" fn __data(this: *const RustVec<$ty>) -> *const $ty {
                    (*this).repr.as_ptr()
                }
            }
            attr! {
                #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$stride")]
                unsafe extern "C" fn __stride() -> usize {
                    mem::size_of::<$ty>()
                }
            }
        };
    };
}

rust_vec_shims_for_primitive!(u8);
rust_vec_shims_for_primitive!(u16);
rust_vec_shims_for_primitive!(u32);
rust_vec_shims_for_primitive!(u64);
rust_vec_shims_for_primitive!(i8);
rust_vec_shims_for_primitive!(i16);
rust_vec_shims_for_primitive!(i32);
rust_vec_shims_for_primitive!(i64);
rust_vec_shims_for_primitive!(f32);
rust_vec_shims_for_primitive!(f64);