mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-01-18 04:23:14 +02:00
Chromium: Add a section about the chromium::import!
macro. (#1611)
This commit is contained in:
parent
e1d98521bb
commit
dea36b76cf
@ -255,6 +255,7 @@
|
||||
- [Testing](chromium/testing.md)
|
||||
- [`rust_gtest_interop` Library](chromium/testing/rust-gtest-interop.md)
|
||||
- [GN Rules for Rust Tests](chromium/testing/build-gn.md)
|
||||
- [`chromium::import!` Macro](chromium/testing/chromium-import-macro.md)
|
||||
- [Exercise](exercises/chromium/testing.md)
|
||||
- [Interoperability with C++](chromium/interoperability-with-cpp.md)
|
||||
- [Example Bindings](chromium/interoperability-with-cpp/example-bindings.md)
|
||||
|
40
src/chromium/testing/chromium-import-macro.md
Normal file
40
src/chromium/testing/chromium-import-macro.md
Normal file
@ -0,0 +1,40 @@
|
||||
# `chromium::import!` Macro
|
||||
|
||||
After adding `:my_rust_lib` to GN `deps`, we still need to learn how to import
|
||||
and use `my_rust_lib` from `my_rust_lib_unittest.rs`. We haven't provided an
|
||||
explicit `crate_name` for `my_rust_lib` so its crate name is computed based on
|
||||
the full target path and name. Fortunately we can avoid working with such an
|
||||
unwieldy name by using the `chromium::import!` macro from the
|
||||
automatically-imported `chromium` crate:
|
||||
|
||||
```rust,ignore
|
||||
chromium::import! {
|
||||
"//ui/base:my_rust_lib";
|
||||
}
|
||||
|
||||
use my_rust_lib::my_function_under_test;
|
||||
```
|
||||
|
||||
Under the covers the macro expands to something similar to:
|
||||
|
||||
```rust,ignore
|
||||
extern crate ui_sbase_cmy_urust_ulib as my_rust_lib;
|
||||
|
||||
use my_rust_lib::my_function_under_test;
|
||||
```
|
||||
|
||||
More information can be found in [the doc comment][0] of the `chromium::import`
|
||||
macro.
|
||||
|
||||
<details>
|
||||
|
||||
`rust_static_library` supports specifying an explicit name via `crate_name`
|
||||
property, but doing this is discouraged. And it is discouraged because
|
||||
the crate name has to be globally unique. crates.io guarantees uniqueness
|
||||
of its crate names so `cargo_crate` GN targets (generated by the `gnrt`
|
||||
tool covered in a later section) use short crate names.
|
||||
|
||||
</details>
|
||||
|
||||
|
||||
[0]: https://source.chromium.org/chromium/chromium/src/+/main:build/rust/chromium_prelude/chromium_prelude.rs?q=f:chromium_prelude.rs%20pub.use.*%5Cbimport%5Cb;%20-f:third_party&ss=chromium%2Fchromium%2Fsrc
|
@ -28,7 +28,7 @@ more than that, they probably forgot to turn off the default features.
|
||||
|
||||
Thanks to [Daniel Liu][3] for this crate!
|
||||
|
||||
</detail>
|
||||
</details>
|
||||
|
||||
|
||||
[0]: https://crates.io/crates/uwuify
|
||||
|
Loading…
Reference in New Issue
Block a user