actify/extensions/
set.rs

1use crate as actify;
2use actify_macros::actify;
3use std::collections::HashSet;
4use std::fmt::Debug;
5use std::hash::Hash;
6
7trait ActorSet<K> {
8    fn insert(&mut self, val: K) -> bool;
9
10    fn is_empty(&self) -> bool;
11}
12
13#[actify]
14impl<K> ActorSet<K> for HashSet<K>
15where
16    K: Clone + Debug + Eq + Hash + Send + Sync + 'static,
17{
18    /// Adds a value to the set.
19    /// Returns whether the value was newly inserted. That is:
20    /// - If the set did not previously contain this value, true is returned.
21    /// - If the set already contained this value, false is returned, and the set is not modified: original value is not replaced, and the value passed as argument is dropped.
22    /// # Examples
23    ///
24    /// ```
25    /// # use tokio;
26    /// # use actify::Handle;
27    /// # use std::collections::HashSet;
28    /// # #[tokio::test]
29    /// # async fn insert_at_actor() {
30    /// let handle = Handle::new(HashSet::new());
31    /// let res = handle.insert(10).await.unwrap();
32    /// assert_eq!(res, true);
33    /// # }
34    ///
35    /// # #[tokio::test]
36    /// # async fn insert_already_exists_at_actor() {
37    /// let handle = actify::Handle::new(std::collections::HashSet::new());
38    /// handle.insert(10).await.unwrap();
39    /// let res = handle.insert(10).await.unwrap();
40    /// assert_eq!(res, false);
41    /// # }
42    /// ```
43    fn insert(&mut self, val: K) -> bool {
44        self.insert(val)
45    }
46
47    /// Returns `true` if the set contains no elements.
48    ///
49    /// # Examples
50    ///
51    /// ```
52    /// # use tokio;
53    /// # use actify::Handle;
54    /// # use std::collections::HashSet;
55    /// # #[tokio::test]
56    /// # async fn actor_hash_is_empty() {
57    /// let handle = Handle::new(HashSet::<i32>::new());
58    /// assert!(handle.is_empty().await.unwrap());
59    /// # }
60    /// ```
61    fn is_empty(&self) -> bool {
62        self.is_empty()
63    }
64}