rust-lang / rfcs

RFCs for changes to Rust
https://rust-lang.github.io/rfcs/
Apache License 2.0
5.9k stars 1.56k forks source link

Conditional visibility: for integration testing purpose. #3656

Open crlf0710 opened 3 months ago

crlf0710 commented 3 months ago

For integration testing purpose, it might be necessary to expose more API surface than usual. While it might be possible to directly add integration testing related support, it might be easier to add general purpose mechanism for this.

Strawman proposal:

#[cfg_vis(feature = "internals", pub)]
pub(crate) mod internal_api {
     #[cfg_vis(feature = "internals", pub)]
     pub(crate) fn foo() {
          todo!()
     }
}

or

pub (crate if cfg!(not(feature = "internals"))) mod internal_api {
     pub (crate if cfg!(not(feature = "internals"))) fn foo() {
          todo!()
     }
}

or even

#[cfg(feature = "internals")]
vis internals = pub;

#[cfg(not(feature = "internals"))]
vis internals = pub(crate);

pub(internals) mod internal_api {
     pub(internals) fn foo() {
          todo!()
     }
}

Related existing macro-based crates.

kennytm commented 3 months ago
pub (crate if cfg!(not(feature = "internals"))) mod internal_api {

Maybe more natural as

pub(#[cfg(not(feature = "internals"))] crate) mod internal_api

assuming we accept pub() means pub. But you need to copy this huge thing everywhere, unlike the vis alias.


How is this proposal going to interact with #3323?