You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-20 16:05:38 +02:00
Format all Markdown files with dprint
(#1157)
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 dprint/check#11 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.
This commit is contained in:
@ -5,52 +5,51 @@ together everything you already learned.
|
||||
|
||||
## The Brief from Product Management
|
||||
|
||||
A community of pixies has been discovered living in a remote rainforest.
|
||||
It's important that we get Chromium for Pixies delivered to them as soon
|
||||
as possible.
|
||||
A community of pixies has been discovered living in a remote rainforest. It's
|
||||
important that we get Chromium for Pixies delivered to them as soon as possible.
|
||||
|
||||
The requirement is to translate all Chromium's UI strings into Pixie language.
|
||||
|
||||
There's not time to wait for proper translations, but fortunately pixie
|
||||
language is very close to English, and it turns out there's a Rust crate
|
||||
which does the translation.
|
||||
There's not time to wait for proper translations, but fortunately pixie language
|
||||
is very close to English, and it turns out there's a Rust crate which does the
|
||||
translation.
|
||||
|
||||
In fact, you already [imported that crate in the previous exercise][0].
|
||||
|
||||
(Obviously, real translations of Chrome require incredible care and
|
||||
diligence. Don't ship this!)
|
||||
(Obviously, real translations of Chrome require incredible care and diligence.
|
||||
Don't ship this!)
|
||||
|
||||
## Steps
|
||||
|
||||
Modify `ResourceBundle::MaybeMangleLocalizedString` so that it uwuifies
|
||||
all strings before display. In this special build of Chromium, it should
|
||||
always do this irrespective of the setting of `mangle_localized_strings_`.
|
||||
Modify `ResourceBundle::MaybeMangleLocalizedString` so that it uwuifies all
|
||||
strings before display. In this special build of Chromium, it should always do
|
||||
this irrespective of the setting of `mangle_localized_strings_`.
|
||||
|
||||
If you've done everything right across all these exercises, congratulations,
|
||||
you should have created Chrome for pixies!
|
||||
If you've done everything right across all these exercises, congratulations, you
|
||||
should have created Chrome for pixies!
|
||||
|
||||
<img src="chwomium.png" alt="Chromium UI screenshot with uwu language">
|
||||
|
||||
<details>
|
||||
Students will likely need some hints here. Hints include:
|
||||
|
||||
* UTF16 vs UTF8. Students should be aware that Rust strings are always
|
||||
UTF8, and will probably decide that it's better to do the conversion
|
||||
on the C++ side using `base::UTF16ToUTF8` and back again.
|
||||
* If students decide to do the conversion on the Rust side, they'll need to
|
||||
consider [`String::from_utf16`][1], consider error handling, and
|
||||
consider which [CXX supported types can transfer a lot of u16s][2].
|
||||
* Students may design the C++/Rust boundary in several different ways,
|
||||
e.g. taking and returning strings by value, or taking a mutable reference
|
||||
to a string. If a mutable reference is used, CXX will likely
|
||||
tell the student that they need to use [`Pin`][3]. You may need to explain
|
||||
what `Pin` does, and then explain why CXX needs it for mutable references
|
||||
to C++ data: the answer is that C++ data can't be moved around like Rust
|
||||
data, because it may contain self-referential pointers.
|
||||
* The C++ target containing `ResourceBundle::MaybeMangleLocalizedString`
|
||||
will need to depend on a `rust_static_library` target. The student
|
||||
probably already did this.
|
||||
* The `rust_static_library` target will need to depend on
|
||||
- UTF16 vs UTF8. Students should be aware that Rust strings are always UTF8, and
|
||||
will probably decide that it's better to do the conversion on the C++ side
|
||||
using `base::UTF16ToUTF8` and back again.
|
||||
- If students decide to do the conversion on the Rust side, they'll need to
|
||||
consider [`String::from_utf16`][1], consider error handling, and consider
|
||||
which [CXX supported types can transfer a lot of u16s][2].
|
||||
- Students may design the C++/Rust boundary in several different ways, e.g.
|
||||
taking and returning strings by value, or taking a mutable reference to a
|
||||
string. If a mutable reference is used, CXX will likely tell the student that
|
||||
they need to use [`Pin`][3]. You may need to explain what `Pin` does, and then
|
||||
explain why CXX needs it for mutable references to C++ data: the answer is
|
||||
that C++ data can't be moved around like Rust data, because it may contain
|
||||
self-referential pointers.
|
||||
- The C++ target containing `ResourceBundle::MaybeMangleLocalizedString` will
|
||||
need to depend on a `rust_static_library` target. The student probably already
|
||||
did this.
|
||||
- The `rust_static_library` target will need to depend on
|
||||
`//third_party/rust/uwuify/v0_2:lib`.
|
||||
|
||||
</details>
|
||||
|
@ -1,6 +1,7 @@
|
||||
# Build rules exercise
|
||||
|
||||
In your Chromium build, add a new Rust target to `//ui/base/BUILD.gn` containing:
|
||||
In your Chromium build, add a new Rust target to `//ui/base/BUILD.gn`
|
||||
containing:
|
||||
|
||||
```rust
|
||||
#[no_mangle]
|
||||
@ -8,32 +9,33 @@ pub extern "C" fn hello_from_rust() {
|
||||
println!("Hello from Rust!")
|
||||
}
|
||||
```
|
||||
**Important**: note that `no_mangle` here is considered a type of unsafety
|
||||
by the Rust compiler, so you'll need to to allow unsafe code in your
|
||||
`gn` target.
|
||||
|
||||
Add this new Rust target as a dependency of `//ui/base:base`.
|
||||
Declare this function at the top of `ui/base/resource/resource_bundle.cc`
|
||||
(later, we'll see how this can be automated by bindings generation tools):
|
||||
**Important**: note that `no_mangle` here is considered a type of unsafety by
|
||||
the Rust compiler, so you'll need to to allow unsafe code in your `gn` target.
|
||||
|
||||
Add this new Rust target as a dependency of `//ui/base:base`. Declare this
|
||||
function at the top of `ui/base/resource/resource_bundle.cc` (later, we'll see
|
||||
how this can be automated by bindings generation tools):
|
||||
|
||||
```cpp
|
||||
extern "C" void hello_from_rust();
|
||||
```
|
||||
|
||||
Call this function from somewhere in `ui/base/resource/resource_bundle.cc` -
|
||||
we suggest the top of `ResourceBundle::MaybeMangleLocalizedString`.
|
||||
Build and run Chromium, and ensure that "Hello from Rust!" is printed lots of times.
|
||||
Call this function from somewhere in `ui/base/resource/resource_bundle.cc` - we
|
||||
suggest the top of `ResourceBundle::MaybeMangleLocalizedString`. Build and run
|
||||
Chromium, and ensure that "Hello from Rust!" is printed lots of times.
|
||||
|
||||
If you use VSCode, now set up Rust to work well in VSCode. It will be useful
|
||||
in subsequent exercises. If you've succeeded, you will be able to use
|
||||
right-click "Go to definition" on `println!`.
|
||||
If you use VSCode, now set up Rust to work well in VSCode. It will be useful in
|
||||
subsequent exercises. If you've succeeded, you will be able to use right-click
|
||||
"Go to definition" on `println!`.
|
||||
|
||||
## Where to find help
|
||||
|
||||
* The options available to the [`rust_static_library` gn template][0]
|
||||
* Information about [`#[no_mangle]`][1]
|
||||
* Information about [`extern "C"`][2]
|
||||
* Information about gn's [`--export-rust-project`][3] switch
|
||||
* [How to install rust-analyzer in VSCode][4]
|
||||
- The options available to the [`rust_static_library` gn template][0]
|
||||
- Information about [`#[no_mangle]`][1]
|
||||
- Information about [`extern "C"`][2]
|
||||
- Information about gn's [`--export-rust-project`][3] switch
|
||||
- [How to install rust-analyzer in VSCode][4]
|
||||
|
||||
<details>
|
||||
It's really important that students get this running, because future exercises
|
||||
@ -49,10 +51,11 @@ that the right one is called.
|
||||
|
||||
If you need a pure Rust executable, you can also do that using the
|
||||
`rust_executable` gn template.
|
||||
|
||||
</details>
|
||||
|
||||
[0]: https://source.chromium.org/chromium/chromium/src/+/main:build/rust/rust_static_library.gni;l=16
|
||||
[1]: https://doc.rust-lang.org/beta/reference/abi.html#the-no_mangle-attribute
|
||||
[2]: https://doc.rust-lang.org/std/keyword.extern.html
|
||||
[3]: https://gn.googlesource.com/gn/+/main/docs/reference.md#compilation-database
|
||||
[4]: https://code.visualstudio.com/docs/languages/rust
|
||||
[4]: https://code.visualstudio.com/docs/languages/rust
|
||||
|
@ -2,70 +2,70 @@
|
||||
|
||||
## 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!
|
||||
- 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.
|
||||
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`.
|
||||
- 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][0].
|
||||
* 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++.
|
||||
- An `unsafe` block to call such a function, or alternatively specify the
|
||||
`unsafe` keyword in your `#[cxx::bridge]` [as described here][0].
|
||||
- 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
|
||||
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
|
||||
|
||||
* The [`cxx` binding reference][1]
|
||||
* The [`rust_static_library` gn template][2]
|
||||
- The [`cxx` binding reference][1]
|
||||
- The [`rust_static_library` gn template][2]
|
||||
|
||||
<details>
|
||||
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.
|
||||
</details>
|
||||
|
||||
- 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.
|
||||
|
||||
</details>
|
||||
|
||||
[0]: https://cxx.rs/extern-c++.html#functions-and-member-functions
|
||||
[1]: https://cxx.rs/bindings.html
|
||||
[2]: https://source.chromium.org/chromium/chromium/src/+/main:build/rust/rust_static_library.gni;l=16
|
||||
[2]: https://source.chromium.org/chromium/chromium/src/+/main:build/rust/rust_static_library.gni;l=16
|
||||
|
@ -1,6 +1,5 @@
|
||||
# Exercise Solutions
|
||||
|
||||
Solutions to the Chromium exercises can be found in
|
||||
[this series of CLs][0].
|
||||
Solutions to the Chromium exercises can be found in [this series of CLs][0].
|
||||
|
||||
[0]: https://chromium-review.googlesource.com/c/chromium/src/+/5096560
|
||||
[0]: https://chromium-review.googlesource.com/c/chromium/src/+/5096560
|
||||
|
@ -4,11 +4,9 @@ Time for another exercise!
|
||||
|
||||
In your Chromium build:
|
||||
|
||||
* Add a testable function next to `hello_from_rust`.
|
||||
Some suggestions:
|
||||
adding two integers received as arguments,
|
||||
computing the nth Fibonacci number,
|
||||
- Add a testable function next to `hello_from_rust`. Some suggestions: adding
|
||||
two integers received as arguments, computing the nth Fibonacci number,
|
||||
summing integers in a slice, etc.
|
||||
* Add a separate `..._unittest.rs` file with a test for the new function.
|
||||
* Add the new tests to `BUILD.gn`.
|
||||
* Build the tests, run them, and verify that the new test works.
|
||||
- Add a separate `..._unittest.rs` file with a test for the new function.
|
||||
- Add the new tests to `BUILD.gn`.
|
||||
- Build the tests, run them, and verify that the new test works.
|
||||
|
@ -1,11 +1,11 @@
|
||||
# Exercise
|
||||
|
||||
Add [uwuify][0] to Chromium, turning off the crate's [default features][1].
|
||||
Assume that the crate will be used in shipping Chromium, but won't be used
|
||||
to handle untrustworthy input.
|
||||
Assume that the crate will be used in shipping Chromium, but won't be used to
|
||||
handle untrustworthy input.
|
||||
|
||||
(In the next exercise we'll use uwuify from Chromium, but feel free to
|
||||
skip ahead and do that now if you like. Or, you could create a new
|
||||
(In the next exercise we'll use uwuify from Chromium, but feel free to skip
|
||||
ahead and do that now if you like. Or, you could create a new
|
||||
[`rust_executable` target][2] which uses `uwuify`).
|
||||
|
||||
<details>
|
||||
@ -23,15 +23,14 @@ The total crates needed are:
|
||||
- `smallvec`, and
|
||||
- `uwuify`.
|
||||
|
||||
If students are downloading even
|
||||
more than that, they probably forgot to turn off the default features.
|
||||
If students are downloading even more than that, they probably forgot to turn
|
||||
off the default features.
|
||||
|
||||
Thanks to [Daniel Liu][3] for this crate!
|
||||
|
||||
</details>
|
||||
|
||||
|
||||
[0]: https://crates.io/crates/uwuify
|
||||
[1]: https://doc.rust-lang.org/cargo/reference/features.html#the-default-feature
|
||||
[2]: https://source.chromium.org/chromium/chromium/src/+/main:build/rust/rust_executable.gni
|
||||
[3]: https://github.com/Daniel-Liu-c0deb0t
|
||||
[3]: https://github.com/Daniel-Liu-c0deb0t
|
||||
|
Reference in New Issue
Block a user