aboutsummaryrefslogtreecommitdiff
path: root/src/symbols/rust_vec.rs
blob: d7d2e34a679e755a2274b27cc32820d7a5a1ab70 (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
66
67
68
69
70
71
72
73
74
75
#![cfg(feature = "alloc")]

use crate::c_char::c_char;
use crate::rust_string::RustString;
use crate::rust_vec::RustVec;
use alloc::vec::Vec;
use core::mem;
use core::ptr;

macro_rules! rust_vec_shims {
    ($segment:expr, $ty:ty) => {
        const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<RustVec<$ty>>());
        const_assert_eq!(mem::size_of::<Vec<$ty>>(), mem::size_of::<RustVec<$ty>>());
        const_assert_eq!(mem::align_of::<Vec<$ty>>(), mem::align_of::<RustVec<$ty>>());

        const _: () = {
            #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$new")]
            unsafe extern "C" fn __new(this: *mut RustVec<$ty>) {
                unsafe { ptr::write(this, RustVec::new()) }
            }
            #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$drop")]
            unsafe extern "C" fn __drop(this: *mut RustVec<$ty>) {
                unsafe { ptr::drop_in_place(this) }
            }
            #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$len")]
            unsafe extern "C" fn __len(this: *const RustVec<$ty>) -> usize {
                unsafe { &*this }.len()
            }
            #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$capacity")]
            unsafe extern "C" fn __capacity(this: *const RustVec<$ty>) -> usize {
                unsafe { &*this }.capacity()
            }
            #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$data")]
            unsafe extern "C" fn __data(this: *const RustVec<$ty>) -> *const $ty {
                unsafe { &*this }.as_ptr()
            }
            #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$reserve_total")]
            unsafe extern "C" fn __reserve_total(this: *mut RustVec<$ty>, new_cap: usize) {
                unsafe { &mut *this }.reserve_total(new_cap);
            }
            #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$set_len")]
            unsafe extern "C" fn __set_len(this: *mut RustVec<$ty>, len: usize) {
                unsafe { (*this).set_len(len) }
            }
            #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$truncate")]
            unsafe extern "C" fn __truncate(this: *mut RustVec<$ty>, len: usize) {
                unsafe { (*this).truncate(len) }
            }
        };
    };
}

macro_rules! rust_vec_shims_for_primitive {
    ($ty:ident) => {
        rust_vec_shims!(stringify!($ty), $ty);
    };
}

rust_vec_shims_for_primitive!(bool);
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!(usize);
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!(isize);
rust_vec_shims_for_primitive!(f32);
rust_vec_shims_for_primitive!(f64);

rust_vec_shims!("char", c_char);
rust_vec_shims!("string", RustString);
rust_vec_shims!("str", &str);