[yew-devel] [PATCH proxmox-yew-widget-toolkit 1/1] store: implement FromIterator

Stefan Hanreich s.hanreich at proxmox.com
Wed Jan 22 16:49:22 CET 2025


Enables better ergonomics for building stores from iterators and
is generally considered nice to have for structs representing some
form of collection.

For example:

```
self.store = Store::new();

store.set_data(
   it.collect()
);
```

vs

```
self.store = it.collect()
```

or

```
Store::from_iter(it)
```

Signed-off-by: Stefan Hanreich <s.hanreich at proxmox.com>
---
 src/state/store.rs | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/state/store.rs b/src/state/store.rs
index e0ac683..11d7262 100644
--- a/src/state/store.rs
+++ b/src/state/store.rs
@@ -89,6 +89,18 @@ impl<T: ExtractPrimaryKey + 'static> Store<T> {
     }
 }
 
+impl<T: ExtractPrimaryKey + 'static> FromIterator<T> for Store<T> {
+    fn from_iter<A>(iter: A) -> Self
+    where
+        A: IntoIterator<Item = T>,
+    {
+        let store = Self::new();
+        store.set_data(iter.into_iter().collect());
+
+        store
+    }
+}
+
 impl<T: 'static> Store<T> {
     /// Creates a new instance with the specifies extract key function.
     pub fn with_extract_key(extract_key: impl Into<ExtractKeyFn<T>>) -> Self {
-- 
2.39.5




More information about the yew-devel mailing list