FactoryHashMap

Struct FactoryHashMap 

Source
pub struct FactoryHashMap<K, C: FactoryComponent, S = RandomState> { /* private fields */ }
Expand description

A container similar to HashMap that can be used to store values of type FactoryComponent.

Implementations§

Source§

impl<K, C> FactoryHashMap<K, C, RandomState>

Source

pub fn builder() -> FactoryHashMapBuilder<K, C>

Creates a new FactoryHashMap.

Examples found in repository?
relm4/examples/factory_hash_map.rs (line 149)
144    fn init(
145        counter: Self::Init,
146        root: Self::Root,
147        sender: ComponentSender<Self>,
148    ) -> ComponentParts<Self> {
149        let counters = FactoryHashMap::builder().launch_default().detach();
150
151        let model = App {
152            created_widgets: counter,
153            counters,
154            entry_buffer: gtk::EntryBuffer::default(),
155        };
156
157        let counter_stack = model.counters.widget();
158        let widgets = view_output!();
159
160        ComponentParts { model, widgets }
161    }
Source§

impl<K, C, S> FactoryHashMap<K, C, S>

Source

pub fn len(&self) -> usize

Returns the number of elements in the FactoryHashMap.

Source

pub fn is_empty(&self) -> bool

Returns true if the FactoryHashMap is empty.

Source

pub fn broadcast(&self, msg: C::Input)
where C::Input: Clone,

Send clone of a message to all of the elements.

Source

pub const fn widget(&self) -> &C::ParentWidget

Returns the widget all components are attached to.

Examples found in repository?
relm4/examples/factory_hash_map.rs (line 157)
144    fn init(
145        counter: Self::Init,
146        root: Self::Root,
147        sender: ComponentSender<Self>,
148    ) -> ComponentParts<Self> {
149        let counters = FactoryHashMap::builder().launch_default().detach();
150
151        let model = App {
152            created_widgets: counter,
153            counters,
154            entry_buffer: gtk::EntryBuffer::default(),
155        };
156
157        let counter_stack = model.counters.widget();
158        let widgets = view_output!();
159
160        ComponentParts { model, widgets }
161    }
Source

pub fn iter(&self) -> impl ExactSizeIterator<Item = (&K, &C)> + FusedIterator

An iterator visiting all key-value pairs in arbitrary order.

Source

pub fn values(&self) -> impl ExactSizeIterator<Item = &C> + FusedIterator

Returns an iterator over the factory components.

Source

pub fn keys(&self) -> impl ExactSizeIterator<Item = &K> + FusedIterator

Returns an iterator over the keys of the hash map.

Source

pub fn clear(&mut self)

Clears the map, removing all factory components.

Source§

impl<K, C> FactoryHashMap<K, C, RandomState>
where C: FactoryComponent<Index = K>, K: Hash + Eq,

Source

pub fn from_vec( component_vec: Vec<(K, C::Init)>, widget: C::ParentWidget, ) -> Self

Creates a FactoryHashMap from a Vec.

Source§

impl<K, C, S> FactoryHashMap<K, C, S>
where C: FactoryComponent<Index = K>, K: Hash + Eq, S: BuildHasher,

Source

pub fn send(&self, key: &K, msg: C::Input)

Send a mage to one of the elements.

Source

pub fn get(&self, key: &K) -> Option<&C>

Tries to get an immutable reference to the model of one element.

Returns None if key is invalid.

Source

pub fn get_mut(&mut self, key: &K) -> Option<FactoryElementGuard<'_, C>>

Tries to get a mutable reference to the model of one element.

Returns None if key is invalid.

Examples found in repository?
relm4/examples/factory_hash_map.rs (line 175)
163    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
164        match msg {
165            AppMsg::AddCounter => {
166                let index = self.entry_buffer.text().to_string();
167                if !index.is_empty() {
168                    self.counters.insert(index.clone(), self.created_widgets);
169                    // Change focus to the currently created element
170                    self.counters.set_visible(&index);
171                    self.created_widgets = self.created_widgets.wrapping_add(1);
172                }
173            }
174            AppMsg::Increment(key) => {
175                let mut elem = self.counters.get_mut(&key).unwrap();
176                elem.value = elem.value.saturating_add(1);
177            }
178            AppMsg::Decrement(key) => {
179                let mut elem = self.counters.get_mut(&key).unwrap();
180                elem.value = elem.value.saturating_sub(1);
181            }
182            AppMsg::RemoveCounter(key) => {
183                self.counters.remove(&key);
184            }
185            AppMsg::UpdateView => (),
186        }
187    }
Source

pub fn insert(&mut self, key: K, init: C::Init) -> Option<C>

Inserts a new factory component into the map.

If the map did not have this key present, None is returned.

If the map did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be == without being identical. See the module-level documentation for more.

Examples found in repository?
relm4/examples/factory_hash_map.rs (line 168)
163    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
164        match msg {
165            AppMsg::AddCounter => {
166                let index = self.entry_buffer.text().to_string();
167                if !index.is_empty() {
168                    self.counters.insert(index.clone(), self.created_widgets);
169                    // Change focus to the currently created element
170                    self.counters.set_visible(&index);
171                    self.created_widgets = self.created_widgets.wrapping_add(1);
172                }
173            }
174            AppMsg::Increment(key) => {
175                let mut elem = self.counters.get_mut(&key).unwrap();
176                elem.value = elem.value.saturating_add(1);
177            }
178            AppMsg::Decrement(key) => {
179                let mut elem = self.counters.get_mut(&key).unwrap();
180                elem.value = elem.value.saturating_sub(1);
181            }
182            AppMsg::RemoveCounter(key) => {
183                self.counters.remove(&key);
184            }
185            AppMsg::UpdateView => (),
186        }
187    }
Source

pub fn remove(&mut self, key: &K) -> Option<C>

Removes a key from the map, returning the factory component at the key if the key was previously in the map.

Examples found in repository?
relm4/examples/factory_hash_map.rs (line 183)
163    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
164        match msg {
165            AppMsg::AddCounter => {
166                let index = self.entry_buffer.text().to_string();
167                if !index.is_empty() {
168                    self.counters.insert(index.clone(), self.created_widgets);
169                    // Change focus to the currently created element
170                    self.counters.set_visible(&index);
171                    self.created_widgets = self.created_widgets.wrapping_add(1);
172                }
173            }
174            AppMsg::Increment(key) => {
175                let mut elem = self.counters.get_mut(&key).unwrap();
176                elem.value = elem.value.saturating_add(1);
177            }
178            AppMsg::Decrement(key) => {
179                let mut elem = self.counters.get_mut(&key).unwrap();
180                elem.value = elem.value.saturating_sub(1);
181            }
182            AppMsg::RemoveCounter(key) => {
183                self.counters.remove(&key);
184            }
185            AppMsg::UpdateView => (),
186        }
187    }
Source§

impl<K, C> FactoryHashMap<K, C, RandomState>

Source

pub fn set_visible(&self, key: &K) -> bool

Makes the element at a given key visible in a gtk::Stack. Returns true on success, otherwise false.

Examples found in repository?
relm4/examples/factory_hash_map.rs (line 170)
163    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
164        match msg {
165            AppMsg::AddCounter => {
166                let index = self.entry_buffer.text().to_string();
167                if !index.is_empty() {
168                    self.counters.insert(index.clone(), self.created_widgets);
169                    // Change focus to the currently created element
170                    self.counters.set_visible(&index);
171                    self.created_widgets = self.created_widgets.wrapping_add(1);
172                }
173            }
174            AppMsg::Increment(key) => {
175                let mut elem = self.counters.get_mut(&key).unwrap();
176                elem.value = elem.value.saturating_add(1);
177            }
178            AppMsg::Decrement(key) => {
179                let mut elem = self.counters.get_mut(&key).unwrap();
180                elem.value = elem.value.saturating_sub(1);
181            }
182            AppMsg::RemoveCounter(key) => {
183                self.counters.remove(&key);
184            }
185            AppMsg::UpdateView => (),
186        }
187    }

Trait Implementations§

Source§

impl<K, C> Clone for FactoryHashMap<K, C, RandomState>
where C: CloneableFactoryComponent + FactoryComponent<Index = K>, K: Clone + Hash + Eq,

Implements the Clone Trait for FactoryHashMap if the component implements CloneableFactoryComponent.

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, C: Debug + FactoryComponent, S: Debug> Debug for FactoryHashMap<K, C, S>
where C::ParentWidget: Debug, C::Output: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, C, S> Drop for FactoryHashMap<K, C, S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<K, C, S> Index<&K> for FactoryHashMap<K, C, S>
where C: FactoryComponent<Index = K>, K: Hash + Eq, S: BuildHasher,

Source§

type Output = C

The returned type after indexing.
Source§

fn index(&self, key: &K) -> &Self::Output

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<K, C, S> Freeze for FactoryHashMap<K, C, S>

§

impl<K, C, S = RandomState> !RefUnwindSafe for FactoryHashMap<K, C, S>

§

impl<K, C, S = RandomState> !Send for FactoryHashMap<K, C, S>

§

impl<K, C, S = RandomState> !Sync for FactoryHashMap<K, C, S>

§

impl<K, C, S> Unpin for FactoryHashMap<K, C, S>

§

impl<K, C, S = RandomState> !UnwindSafe for FactoryHashMap<K, C, S>

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<C> AsyncPosition<()> for C

Source§

fn position(_index: usize)

Returns the position. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<C, I> Position<(), I> for C

Source§

fn position(&self, _index: &I)

Returns the position. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more