diff --git a/README.md b/README.md index f1b65c4..a3d4563 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ [docs.rs](https://docs.rs/zngur) [build status](https://github.com/hkalbasi/zngur/actions?query=branch%3Amain) - Zngur (/zængɑr/) is a C++/Rust interop tool. It tries to expose arbitrary Rust types, methods and functions, while preserving its semantics and ergonomics as much as possible. Using Zngur, you can use arbitrary Rust crate in your C++ code as easily as using it in normal Rust code, and you can write idiomatic Rusty API for your C++ library inside C++. See [the documentation](https://hkalbasi.github.io/zngur/) @@ -66,7 +65,7 @@ int main() { } int state = 0; // You can convert a C++ lambda into a `Box` and friends. - auto f = BoxDyn>::build([&](int32_t x) { + auto f = BoxDyn>::make_box([&](int32_t x) { state += x; std::cout << "hello " << x << " " << state << "\n"; return x * 2; diff --git a/book/src/zngur.md b/book/src/zngur.md index f31769a..87d6ed7 100644 --- a/book/src/zngur.md +++ b/book/src/zngur.md @@ -86,7 +86,7 @@ int main() { } int state = 0; // You can convert a C++ lambda into a `Box` and friends. - auto f = BoxDyn>::build([&](int32_t x) { + auto f = BoxDyn>::make_box([&](int32_t x) { state += x; std::cout << "hello " << x << " " << state << "\n"; return x * 2; diff --git a/examples/memory_management/main.cpp b/examples/memory_management/main.cpp index ae19f7e..3058e63 100644 --- a/examples/memory_management/main.cpp +++ b/examples/memory_management/main.cpp @@ -72,7 +72,7 @@ int main() { CppPrintOnDropHolder c; { std::cout << "Checkpoint 15" << std::endl; - auto holder = RmDyn::build(c); + auto holder = RmDyn(c); std::cout << "Checkpoint 16" << std::endl; consume_n_times(holder, rust::Str::from_char_star("P2"), 3); std::cout << "Checkpoint 17" << std::endl; diff --git a/examples/memory_management/out.txt b/examples/memory_management/out.txt new file mode 100644 index 0000000..8af0eff --- /dev/null +++ b/examples/memory_management/out.txt @@ -0,0 +1,59 @@ +Checkpoint 1 +PrintOnDrop(B) has been dropped +Checkpoint 2 +Checkpoint 3 +Checkpoint 4 +PrintOnDrop(A) has been dropped +Checkpoint 5 +PrintOnDrop(cpp_V3) has been dropped +Checkpoint 6 +PrintOnDrop(cpp_V1) has been dropped +PrintOnDrop(cpp_V2) has been dropped +Checkpoint 7 +Checkpoint 8 +PrintOnDrop(rust_V1) has been dropped +PrintOnDrop(rust_V2) has been dropped +Checkpoint 9 +PrintOnDrop(rust_V1) has been dropped +PrintOnDrop(rust_V2) has been dropped +Checkpoint 10 +Checkpoint 11 +PrintOnDrop(P) has been dropped +PrintOnDrop(P) has been dropped +Checkpoint 12 +PrintOnDrop(P) has been dropped +PrintOnDrop(Q) has been dropped +Checkpoint 13 +PrintOnDrop(Q) has been dropped +Checkpoint 14 +Checkpoint 15 +Checkpoint 16 +PrintOnDrop(P2) has been dropped +PrintOnDrop(P2) has been dropped +Checkpoint 17 +PrintOnDrop(P2) has been dropped +PrintOnDrop(Q2) has been dropped +Checkpoint 18 +Checkpoint 19 +PrintOnDrop(Q2) has been dropped +Checkpoint 20 +Checkpoint 21 +PrintOnDrop(A) has been dropped +Checkpoint 22 +PrintOnDrop(B) has been dropped +PrintOnDrop(A) has been dropped +Checkpoint 24 +Checkpoint 25 +Checkpoint 26 +PrintOnDrop(first) has been dropped +PrintOnDrop(second) has been dropped +Checkpoint 27 +Checkpoint 28 +PrintOnDrop(elem1) has been dropped +Checkpoint 29 +PrintOnDrop(elem1) has been dropped +Checkpoint 30 +PrintOnDrop(elem1) has been dropped +PrintOnDrop(elem2) has been dropped +Checkpoint 31 +PrintOnDrop(C) has been dropped diff --git a/examples/osmium/impl.cpp b/examples/osmium/impl.cpp index 72aa2f6..19651d8 100644 --- a/examples/osmium/impl.cpp +++ b/examples/osmium/impl.cpp @@ -23,10 +23,7 @@ class RustHandler : public osmium::handler::Handler { BendHandler inner; public: - void way(osmium::Way &way) { - auto rusty_way = rust::Ref::build(way); - inner.way(rusty_way); - } + void way(osmium::Way &way) { inner.way(way); } RustHandler(BendHandler &&inner) : inner(std::move(inner)) {} }; @@ -45,11 +42,11 @@ rust::Tuple<> rust::exported_functions::apply(rust::Ref reader, } rust::Ref rust::Impl::nodes(rust::Ref self) { - return rust::Ref::build(self.cpp().nodes()); + return self.cpp().nodes(); } rust::Ref rust::Impl::tags(rust::Ref self) { - return rust::Ref::build(self.cpp().tags()); + return self.cpp().tags(); } rust::std::option::Option> @@ -71,7 +68,7 @@ size_t rust::Impl::len(rust::Ref self) { rust::Ref rust::Impl>::index( rust::Ref self, size_t i) { - return rust::Ref::build(self.cpp()[i]); + return self.cpp()[i]; } double rust::Impl::distance(rust::Ref self, rust::Ref other) { diff --git a/examples/simple/main.cpp b/examples/simple/main.cpp index 5d91d5c..7bee961 100644 --- a/examples/simple/main.cpp +++ b/examples/simple/main.cpp @@ -50,7 +50,7 @@ int main() { } int state = 0; // You can convert a C++ lambda into a `Box` and friends. - auto f = BoxDyn>::build([&](int32_t x) { + auto f = BoxDyn>::make_box([&](int32_t x) { state += x; std::cout << "hello " << x << " " << state << "\n"; return x * 2; diff --git a/zngur-generator/src/cpp.rs b/zngur-generator/src/cpp.rs index 69d41d7..f2a6b53 100644 --- a/zngur-generator/src/cpp.rs +++ b/zngur-generator/src/cpp.rs @@ -574,7 +574,7 @@ private: writeln!( state, r#" -static inline {ty} build({as_std_function} f); +inline {ty}({as_std_function} f); "#, ty = self.ty.path.name(), )?; @@ -584,7 +584,7 @@ static inline {ty} build({as_std_function} f); writeln!( state, r#" - static inline {ref_kind} build({tr}& arg); + inline {ref_kind}({tr}& arg); "#, )?; } @@ -610,11 +610,7 @@ static inline {ty} build({as_std_function} f); writeln!( state, r#" - inline static {ref_kind} build(const {cpp_ty}& t) {{ - {ref_kind} o; - o.data = reinterpret_cast(&t); - return o; - }}"# + inline {ref_kind}(const {cpp_ty}& t) : data(reinterpret_cast(&t)) {{}}"# )?; } for method in &self.methods { @@ -894,7 +890,7 @@ private: writeln!( state, r#" - static inline {ty} build({as_std_function} f); + static inline {ty} make_box({as_std_function} f); "#, ty = self.ty.path.name(), )?; @@ -1105,7 +1101,7 @@ namespace rust {{ writeln!( state, r#" -{my_name} {my_name}::build({as_std_function} f) {{ +{my_name} {my_name}::make_box({as_std_function} f) {{ auto data = new {as_std_function}(f); {my_name} o; ::rust::__zngur_internal_assume_init(o); @@ -1170,10 +1166,9 @@ return o; writeln!( state, r#" -rust::{ref_kind}<{my_name}> rust::{ref_kind}<{my_name}>::build({as_ty}& args) {{ +rust::{ref_kind}<{my_name}>::{ref_kind}({as_ty}& args) {{ auto data_as_impl = &args; -rust::{ref_kind}<{my_name}> o; -::rust::__zngur_internal_assume_init(o); +::rust::__zngur_internal_assume_init(*this); {link_name_ref}( (uint8_t *)data_as_impl, "#, @@ -1181,8 +1176,7 @@ rust::{ref_kind}<{my_name}> o; writeln!( state, r#" -::rust::__zngur_internal_data_ptr(o)); -return o; +::rust::__zngur_internal_data_ptr(*this)); }} "#, )?;