@@ -20,137 +20,18 @@ use smallvec::SmallVec;
20
20
21
21
use crate :: {
22
22
Accessed , AddOperation , Buffer , BufferSettings , Buffered , Builder , Chain ,
23
- CleanupWorkflowConditions , CloneFromBuffer , Join , Joined , Listen , Output , Scope , ScopeSettings ,
24
- UnusedTarget ,
23
+ CloneFromBuffer , Join , Joined , Output , UnusedTarget ,
25
24
} ;
26
25
27
26
pub type BufferKeys < B > = <<B as Bufferable >:: BufferType as Accessed >:: Key ;
28
27
pub type JoinedItem < B > = <<B as Bufferable >:: BufferType as Joined >:: Item ;
29
28
30
29
pub trait Bufferable {
31
- type BufferType : Joined + Accessed ;
30
+ type BufferType : Buffered ;
32
31
33
32
/// Convert these bufferable workflow elements into buffers if they are not
34
33
/// buffers already.
35
34
fn into_buffer ( self , builder : & mut Builder ) -> Self :: BufferType ;
36
-
37
- /// Join these bufferable workflow elements. Each time every buffer contains
38
- /// at least one element, this will pull the oldest element from each buffer
39
- /// and join them into a tuple that gets sent to the target.
40
- ///
41
- /// If you need a more general way to get access to one or more buffers,
42
- /// use [`listen`](Self::listen) instead.
43
- fn join < ' w , ' s , ' a , ' b > (
44
- self ,
45
- builder : & ' b mut Builder < ' w , ' s , ' a > ,
46
- ) -> Chain < ' w , ' s , ' a , ' b , JoinedItem < Self > >
47
- where
48
- Self : Sized ,
49
- Self :: BufferType : ' static + Send + Sync ,
50
- JoinedItem < Self > : ' static + Send + Sync ,
51
- {
52
- let scope = builder. scope ( ) ;
53
- let buffers = self . into_buffer ( builder) ;
54
- buffers. verify_scope ( scope) ;
55
-
56
- let join = builder. commands . spawn ( ( ) ) . id ( ) ;
57
- let target = builder. commands . spawn ( UnusedTarget ) . id ( ) ;
58
- builder. commands . add ( AddOperation :: new (
59
- Some ( scope) ,
60
- join,
61
- Join :: new ( buffers, target) ,
62
- ) ) ;
63
-
64
- Output :: new ( scope, target) . chain ( builder)
65
- }
66
-
67
- /// Create an operation that will output buffer access keys each time any
68
- /// one of the buffers is modified. This can be used to create a node in a
69
- /// workflow that wakes up every time one or more buffers change, and then
70
- /// operates on those buffers.
71
- ///
72
- /// For an operation that simply joins the contents of two or more outputs
73
- /// or buffers, use [`join`](Self::join) instead.
74
- fn listen < ' w , ' s , ' a , ' b > (
75
- self ,
76
- builder : & ' b mut Builder < ' w , ' s , ' a > ,
77
- ) -> Chain < ' w , ' s , ' a , ' b , BufferKeys < Self > >
78
- where
79
- Self : Sized ,
80
- Self :: BufferType : ' static + Send + Sync ,
81
- BufferKeys < Self > : ' static + Send + Sync ,
82
- {
83
- let scope = builder. scope ( ) ;
84
- let buffers = self . into_buffer ( builder) ;
85
- buffers. verify_scope ( scope) ;
86
-
87
- let listen = builder. commands . spawn ( ( ) ) . id ( ) ;
88
- let target = builder. commands . spawn ( UnusedTarget ) . id ( ) ;
89
- builder. commands . add ( AddOperation :: new (
90
- Some ( scope) ,
91
- listen,
92
- Listen :: new ( buffers, target) ,
93
- ) ) ;
94
-
95
- Output :: new ( scope, target) . chain ( builder)
96
- }
97
-
98
- /// Alternative way to call [`Builder::on_cleanup`].
99
- fn on_cleanup < Settings > (
100
- self ,
101
- builder : & mut Builder ,
102
- build : impl FnOnce ( Scope < BufferKeys < Self > , ( ) , ( ) > , & mut Builder ) -> Settings ,
103
- ) where
104
- Self : Sized ,
105
- Self :: BufferType : ' static + Send + Sync ,
106
- BufferKeys < Self > : ' static + Send + Sync ,
107
- Settings : Into < ScopeSettings > ,
108
- {
109
- builder. on_cleanup ( self , build)
110
- }
111
-
112
- /// Alternative way to call [`Builder::on_cancel`].
113
- fn on_cancel < Settings > (
114
- self ,
115
- builder : & mut Builder ,
116
- build : impl FnOnce ( Scope < BufferKeys < Self > , ( ) , ( ) > , & mut Builder ) -> Settings ,
117
- ) where
118
- Self : Sized ,
119
- Self :: BufferType : ' static + Send + Sync ,
120
- BufferKeys < Self > : ' static + Send + Sync ,
121
- Settings : Into < ScopeSettings > ,
122
- {
123
- builder. on_cancel ( self , build)
124
- }
125
-
126
- /// Alternative way to call [`Builder::on_terminate`].
127
- fn on_terminate < Settings > (
128
- self ,
129
- builder : & mut Builder ,
130
- build : impl FnOnce ( Scope < BufferKeys < Self > , ( ) , ( ) > , & mut Builder ) -> Settings ,
131
- ) where
132
- Self : Sized ,
133
- Self :: BufferType : ' static + Send + Sync ,
134
- BufferKeys < Self > : ' static + Send + Sync ,
135
- Settings : Into < ScopeSettings > ,
136
- {
137
- builder. on_terminate ( self , build)
138
- }
139
-
140
- /// Alternative way to call [`Builder::on_cleanup_if`].
141
- fn on_cleanup_if < Settings > (
142
- self ,
143
- builder : & mut Builder ,
144
- conditions : CleanupWorkflowConditions ,
145
- build : impl FnOnce ( Scope < BufferKeys < Self > , ( ) , ( ) > , & mut Builder ) -> Settings ,
146
- ) where
147
- Self : Sized ,
148
- Self :: BufferType : ' static + Send + Sync ,
149
- BufferKeys < Self > : ' static + Send + Sync ,
150
- Settings : Into < ScopeSettings > ,
151
- {
152
- builder. on_cleanup_if ( conditions, self , build)
153
- }
154
35
}
155
36
156
37
impl < T : ' static + Send + Sync > Bufferable for Buffer < T > {
@@ -179,6 +60,54 @@ impl<T: 'static + Send + Sync> Bufferable for Output<T> {
179
60
}
180
61
}
181
62
63
+ pub trait Joinable : Bufferable {
64
+ type Item ;
65
+
66
+ fn join < ' w , ' s , ' a , ' b > (
67
+ self ,
68
+ builder : & ' b mut Builder < ' w , ' s , ' a > ,
69
+ ) -> Chain < ' w , ' s , ' a , ' b , Self :: Item > ;
70
+ }
71
+
72
+ impl < B > Joinable for B
73
+ where
74
+ B : Bufferable ,
75
+ B :: BufferType : Joined ,
76
+ {
77
+ type Item = JoinedItem < B > ;
78
+
79
+ fn join < ' w , ' s , ' a , ' b > (
80
+ self ,
81
+ builder : & ' b mut Builder < ' w , ' s , ' a > ,
82
+ ) -> Chain < ' w , ' s , ' a , ' b , Self :: Item > {
83
+ self . into_buffer ( builder) . join ( builder)
84
+ }
85
+ }
86
+
87
+ pub trait Accessible : Bufferable {
88
+ type Keys ;
89
+
90
+ fn listen < ' w , ' s , ' a , ' b > (
91
+ self ,
92
+ builder : & ' b mut Builder < ' w , ' s , ' a > ,
93
+ ) -> Chain < ' w , ' s , ' a , ' b , Self :: Keys > ;
94
+ }
95
+
96
+ impl < B > Accessible for B
97
+ where
98
+ B : Bufferable ,
99
+ B :: BufferType : Accessed ,
100
+ {
101
+ type Keys = BufferKeys < Self > ;
102
+
103
+ fn listen < ' w , ' s , ' a , ' b > (
104
+ self ,
105
+ builder : & ' b mut Builder < ' w , ' s , ' a > ,
106
+ ) -> Chain < ' w , ' s , ' a , ' b , Self :: Keys > {
107
+ self . into_buffer ( builder) . listen ( builder)
108
+ }
109
+ }
110
+
182
111
macro_rules! impl_bufferable_for_tuple {
183
112
( $( $T: ident) ,* ) => {
184
113
#[ allow( non_snake_case) ]
@@ -248,6 +177,7 @@ impl<T> IterBufferable for T
248
177
where
249
178
T : IntoIterator ,
250
179
T :: Item : Bufferable ,
180
+ <T :: Item as Bufferable >:: BufferType : Joined ,
251
181
{
252
182
type BufferElement = <T :: Item as Bufferable >:: BufferType ;
253
183
0 commit comments