Is it possible to print the documentation with/without the feature flag at the same time?

Hi,

Is it possible to print the documentation for TryFrom<u64> for Foo and From<u64> for Foo in the example below at the same time?

#[cfg(not(feature = "u64"))]
struct Foo(u32);

#[cfg(feature = "u64")]
struct Foo(u64);

#[cfg(not(feature = "u64"))]
impl TryFrom<u64> for Foo {
    type Error = std::num::TryFromIntError;

    /// Converts `u64` to [`Foo`].
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if `v` is out of range for [`Foo`].
    fn try_from(v: u64) -> Result<Self, Self::Error> {
        v.try_into().map(Self)
    }
}

#[cfg(feature = "u64")]
impl From<u64> for Foo {
    /// Converts `u64` to [`Foo`].
    fn from(v: u64) -> Self {
        Self(v)
    }
}

If possible, I would like to be able to print the documentation for TryFrom<u64> for Foo even when the all features is enabled.

No, it is not possible; documentation is always built from one version of the code.

But this feature conditional is incorrect anyway, because the feature is non-additive: when the feature is disabled, <Foo as TryFrom<u64>>::Error is std::num::TryFromIntError, but when the feature is enabled, the error type is std::convert::Infallible. This can break callers who are relying on the error type being a TryFromIntError for their own error enum or conversion, or simply putting it in their own -> Result signature.

Instead, always implement TryFrom, then provide a separate method (not a From implementation) for infallible conversion when the "u64" feature is enabled.

6 Likes

I didn't know that the feature should be additive. Thanks for answering!