You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/doc/rustdoc/src/unstable-features.md
+268
Original file line number
Diff line number
Diff line change
@@ -748,3 +748,271 @@ will be split as follows:
748
748
"you today?",
749
749
]
750
750
```
751
+
752
+
## `#[doc(cfg)]`
753
+
754
+
This feature aims at providing rustdoc users the possibility to add visual markers to the rendered documentation to know under which conditions an item is available (currently possible through the following unstable features: `doc_cfg`, `doc_auto_cfg` and `doc_cfg_hide`).
755
+
756
+
It does not aim to allow having a same item with different `cfg`s to appear more than once in the generated documentation.
757
+
758
+
It does not aim to document items which are *inactive* under the current configuration (i.e., “`cfg`ed out”).
All of these attributes can be added to a module or to the crate root, and they will be inherited by the child items unless another attribute overrides it. This is why "opposite" attributes like `auto_cfg(hide(...))` and `auto_cfg(show(...))` are provided: they allow a child item to override its parent.
By default, `#[doc(auto_cfg)]` is enabled at the crate-level. When it's enabled, Rustdoc will automatically display `cfg(...)` compatibility information as-if the same `#[doc(cfg(...))]` had been specified.
771
+
772
+
This attribute impacts the item on which it is used and its descendants.
773
+
774
+
So if we take back the previous example:
775
+
776
+
```rust
777
+
#[cfg(feature ="futures-io")]
778
+
pubmodfutures {}
779
+
```
780
+
781
+
There's no need to "duplicate" the `cfg` into a `doc(cfg())` to make Rustdoc display it.
782
+
783
+
In some situations, the detailed conditional compilation rules used to implement the feature might not serve as good documentation (for example, the list of supported platforms might be very long, and it might be better to document them in one place). To turn it off, add the `#[doc(auto_cfg = false)]` attribute on the item.
784
+
785
+
If no argument is specified (ie `#[doc(auto_cfg)]`), it's the same as writing `#[doc(auto_cfg = true)]`.
786
+
787
+
### `#[doc(cfg(...))]`
788
+
789
+
This attribute provides a standardized format to override `#[cfg()]` attributes to document conditionally available items. Example:
790
+
791
+
```rust
792
+
// the "real" cfg condition
793
+
#[cfg(feature ="futures-io")]
794
+
// the `doc(cfg())` so it's displayed to the readers
795
+
#[doc(cfg(feature ="futures-io"))]
796
+
pubmodfutures {}
797
+
```
798
+
799
+
It will display in the documentation for this module:
800
+
801
+
```
802
+
This is supported on feature="futures-io" only.
803
+
```
804
+
805
+
You can use it to display information in generated documentation, whether or not there is a `#[cfg()]` attribute:
806
+
807
+
```rust
808
+
#[doc(cfg(feature ="futures-io"))]
809
+
pubmodfutures {}
810
+
```
811
+
812
+
It will be displayed exactly the same as the previous code.
813
+
814
+
This attribute has the same syntax as conditional compilation, but it only causes documentation to be added. This means `#[doc(cfg(not(windows)))]` will not cause your docs to be hidden on non-windows targets, even though `#[cfg(not(windows))]` does do that.
815
+
816
+
If `doc(auto_cfg)` is enabled on the item, `doc(cfg)` will override it anyway so in the two previous examples, even if the `doc(auto_cfg)` feature was enabled, it would still display the same thing.
817
+
818
+
This attribute works on modules and on items.
819
+
820
+
### `#[doc(auto_cfg(hide(...)))]`
821
+
822
+
This attribute is used to prevent some `cfg` to be generated in the visual markers. It only applies to `#[doc(auto_cfg = true)]`, not to `#[doc(cfg(...))]`. So in the previous example:
823
+
824
+
```rust
825
+
#[cfg(any(unix, feature ="futures-io"))]
826
+
pubmodfutures {}
827
+
```
828
+
829
+
It currently displays both `unix` and `feature = "futures-io"` into the documentation, which is not great. To prevent the `unix` cfg to ever be displayed, you can use this attribute at the crate root level:
830
+
831
+
```rust
832
+
#![doc(auto_cfg(hide(unix)))]
833
+
```
834
+
835
+
Or directly on a given item/module as it covers any of the item's descendants:
836
+
837
+
```rust
838
+
#[doc(auto_cfg(hide(unix)))]
839
+
#[cfg(any(unix, feature ="futures-io"))]
840
+
pubmodfutures {
841
+
// `futures` and all its descendants won't display "unix" in their cfgs.
842
+
}
843
+
```
844
+
845
+
Then, the `unix` cfg will never be displayed into the documentation.
846
+
847
+
Rustdoc currently hides `doc` and `doctest` attributes by default and reserves the right to change the list of "hidden by default" attributes.
848
+
849
+
The attribute accepts only a list of identifiers or key/value items. So you can write:
So if we use `doc(auto_cfg(hide(unix)))`, it means it will hide all mentions of `unix`:
863
+
864
+
```rust
865
+
#[cfg(unix)] // nothing displayed
866
+
#[cfg(any(unix))] // nothing displayed
867
+
#[cfg(any(unix, windows))] // only `windows` displayed
868
+
```
869
+
870
+
However, it only impacts the `unix` cfg, not the feature:
871
+
872
+
```rust
873
+
#[cfg(feature ="unix")] // `feature = "unix"` is displayed
874
+
```
875
+
876
+
If `cfg_auto(show(...))` and `cfg_auto(hide(...))` are used to show/hide a same `cfg` on a same item, it'll emit an error. Example:
877
+
878
+
```rust
879
+
#[doc(auto_cfg(hide(unix)))]
880
+
#[doc(auto_cfg(show(unix)))] // Error!
881
+
pubfnfoo() {}
882
+
```
883
+
884
+
Using this attribute will re-enable `auto_cfg` if it was disabled at this location:
885
+
886
+
```rust
887
+
#[doc(auto_cfg = false)] // Disabling `auto_cfg`
888
+
pubfnfoo() {}
889
+
```
890
+
891
+
And using `doc(auto_cfg)` will re-enable it:
892
+
893
+
```rust
894
+
#[doc(auto_cfg = false)] // Disabling `auto_cfg`
895
+
pubmodmodule {
896
+
#[doc(auto_cfg(hide(unix)))] // `auto_cfg` is re-enabled.
897
+
pubfnfoo() {}
898
+
}
899
+
```
900
+
901
+
However, using `doc(auto_cfg = ...)` and `doc(auto_cfg(...))` on the same item will emit an error:
902
+
903
+
```rust
904
+
#[doc(auto_cfg = false)]
905
+
#[doc(auto_cfg(hide(unix)))] // error
906
+
pubfnfoo() {}
907
+
```
908
+
909
+
The reason behind this is that `doc(auto_cfg = ...)` enables or disables the feature, whereas `doc(auto_cfg(...))` enables it unconditionally, making the first attribute to appear useless as it will be overidden by the next `doc(auto_cfg)` attribute.
910
+
911
+
### `#[doc(auto_cfg(show(...)))]`
912
+
913
+
This attribute does the opposite of `#[doc(auto_cfg(hide(...)))]`: if you used `#[doc(auto_cfg(hide(...)))]` and want to revert its effect on an item and its descendants, you can use `#[doc(auto_cfg(show(...)))]`.
914
+
It only applies to `#[doc(auto_cfg = true)]`, not to `#[doc(cfg(...))]`.
915
+
916
+
For example:
917
+
918
+
```rust
919
+
#[doc(auto_cfg(hide(unix)))]
920
+
#[cfg(any(unix, feature ="futures-io"))]
921
+
pubmodfutures {
922
+
// `futures` and all its descendants won't display "unix" in their cfgs.
923
+
#[doc(auto_cfg(show(unix)))]
924
+
pubmodchild {
925
+
// `child` and all its descendants will display "unix" in their cfgs.
926
+
}
927
+
}
928
+
```
929
+
930
+
The attribute accepts only a list of identifiers or key/value items. So you can write:
If `auto_cfg(show(...))` and `auto_cfg(hide(...))` are used to show/hide a same `cfg` on a same item, it'll emit an error. Example:
944
+
945
+
```rust
946
+
#[doc(auto_cfg(show(unix)))]
947
+
#[doc(auto_cfg(hide(unix)))] // Error!
948
+
pubfnfoo() {}
949
+
```
950
+
951
+
Using this attribute will re-enable `auto_cfg` if it was disabled at this location:
952
+
953
+
```rust
954
+
#[doc(auto_cfg = false)] // Disabling `auto_cfg`
955
+
#[doc(auto_cfg(show(unix)))] // `auto_cfg` is re-enabled.
956
+
pubfnfoo() {}
957
+
```
958
+
959
+
## Inheritance
960
+
961
+
Rustdoc merges `cfg` attributes from parent modules to its children. For example, in this case, the module `non_unix` will describe the entire compatibility matrix for the module, and not just its directly attached information:
962
+
963
+
```rust
964
+
#[doc(cfg(any(windows, unix)))]
965
+
pubmoddesktop {
966
+
#[doc(cfg(not(unix)))]
967
+
pubmodnon_unix {
968
+
// ...
969
+
}
970
+
}
971
+
```
972
+
973
+
This code will display:
974
+
975
+
```
976
+
Available on (Windows or Unix) and non-Unix only.
977
+
```
978
+
979
+
### Re-exports and inlining
980
+
981
+
`cfg` attributes of a re-export are never merged with the re-exported item(s) attributes except if the re-export has the `#[doc(inline)]` attribute. In this case, the `cfg` of the re-exported item will be merged with the re-export's.
982
+
983
+
When talking about "attributes merge", we mean that if the re-export has `#[cfg(unix)]` and the re-exported item has `#[cfg(feature = "foo")]`, you will only see `cfg(unix)` on the re-export and only `cfg(feature = "foo")` on the re-exported item, unless the re-export has `#[doc(inline)]`, then you will only see the re-exported item with both `cfg(unix)` and `cfg(feature = "foo")`.
984
+
985
+
Example:
986
+
987
+
```rust
988
+
#[doc(cfg(any(windows, unix)))]
989
+
pubmoddesktop {
990
+
#[doc(cfg(not(unix)))]
991
+
pubmodnon_unix {
992
+
// code
993
+
}
994
+
}
995
+
996
+
#[doc(cfg(target_os ="freebsd"))]
997
+
pubusedesktop::non_unix as non_unix_desktop;
998
+
#[doc(cfg(target_os ="macos"))]
999
+
#[doc(inline)]
1000
+
pubusedesktop::non_unix as inlined_non_unix_desktop;
1001
+
```
1002
+
1003
+
In this example, `non_unix_desktop` will only display `cfg(target_os = "freeebsd")` and not display any `cfg` from `desktop::non_unix`.
1004
+
1005
+
On the contrary, `inlined_non_unix_desktop` will have cfgs from both the re-export and the re-exported item.
1006
+
1007
+
So that also means that if a crate re-exports a foreign item, unless it has `#[doc(inline)]`, the `cfg` and `doc(cfg)` attributes will not be visible:
1008
+
1009
+
```rust
1010
+
// dep:
1011
+
#[cfg(feature ="a")]
1012
+
pubstructS;
1013
+
1014
+
// crate using dep:
1015
+
1016
+
// There will be no mention of `feature = "a"` in the documentation.
0 commit comments