I have a non-exhaustive struct like this:
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Config {
pub event_interval: u32,
pub ring_entries: u32,
#[cfg(target_os = "linux")]
pub mode: Mode,
}
While documenting the struct I noticed the following doc example didn't compile:
let config = Config {
#[cfg(target_os = "linux")]
mode: Mode::Polling { idle_timeout: 100 },
.. Config::default()
};
let runtime = config.build()?;
runtime.block_on(async {
/* ... */
});
That is because base structs cannot be used with non exhaustive structs, since base structs are considered struct expressions.
error[E0639]: cannot create non-exhaustive struct using struct expression
--> src/runtime/config.rs:14:14
|
7 | let config = Config {
| ______________^
8 | | #[cfg(target_os = "linux")]
9 | | mode: Mode::Polling { idle_timeout: 100 },
10 | | .. Config::default()
11 | | };
| |_^
error: aborting due to previous error
using a dummy priv_: ()
field didn't work either. It would be nice if this were allowed, since this case is not actually broken when fields are added to the type. And I feel that base struct initialization often looks cleaner than the builder types pattern.