A recent blog post reminded me of a slight change to the #[non_exhaustive]
attribute I'd like to see, one that provides a nice alternative to the builder pattern.
The idea is to provide a relaxed version of #[non_exhaustive]
for structs that would allow other crates to continue constructing the struct, but only with functional update syntax.
Example
#[non_exhaustive(pub)]
pub struct Options {
pub a: bool,
pub b: bool,
}
impl Default for Options {
fn default() -> Options {
// omitted
}
}
// Crates other than the declaring crate may use functional update struct expressions
let options = Options { a: false, b: true, ..Default::default() }; // Allowed
// However, struct expression without functional update are still disallowed outside declaring crate
// let options = Options { a: false, b: true }; // Not allowed!
The idea would be that the #[non_exhaustive(pub)]
attribute would only apply to pub
fields and not to more private fields. Adding new public fields to the struct would be a non-breaking change. Private fields would still be considered exhaustive, and additional private fields would be a breaking change.