public inbox for yew-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Stefan Hanreich <s.hanreich@proxmox.com>
To: yew-devel@lists.proxmox.com
Subject: [yew-devel] [PATCH proxmox-yew-widget-toolkit 1/1] store: implement FromIterator
Date: Wed, 22 Jan 2025 16:49:22 +0100	[thread overview]
Message-ID: <20250122154922.270644-1-s.hanreich@proxmox.com> (raw)

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@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


_______________________________________________
yew-devel mailing list
yew-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/yew-devel


             reply	other threads:[~2025-01-22 15:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-22 15:49 Stefan Hanreich [this message]
2025-01-25 16:57 ` [yew-devel] applied: " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250122154922.270644-1-s.hanreich@proxmox.com \
    --to=s.hanreich@proxmox.com \
    --cc=yew-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal