Skip to content

Commit 1a77078

Browse files
xizheyinBoxyUwU
andauthored
Update src/query.md
Co-authored-by: Boxy <rust@boxyuwu.dev>
1 parent 1e0f0d2 commit 1a77078

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/query.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ To register providers, each crate exposes a [`provide`][provide_fn] function tha
143143

144144
[provide_fn]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/fn.provide.html
145145

146+
```rust,ignore
147+
pub fn provide(providers: &mut query::Providers) {
148+
*providers = query::Providers {
149+
type_of,
150+
// ... add more providers here
151+
..*providers
152+
};
153+
}
154+
```
155+
156+
Note that this function accepts `query::Providers` not `util::Providers`. It is exceedingly rare to need a `provide` function that doesn't just accept `query::Providers`. If more than the `queries` field of `util::Providers` is being updated then `util::Providers` can be accepted instead:
146157
```rust,ignore
147158
pub fn provide(providers: &mut rustc_middle::util::Providers) {
148159
providers.queries.type_of = type_of;
@@ -156,6 +167,8 @@ pub fn provide(providers: &mut rustc_middle::util::Providers) {
156167
}
157168
```
158169

170+
Note that `util::Providers` implements `DerefMut` to `query::Providers` so callers of the `provide` functions can pass in a `util::Providers` and it will just work for provider functions that accept `query::Providers` too
171+
159172
- This function takes a mutable reference to the `Providers` struct and sets the fields to point to the correct provider functions.
160173
- You can assign fields individually for each provider type (local, external, and hooks).
161174

0 commit comments

Comments
 (0)