Summary
Having to insert pub(...) in front of multiple declarations in a row is very cumbersome, repetitive, and ugly:
pub(crate) use foo::Something;
pub(crate) fn bar() { ... }
pub(crate) mod baz { ... }
// etc.
Of course a way around this would be via mod wrapping and use module::*, but it's obviously a workaround rather than an idiom. And you still have to declare all inner declarations pub anyways, which mostly defeats the purpose.
So I propose syntax to do this in bulk:
pub crate {
use foo::Something;
fn bar() { ... }
mod baz { ... }
}
Or alternatively, the following syntax
pub(crate) {
use foo::Something;
fn bar() { ... }
mod baz { ... }
}
It would be essentially the same as bulk extern "X" { ... }.
Open to discussion
A list of points that I was able to name off the top of my head that I think are worth discussing. By no means a complete list!
How would the compiler handle conflicting visibility qualifiers?
As mentioned earlier, bulk extern declarations already solve this by forbidding inner declarations from having an extern qualifier all together. Though this is open to discussion because extern and visibility qualifiers work in notably different ways internally.
Should it also be allowed in struct/impl bodies?
Consider the following examples in which this proposed syntax is used in struct/impl bodies.
struct Foo {
pub crate {
pub_crate_field0: u32,
pub_crate_field1: i32,
}
pub {
pub_field0: u32,
pub_field1: i32,
}
private_field: bool,
}
impl Foo {
pub crate {
fn pub_crate_method0() { ... }
fn pub_crate_method1() { ... }
}
pub {
fn pub_field0() { ... },
fn pub_field1() { ... },
}
}
- Will these create grammatical ambiguity?
- Do they adhere to Rust's core philosophy?
- Is the extra indentation worth it? (especially in
impl) - etc.
rustdoc
How would this new syntax affect rustdoc? Would it make it difficult to document each sub-declaration?
Any and all feedback is appreciated, thank you in advance!