• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![allow(
2     clippy::boxed_local,
3     clippy::derive_partial_eq_without_eq,
4     clippy::just_underscores_and_digits,
5     clippy::missing_safety_doc,
6     clippy::must_use_candidate,
7     clippy::needless_lifetimes,
8     clippy::needless_pass_by_value,
9     clippy::ptr_arg,
10     clippy::trivially_copy_pass_by_ref,
11     clippy::unnecessary_wraps,
12     clippy::unused_self
13 )]
14 
15 pub mod cast;
16 pub mod module;
17 
18 use cxx::{type_id, CxxString, CxxVector, ExternType, SharedPtr, UniquePtr};
19 use std::fmt::{self, Display};
20 use std::mem::MaybeUninit;
21 use std::os::raw::c_char;
22 
23 #[cxx::bridge(namespace = "tests")]
24 pub mod ffi {
25     #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
26     struct Shared {
27         z: usize,
28     }
29 
30     #[derive(PartialEq, PartialOrd)]
31     struct SharedString {
32         msg: String,
33     }
34 
35     #[derive(Debug, Hash, PartialOrd, Ord)]
36     enum Enum {
37         AVal,
38         BVal = 2020,
39         #[cxx_name = "CVal"]
40         LastVal,
41     }
42 
43     #[namespace = "A"]
44     #[derive(Copy, Clone, Default)]
45     struct AShared {
46         #[cxx_name = "type"]
47         z: usize,
48     }
49 
50     #[namespace = "A"]
51     enum AEnum {
52         AAVal,
53         ABVal = 2020,
54         ACVal,
55     }
56 
57     #[namespace = "A::B"]
58     enum ABEnum {
59         ABAVal,
60         ABBVal = 2020,
61         ABCVal,
62     }
63 
64     #[namespace = "A::B"]
65     #[derive(Clone)]
66     struct ABShared {
67         z: usize,
68     }
69 
70     #[namespace = "first"]
71     struct First {
72         second: Box<Second>,
73     }
74 
75     #[namespace = "second"]
76     #[derive(Hash)]
77     struct Second {
78         i: i32,
79         e: COwnedEnum,
80     }
81 
82     pub struct Array {
83         a: [i32; 4],
84         b: Buffer,
85     }
86 
87     #[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
88     pub struct StructWithLifetime<'a> {
89         s: &'a str,
90     }
91 
92     unsafe extern "C++" {
93         include!("tests/ffi/tests.h");
94 
95         type C;
96 
c_return_primitive() -> usize97         fn c_return_primitive() -> usize;
c_return_shared() -> Shared98         fn c_return_shared() -> Shared;
c_return_box() -> Box<R>99         fn c_return_box() -> Box<R>;
c_return_unique_ptr() -> UniquePtr<C>100         fn c_return_unique_ptr() -> UniquePtr<C>;
c_return_shared_ptr() -> SharedPtr<C>101         fn c_return_shared_ptr() -> SharedPtr<C>;
c_return_ref(shared: &Shared) -> &usize102         fn c_return_ref(shared: &Shared) -> &usize;
c_return_mut(shared: &mut Shared) -> &mut usize103         fn c_return_mut(shared: &mut Shared) -> &mut usize;
c_return_str(shared: &Shared) -> &str104         fn c_return_str(shared: &Shared) -> &str;
c_return_slice_char(shared: &Shared) -> &[c_char]105         fn c_return_slice_char(shared: &Shared) -> &[c_char];
c_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8]106         fn c_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8];
c_return_rust_string() -> String107         fn c_return_rust_string() -> String;
c_return_rust_string_lossy() -> String108         fn c_return_rust_string_lossy() -> String;
c_return_unique_ptr_string() -> UniquePtr<CxxString>109         fn c_return_unique_ptr_string() -> UniquePtr<CxxString>;
c_return_unique_ptr_vector_u8() -> UniquePtr<CxxVector<u8>>110         fn c_return_unique_ptr_vector_u8() -> UniquePtr<CxxVector<u8>>;
c_return_unique_ptr_vector_f64() -> UniquePtr<CxxVector<f64>>111         fn c_return_unique_ptr_vector_f64() -> UniquePtr<CxxVector<f64>>;
c_return_unique_ptr_vector_string() -> UniquePtr<CxxVector<CxxString>>112         fn c_return_unique_ptr_vector_string() -> UniquePtr<CxxVector<CxxString>>;
c_return_unique_ptr_vector_shared() -> UniquePtr<CxxVector<Shared>>113         fn c_return_unique_ptr_vector_shared() -> UniquePtr<CxxVector<Shared>>;
c_return_unique_ptr_vector_opaque() -> UniquePtr<CxxVector<C>>114         fn c_return_unique_ptr_vector_opaque() -> UniquePtr<CxxVector<C>>;
c_return_ref_vector(c: &C) -> &CxxVector<u8>115         fn c_return_ref_vector(c: &C) -> &CxxVector<u8>;
c_return_mut_vector(c: Pin<&mut C>) -> Pin<&mut CxxVector<u8>>116         fn c_return_mut_vector(c: Pin<&mut C>) -> Pin<&mut CxxVector<u8>>;
c_return_rust_vec_u8() -> Vec<u8>117         fn c_return_rust_vec_u8() -> Vec<u8>;
c_return_ref_rust_vec(c: &C) -> &Vec<u8>118         fn c_return_ref_rust_vec(c: &C) -> &Vec<u8>;
c_return_mut_rust_vec(c: Pin<&mut C>) -> &mut Vec<u8>119         fn c_return_mut_rust_vec(c: Pin<&mut C>) -> &mut Vec<u8>;
c_return_rust_vec_string() -> Vec<String>120         fn c_return_rust_vec_string() -> Vec<String>;
c_return_rust_vec_bool() -> Vec<bool>121         fn c_return_rust_vec_bool() -> Vec<bool>;
c_return_identity(_: usize) -> usize122         fn c_return_identity(_: usize) -> usize;
c_return_sum(_: usize, _: usize) -> usize123         fn c_return_sum(_: usize, _: usize) -> usize;
c_return_enum(n: u16) -> Enum124         fn c_return_enum(n: u16) -> Enum;
c_return_ns_ref(shared: &AShared) -> &usize125         fn c_return_ns_ref(shared: &AShared) -> &usize;
c_return_nested_ns_ref(shared: &ABShared) -> &usize126         fn c_return_nested_ns_ref(shared: &ABShared) -> &usize;
c_return_ns_enum(n: u16) -> AEnum127         fn c_return_ns_enum(n: u16) -> AEnum;
c_return_nested_ns_enum(n: u16) -> ABEnum128         fn c_return_nested_ns_enum(n: u16) -> ABEnum;
c_return_const_ptr(n: usize) -> *const C129         fn c_return_const_ptr(n: usize) -> *const C;
c_return_mut_ptr(n: usize) -> *mut C130         fn c_return_mut_ptr(n: usize) -> *mut C;
131 
c_take_primitive(n: usize)132         fn c_take_primitive(n: usize);
c_take_shared(shared: Shared)133         fn c_take_shared(shared: Shared);
c_take_box(r: Box<R>)134         fn c_take_box(r: Box<R>);
c_take_ref_r(r: &R)135         fn c_take_ref_r(r: &R);
c_take_ref_c(c: &C)136         fn c_take_ref_c(c: &C);
c_take_str(s: &str)137         fn c_take_str(s: &str);
c_take_slice_char(s: &[c_char])138         fn c_take_slice_char(s: &[c_char]);
c_take_slice_shared(s: &[Shared])139         fn c_take_slice_shared(s: &[Shared]);
c_take_slice_shared_sort(s: &mut [Shared])140         fn c_take_slice_shared_sort(s: &mut [Shared]);
c_take_slice_r(s: &[R])141         fn c_take_slice_r(s: &[R]);
c_take_slice_r_sort(s: &mut [R])142         fn c_take_slice_r_sort(s: &mut [R]);
c_take_rust_string(s: String)143         fn c_take_rust_string(s: String);
c_take_unique_ptr_string(s: UniquePtr<CxxString>)144         fn c_take_unique_ptr_string(s: UniquePtr<CxxString>);
c_take_unique_ptr_vector_u8(v: UniquePtr<CxxVector<u8>>)145         fn c_take_unique_ptr_vector_u8(v: UniquePtr<CxxVector<u8>>);
c_take_unique_ptr_vector_f64(v: UniquePtr<CxxVector<f64>>)146         fn c_take_unique_ptr_vector_f64(v: UniquePtr<CxxVector<f64>>);
c_take_unique_ptr_vector_string(v: UniquePtr<CxxVector<CxxString>>)147         fn c_take_unique_ptr_vector_string(v: UniquePtr<CxxVector<CxxString>>);
c_take_unique_ptr_vector_shared(v: UniquePtr<CxxVector<Shared>>)148         fn c_take_unique_ptr_vector_shared(v: UniquePtr<CxxVector<Shared>>);
c_take_ref_vector(v: &CxxVector<u8>)149         fn c_take_ref_vector(v: &CxxVector<u8>);
c_take_rust_vec(v: Vec<u8>)150         fn c_take_rust_vec(v: Vec<u8>);
c_take_rust_vec_shared(v: Vec<Shared>)151         fn c_take_rust_vec_shared(v: Vec<Shared>);
c_take_rust_vec_string(v: Vec<String>)152         fn c_take_rust_vec_string(v: Vec<String>);
c_take_rust_vec_index(v: Vec<u8>)153         fn c_take_rust_vec_index(v: Vec<u8>);
c_take_rust_vec_shared_index(v: Vec<Shared>)154         fn c_take_rust_vec_shared_index(v: Vec<Shared>);
c_take_rust_vec_shared_push(v: Vec<Shared>)155         fn c_take_rust_vec_shared_push(v: Vec<Shared>);
c_take_rust_vec_shared_truncate(v: Vec<Shared>)156         fn c_take_rust_vec_shared_truncate(v: Vec<Shared>);
c_take_rust_vec_shared_clear(v: Vec<Shared>)157         fn c_take_rust_vec_shared_clear(v: Vec<Shared>);
c_take_rust_vec_shared_forward_iterator(v: Vec<Shared>)158         fn c_take_rust_vec_shared_forward_iterator(v: Vec<Shared>);
c_take_rust_vec_shared_sort(v: Vec<Shared>)159         fn c_take_rust_vec_shared_sort(v: Vec<Shared>);
c_take_ref_rust_vec(v: &Vec<u8>)160         fn c_take_ref_rust_vec(v: &Vec<u8>);
c_take_ref_rust_vec_string(v: &Vec<String>)161         fn c_take_ref_rust_vec_string(v: &Vec<String>);
c_take_ref_rust_vec_index(v: &Vec<u8>)162         fn c_take_ref_rust_vec_index(v: &Vec<u8>);
c_take_ref_rust_vec_copy(v: &Vec<u8>)163         fn c_take_ref_rust_vec_copy(v: &Vec<u8>);
c_take_ref_shared_string(s: &SharedString) -> &SharedString164         fn c_take_ref_shared_string(s: &SharedString) -> &SharedString;
c_take_callback(callback: fn(String) -> usize)165         fn c_take_callback(callback: fn(String) -> usize);
c_take_callback_ref(callback: fn(&String))166         fn c_take_callback_ref(callback: fn(&String));
167         #[cxx_name = "c_take_callback_ref"]
c_take_callback_ref_lifetime<'a>(callback: fn(&'a String))168         fn c_take_callback_ref_lifetime<'a>(callback: fn(&'a String));
c_take_callback_mut(callback: fn(&mut String))169         fn c_take_callback_mut(callback: fn(&mut String));
c_take_enum(e: Enum)170         fn c_take_enum(e: Enum);
c_take_ns_enum(e: AEnum)171         fn c_take_ns_enum(e: AEnum);
c_take_nested_ns_enum(e: ABEnum)172         fn c_take_nested_ns_enum(e: ABEnum);
c_take_ns_shared(shared: AShared)173         fn c_take_ns_shared(shared: AShared);
c_take_nested_ns_shared(shared: ABShared)174         fn c_take_nested_ns_shared(shared: ABShared);
c_take_rust_vec_ns_shared(v: Vec<AShared>)175         fn c_take_rust_vec_ns_shared(v: Vec<AShared>);
c_take_rust_vec_nested_ns_shared(v: Vec<ABShared>)176         fn c_take_rust_vec_nested_ns_shared(v: Vec<ABShared>);
c_take_const_ptr(c: *const C) -> usize177         unsafe fn c_take_const_ptr(c: *const C) -> usize;
c_take_mut_ptr(c: *mut C) -> usize178         unsafe fn c_take_mut_ptr(c: *mut C) -> usize;
179 
c_try_return_void() -> Result<()>180         fn c_try_return_void() -> Result<()>;
c_try_return_primitive() -> Result<usize>181         fn c_try_return_primitive() -> Result<usize>;
c_fail_return_primitive() -> Result<usize>182         fn c_fail_return_primitive() -> Result<usize>;
c_try_return_box() -> Result<Box<R>>183         fn c_try_return_box() -> Result<Box<R>>;
c_try_return_ref(s: &String) -> Result<&String>184         fn c_try_return_ref(s: &String) -> Result<&String>;
c_try_return_str(s: &str) -> Result<&str>185         fn c_try_return_str(s: &str) -> Result<&str>;
c_try_return_sliceu8(s: &[u8]) -> Result<&[u8]>186         fn c_try_return_sliceu8(s: &[u8]) -> Result<&[u8]>;
c_try_return_mutsliceu8(s: &mut [u8]) -> Result<&mut [u8]>187         fn c_try_return_mutsliceu8(s: &mut [u8]) -> Result<&mut [u8]>;
c_try_return_rust_string() -> Result<String>188         fn c_try_return_rust_string() -> Result<String>;
c_try_return_unique_ptr_string() -> Result<UniquePtr<CxxString>>189         fn c_try_return_unique_ptr_string() -> Result<UniquePtr<CxxString>>;
c_try_return_rust_vec() -> Result<Vec<u8>>190         fn c_try_return_rust_vec() -> Result<Vec<u8>>;
c_try_return_rust_vec_string() -> Result<Vec<String>>191         fn c_try_return_rust_vec_string() -> Result<Vec<String>>;
c_try_return_ref_rust_vec(c: &C) -> Result<&Vec<u8>>192         fn c_try_return_ref_rust_vec(c: &C) -> Result<&Vec<u8>>;
193 
get(self: &C) -> usize194         fn get(self: &C) -> usize;
set(self: Pin<&mut C>, n: usize) -> usize195         fn set(self: Pin<&mut C>, n: usize) -> usize;
get2(&self) -> usize196         fn get2(&self) -> usize;
getRef(self: &C) -> &usize197         fn getRef(self: &C) -> &usize;
getMut(self: Pin<&mut C>) -> &mut usize198         fn getMut(self: Pin<&mut C>) -> &mut usize;
set_succeed(self: Pin<&mut C>, n: usize) -> Result<usize>199         fn set_succeed(self: Pin<&mut C>, n: usize) -> Result<usize>;
get_fail(self: Pin<&mut C>) -> Result<usize>200         fn get_fail(self: Pin<&mut C>) -> Result<usize>;
c_method_on_shared(self: &Shared) -> usize201         fn c_method_on_shared(self: &Shared) -> usize;
c_method_ref_on_shared(self: &Shared) -> &usize202         fn c_method_ref_on_shared(self: &Shared) -> &usize;
c_method_mut_on_shared(self: &mut Shared) -> &mut usize203         fn c_method_mut_on_shared(self: &mut Shared) -> &mut usize;
c_set_array(self: &mut Array, value: i32)204         fn c_set_array(self: &mut Array, value: i32);
205 
c_get_use_count(weak: &WeakPtr<C>) -> usize206         fn c_get_use_count(weak: &WeakPtr<C>) -> usize;
207 
208         #[rust_name = "i32_overloaded_method"]
cOverloadedMethod(&self, x: i32) -> String209         fn cOverloadedMethod(&self, x: i32) -> String;
210         #[rust_name = "str_overloaded_method"]
cOverloadedMethod(&self, x: &str) -> String211         fn cOverloadedMethod(&self, x: &str) -> String;
212         #[rust_name = "i32_overloaded_function"]
cOverloadedFunction(x: i32) -> String213         fn cOverloadedFunction(x: i32) -> String;
214         #[rust_name = "str_overloaded_function"]
cOverloadedFunction(x: &str) -> String215         fn cOverloadedFunction(x: &str) -> String;
216 
217         #[namespace = "other"]
ns_c_take_ns_shared(shared: AShared)218         fn ns_c_take_ns_shared(shared: AShared);
219     }
220 
221     extern "C++" {
222         include!("tests/ffi/module.rs.h");
223 
224         type COwnedEnum;
225         type Job = crate::module::ffi::Job;
226     }
227 
228     extern "Rust" {
229         #[derive(ExternType)]
230         type Reference<'a>;
231     }
232 
233     unsafe extern "C++" {
234         type Borrow<'a>;
235 
c_return_borrow<'a>(s: &'a CxxString) -> UniquePtr<Borrow<'a>>236         fn c_return_borrow<'a>(s: &'a CxxString) -> UniquePtr<Borrow<'a>>;
237 
238         #[rust_name = "c_return_borrow_elided"]
c_return_borrow(s: &CxxString) -> UniquePtr<Borrow>239         fn c_return_borrow(s: &CxxString) -> UniquePtr<Borrow>;
240 
const_member(self: &Borrow)241         fn const_member(self: &Borrow);
nonconst_member(self: Pin<&mut Borrow>)242         fn nonconst_member(self: Pin<&mut Borrow>);
243     }
244 
245     #[repr(u32)]
246     #[derive(Hash)]
247     enum COwnedEnum {
248         #[cxx_name = "CVAL1"]
249         CVal1,
250         #[cxx_name = "CVAL2"]
251         CVal2,
252     }
253 
254     extern "C++" {
255         type Buffer = crate::Buffer;
256     }
257 
258     extern "Rust" {
259         type R;
260 
r_return_primitive() -> usize261         fn r_return_primitive() -> usize;
r_return_shared() -> Shared262         fn r_return_shared() -> Shared;
r_return_box() -> Box<R>263         fn r_return_box() -> Box<R>;
r_return_unique_ptr() -> UniquePtr<C>264         fn r_return_unique_ptr() -> UniquePtr<C>;
r_return_shared_ptr() -> SharedPtr<C>265         fn r_return_shared_ptr() -> SharedPtr<C>;
r_return_ref(shared: &Shared) -> &usize266         fn r_return_ref(shared: &Shared) -> &usize;
r_return_mut(shared: &mut Shared) -> &mut usize267         fn r_return_mut(shared: &mut Shared) -> &mut usize;
r_return_str(shared: &Shared) -> &str268         fn r_return_str(shared: &Shared) -> &str;
r_return_sliceu8(shared: &Shared) -> &[u8]269         fn r_return_sliceu8(shared: &Shared) -> &[u8];
r_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8]270         fn r_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8];
r_return_rust_string() -> String271         fn r_return_rust_string() -> String;
r_return_unique_ptr_string() -> UniquePtr<CxxString>272         fn r_return_unique_ptr_string() -> UniquePtr<CxxString>;
r_return_rust_vec() -> Vec<u8>273         fn r_return_rust_vec() -> Vec<u8>;
r_return_rust_vec_string() -> Vec<String>274         fn r_return_rust_vec_string() -> Vec<String>;
r_return_rust_vec_extern_struct() -> Vec<Job>275         fn r_return_rust_vec_extern_struct() -> Vec<Job>;
r_return_ref_rust_vec(shared: &Shared) -> &Vec<u8>276         fn r_return_ref_rust_vec(shared: &Shared) -> &Vec<u8>;
r_return_mut_rust_vec(shared: &mut Shared) -> &mut Vec<u8>277         fn r_return_mut_rust_vec(shared: &mut Shared) -> &mut Vec<u8>;
r_return_identity(_: usize) -> usize278         fn r_return_identity(_: usize) -> usize;
r_return_sum(_: usize, _: usize) -> usize279         fn r_return_sum(_: usize, _: usize) -> usize;
r_return_enum(n: u32) -> Enum280         fn r_return_enum(n: u32) -> Enum;
281 
r_take_primitive(n: usize)282         fn r_take_primitive(n: usize);
r_take_shared(shared: Shared)283         fn r_take_shared(shared: Shared);
r_take_box(r: Box<R>)284         fn r_take_box(r: Box<R>);
r_take_unique_ptr(c: UniquePtr<C>)285         fn r_take_unique_ptr(c: UniquePtr<C>);
r_take_shared_ptr(c: SharedPtr<C>)286         fn r_take_shared_ptr(c: SharedPtr<C>);
r_take_ref_r(r: &R)287         fn r_take_ref_r(r: &R);
r_take_ref_c(c: &C)288         fn r_take_ref_c(c: &C);
r_take_str(s: &str)289         fn r_take_str(s: &str);
r_take_slice_char(s: &[c_char])290         fn r_take_slice_char(s: &[c_char]);
r_take_rust_string(s: String)291         fn r_take_rust_string(s: String);
r_take_unique_ptr_string(s: UniquePtr<CxxString>)292         fn r_take_unique_ptr_string(s: UniquePtr<CxxString>);
r_take_ref_vector(v: &CxxVector<u8>)293         fn r_take_ref_vector(v: &CxxVector<u8>);
r_take_ref_empty_vector(v: &CxxVector<u64>)294         fn r_take_ref_empty_vector(v: &CxxVector<u64>);
r_take_rust_vec(v: Vec<u8>)295         fn r_take_rust_vec(v: Vec<u8>);
r_take_rust_vec_string(v: Vec<String>)296         fn r_take_rust_vec_string(v: Vec<String>);
r_take_ref_rust_vec(v: &Vec<u8>)297         fn r_take_ref_rust_vec(v: &Vec<u8>);
r_take_ref_rust_vec_string(v: &Vec<String>)298         fn r_take_ref_rust_vec_string(v: &Vec<String>);
r_take_enum(e: Enum)299         fn r_take_enum(e: Enum);
300 
r_try_return_void() -> Result<()>301         fn r_try_return_void() -> Result<()>;
r_try_return_primitive() -> Result<usize>302         fn r_try_return_primitive() -> Result<usize>;
r_try_return_box() -> Result<Box<R>>303         fn r_try_return_box() -> Result<Box<R>>;
r_fail_return_primitive() -> Result<usize>304         fn r_fail_return_primitive() -> Result<usize>;
r_try_return_sliceu8(s: &[u8]) -> Result<&[u8]>305         fn r_try_return_sliceu8(s: &[u8]) -> Result<&[u8]>;
r_try_return_mutsliceu8(s: &mut [u8]) -> Result<&mut [u8]>306         fn r_try_return_mutsliceu8(s: &mut [u8]) -> Result<&mut [u8]>;
307 
get(self: &R) -> usize308         fn get(self: &R) -> usize;
set(self: &mut R, n: usize) -> usize309         fn set(self: &mut R, n: usize) -> usize;
r_method_on_shared(self: &Shared) -> String310         fn r_method_on_shared(self: &Shared) -> String;
r_get_array_sum(self: &Array) -> i32311         fn r_get_array_sum(self: &Array) -> i32;
312 
313         #[cxx_name = "rAliasedFunction"]
r_aliased_function(x: i32) -> String314         fn r_aliased_function(x: i32) -> String;
315     }
316 
317     struct Dag0 {
318         i: i32,
319     }
320 
321     struct Dag1 {
322         dag2: Dag2,
323         vec: Vec<Dag3>,
324     }
325 
326     struct Dag2 {
327         dag4: Dag4,
328     }
329 
330     struct Dag3 {
331         dag1: Dag1,
332     }
333 
334     struct Dag4 {
335         dag0: Dag0,
336     }
337 
338     impl Box<Shared> {}
339     impl CxxVector<SharedString> {}
340 }
341 
342 mod other {
343     use cxx::kind::{Opaque, Trivial};
344     use cxx::{type_id, CxxString, ExternType};
345 
346     #[repr(C)]
347     pub struct D {
348         pub d: u64,
349     }
350 
351     #[repr(C)]
352     pub struct E {
353         e: u64,
354         e_str: CxxString,
355     }
356 
357     pub mod f {
358         use cxx::kind::Opaque;
359         use cxx::{type_id, CxxString, ExternType};
360 
361         #[repr(C)]
362         pub struct F {
363             e: u64,
364             e_str: CxxString,
365         }
366 
367         unsafe impl ExternType for F {
368             type Id = type_id!("F::F");
369             type Kind = Opaque;
370         }
371     }
372 
373     #[repr(C)]
374     pub struct G {
375         pub g: u64,
376     }
377 
378     unsafe impl ExternType for G {
379         type Id = type_id!("G::G");
380         type Kind = Trivial;
381     }
382 
383     unsafe impl ExternType for D {
384         type Id = type_id!("tests::D");
385         type Kind = Trivial;
386     }
387 
388     unsafe impl ExternType for E {
389         type Id = type_id!("tests::E");
390         type Kind = Opaque;
391     }
392 }
393 
394 #[derive(PartialEq, Debug)]
395 pub struct R(pub usize);
396 
397 impl R {
get(&self) -> usize398     fn get(&self) -> usize {
399         self.0
400     }
401 
set(&mut self, n: usize) -> usize402     fn set(&mut self, n: usize) -> usize {
403         self.0 = n;
404         n
405     }
406 }
407 
408 pub struct Reference<'a>(&'a String);
409 
410 impl ffi::Shared {
r_method_on_shared(&self) -> String411     fn r_method_on_shared(&self) -> String {
412         "2020".to_owned()
413     }
414 }
415 
416 impl ffi::Array {
r_get_array_sum(&self) -> i32417     pub fn r_get_array_sum(&self) -> i32 {
418         self.a.iter().sum()
419     }
420 }
421 
422 #[derive(Default)]
423 #[repr(C)]
424 pub struct Buffer([c_char; 12]);
425 
426 unsafe impl ExternType for Buffer {
427     type Id = type_id!("tests::Buffer");
428     type Kind = cxx::kind::Trivial;
429 }
430 
431 #[derive(Debug)]
432 struct Error;
433 
434 impl std::error::Error for Error {}
435 
436 impl Display for Error {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result437     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
438         f.write_str("rust error")
439     }
440 }
441 
r_return_primitive() -> usize442 fn r_return_primitive() -> usize {
443     2020
444 }
445 
r_return_shared() -> ffi::Shared446 fn r_return_shared() -> ffi::Shared {
447     ffi::Shared { z: 2020 }
448 }
449 
r_return_box() -> Box<R>450 fn r_return_box() -> Box<R> {
451     Box::new(R(2020))
452 }
453 
r_return_unique_ptr() -> UniquePtr<ffi::C>454 fn r_return_unique_ptr() -> UniquePtr<ffi::C> {
455     extern "C" {
456         fn cxx_test_suite_get_unique_ptr() -> *mut ffi::C;
457     }
458     unsafe { UniquePtr::from_raw(cxx_test_suite_get_unique_ptr()) }
459 }
460 
r_return_shared_ptr() -> SharedPtr<ffi::C>461 fn r_return_shared_ptr() -> SharedPtr<ffi::C> {
462     extern "C" {
463         fn cxx_test_suite_get_shared_ptr(repr: *mut SharedPtr<ffi::C>);
464     }
465     let mut shared_ptr = MaybeUninit::<SharedPtr<ffi::C>>::uninit();
466     let repr = shared_ptr.as_mut_ptr();
467     unsafe {
468         cxx_test_suite_get_shared_ptr(repr);
469         shared_ptr.assume_init()
470     }
471 }
472 
r_return_ref(shared: &ffi::Shared) -> &usize473 fn r_return_ref(shared: &ffi::Shared) -> &usize {
474     &shared.z
475 }
476 
r_return_mut(shared: &mut ffi::Shared) -> &mut usize477 fn r_return_mut(shared: &mut ffi::Shared) -> &mut usize {
478     &mut shared.z
479 }
480 
r_return_str(shared: &ffi::Shared) -> &str481 fn r_return_str(shared: &ffi::Shared) -> &str {
482     let _ = shared;
483     "2020"
484 }
485 
r_return_sliceu8(shared: &ffi::Shared) -> &[u8]486 fn r_return_sliceu8(shared: &ffi::Shared) -> &[u8] {
487     let _ = shared;
488     b"2020"
489 }
490 
r_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8]491 fn r_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8] {
492     slice
493 }
494 
r_return_rust_string() -> String495 fn r_return_rust_string() -> String {
496     "2020".to_owned()
497 }
498 
r_return_unique_ptr_string() -> UniquePtr<CxxString>499 fn r_return_unique_ptr_string() -> UniquePtr<CxxString> {
500     extern "C" {
501         fn cxx_test_suite_get_unique_ptr_string() -> *mut CxxString;
502     }
503     unsafe { UniquePtr::from_raw(cxx_test_suite_get_unique_ptr_string()) }
504 }
505 
r_return_rust_vec() -> Vec<u8>506 fn r_return_rust_vec() -> Vec<u8> {
507     Vec::new()
508 }
509 
r_return_rust_vec_string() -> Vec<String>510 fn r_return_rust_vec_string() -> Vec<String> {
511     Vec::new()
512 }
513 
r_return_rust_vec_extern_struct() -> Vec<ffi::Job>514 fn r_return_rust_vec_extern_struct() -> Vec<ffi::Job> {
515     Vec::new()
516 }
517 
r_return_ref_rust_vec(shared: &ffi::Shared) -> &Vec<u8>518 fn r_return_ref_rust_vec(shared: &ffi::Shared) -> &Vec<u8> {
519     let _ = shared;
520     unimplemented!()
521 }
522 
r_return_mut_rust_vec(shared: &mut ffi::Shared) -> &mut Vec<u8>523 fn r_return_mut_rust_vec(shared: &mut ffi::Shared) -> &mut Vec<u8> {
524     let _ = shared;
525     unimplemented!()
526 }
527 
r_return_identity(n: usize) -> usize528 fn r_return_identity(n: usize) -> usize {
529     n
530 }
531 
r_return_sum(n1: usize, n2: usize) -> usize532 fn r_return_sum(n1: usize, n2: usize) -> usize {
533     n1 + n2
534 }
535 
r_return_enum(n: u32) -> ffi::Enum536 fn r_return_enum(n: u32) -> ffi::Enum {
537     if n == 0 {
538         ffi::Enum::AVal
539     } else if n <= 2020 {
540         ffi::Enum::BVal
541     } else {
542         ffi::Enum::LastVal
543     }
544 }
545 
r_take_primitive(n: usize)546 fn r_take_primitive(n: usize) {
547     assert_eq!(n, 2020);
548 }
549 
r_take_shared(shared: ffi::Shared)550 fn r_take_shared(shared: ffi::Shared) {
551     assert_eq!(shared.z, 2020);
552 }
553 
r_take_box(r: Box<R>)554 fn r_take_box(r: Box<R>) {
555     let _ = r;
556 }
557 
r_take_unique_ptr(c: UniquePtr<ffi::C>)558 fn r_take_unique_ptr(c: UniquePtr<ffi::C>) {
559     let _ = c;
560 }
561 
r_take_shared_ptr(c: SharedPtr<ffi::C>)562 fn r_take_shared_ptr(c: SharedPtr<ffi::C>) {
563     let _ = c;
564 }
565 
r_take_ref_r(r: &R)566 fn r_take_ref_r(r: &R) {
567     let _ = r;
568 }
569 
r_take_ref_c(c: &ffi::C)570 fn r_take_ref_c(c: &ffi::C) {
571     let _ = c;
572 }
573 
r_take_str(s: &str)574 fn r_take_str(s: &str) {
575     assert_eq!(s, "2020");
576 }
577 
r_take_rust_string(s: String)578 fn r_take_rust_string(s: String) {
579     assert_eq!(s, "2020");
580 }
581 
r_take_slice_char(s: &[c_char])582 fn r_take_slice_char(s: &[c_char]) {
583     assert_eq!(s.len(), 5);
584     let s = cast::c_char_to_unsigned(s);
585     assert_eq!(std::str::from_utf8(s).unwrap(), "2020\0");
586 }
587 
r_take_unique_ptr_string(s: UniquePtr<CxxString>)588 fn r_take_unique_ptr_string(s: UniquePtr<CxxString>) {
589     assert_eq!(s.as_ref().unwrap().to_str().unwrap(), "2020");
590 }
591 
r_take_ref_vector(v: &CxxVector<u8>)592 fn r_take_ref_vector(v: &CxxVector<u8>) {
593     let slice = v.as_slice();
594     assert_eq!(slice, [20, 2, 0]);
595 }
596 
r_take_ref_empty_vector(v: &CxxVector<u64>)597 fn r_take_ref_empty_vector(v: &CxxVector<u64>) {
598     assert!(v.as_slice().is_empty());
599     assert!(v.is_empty());
600 }
601 
r_take_rust_vec(v: Vec<u8>)602 fn r_take_rust_vec(v: Vec<u8>) {
603     let _ = v;
604 }
605 
r_take_rust_vec_string(v: Vec<String>)606 fn r_take_rust_vec_string(v: Vec<String>) {
607     let _ = v;
608 }
609 
r_take_ref_rust_vec(v: &Vec<u8>)610 fn r_take_ref_rust_vec(v: &Vec<u8>) {
611     let _ = v;
612 }
613 
r_take_ref_rust_vec_string(v: &Vec<String>)614 fn r_take_ref_rust_vec_string(v: &Vec<String>) {
615     let _ = v;
616 }
617 
r_take_enum(e: ffi::Enum)618 fn r_take_enum(e: ffi::Enum) {
619     let _ = e;
620 }
621 
r_try_return_void() -> Result<(), Error>622 fn r_try_return_void() -> Result<(), Error> {
623     Ok(())
624 }
625 
r_try_return_primitive() -> Result<usize, Error>626 fn r_try_return_primitive() -> Result<usize, Error> {
627     Ok(2020)
628 }
629 
r_try_return_box() -> Result<Box<R>, Error>630 fn r_try_return_box() -> Result<Box<R>, Error> {
631     Ok(Box::new(R(2020)))
632 }
633 
r_fail_return_primitive() -> Result<usize, Error>634 fn r_fail_return_primitive() -> Result<usize, Error> {
635     Err(Error)
636 }
637 
r_try_return_sliceu8(slice: &[u8]) -> Result<&[u8], Error>638 fn r_try_return_sliceu8(slice: &[u8]) -> Result<&[u8], Error> {
639     Ok(slice)
640 }
641 
r_try_return_mutsliceu8(slice: &mut [u8]) -> Result<&mut [u8], Error>642 fn r_try_return_mutsliceu8(slice: &mut [u8]) -> Result<&mut [u8], Error> {
643     Ok(slice)
644 }
645 
r_aliased_function(x: i32) -> String646 fn r_aliased_function(x: i32) -> String {
647     x.to_string()
648 }
649