1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-28 08:36:11 +02:00
comprehensive-rust/src/exercises/chromium/interoperability-with-cpp.md
Martin Geisler c9f66fd425
Format all Markdown files with dprint ()
This is the result of running `dprint fmt` after removing `src/` from
the list of excluded directories.

This also reformats the Rust code: we might want to tweak this a bit in
the future since some of the changes removes the hand-formatting. Of
course, this formatting can be seen as a mis-feature, so maybe this is
good overall.

Thanks to mdbook-i18n-helpers 0.2, the POT file is nearly unchanged
after this, meaning that all existing translations remain valid! A few
messages were changed because of stray whitespace characters:

     msgid ""
     "Slices always borrow from another object. In this example, `a` has to remain "
    -"'alive' (in scope) for at least as long as our slice. "
    +"'alive' (in scope) for at least as long as our slice."
     msgstr ""

The formatting is enforced in CI and we will have to see how annoying
this is in practice for the many contributors. If it becomes annoying,
we should look into fixing  so that `dprint` can annotate
the lines that need fixing directly, then I think we can consider more
strict formatting checks.

I added more customization to `rustfmt.toml`. This is to better emulate
the dense style used in the course:

- `max_width = 85` allows lines to take up the full width available in
our code blocks (when taking margins and the line numbers into account).
- `wrap_comments = true` ensures that we don't show very long comments
in the code examples. I edited some comments to shorten them and avoid
unnecessary line breaks — please trim other unnecessarily long comments
when you see them! Remember we're writing code for slides 😄
- `use_small_heuristics = "Max"` allows for things like struct literals
and if-statements to take up the full line width configured above.

The formatting settings apply to all our Rust code right now — I think
we could improve this with https://github.com/dprint/dprint/issues/711
which lets us add per-directory `dprint` configuration files. However,
the `inherit: true` setting is not yet implemented (as far as I can
tell), so a nested configuration file will have to copy most or all of
the top-level file.
2023-12-31 00:15:07 +01:00

3.0 KiB

Exercise: Interoperability with C++

Part one

  • In the Rust file you previously created, add a #[cxx::bridge] which specifies a single function, to be called from C++, called hello_from_rust, taking no parameters and returning no value.
  • Modify your previous hello_from_rust function to remove extern "C" and #[no_mangle]. This is now just a standard Rust function.
  • Modify your gn target to build these bindings.
  • In your C++ code, remove the forward-declaration of hello_from_rust. Instead, include the generated header file.
  • Build and run!

Part two

It's a good idea to play with CXX a little. It helps you think about how flexible Rust in Chromium actually is.

Some things to try:

  • Call back into C++ from Rust. You will need:
    • An additional header file which you can include! from your cxx::bridge. You'll need to declare your C++ function in that new header file.
    • An unsafe block to call such a function, or alternatively specify the unsafe keyword in your #[cxx::bridge] as described here.
    • You may also need to #include "third_party/rust/cxx/v1/crate/include/cxx.h"
  • Pass a C++ string from C++ into Rust.
  • Pass a reference to a C++ object into Rust.
  • Intentionally get the Rust function signatures mismatched from the #[cxx::bridge], and get used to the errors you see.
  • Intentionally get the C++ function signatures mismatched from the #[cxx::bridge], and get used to the errors you see.
  • Pass a std::unique_ptr of some type from C++ into Rust, so that Rust can own some C++ object.
  • Create a Rust object and pass it into C++, so that C++ owns it. (Hint: you need a Box).
  • Declare some methods on a C++ type. Call them from Rust.
  • Declare some methods on a Rust type. Call them from C++.

Part three

Now you understand the strengths and limitations of CXX interop, think of a couple of use-cases for Rust in Chromium where the interface would be sufficiently simple. Sketch how you might define that interface.

Where to find help

As students explore Part Two, they're bound to have lots of questions about how to achieve these things, and also how CXX works behind the scenes.

Some of the questions you may encounter:

  • I'm seeing a problem initializing a variable of type X with type Y, where X and Y are both function types. This is because your C++ function doesn't quite match the declaration in your cxx::bridge.
  • I seem to be able to freely convert C++ references into Rust references. Doesn't that risk UB? For CXX's opaque types, no, because they are zero-sized. For CXX trivial types yes, it's possible to cause UB, although CXX's design makes it quite difficult to craft such an example.