Skip to content

Commit

Permalink
Add C++ from Rust index page
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Sep 10, 2023
1 parent 15b1a58 commit 6009bbe
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
- [Wellknown traits](./call_rust_from_cpp/wellknown_traits.md)
- [Layout policy](./call_rust_from_cpp/layout_policy.md)
- [Types with special support](./call_rust_from_cpp/special_types.md)
- [Calling C++ from Rust]()
- [Calling C++ from Rust](./call_cpp_from_rust/index.md)
- [`Box<dyn Fn>`]()
- [Converting C++ objects to `Box<dyn Trait>`]()
- [Calling C++ free functions]()
- [Adding methods to Rust types]()
- [Implementing Rust traits for Rust types]()
- [Footguns]()
- [How it compares to other tools](./how_it_compares.md)
- [Philosophy](./philosophy.md)
- [How it works?](./how_it_works.md)
18 changes: 18 additions & 0 deletions book/src/call_cpp_from_rust/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Calling C++ from Rust

Until this point, all Zngur features discussed was about using Rust code inside C++, but no C++/Rust interop tool is complete
without supporting the C++ to Rust direction. In fact, this direction is arguably more important since in a C++/Rust project
most of the existing code is in C++ and Rust code needs a way to use them in order to be useful. So Zngur also supports this
direction.

Zngur general idea is that Rust semantics is a subset of C++ semantics, so we should use Rust things in C++ and avoid bringing
C++ things in Rust (See [Philosophy](../philosophy.md)). So, even in the C++ to Rust direction, Zngur operates only on Rust types. For
example, Zngur allows you to call a C++ function that takes Rust types in inputs in Rust, but you can't call a function that takes
a C++ object. Or you can write an `impl` block for a Rust type in C++ and call those methods in Rust, but you can't call C++ methods
of a C++ object in Rust.

So you can't call your C++ code directly in Rust, and you need to write a Rusty wrapper for your C++ code. This is often unavoidable
even if the interop tool supports calling C++ directly if you have a meaningful amount of Rust code, since C++ value cannot be owned
in Rust, and Rust is more expressive in API and function signatures (e.g. it has lifetimes) so such a wrapper is required anyway. Zngur
tries to enable writing a Rusty wrapper for your C++ code as idiomatic as a normal Rust code, and all features in this section try
to help in achieving this goal.
1 change: 1 addition & 0 deletions book/src/philosophy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Philosophy
2 changes: 1 addition & 1 deletion book/src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ int main() {
}
```

There are some traits that Zngur has special support for them, and `Debug` is among them. [This page](./call_rust_from_cpp/wellknown_traits.html) has the
There are some traits that Zngur has special support for them, and `Debug` is among them. [This page](./call_rust_from_cpp/wellknown_traits.md) has the
complete list of them.

Assuming that everything works correctly, you should see something like this after executing the program:
Expand Down

0 comments on commit 6009bbe

Please sign in to comment.