Skip to content

Commit f2707fe

Browse files
committed
Auto merge of #90339 - GuillaumeGomez:doc-alias-doc, r=GuillaumeGomez
Add missing documentation for doc alias
2 parents 4e0d397 + 47786d3 commit f2707fe

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

src/doc/rustdoc/src/the-doc-attribute.md

+49-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ example, if you want your doctests to fail if they produce any warnings, you cou
153153
These forms of the `#[doc]` attribute are used on individual items, to control how
154154
they are documented.
155155

156-
## `#[doc(no_inline)]`/`#[doc(inline)]`
156+
### `#[doc(no_inline)]`/`#[doc(inline)]`
157157

158158
These attributes are used on `use` statements, and control where the documentation shows
159159
up. For example, consider this Rust code:
@@ -219,7 +219,54 @@ Now we'll have a `Re-exports` line, and `Bar` will not link to anywhere.
219219
One special case: In Rust 2018 and later, if you `pub use` one of your dependencies, `rustdoc` will
220220
not eagerly inline it as a module unless you add `#[doc(inline)]`.
221221

222-
## `#[doc(hidden)]`
222+
### `#[doc(hidden)]`
223223

224224
Any item annotated with `#[doc(hidden)]` will not appear in the documentation, unless
225225
the `strip-hidden` pass is removed.
226+
227+
### `alias`
228+
229+
This attribute adds an alias in the search index.
230+
231+
Let's take an example:
232+
233+
```rust,no_run
234+
#[doc(alias = "TheAlias")]
235+
pub struct SomeType;
236+
```
237+
238+
So now, if you enter "TheAlias" in the search, it'll display `SomeType`.
239+
Of course, if you enter `SomeType` it'll return `SomeType` as expected!
240+
241+
#### FFI example
242+
243+
This doc attribute is especially useful when writing bindings for a C library.
244+
For example, let's say we have a C function that looks like this:
245+
246+
```c
247+
int lib_name_do_something(Obj *obj);
248+
```
249+
250+
It takes a pointer to an `Obj` type and returns an integer. In Rust, it might
251+
be written like this:
252+
253+
```ignore (using non-existing ffi types)
254+
pub struct Obj {
255+
inner: *mut ffi::Obj,
256+
}
257+
258+
impl Obj {
259+
pub fn do_something(&mut self) -> i32 {
260+
unsafe { ffi::lib_name_do_something(self.inner) }
261+
}
262+
}
263+
```
264+
265+
The function has been turned into a method to make it more convenient to use.
266+
However, if you want to look for the Rust equivalent of `lib_name_do_something`,
267+
you have no way to do so.
268+
269+
To get around this limitation, we just add `#[doc(alias = "lib_name_do_something")]`
270+
on the `do_something` method and then it's all good!
271+
Users can now look for `lib_name_do_something` in our crate directly and find
272+
`Obj::do_something`.

0 commit comments

Comments
 (0)