2

I understand that I can use the cfg_attr attribute to conditionally derive Trait implementations for types. For example, A struct MyStruct which always implements Clone, Copy, and Debug, but which implements PartialEq if and only if the my-feature feature is enabled, can be defined as follows:

#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "my-feature", derive(PartialEq)]
struct MyStruct;

But is it possible to make the generated documentation reflect this? I would like the rustdoc-generated documentation to state that PartialEq is implemented for MyStruct if and only if the my-feature feature is enabled.

So far, I have only accomplished two outcomes, neither of which I want:

  1. If I compile the docs with the my-feature feature enabled, the generated documentation just says that MyStruct implements PartialEq, with no mention of feature gating
  2. If I compile the docs without the my-feautre feature enabled, generated documentation does not indicate that PartialEq is or can be implemented for MyStruct.
RBF06
  • 2,013
  • 2
  • 21
  • 20
  • 3
    I don't believe it is possible. You'd need to add `#[doc(cfg(feature = "my-feature"))]` ([which is not yet stable](https://github.com/rust-lang/rust/issues/43781)) to the *implementation*, but there's no way to provide that when using `#[derive(...)]` as far as I know. – kmdreko Dec 17 '22 at 23:51

0 Answers0