actify/extensions/vec.rs
1use crate as actify;
2use actify_macros::actify;
3use core::ops::RangeBounds;
4use std::fmt::Debug;
5
6trait ActorVec<T> {
7 fn push(&mut self, value: T);
8
9 fn is_empty(&self) -> bool;
10
11 fn drain<R>(&mut self, range: R) -> Vec<T>
12 where
13 R: RangeBounds<usize> + Send + Sync + 'static;
14}
15
16#[actify]
17impl<T> ActorVec<T> for Vec<T>
18where
19 T: Clone + Debug + Send + Sync + 'static,
20{
21 /// Appends an element to the back of a collection.
22 ///
23 /// # Examples
24 ///
25 /// ```
26 /// # use tokio;
27 /// # #[tokio::test]
28 /// # async fn push_to_actor() {
29 /// let handle = actify::Handle::new(vec![1, 2]);
30 /// handle.push(100).await.unwrap();
31 /// assert_eq!(handle.get().await.unwrap(), vec![1, 2, 100]);
32 /// # }
33 /// ```
34 fn push(&mut self, value: T) {
35 self.push(value)
36 }
37
38 /// Returns `true` if the vector contains no elements.
39 ///
40 /// # Examples
41 ///
42 /// ```
43 /// # use tokio;
44 /// # use actify;
45 /// # #[tokio::test]
46 /// # async fn actor_vec_is_empty() {
47 /// let handle = Handle::new(Vec::<i32>::new());
48 /// assert!(handle.is_empty().await.unwrap());
49 /// # }
50 /// ```
51 fn is_empty(&self) -> bool {
52 self.is_empty()
53 }
54
55 /// Removes the complete range from the vector in bulk, and returns it as a new vector
56 ///
57 /// # Examples
58 ///
59 /// ```
60 /// # use tokio;
61 /// # #[tokio::test]
62 /// # async fn drain_actor() {
63 /// let handle = Handle::new(vec![1, 2]);
64 /// let res = handle.drain(..).await.unwrap();
65 /// assert_eq!(res, vec![1, 2]);
66 /// assert_eq!(handle.get().await.unwrap(), Vec::<i32>::new());
67 /// # }
68 /// ```
69 fn drain<R>(&mut self, range: R) -> Vec<T>
70 where
71 R: RangeBounds<usize> + Send + Sync + 'static,
72 {
73 self.drain(range).collect()
74 }
75}