pub struct ModelRc<T>(/* private fields */);
Expand description
ModelRc is a type wrapper for a reference counted implementation of the Model
trait.
Models are used to represent sequences of the same data type. In .slint
code those
are represented using the [T]
array syntax and typically used in for
expressions,
array properties, and array struct fields.
For example, a property <[string]> foo
will be of type ModelRc<SharedString>
and, behind the scenes, wraps a Rc<dyn Model<Data = SharedString>>.
An array struct field will also be of type ModelRc
:
export struct AddressBook {
names: [string]
}
When accessing AddressBook
from Rust, the names
field will be of type ModelRc<SharedString>
.
There are several ways of constructing a ModelRc in Rust:
- An empty ModelRc can be constructed with
ModelRc::default()
. - A
ModelRc
can be constructed from a slice or an array using theFrom
trait. This allocates aVecModel
. - Use
ModelRc::new()
to construct aModelRc
from a type that implements theModel
trait, such asVecModel
or your own implementation. - If you have your model already in an
Rc
, then you can use theFrom
trait to convert fromRc<dyn Model<Data = T>>
toModelRc
.
§Example
use slint::{slint, SharedString, ModelRc, Model, VecModel};
use std::rc::Rc;
slint!{
import { Button } from "std-widgets.slint";
export component Example {
callback add_item <=> btn.clicked;
in property <[string]> the_model;
HorizontalLayout {
for it in the_model : Text { text: it; }
btn := Button { text: "Add"; }
}
}
}
let ui = Example::new().unwrap();
// Create a VecModel and put it in an Rc.
let the_model : Rc<VecModel<SharedString>> =
Rc::new(VecModel::from(vec!["Hello".into(), "World".into()]));
// Convert it to a ModelRc.
let the_model_rc = ModelRc::from(the_model.clone());
// Pass the model to the ui: The generated set_the_model setter from the
// the_model property takes a ModelRc.
ui.set_the_model(the_model_rc);
// We have kept a strong reference to the_model, to modify it in a callback.
ui.on_add_item(move || {
// Use VecModel API: VecModel uses the Model notification mechanism to let Slint
// know it needs to refresh the UI.
the_model.push("SomeValue".into());
});
// Alternative: we can re-use a getter.
let ui_weak = ui.as_weak();
ui.on_add_item(move || {
let ui = ui_weak.unwrap();
let the_model_rc = ui.get_the_model();
let the_model = the_model_rc.as_any().downcast_ref::<VecModel<SharedString>>()
.expect("We know we set a VecModel earlier");
the_model.push("An Item".into());
});
Implementations§
Trait Implementations§
§impl<T> Model for ModelRc<T>
impl<T> Model for ModelRc<T>
§fn row_data(&self, row: usize) -> Option<<ModelRc<T> as Model>::Data>
fn row_data(&self, row: usize) -> Option<<ModelRc<T> as Model>::Data>
Returns the data for a particular row. Read more
§fn set_row_data(&self, row: usize, data: <ModelRc<T> as Model>::Data)
fn set_row_data(&self, row: usize, data: <ModelRc<T> as Model>::Data)
Sets the data for a particular row. Read more
§fn model_tracker(&self) -> &dyn ModelTracker
fn model_tracker(&self) -> &dyn ModelTracker
The implementation should return a reference to its
ModelNotify
field. Read moreAuto Trait Implementations§
impl<T> Freeze for ModelRc<T>
impl<T> !RefUnwindSafe for ModelRc<T>
impl<T> !Send for ModelRc<T>
impl<T> !Sync for ModelRc<T>
impl<T> Unpin for ModelRc<T>
impl<T> !UnwindSafe for ModelRc<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<T> ModelExt for Twhere
T: Model,
impl<T> ModelExt for Twhere
T: Model,
§fn row_data_tracked(&self, row: usize) -> Option<Self::Data>
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§fn map<F, U>(self, map_function: F) -> MapModel<Self, F>
fn map<F, U>(self, map_function: F) -> MapModel<Self, F>
Returns a new Model where all elements are mapped by the function
map_function
.
This is a shortcut for MapModel::new()
.§fn filter<F>(self, filter_function: F) -> FilterModel<Self, F>
fn filter<F>(self, filter_function: F) -> FilterModel<Self, F>
Returns a new Model where the elements are filtered by the function
filter_function
.
This is a shortcut for FilterModel::new()
.§fn sort(self) -> SortModel<Self, AscendingSortHelper>
fn sort(self) -> SortModel<Self, AscendingSortHelper>
Returns a new Model where the elements are sorted ascending.
This is a shortcut for
SortModel::new_ascending()
.§fn sort_by<F>(self, sort_function: F) -> SortModel<Self, F>
fn sort_by<F>(self, sort_function: F) -> SortModel<Self, F>
Returns a new Model where the elements are sorted by the function
sort_function
.
This is a shortcut for SortModel::new()
.§fn reverse(self) -> ReverseModel<Self>where
Self: Sized + 'static,
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()
.§impl<T> NoneValue for Twhere
T: Default,
impl<T> NoneValue for Twhere
T: Default,
type NoneType = T
§fn null_value() -> T
fn null_value() -> T
The none-equivalent value.
§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian()
.