I'm working on a project where I have a struct for which I want a default implementation.
#[derive(Debug)]
struct State {
view_type: ViewType,
booking_state: booking::State,
calendar_state: calendar::State,
history_state: history::State,
configurator_state: configurator::State,
theme: ds::widget::ComboBoxState<iced::Theme>, // iced::Theme doesn't impl IntoEnumIter so default implementation cannot be used
}
All fields in this struct except the theme (which due to some trait limitations can't) implement Default. Meaning that I can't derive Default and have to implement it manually:
impl std::default::Default for State {
fn default() -> Self {
Self {
view_type: ViewType::default(),
booking_state: booking::State::default(),
calendar_state: calendar::State::default(),
history_state: history::State::default(),
configurator_state: configurator::State::default(),
theme: ds::widget::ComboBoxState::new(
wgt::combo_box::State::new(iced::Theme::ALL.to_vec()),
Some(iced::Theme::Dark),
),
}
}
}
Is there a way to do this where I just define the default for the theme field and tell rust to use the default values for the remaining fields? I.e. something like:
impl std::default::Default for State {
fn default() -> Self {
Self {
// view_type: ViewType::default(),
// booking_state: booking::State::default(),
// calendar_state: calendar::State::default(),
// history_state: history::State::default(),
// configurator_state: configurator::State::default(),
theme: ds::widget::ComboBoxState::new(
wgt::combo_box::State::new(iced::Theme::ALL.to_vec()),
Some(iced::Theme::Dark),
),
..Default::default() // recurses rather than using default values for remaining fields.
}
}
}
Self { theme: ComboBoxState::new(...), state: Default::default() }, although the cure may be worse than the disease.