mirror of
https://gitee.com/openharmony/third_party_rust_cxx
synced 2024-11-23 23:29:50 +00:00
C++-defined methods on shared structs
This commit is contained in:
parent
2e159920da
commit
102c7eaf68
@ -66,7 +66,7 @@ fn write_forward_declarations(out: &mut OutFile, apis: &[Api]) {
|
||||
fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
|
||||
let mut methods_for_type = HashMap::new();
|
||||
for api in apis {
|
||||
if let Api::RustFunction(efn) = api {
|
||||
if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
|
||||
if let Some(receiver) = &efn.sig.receiver {
|
||||
methods_for_type
|
||||
.entry(&receiver.ty.rust)
|
||||
@ -84,7 +84,11 @@ fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
|
||||
for next in &mut toposorted_structs {
|
||||
if !out.types.cxx.contains(&strct.name.rust) {
|
||||
out.next_section();
|
||||
write_struct(out, next);
|
||||
let methods = methods_for_type
|
||||
.get(&strct.name.rust)
|
||||
.map(Vec::as_slice)
|
||||
.unwrap_or_default();
|
||||
write_struct(out, next, methods);
|
||||
}
|
||||
structs_written.insert(&next.name.rust);
|
||||
if next.name.rust == strct.name.rust {
|
||||
@ -173,7 +177,7 @@ fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
|
||||
}
|
||||
}
|
||||
|
||||
fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
|
||||
fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct, methods: &[&ExternFn]) {
|
||||
out.set_namespace(&strct.name.namespace);
|
||||
let guard = format!("CXXBRIDGE05_STRUCT_{}", strct.name.to_symbol());
|
||||
writeln!(out, "#ifndef {}", guard);
|
||||
@ -187,6 +191,16 @@ fn write_struct<'a>(out: &mut OutFile<'a>, strct: &'a Struct) {
|
||||
write_type_space(out, &field.ty);
|
||||
writeln!(out, "{};", field.ident);
|
||||
}
|
||||
if !methods.is_empty() {
|
||||
writeln!(out);
|
||||
}
|
||||
for method in methods {
|
||||
write!(out, " ");
|
||||
let sig = &method.sig;
|
||||
let local_name = method.name.cxx.to_string();
|
||||
write_rust_function_shim_decl(out, &local_name, sig, false);
|
||||
writeln!(out, ";");
|
||||
}
|
||||
writeln!(out, "}};");
|
||||
writeln!(out, "#endif // {}", guard);
|
||||
}
|
||||
|
@ -202,6 +202,7 @@ 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>;
|
||||
fn method_on_shared(self: &Shared) -> usize;
|
||||
|
||||
#[rust_name = "i32_overloaded_method"]
|
||||
fn cOverloadedMethod(&self, x: i32) -> String;
|
||||
|
@ -35,6 +35,8 @@ size_t C::set_succeed(size_t n) { return this->set2(n); }
|
||||
|
||||
size_t C::get_fail() { throw std::runtime_error("unimplemented"); }
|
||||
|
||||
size_t Shared::method_on_shared() const noexcept { return 2021; }
|
||||
|
||||
const std::vector<uint8_t> &C::get_v() const { return this->v; }
|
||||
|
||||
std::vector<uint8_t> &C::get_v() { return this->v; }
|
||||
|
@ -193,6 +193,7 @@ fn test_c_method_calls() {
|
||||
assert_eq!(old_value, unique_ptr.get2());
|
||||
assert_eq!(2022, unique_ptr.set_succeed(2022).unwrap());
|
||||
assert!(unique_ptr.get_fail().is_err());
|
||||
assert_eq!(2021, ffi::Shared { z: 0 }.method_on_shared());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user