i_slint_core::model

Struct MapModel

Source
pub struct MapModel<M, F> { /* private fields */ }
Expand description

Provides rows that are generated by a map function based on the rows of another Model

When the other Model is updated, the MapModel is updated accordingly.

Generic parameters:

  • M the type of the wrapped Model.
  • F the map function.

§Example

Here we have a VecModel holding rows of a custom type Name. It is then mapped into a MapModel of SharedStrings

#[derive(Clone)]
struct Name {
    first: String,
    last: String,
}

let model = VecModel::from(vec![
    Name { first: "Hans".to_string(), last: "Emil".to_string() },
    Name { first: "Max".to_string(), last: "Mustermann".to_string() },
    Name { first: "Roman".to_string(), last: "Tisch".to_string() },
]);

let mapped_model = MapModel::new(model, |n|
    slint::format!("{}, {}", n.last, n.first)
);

assert_eq!(mapped_model.row_data(0).unwrap(), SharedString::from("Emil, Hans"));
assert_eq!(mapped_model.row_data(1).unwrap(), SharedString::from("Mustermann, Max"));
assert_eq!(mapped_model.row_data(2).unwrap(), SharedString::from("Tisch, Roman"));

Alternatively you can use the shortcut ModelExt::map.

let mapped_model = VecModel::from(vec![
    Name { first: "Hans".to_string(), last: "Emil".to_string() },
    Name { first: "Max".to_string(), last: "Mustermann".to_string() },
    Name { first: "Roman".to_string(), last: "Tisch".to_string() },
])
.map(|n| slint::format!("{}, {}", n.last, n.first));

If you want to modify the underlying VecModel you can give it a Rc of the MapModel:

let model = Rc::new(VecModel::from(vec![
    Name { first: "Hans".to_string(), last: "Emil".to_string() },
    Name { first: "Max".to_string(), last: "Mustermann".to_string() },
    Name { first: "Roman".to_string(), last: "Tisch".to_string() },
]));

let mapped_model = MapModel::new(model.clone(), |n|
    slint::format!("{}, {}", n.last, n.first)
);

model.set_row_data(1, Name { first: "Minnie".to_string(), last: "Musterfrau".to_string() });

assert_eq!(mapped_model.row_data(0).unwrap(), SharedString::from("Emil, Hans"));
assert_eq!(mapped_model.row_data(1).unwrap(), SharedString::from("Musterfrau, Minnie"));
assert_eq!(mapped_model.row_data(2).unwrap(), SharedString::from("Tisch, Roman"));

Implementations§

Source§

impl<M, F, T, U> MapModel<M, F>
where M: 'static + Model<Data = T>, F: 'static + Fn(T) -> U,

Source

pub fn new(wrapped_model: M, map_function: F) -> Self

Creates a new MapModel based on the given wrapped_model and map_function. Alternatively you can use ModelExt::map on your Model.

Source

pub fn source_model(&self) -> &M

Returns a reference to the inner model

Trait Implementations§

Source§

impl<M, F, T, U> Model for MapModel<M, F>
where M: 'static + Model<Data = T>, F: 'static + Fn(T) -> U,

Source§

type Data = U

The model data: A model is a set of rows and each row has this data
Source§

fn row_count(&self) -> usize

The number of rows in the model
Source§

fn row_data(&self, row: usize) -> Option<Self::Data>

Returns the data for a particular row. Read more
Source§

fn model_tracker(&self) -> &dyn ModelTracker

The implementation should return a reference to its ModelNotify field. Read more
Source§

fn as_any(&self) -> &dyn Any

Return something that can be downcast’ed (typically self) Read more
Source§

fn set_row_data(&self, _row: usize, _data: Self::Data)

Sets the data for a particular row. Read more
Source§

fn iter(&self) -> ModelIterator<'_, Self::Data>
where Self: Sized,

Returns an iterator visiting all elements of the model.

Auto Trait Implementations§

§

impl<M, F> Freeze for MapModel<M, F>
where M: Freeze, F: Freeze,

§

impl<M, F> RefUnwindSafe for MapModel<M, F>

§

impl<M, F> Send for MapModel<M, F>
where M: Send, F: Send,

§

impl<M, F> Sync for MapModel<M, F>
where M: Sync, F: Sync,

§

impl<M, F> Unpin for MapModel<M, F>
where M: Unpin, F: Unpin,

§

impl<M, F> UnwindSafe for MapModel<M, F>
where M: UnwindSafe, F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ModelExt for T
where T: Model,

Source§

fn row_data_tracked(&self, row: usize) -> Option<Self::Data>

Convenience function that calls ModelTracker::track_row_data_changes before returning Model::row_data. Read more
Source§

fn map<F, U>(self, map_function: F) -> MapModel<Self, F>
where Self: Sized + 'static, F: Fn(Self::Data) -> U + 'static,

Returns a new Model where all elements are mapped by the function map_function. This is a shortcut for MapModel::new().
Source§

fn filter<F>(self, filter_function: F) -> FilterModel<Self, F>
where Self: Sized + 'static, F: Fn(&Self::Data) -> bool + 'static,

Returns a new Model where the elements are filtered by the function filter_function. This is a shortcut for FilterModel::new().
Source§

fn sort(self) -> SortModel<Self, AscendingSortHelper>
where Self: Sized + 'static, Self::Data: Ord,

Returns a new Model where the elements are sorted ascending. This is a shortcut for SortModel::new_ascending().
Source§

fn sort_by<F>(self, sort_function: F) -> SortModel<Self, F>
where Self: Sized + 'static, F: FnMut(&Self::Data, &Self::Data) -> Ordering + 'static,

Returns a new Model where the elements are sorted by the function sort_function. This is a shortcut for SortModel::new().
Source§

fn reverse(self) -> ReverseModel<Self>
where Self: Sized + 'static,

Returns a new Model where the elements are reversed. This is a shortcut for ReverseModel::new().
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.