diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 42272f76..fa3f9536 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/chromium/testing/chromium-import-macro.md b/src/chromium/testing/chromium-import-macro.md new file mode 100644 index 00000000..bfdff61a --- /dev/null +++ b/src/chromium/testing/chromium-import-macro.md @@ -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. + +
+ +`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. + +
+ + +[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 diff --git a/src/exercises/chromium/third-party.md b/src/exercises/chromium/third-party.md index e07df734..704544c3 100644 --- a/src/exercises/chromium/third-party.md +++ b/src/exercises/chromium/third-party.md @@ -28,7 +28,7 @@ more than that, they probably forgot to turn off the default features. Thanks to [Daniel Liu][3] for this crate! - + [0]: https://crates.io/crates/uwuify