Skip to content

Commit

Permalink
Replace make with proper constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Oct 9, 2023
1 parent bf029f8 commit 799eacc
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 26 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-zngur-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/zngur)
[<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/hkalbasi/zngur/ci.yml?branch=main&style=for-the-badge" height="20">](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/)
Expand Down Expand Up @@ -66,7 +65,7 @@ int main() {
}
int state = 0;
// You can convert a C++ lambda into a `Box<dyn Fn>` and friends.
auto f = BoxDyn<rust::Fn<int32_t, int32_t>>::build([&](int32_t x) {
auto f = BoxDyn<rust::Fn<int32_t, int32_t>>::make_box([&](int32_t x) {
state += x;
std::cout << "hello " << x << " " << state << "\n";
return x * 2;
Expand Down
2 changes: 1 addition & 1 deletion book/src/zngur.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int main() {
}
int state = 0;
// You can convert a C++ lambda into a `Box<dyn Fn>` and friends.
auto f = BoxDyn<rust::Fn<int32_t, int32_t>>::build([&](int32_t x) {
auto f = BoxDyn<rust::Fn<int32_t, int32_t>>::make_box([&](int32_t x) {
state += x;
std::cout << "hello " << x << " " << state << "\n";
return x * 2;
Expand Down
2 changes: 1 addition & 1 deletion examples/memory_management/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ int main() {
CppPrintOnDropHolder c;
{
std::cout << "Checkpoint 15" << std::endl;
auto holder = RmDyn<PrintOnDropConsumer>::build(c);
auto holder = RmDyn<PrintOnDropConsumer>(c);
std::cout << "Checkpoint 16" << std::endl;
consume_n_times(holder, rust::Str::from_char_star("P2"), 3);
std::cout << "Checkpoint 17" << std::endl;
Expand Down
59 changes: 59 additions & 0 deletions examples/memory_management/out.txt
Original file line number Diff line number Diff line change
@@ -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
11 changes: 4 additions & 7 deletions examples/osmium/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ class RustHandler : public osmium::handler::Handler {
BendHandler inner;

public:
void way(osmium::Way &way) {
auto rusty_way = rust::Ref<Way>::build(way);
inner.way(rusty_way);
}
void way(osmium::Way &way) { inner.way(way); }

RustHandler(BendHandler &&inner) : inner(std::move(inner)) {}
};
Expand All @@ -45,11 +42,11 @@ rust::Tuple<> rust::exported_functions::apply(rust::Ref<Reader> reader,
}

rust::Ref<WayNodeList> rust::Impl<Way>::nodes(rust::Ref<Way> self) {
return rust::Ref<WayNodeList>::build(self.cpp().nodes());
return self.cpp().nodes();
}

rust::Ref<TagList> rust::Impl<Way>::tags(rust::Ref<Way> self) {
return rust::Ref<TagList>::build(self.cpp().tags());
return self.cpp().tags();
}

rust::std::option::Option<rust::Ref<rust::Str>>
Expand All @@ -71,7 +68,7 @@ size_t rust::Impl<WayNodeList>::len(rust::Ref<WayNodeList> self) {
rust::Ref<Node>
rust::Impl<WayNodeList, rust::std::ops::Index<size_t, Node>>::index(
rust::Ref<WayNodeList> self, size_t i) {
return rust::Ref<Node>::build(self.cpp()[i]);
return self.cpp()[i];
}

double rust::Impl<Node>::distance(rust::Ref<Node> self, rust::Ref<Node> other) {
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int main() {
}
int state = 0;
// You can convert a C++ lambda into a `Box<dyn Fn>` and friends.
auto f = BoxDyn<rust::Fn<int32_t, int32_t>>::build([&](int32_t x) {
auto f = BoxDyn<rust::Fn<int32_t, int32_t>>::make_box([&](int32_t x) {
state += x;
std::cout << "hello " << x << " " << state << "\n";
return x * 2;
Expand Down
22 changes: 8 additions & 14 deletions zngur-generator/src/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)?;
Expand All @@ -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);
"#,
)?;
}
Expand All @@ -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<size_t>(&t);
return o;
}}"#
inline {ref_kind}(const {cpp_ty}& t) : data(reinterpret_cast<size_t>(&t)) {{}}"#
)?;
}
for method in &self.methods {
Expand Down Expand Up @@ -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(),
)?;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1170,19 +1166,17 @@ 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,
"#,
)?;
writeln!(
state,
r#"
::rust::__zngur_internal_data_ptr(o));
return o;
::rust::__zngur_internal_data_ptr(*this));
}}
"#,
)?;
Expand Down

0 comments on commit 799eacc

Please sign in to comment.