Skip to content

Commit 464aeeb

Browse files
committed
Rust-defined methods on shared structs
1 parent 102c7ea commit 464aeeb

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

tests/ffi/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ pub mod ffi {
202202
fn set2(&mut self, n: usize) -> usize;
203203
fn set_succeed(&mut self, n: usize) -> Result<usize>;
204204
fn get_fail(&mut self) -> Result<usize>;
205-
fn method_on_shared(self: &Shared) -> usize;
205+
fn c_method_on_shared(self: &Shared) -> usize;
206206

207207
#[rust_name = "i32_overloaded_method"]
208208
fn cOverloadedMethod(&self, x: i32) -> String;
@@ -274,6 +274,7 @@ pub mod ffi {
274274
fn r_return_r2(n: usize) -> Box<R2>;
275275
fn get(self: &R2) -> usize;
276276
fn set(self: &mut R2, n: usize) -> usize;
277+
fn r_method_on_shared(self: &Shared) -> usize;
277278

278279
#[cxx_name = "rAliasedFunction"]
279280
fn r_aliased_function(x: i32) -> String;
@@ -316,6 +317,12 @@ impl R2 {
316317
}
317318
}
318319

320+
impl ffi::Shared {
321+
fn r_method_on_shared(&self) -> usize {
322+
2020
323+
}
324+
}
325+
319326
#[derive(Debug)]
320327
struct Error;
321328

tests/ffi/tests.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ size_t C::set_succeed(size_t n) { return this->set2(n); }
3535

3636
size_t C::get_fail() { throw std::runtime_error("unimplemented"); }
3737

38-
size_t Shared::method_on_shared() const noexcept { return 2021; }
38+
size_t Shared::c_method_on_shared() const noexcept { return 2021; }
3939

4040
const std::vector<uint8_t> &C::get_v() const { return this->v; }
4141

@@ -626,6 +626,7 @@ extern "C" const char *cxx_run_test() noexcept {
626626
ASSERT(r2->get() == 2021);
627627
ASSERT(r2->set(2020) == 2020);
628628
ASSERT(r2->get() == 2020);
629+
ASSERT(Shared{0}.r_method_on_shared() == 2020);
629630

630631
ASSERT(std::string(rAliasedFunction(2020)) == "2020");
631632

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ fn test_c_method_calls() {
193193
assert_eq!(old_value, unique_ptr.get2());
194194
assert_eq!(2022, unique_ptr.set_succeed(2022).unwrap());
195195
assert!(unique_ptr.get_fail().is_err());
196-
assert_eq!(2021, ffi::Shared { z: 0 }.method_on_shared());
196+
assert_eq!(2021, ffi::Shared { z: 0 }.c_method_on_shared());
197197
}
198198

199199
#[test]

0 commit comments

Comments
 (0)