Mark StandardListViewItem and TableColumn as #[non_exhaustive]

Closes #2330
This commit is contained in:
Olivier Goffart 2023-03-13 11:55:37 +01:00 committed by Olivier Goffart
parent 45032716dd
commit 4cf44ea69d
4 changed files with 15 additions and 12 deletions

View file

@ -27,7 +27,7 @@ pub fn main() {
let items = Rc::new(VecModel::default());
for c in 1..5 {
items.push(StandardListViewItem { text: format!("Item {r}.{c}").into() });
items.push(slint::format!("Item {r}.{c}").into());
}
row_data.push(items.into());

View file

@ -1799,10 +1799,10 @@ fn compile_expression(expr: &Expression, ctx: &EvaluationContext) -> TokenStream
let fields = fields.iter().enumerate().map(|(index, (name, _))| {
let index = proc_macro2::Literal::usize_unsuffixed(index);
let name = ident(name);
quote!(#name: obj.#index as _)
quote!(the_struct.#name = obj.#index as _;)
});
let id = struct_name_to_tokens(n);
quote!({ let obj = #f; #id { #(#fields),*} })
quote!({ let obj = #f; let mut the_struct = #id::default(); #(#fields)* the_struct })
}
(Type::Array(..), Type::PathData)
if matches!(
@ -2057,10 +2057,10 @@ fn compile_expression(expr: &Expression, ctx: &EvaluationContext) -> TokenStream
if let Some(name) = name {
let name_tokens: TokenStream = struct_name_to_tokens(name.as_str());
let keys = fields.keys().map(|k| ident(k));
if name == "Point" {
quote!(#name_tokens{#(#keys: #elem as _,)* ..Default::default()})
if name.contains("LayoutData") {
quote!(#name_tokens{#(#keys: #elem as _,)*})
} else {
quote!(#name_tokens { #(#keys: #elem as _,)* })
quote!({ let mut the_struct = #name_tokens::default(); #(the_struct.#keys = #elem as _;)* the_struct})
}
} else {
let as_ = fields.values().map(|t| {

View file

@ -1118,6 +1118,7 @@ impl<C: RepeatedComponent + 'static> Repeater<C> {
/// the StandardListViewItem type in Slint files, when declaring for example a `property <[StandardListViewItem]> my-list-view-model;`.
#[repr(C)]
#[derive(Clone, Default, Debug, PartialEq)]
#[non_exhaustive]
pub struct StandardListViewItem {
/// The text content of the item.
pub text: SharedString,
@ -1138,6 +1139,7 @@ impl From<&str> for StandardListViewItem {
/// Represent an TableColumn header
#[repr(C)]
#[derive(Clone, Default, Debug, PartialEq)]
#[non_exhaustive]
pub struct TableColumn {
/// The title of the column header
pub title: SharedString,

View file

@ -238,10 +238,11 @@ macro_rules! declare_value_struct_conversion {
match v {
Value::Struct(x) => {
type Ty = $name;
Ok(Ty {
$($field: x.get_field(stringify!($field)).ok_or(())?.clone().try_into().map_err(|_|())?),*
$(, ..$extra)?
})
#[allow(unused)]
let mut res: Ty = Ty::default();
$(let mut res: Ty = $extra;)?
$(res.$field = x.get_field(stringify!($field)).ok_or(())?.clone().try_into().map_err(|_|())?;)*
Ok(res)
}
_ => Err(()),
}
@ -250,8 +251,8 @@ macro_rules! declare_value_struct_conversion {
};
}
declare_value_struct_conversion!(struct i_slint_core::model::StandardListViewItem { text });
declare_value_struct_conversion!(struct i_slint_core::model::TableColumn { title, min_width, horizontal_stretch, sort_order, width });
declare_value_struct_conversion!(struct i_slint_core::model::StandardListViewItem { text , ..Default::default()});
declare_value_struct_conversion!(struct i_slint_core::model::TableColumn { title, min_width, horizontal_stretch, sort_order, width, ..Default::default() });
declare_value_struct_conversion!(struct i_slint_core::properties::StateInfo { current_state, previous_state, change_time });
declare_value_struct_conversion!(struct i_slint_core::input::KeyboardModifiers { control, alt, shift, meta });
declare_value_struct_conversion!(struct i_slint_core::input::KeyEvent { text, modifiers, ..Default::default() });