Add test of cxx_/rust_name attributes

This commit is contained in:
David Tolnay 2020-10-09 19:29:44 -07:00
parent 938ca85856
commit 3bbcdbbbf6
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
4 changed files with 48 additions and 0 deletions

View File

@ -101,6 +101,15 @@ pub mod ffi {
fn set2(&mut self, n: usize) -> usize;
fn set_succeed(&mut self, n: usize) -> Result<usize>;
fn get_fail(&mut self) -> Result<usize>;
#[rust_name = "i32_overloaded_method"]
fn cOverloadedMethod(&self, x: i32) -> String;
#[rust_name = "str_overloaded_method"]
fn cOverloadedMethod(&self, x: &str) -> String;
#[rust_name = "i32_overloaded_function"]
fn cOverloadedFunction(x: i32) -> String;
#[rust_name = "str_overloaded_function"]
fn cOverloadedFunction(x: &str) -> String;
}
extern "C" {
@ -160,6 +169,9 @@ pub mod ffi {
fn r_return_r2(n: usize) -> Box<R2>;
fn get(self: &R2) -> usize;
fn set(self: &mut R2, n: usize) -> usize;
#[cxx_name = "rAliasedFunction"]
fn r_aliased_function(x: i32) -> String;
}
}
@ -358,3 +370,7 @@ fn r_fail_return_primitive() -> Result<usize, Error> {
fn r_return_r2(n: usize) -> Box<R2> {
Box::new(R2(n))
}
fn r_aliased_function(x: i32) -> String {
x.to_string()
}

View File

@ -365,6 +365,22 @@ extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
return std::unique_ptr<std::string>(new std::string("2020")).release();
}
rust::String C::cOverloadedMethod(int32_t x) const {
return rust::String(std::to_string(x));
}
rust::String C::cOverloadedMethod(rust::Str x) const {
return rust::String(std::string(x));
}
rust::String cOverloadedFunction(int x) {
return rust::String(std::to_string(x));
}
rust::String cOverloadedFunction(rust::Str x) {
return rust::String(std::string(x));
}
extern "C" const char *cxx_run_test() noexcept {
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
@ -421,6 +437,8 @@ extern "C" const char *cxx_run_test() noexcept {
ASSERT(r2->set(2020) == 2020);
ASSERT(r2->get() == 2020);
ASSERT(std::string(rAliasedFunction(2020)) == "2020");
cxx_test_suite_set_correct();
return nullptr;
}

View File

@ -20,6 +20,8 @@ public:
size_t get_fail();
const std::vector<uint8_t> &get_v() const;
std::vector<uint8_t> &get_v();
rust::String cOverloadedMethod(int32_t x) const;
rust::String cOverloadedMethod(rust::Str x) const;
private:
size_t n;
@ -101,4 +103,7 @@ rust::Vec<uint8_t> c_try_return_rust_vec();
rust::Vec<rust::String> c_try_return_rust_vec_string();
const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c);
rust::String cOverloadedFunction(int32_t x);
rust::String cOverloadedFunction(rust::Str x);
} // namespace tests

View File

@ -181,3 +181,12 @@ extern "C" fn cxx_test_suite_get_box() -> *mut cxx_test_suite::R {
unsafe extern "C" fn cxx_test_suite_r_is_correct(r: *const cxx_test_suite::R) -> bool {
*r == 2020
}
#[test]
fn test_rust_name_attribute() {
assert_eq!("2020", ffi::i32_overloaded_function(2020));
assert_eq!("2020", ffi::str_overloaded_function("2020"));
let unique_ptr = ffi::c_return_unique_ptr();
assert_eq!("2020", unique_ptr.i32_overloaded_method(2020));
assert_eq!("2020", unique_ptr.str_overloaded_method("2020"));
}