• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![allow(
2     clippy::boxed_local,
3     clippy::just_underscores_and_digits,
4     clippy::let_underscore_drop,
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::{CxxString, CxxVector, 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         z: usize,
47     }
48 
49     #[namespace = "A"]
50     enum AEnum {
51         AAVal,
52         ABVal = 2020,
53         ACVal,
54     }
55 
56     #[namespace = "A::B"]
57     enum ABEnum {
58         ABAVal,
59         ABBVal = 2020,
60         ABCVal,
61     }
62 
63     #[namespace = "A::B"]
64     #[derive(Clone)]
65     struct ABShared {
66         z: usize,
67     }
68 
69     #[namespace = "first"]
70     struct First {
71         second: Box<Second>,
72     }
73 
74     #[namespace = "second"]
75     #[derive(Hash)]
76     struct Second {
77         i: i32,
78         e: COwnedEnum,
79     }
80 
81     pub struct Array {
82         a: [i32; 4],
83     }
84 
85     #[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
86     pub struct StructWithLifetime<'a> {
87         s: &'a str,
88     }
89 
90     unsafe extern "C++" {
91         include!("tests/ffi/tests.h");
92 
93         type C;
94 
c_return_primitive() -> usize95         fn c_return_primitive() -> usize;
c_return_shared() -> Shared96         fn c_return_shared() -> Shared;
c_return_box() -> Box<R>97         fn c_return_box() -> Box<R>;
c_return_unique_ptr() -> UniquePtr<C>98         fn c_return_unique_ptr() -> UniquePtr<C>;
c_return_shared_ptr() -> SharedPtr<C>99         fn c_return_shared_ptr() -> SharedPtr<C>;
c_return_ref(shared: &Shared) -> &usize100         fn c_return_ref(shared: &Shared) -> &usize;
c_return_mut(shared: &mut Shared) -> &mut usize101         fn c_return_mut(shared: &mut Shared) -> &mut usize;
c_return_str(shared: &Shared) -> &str102         fn c_return_str(shared: &Shared) -> &str;
c_return_slice_char(shared: &Shared) -> &[c_char]103         fn c_return_slice_char(shared: &Shared) -> &[c_char];
c_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8]104         fn c_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8];
c_return_rust_string() -> String105         fn c_return_rust_string() -> String;
c_return_unique_ptr_string() -> UniquePtr<CxxString>106         fn c_return_unique_ptr_string() -> UniquePtr<CxxString>;
c_return_unique_ptr_vector_u8() -> UniquePtr<CxxVector<u8>>107         fn c_return_unique_ptr_vector_u8() -> UniquePtr<CxxVector<u8>>;
c_return_unique_ptr_vector_f64() -> UniquePtr<CxxVector<f64>>108         fn c_return_unique_ptr_vector_f64() -> UniquePtr<CxxVector<f64>>;
c_return_unique_ptr_vector_string() -> UniquePtr<CxxVector<CxxString>>109         fn c_return_unique_ptr_vector_string() -> UniquePtr<CxxVector<CxxString>>;
c_return_unique_ptr_vector_shared() -> UniquePtr<CxxVector<Shared>>110         fn c_return_unique_ptr_vector_shared() -> UniquePtr<CxxVector<Shared>>;
c_return_unique_ptr_vector_opaque() -> UniquePtr<CxxVector<C>>111         fn c_return_unique_ptr_vector_opaque() -> UniquePtr<CxxVector<C>>;
c_return_ref_vector(c: &C) -> &CxxVector<u8>112         fn c_return_ref_vector(c: &C) -> &CxxVector<u8>;
c_return_mut_vector(c: Pin<&mut C>) -> Pin<&mut CxxVector<u8>>113         fn c_return_mut_vector(c: Pin<&mut C>) -> Pin<&mut CxxVector<u8>>;
c_return_rust_vec() -> Vec<u8>114         fn c_return_rust_vec() -> Vec<u8>;
c_return_ref_rust_vec(c: &C) -> &Vec<u8>115         fn c_return_ref_rust_vec(c: &C) -> &Vec<u8>;
c_return_mut_rust_vec(c: Pin<&mut C>) -> &mut Vec<u8>116         fn c_return_mut_rust_vec(c: Pin<&mut C>) -> &mut Vec<u8>;
c_return_rust_vec_string() -> Vec<String>117         fn c_return_rust_vec_string() -> Vec<String>;
c_return_identity(_: usize) -> usize118         fn c_return_identity(_: usize) -> usize;
c_return_sum(_: usize, _: usize) -> usize119         fn c_return_sum(_: usize, _: usize) -> usize;
c_return_enum(n: u16) -> Enum120         fn c_return_enum(n: u16) -> Enum;
c_return_ns_ref(shared: &AShared) -> &usize121         fn c_return_ns_ref(shared: &AShared) -> &usize;
c_return_nested_ns_ref(shared: &ABShared) -> &usize122         fn c_return_nested_ns_ref(shared: &ABShared) -> &usize;
c_return_ns_enum(n: u16) -> AEnum123         fn c_return_ns_enum(n: u16) -> AEnum;
c_return_nested_ns_enum(n: u16) -> ABEnum124         fn c_return_nested_ns_enum(n: u16) -> ABEnum;
c_return_const_ptr(n: usize) -> *const C125         fn c_return_const_ptr(n: usize) -> *const C;
c_return_mut_ptr(n: usize) -> *mut C126         fn c_return_mut_ptr(n: usize) -> *mut C;
127 
c_take_primitive(n: usize)128         fn c_take_primitive(n: usize);
c_take_shared(shared: Shared)129         fn c_take_shared(shared: Shared);
c_take_box(r: Box<R>)130         fn c_take_box(r: Box<R>);
c_take_ref_r(r: &R)131         fn c_take_ref_r(r: &R);
c_take_ref_c(c: &C)132         fn c_take_ref_c(c: &C);
c_take_str(s: &str)133         fn c_take_str(s: &str);
c_take_slice_char(s: &[c_char])134         fn c_take_slice_char(s: &[c_char]);
c_take_slice_shared(s: &[Shared])135         fn c_take_slice_shared(s: &[Shared]);
c_take_slice_shared_sort(s: &mut [Shared])136         fn c_take_slice_shared_sort(s: &mut [Shared]);
c_take_slice_r(s: &[R])137         fn c_take_slice_r(s: &[R]);
c_take_slice_r_sort(s: &mut [R])138         fn c_take_slice_r_sort(s: &mut [R]);
c_take_rust_string(s: String)139         fn c_take_rust_string(s: String);
c_take_unique_ptr_string(s: UniquePtr<CxxString>)140         fn c_take_unique_ptr_string(s: UniquePtr<CxxString>);
c_take_unique_ptr_vector_u8(v: UniquePtr<CxxVector<u8>>)141         fn c_take_unique_ptr_vector_u8(v: UniquePtr<CxxVector<u8>>);
c_take_unique_ptr_vector_f64(v: UniquePtr<CxxVector<f64>>)142         fn c_take_unique_ptr_vector_f64(v: UniquePtr<CxxVector<f64>>);
c_take_unique_ptr_vector_string(v: UniquePtr<CxxVector<CxxString>>)143         fn c_take_unique_ptr_vector_string(v: UniquePtr<CxxVector<CxxString>>);
c_take_unique_ptr_vector_shared(v: UniquePtr<CxxVector<Shared>>)144         fn c_take_unique_ptr_vector_shared(v: UniquePtr<CxxVector<Shared>>);
c_take_ref_vector(v: &CxxVector<u8>)145         fn c_take_ref_vector(v: &CxxVector<u8>);
c_take_rust_vec(v: Vec<u8>)146         fn c_take_rust_vec(v: Vec<u8>);
c_take_rust_vec_shared(v: Vec<Shared>)147         fn c_take_rust_vec_shared(v: Vec<Shared>);
c_take_rust_vec_string(v: Vec<String>)148         fn c_take_rust_vec_string(v: Vec<String>);
c_take_rust_vec_index(v: Vec<u8>)149         fn c_take_rust_vec_index(v: Vec<u8>);
c_take_rust_vec_shared_index(v: Vec<Shared>)150         fn c_take_rust_vec_shared_index(v: Vec<Shared>);
c_take_rust_vec_shared_push(v: Vec<Shared>)151         fn c_take_rust_vec_shared_push(v: Vec<Shared>);
c_take_rust_vec_shared_forward_iterator(v: Vec<Shared>)152         fn c_take_rust_vec_shared_forward_iterator(v: Vec<Shared>);
c_take_rust_vec_shared_sort(v: Vec<Shared>)153         fn c_take_rust_vec_shared_sort(v: Vec<Shared>);
c_take_ref_rust_vec(v: &Vec<u8>)154         fn c_take_ref_rust_vec(v: &Vec<u8>);
c_take_ref_rust_vec_string(v: &Vec<String>)155         fn c_take_ref_rust_vec_string(v: &Vec<String>);
c_take_ref_rust_vec_index(v: &Vec<u8>)156         fn c_take_ref_rust_vec_index(v: &Vec<u8>);
c_take_ref_rust_vec_copy(v: &Vec<u8>)157         fn c_take_ref_rust_vec_copy(v: &Vec<u8>);
c_take_ref_shared_string(s: &SharedString) -> &SharedString158         fn c_take_ref_shared_string(s: &SharedString) -> &SharedString;
c_take_callback(callback: fn(String) -> usize)159         fn c_take_callback(callback: fn(String) -> usize);
c_take_enum(e: Enum)160         fn c_take_enum(e: Enum);
c_take_ns_enum(e: AEnum)161         fn c_take_ns_enum(e: AEnum);
c_take_nested_ns_enum(e: ABEnum)162         fn c_take_nested_ns_enum(e: ABEnum);
c_take_ns_shared(shared: AShared)163         fn c_take_ns_shared(shared: AShared);
c_take_nested_ns_shared(shared: ABShared)164         fn c_take_nested_ns_shared(shared: ABShared);
c_take_rust_vec_ns_shared(v: Vec<AShared>)165         fn c_take_rust_vec_ns_shared(v: Vec<AShared>);
c_take_rust_vec_nested_ns_shared(v: Vec<ABShared>)166         fn c_take_rust_vec_nested_ns_shared(v: Vec<ABShared>);
c_take_const_ptr(c: *const C) -> usize167         unsafe fn c_take_const_ptr(c: *const C) -> usize;
c_take_mut_ptr(c: *mut C) -> usize168         unsafe fn c_take_mut_ptr(c: *mut C) -> usize;
169 
c_try_return_void() -> Result<()>170         fn c_try_return_void() -> Result<()>;
c_try_return_primitive() -> Result<usize>171         fn c_try_return_primitive() -> Result<usize>;
c_fail_return_primitive() -> Result<usize>172         fn c_fail_return_primitive() -> Result<usize>;
c_try_return_box() -> Result<Box<R>>173         fn c_try_return_box() -> Result<Box<R>>;
c_try_return_ref(s: &String) -> Result<&String>174         fn c_try_return_ref(s: &String) -> Result<&String>;
c_try_return_str(s: &str) -> Result<&str>175         fn c_try_return_str(s: &str) -> Result<&str>;
c_try_return_sliceu8(s: &[u8]) -> Result<&[u8]>176         fn c_try_return_sliceu8(s: &[u8]) -> Result<&[u8]>;
c_try_return_mutsliceu8(s: &mut [u8]) -> Result<&mut [u8]>177         fn c_try_return_mutsliceu8(s: &mut [u8]) -> Result<&mut [u8]>;
c_try_return_rust_string() -> Result<String>178         fn c_try_return_rust_string() -> Result<String>;
c_try_return_unique_ptr_string() -> Result<UniquePtr<CxxString>>179         fn c_try_return_unique_ptr_string() -> Result<UniquePtr<CxxString>>;
c_try_return_rust_vec() -> Result<Vec<u8>>180         fn c_try_return_rust_vec() -> Result<Vec<u8>>;
c_try_return_rust_vec_string() -> Result<Vec<String>>181         fn c_try_return_rust_vec_string() -> Result<Vec<String>>;
c_try_return_ref_rust_vec(c: &C) -> Result<&Vec<u8>>182         fn c_try_return_ref_rust_vec(c: &C) -> Result<&Vec<u8>>;
183 
get(self: &C) -> usize184         fn get(self: &C) -> usize;
set(self: Pin<&mut C>, n: usize) -> usize185         fn set(self: Pin<&mut C>, n: usize) -> usize;
get2(&self) -> usize186         fn get2(&self) -> usize;
getRef(self: &C) -> &usize187         fn getRef(self: &C) -> &usize;
getMut(self: Pin<&mut C>) -> &mut usize188         fn getMut(self: Pin<&mut C>) -> &mut usize;
set_succeed(self: Pin<&mut C>, n: usize) -> Result<usize>189         fn set_succeed(self: Pin<&mut C>, n: usize) -> Result<usize>;
get_fail(self: Pin<&mut C>) -> Result<usize>190         fn get_fail(self: Pin<&mut C>) -> Result<usize>;
c_method_on_shared(self: &Shared) -> usize191         fn c_method_on_shared(self: &Shared) -> usize;
c_method_ref_on_shared(self: &Shared) -> &usize192         fn c_method_ref_on_shared(self: &Shared) -> &usize;
c_method_mut_on_shared(self: &mut Shared) -> &mut usize193         fn c_method_mut_on_shared(self: &mut Shared) -> &mut usize;
c_set_array(self: &mut Array, value: i32)194         fn c_set_array(self: &mut Array, value: i32);
195 
c_get_use_count(weak: &WeakPtr<C>) -> usize196         fn c_get_use_count(weak: &WeakPtr<C>) -> usize;
197 
198         #[rust_name = "i32_overloaded_method"]
cOverloadedMethod(&self, x: i32) -> String199         fn cOverloadedMethod(&self, x: i32) -> String;
200         #[rust_name = "str_overloaded_method"]
cOverloadedMethod(&self, x: &str) -> String201         fn cOverloadedMethod(&self, x: &str) -> String;
202         #[rust_name = "i32_overloaded_function"]
cOverloadedFunction(x: i32) -> String203         fn cOverloadedFunction(x: i32) -> String;
204         #[rust_name = "str_overloaded_function"]
cOverloadedFunction(x: &str) -> String205         fn cOverloadedFunction(x: &str) -> String;
206 
207         #[namespace = "other"]
ns_c_take_ns_shared(shared: AShared)208         fn ns_c_take_ns_shared(shared: AShared);
209     }
210 
211     extern "C++" {
212         include!("tests/ffi/module.rs.h");
213 
214         type COwnedEnum;
215         type Job = crate::module::ffi::Job;
216     }
217 
218     extern "Rust" {
219         #[derive(ExternType)]
220         type Reference<'a>;
221     }
222 
223     unsafe extern "C++" {
224         type Borrow<'a>;
225 
c_return_borrow<'a>(s: &'a CxxString) -> UniquePtr<Borrow<'a>>226         fn c_return_borrow<'a>(s: &'a CxxString) -> UniquePtr<Borrow<'a>>;
227 
228         #[rust_name = "c_return_borrow_elided"]
c_return_borrow(s: &CxxString) -> UniquePtr<Borrow>229         fn c_return_borrow(s: &CxxString) -> UniquePtr<Borrow>;
230 
const_member(self: &Borrow)231         fn const_member(self: &Borrow);
nonconst_member(self: Pin<&mut Borrow>)232         fn nonconst_member(self: Pin<&mut Borrow>);
233     }
234 
235     #[repr(u32)]
236     #[derive(Hash)]
237     enum COwnedEnum {
238         #[cxx_name = "CVAL1"]
239         CVal1,
240         #[cxx_name = "CVAL2"]
241         CVal2,
242     }
243 
244     extern "Rust" {
245         type R;
246 
r_return_primitive() -> usize247         fn r_return_primitive() -> usize;
r_return_shared() -> Shared248         fn r_return_shared() -> Shared;
r_return_box() -> Box<R>249         fn r_return_box() -> Box<R>;
r_return_unique_ptr() -> UniquePtr<C>250         fn r_return_unique_ptr() -> UniquePtr<C>;
r_return_shared_ptr() -> SharedPtr<C>251         fn r_return_shared_ptr() -> SharedPtr<C>;
r_return_ref(shared: &Shared) -> &usize252         fn r_return_ref(shared: &Shared) -> &usize;
r_return_mut(shared: &mut Shared) -> &mut usize253         fn r_return_mut(shared: &mut Shared) -> &mut usize;
r_return_str(shared: &Shared) -> &str254         fn r_return_str(shared: &Shared) -> &str;
r_return_sliceu8(shared: &Shared) -> &[u8]255         fn r_return_sliceu8(shared: &Shared) -> &[u8];
r_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8]256         fn r_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8];
r_return_rust_string() -> String257         fn r_return_rust_string() -> String;
r_return_unique_ptr_string() -> UniquePtr<CxxString>258         fn r_return_unique_ptr_string() -> UniquePtr<CxxString>;
r_return_rust_vec() -> Vec<u8>259         fn r_return_rust_vec() -> Vec<u8>;
r_return_rust_vec_string() -> Vec<String>260         fn r_return_rust_vec_string() -> Vec<String>;
r_return_rust_vec_extern_struct() -> Vec<Job>261         fn r_return_rust_vec_extern_struct() -> Vec<Job>;
r_return_ref_rust_vec(shared: &Shared) -> &Vec<u8>262         fn r_return_ref_rust_vec(shared: &Shared) -> &Vec<u8>;
r_return_mut_rust_vec(shared: &mut Shared) -> &mut Vec<u8>263         fn r_return_mut_rust_vec(shared: &mut Shared) -> &mut Vec<u8>;
r_return_identity(_: usize) -> usize264         fn r_return_identity(_: usize) -> usize;
r_return_sum(_: usize, _: usize) -> usize265         fn r_return_sum(_: usize, _: usize) -> usize;
r_return_enum(n: u32) -> Enum266         fn r_return_enum(n: u32) -> Enum;
267 
r_take_primitive(n: usize)268         fn r_take_primitive(n: usize);
r_take_shared(shared: Shared)269         fn r_take_shared(shared: Shared);
r_take_box(r: Box<R>)270         fn r_take_box(r: Box<R>);
r_take_unique_ptr(c: UniquePtr<C>)271         fn r_take_unique_ptr(c: UniquePtr<C>);
r_take_shared_ptr(c: SharedPtr<C>)272         fn r_take_shared_ptr(c: SharedPtr<C>);
r_take_ref_r(r: &R)273         fn r_take_ref_r(r: &R);
r_take_ref_c(c: &C)274         fn r_take_ref_c(c: &C);
r_take_str(s: &str)275         fn r_take_str(s: &str);
r_take_slice_char(s: &[c_char])276         fn r_take_slice_char(s: &[c_char]);
r_take_rust_string(s: String)277         fn r_take_rust_string(s: String);
r_take_unique_ptr_string(s: UniquePtr<CxxString>)278         fn r_take_unique_ptr_string(s: UniquePtr<CxxString>);
r_take_ref_vector(v: &CxxVector<u8>)279         fn r_take_ref_vector(v: &CxxVector<u8>);
r_take_ref_empty_vector(v: &CxxVector<u64>)280         fn r_take_ref_empty_vector(v: &CxxVector<u64>);
r_take_rust_vec(v: Vec<u8>)281         fn r_take_rust_vec(v: Vec<u8>);
r_take_rust_vec_string(v: Vec<String>)282         fn r_take_rust_vec_string(v: Vec<String>);
r_take_ref_rust_vec(v: &Vec<u8>)283         fn r_take_ref_rust_vec(v: &Vec<u8>);
r_take_ref_rust_vec_string(v: &Vec<String>)284         fn r_take_ref_rust_vec_string(v: &Vec<String>);
r_take_enum(e: Enum)285         fn r_take_enum(e: Enum);
286 
r_try_return_void() -> Result<()>287         fn r_try_return_void() -> Result<()>;
r_try_return_primitive() -> Result<usize>288         fn r_try_return_primitive() -> Result<usize>;
r_try_return_box() -> Result<Box<R>>289         fn r_try_return_box() -> Result<Box<R>>;
r_fail_return_primitive() -> Result<usize>290         fn r_fail_return_primitive() -> Result<usize>;
r_try_return_sliceu8(s: &[u8]) -> Result<&[u8]>291         fn r_try_return_sliceu8(s: &[u8]) -> Result<&[u8]>;
r_try_return_mutsliceu8(s: &mut [u8]) -> Result<&mut [u8]>292         fn r_try_return_mutsliceu8(s: &mut [u8]) -> Result<&mut [u8]>;
293 
get(self: &R) -> usize294         fn get(self: &R) -> usize;
set(self: &mut R, n: usize) -> usize295         fn set(self: &mut R, n: usize) -> usize;
r_method_on_shared(self: &Shared) -> String296         fn r_method_on_shared(self: &Shared) -> String;
r_get_array_sum(self: &Array) -> i32297         fn r_get_array_sum(self: &Array) -> i32;
298 
299         #[cxx_name = "rAliasedFunction"]
r_aliased_function(x: i32) -> String300         fn r_aliased_function(x: i32) -> String;
301     }
302 
303     struct Dag0 {
304         i: i32,
305     }
306 
307     struct Dag1 {
308         dag2: Dag2,
309         vec: Vec<Dag3>,
310     }
311 
312     struct Dag2 {
313         dag4: Dag4,
314     }
315 
316     struct Dag3 {
317         dag1: Dag1,
318     }
319 
320     struct Dag4 {
321         dag0: Dag0,
322     }
323 
324     impl Box<Shared> {}
325 }
326 
327 mod other {
328     use cxx::kind::{Opaque, Trivial};
329     use cxx::{type_id, CxxString, ExternType};
330 
331     #[repr(C)]
332     pub struct D {
333         pub d: u64,
334     }
335 
336     #[repr(C)]
337     pub struct E {
338         e: u64,
339         e_str: CxxString,
340     }
341 
342     pub mod f {
343         use cxx::kind::Opaque;
344         use cxx::{type_id, CxxString, ExternType};
345 
346         #[repr(C)]
347         pub struct F {
348             e: u64,
349             e_str: CxxString,
350         }
351 
352         unsafe impl ExternType for F {
353             type Id = type_id!("F::F");
354             type Kind = Opaque;
355         }
356     }
357 
358     #[repr(C)]
359     pub struct G {
360         pub g: u64,
361     }
362 
363     unsafe impl ExternType for G {
364         type Id = type_id!("G::G");
365         type Kind = Trivial;
366     }
367 
368     unsafe impl ExternType for D {
369         type Id = type_id!("tests::D");
370         type Kind = Trivial;
371     }
372 
373     unsafe impl ExternType for E {
374         type Id = type_id!("tests::E");
375         type Kind = Opaque;
376     }
377 }
378 
379 #[derive(PartialEq, Debug)]
380 pub struct R(pub usize);
381 
382 impl R {
get(&self) -> usize383     fn get(&self) -> usize {
384         self.0
385     }
386 
set(&mut self, n: usize) -> usize387     fn set(&mut self, n: usize) -> usize {
388         self.0 = n;
389         n
390     }
391 }
392 
393 pub struct Reference<'a>(&'a String);
394 
395 impl ffi::Shared {
r_method_on_shared(&self) -> String396     fn r_method_on_shared(&self) -> String {
397         "2020".to_owned()
398     }
399 }
400 
401 impl ffi::Array {
r_get_array_sum(&self) -> i32402     pub fn r_get_array_sum(&self) -> i32 {
403         self.a.iter().sum()
404     }
405 }
406 
407 #[derive(Debug)]
408 struct Error;
409 
410 impl std::error::Error for Error {}
411 
412 impl Display for Error {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result413     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
414         f.write_str("rust error")
415     }
416 }
417 
r_return_primitive() -> usize418 fn r_return_primitive() -> usize {
419     2020
420 }
421 
r_return_shared() -> ffi::Shared422 fn r_return_shared() -> ffi::Shared {
423     ffi::Shared { z: 2020 }
424 }
425 
r_return_box() -> Box<R>426 fn r_return_box() -> Box<R> {
427     Box::new(R(2020))
428 }
429 
r_return_unique_ptr() -> UniquePtr<ffi::C>430 fn r_return_unique_ptr() -> UniquePtr<ffi::C> {
431     extern "C" {
432         fn cxx_test_suite_get_unique_ptr() -> *mut ffi::C;
433     }
434     unsafe { UniquePtr::from_raw(cxx_test_suite_get_unique_ptr()) }
435 }
436 
r_return_shared_ptr() -> SharedPtr<ffi::C>437 fn r_return_shared_ptr() -> SharedPtr<ffi::C> {
438     extern "C" {
439         fn cxx_test_suite_get_shared_ptr(repr: *mut SharedPtr<ffi::C>);
440     }
441     let mut shared_ptr = MaybeUninit::<SharedPtr<ffi::C>>::uninit();
442     let repr = shared_ptr.as_mut_ptr();
443     unsafe {
444         cxx_test_suite_get_shared_ptr(repr);
445         shared_ptr.assume_init()
446     }
447 }
448 
r_return_ref(shared: &ffi::Shared) -> &usize449 fn r_return_ref(shared: &ffi::Shared) -> &usize {
450     &shared.z
451 }
452 
r_return_mut(shared: &mut ffi::Shared) -> &mut usize453 fn r_return_mut(shared: &mut ffi::Shared) -> &mut usize {
454     &mut shared.z
455 }
456 
r_return_str(shared: &ffi::Shared) -> &str457 fn r_return_str(shared: &ffi::Shared) -> &str {
458     let _ = shared;
459     "2020"
460 }
461 
r_return_sliceu8(shared: &ffi::Shared) -> &[u8]462 fn r_return_sliceu8(shared: &ffi::Shared) -> &[u8] {
463     let _ = shared;
464     b"2020"
465 }
466 
r_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8]467 fn r_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8] {
468     slice
469 }
470 
r_return_rust_string() -> String471 fn r_return_rust_string() -> String {
472     "2020".to_owned()
473 }
474 
r_return_unique_ptr_string() -> UniquePtr<CxxString>475 fn r_return_unique_ptr_string() -> UniquePtr<CxxString> {
476     extern "C" {
477         fn cxx_test_suite_get_unique_ptr_string() -> *mut CxxString;
478     }
479     unsafe { UniquePtr::from_raw(cxx_test_suite_get_unique_ptr_string()) }
480 }
481 
r_return_rust_vec() -> Vec<u8>482 fn r_return_rust_vec() -> Vec<u8> {
483     Vec::new()
484 }
485 
r_return_rust_vec_string() -> Vec<String>486 fn r_return_rust_vec_string() -> Vec<String> {
487     Vec::new()
488 }
489 
r_return_rust_vec_extern_struct() -> Vec<ffi::Job>490 fn r_return_rust_vec_extern_struct() -> Vec<ffi::Job> {
491     Vec::new()
492 }
493 
r_return_ref_rust_vec(shared: &ffi::Shared) -> &Vec<u8>494 fn r_return_ref_rust_vec(shared: &ffi::Shared) -> &Vec<u8> {
495     let _ = shared;
496     unimplemented!()
497 }
498 
r_return_mut_rust_vec(shared: &mut ffi::Shared) -> &mut Vec<u8>499 fn r_return_mut_rust_vec(shared: &mut ffi::Shared) -> &mut Vec<u8> {
500     let _ = shared;
501     unimplemented!()
502 }
503 
r_return_identity(n: usize) -> usize504 fn r_return_identity(n: usize) -> usize {
505     n
506 }
507 
r_return_sum(n1: usize, n2: usize) -> usize508 fn r_return_sum(n1: usize, n2: usize) -> usize {
509     n1 + n2
510 }
511 
r_return_enum(n: u32) -> ffi::Enum512 fn r_return_enum(n: u32) -> ffi::Enum {
513     if n == 0 {
514         ffi::Enum::AVal
515     } else if n <= 2020 {
516         ffi::Enum::BVal
517     } else {
518         ffi::Enum::LastVal
519     }
520 }
521 
r_take_primitive(n: usize)522 fn r_take_primitive(n: usize) {
523     assert_eq!(n, 2020);
524 }
525 
r_take_shared(shared: ffi::Shared)526 fn r_take_shared(shared: ffi::Shared) {
527     assert_eq!(shared.z, 2020);
528 }
529 
r_take_box(r: Box<R>)530 fn r_take_box(r: Box<R>) {
531     let _ = r;
532 }
533 
r_take_unique_ptr(c: UniquePtr<ffi::C>)534 fn r_take_unique_ptr(c: UniquePtr<ffi::C>) {
535     let _ = c;
536 }
537 
r_take_shared_ptr(c: SharedPtr<ffi::C>)538 fn r_take_shared_ptr(c: SharedPtr<ffi::C>) {
539     let _ = c;
540 }
541 
r_take_ref_r(r: &R)542 fn r_take_ref_r(r: &R) {
543     let _ = r;
544 }
545 
r_take_ref_c(c: &ffi::C)546 fn r_take_ref_c(c: &ffi::C) {
547     let _ = c;
548 }
549 
r_take_str(s: &str)550 fn r_take_str(s: &str) {
551     assert_eq!(s, "2020");
552 }
553 
r_take_rust_string(s: String)554 fn r_take_rust_string(s: String) {
555     assert_eq!(s, "2020");
556 }
557 
r_take_slice_char(s: &[c_char])558 fn r_take_slice_char(s: &[c_char]) {
559     assert_eq!(s.len(), 5);
560     let s = cast::c_char_to_unsigned(s);
561     assert_eq!(std::str::from_utf8(s).unwrap(), "2020\0");
562 }
563 
r_take_unique_ptr_string(s: UniquePtr<CxxString>)564 fn r_take_unique_ptr_string(s: UniquePtr<CxxString>) {
565     assert_eq!(s.as_ref().unwrap().to_str().unwrap(), "2020");
566 }
567 
r_take_ref_vector(v: &CxxVector<u8>)568 fn r_take_ref_vector(v: &CxxVector<u8>) {
569     let slice = v.as_slice();
570     assert_eq!(slice, [20, 2, 0]);
571 }
572 
r_take_ref_empty_vector(v: &CxxVector<u64>)573 fn r_take_ref_empty_vector(v: &CxxVector<u64>) {
574     assert!(v.as_slice().is_empty());
575     assert!(v.is_empty());
576 }
577 
r_take_rust_vec(v: Vec<u8>)578 fn r_take_rust_vec(v: Vec<u8>) {
579     let _ = v;
580 }
581 
r_take_rust_vec_string(v: Vec<String>)582 fn r_take_rust_vec_string(v: Vec<String>) {
583     let _ = v;
584 }
585 
r_take_ref_rust_vec(v: &Vec<u8>)586 fn r_take_ref_rust_vec(v: &Vec<u8>) {
587     let _ = v;
588 }
589 
r_take_ref_rust_vec_string(v: &Vec<String>)590 fn r_take_ref_rust_vec_string(v: &Vec<String>) {
591     let _ = v;
592 }
593 
r_take_enum(e: ffi::Enum)594 fn r_take_enum(e: ffi::Enum) {
595     let _ = e;
596 }
597 
r_try_return_void() -> Result<(), Error>598 fn r_try_return_void() -> Result<(), Error> {
599     Ok(())
600 }
601 
r_try_return_primitive() -> Result<usize, Error>602 fn r_try_return_primitive() -> Result<usize, Error> {
603     Ok(2020)
604 }
605 
r_try_return_box() -> Result<Box<R>, Error>606 fn r_try_return_box() -> Result<Box<R>, Error> {
607     Ok(Box::new(R(2020)))
608 }
609 
r_fail_return_primitive() -> Result<usize, Error>610 fn r_fail_return_primitive() -> Result<usize, Error> {
611     Err(Error)
612 }
613 
r_try_return_sliceu8(slice: &[u8]) -> Result<&[u8], Error>614 fn r_try_return_sliceu8(slice: &[u8]) -> Result<&[u8], Error> {
615     Ok(slice)
616 }
617 
r_try_return_mutsliceu8(slice: &mut [u8]) -> Result<&mut [u8], Error>618 fn r_try_return_mutsliceu8(slice: &mut [u8]) -> Result<&mut [u8], Error> {
619     Ok(slice)
620 }
621 
r_aliased_function(x: i32) -> String622 fn r_aliased_function(x: i32) -> String {
623     x.to_string()
624 }
625