futures_signals_ext/
spawn.rs

1use std::future::Future;
2
3use futures_signals::signal_vec::{MutableVec, MutableVecLockMut, VecDiff};
4
5pub trait SignalSpawn<A> {
6    #[cfg(feature = "spawn")]
7    fn spawn<F>(self, f: F)
8    where
9        Self: Send,
10        F: Fn(A) + Send + 'static;
11
12    #[cfg(feature = "spawn")]
13    fn spawn_fut<F, W>(self, f: F)
14    where
15        Self: Send,
16        F: Fn(A) -> W + Send + 'static,
17        W: Future<Output = ()> + Send + 'static;
18
19    #[cfg(feature = "spawn-local")]
20    fn spawn_local<F>(self, f: F)
21    where
22        F: Fn(A) + 'static;
23
24    #[cfg(feature = "spawn-local")]
25    fn spawn_local_fut<F, W>(self, f: F)
26    where
27        F: Fn(A) -> W + 'static,
28        W: Future<Output = ()> + 'static;
29}
30
31pub trait SignalVecSpawn<A> {
32    #[cfg(feature = "spawn")]
33    fn spawn<F>(self, f: F)
34    where
35        Self: Send,
36        F: Fn(VecDiff<A>) + Send + 'static;
37
38    #[cfg(feature = "spawn")]
39    fn feed(self, target: MutableVec<A>)
40    where
41        Self: Sized + Send,
42        A: Copy + Send + Sync + 'static,
43    {
44        self.spawn(move |diff| {
45            MutableVecLockMut::apply_vec_diff(&mut target.lock_mut(), diff);
46        });
47    }
48
49    #[cfg(feature = "spawn")]
50    fn feed_cloned(self, target: MutableVec<A>)
51    where
52        Self: Sized + Send,
53        A: Clone + Send + Sync + 'static,
54    {
55        self.spawn(move |diff| {
56            MutableVecLockMut::apply_vec_diff(&mut target.lock_mut(), diff);
57        });
58    }
59
60    #[cfg(feature = "spawn-local")]
61    fn spawn_local<F>(self, f: F)
62    where
63        F: Fn(VecDiff<A>) + 'static;
64
65    #[cfg(feature = "spawn-local")]
66    fn feed_local(self, target: MutableVec<A>)
67    where
68        Self: Sized,
69        A: Copy + 'static,
70    {
71        self.spawn_local(move |diff| {
72            MutableVecLockMut::apply_vec_diff(&mut target.lock_mut(), diff);
73        });
74    }
75
76    #[cfg(feature = "spawn-local")]
77    fn feed_local_cloned(self, target: MutableVec<A>)
78    where
79        Self: Sized,
80        A: Clone + 'static,
81    {
82        self.spawn_local(move |diff| {
83            MutableVecLockMut::apply_vec_diff(&mut target.lock_mut(), diff);
84        });
85    }
86}
87
88#[cfg(not(target_os = "unknown"))]
89mod os {
90    use std::future::{Future, ready};
91
92    use futures_signals::{
93        signal::{Signal, SignalExt},
94        signal_vec::{SignalVec, SignalVecExt, VecDiff},
95    };
96
97    use crate::{SignalSpawn, SignalVecSpawn};
98
99    impl<A, S> SignalSpawn<A> for S
100    where
101        S: Signal<Item = A> + 'static,
102    {
103        #[cfg(feature = "spawn")]
104        fn spawn<F>(self, f: F)
105        where
106            Self: Send,
107            F: Fn(A) + Send + 'static,
108        {
109            self.spawn_fut(move |new| {
110                f(new);
111                ready(())
112            });
113        }
114
115        #[cfg(feature = "spawn")]
116        fn spawn_fut<F, W>(self, f: F)
117        where
118            Self: Send,
119            F: Fn(A) -> W + Send + 'static,
120            W: Future<Output = ()> + Send + 'static,
121        {
122            artwrap::spawn(self.for_each(move |new| f(new)));
123        }
124
125        #[cfg(feature = "spawn-local")]
126        fn spawn_local<F>(self, f: F)
127        where
128            F: Fn(A) + 'static,
129        {
130            self.spawn_local_fut(move |new| {
131                f(new);
132                ready(())
133            });
134        }
135
136        #[cfg(feature = "spawn-local")]
137        fn spawn_local_fut<F, W>(self, f: F)
138        where
139            F: Fn(A) -> W + 'static,
140            W: Future<Output = ()> + 'static,
141        {
142            artwrap::spawn_local(self.for_each(f));
143        }
144    }
145
146    impl<A, S> SignalVecSpawn<A> for S
147    where
148        S: SignalVec<Item = A> + 'static,
149    {
150        #[cfg(feature = "spawn")]
151        fn spawn<F>(self, f: F)
152        where
153            Self: Send,
154            F: Fn(VecDiff<A>) + Send + 'static,
155        {
156            artwrap::spawn(self.for_each(move |new| {
157                f(new);
158                ready(())
159            }));
160        }
161
162        #[cfg(feature = "spawn-local")]
163        fn spawn_local<F>(self, f: F)
164        where
165            F: Fn(VecDiff<A>) + 'static,
166        {
167            artwrap::spawn_local(self.for_each(move |new| {
168                f(new);
169                ready(())
170            }));
171        }
172    }
173}
174
175#[cfg(all(target_arch = "wasm32", feature = "spawn-local"))]
176mod wasm {
177    use std::future::{Future, ready};
178
179    use futures_signals::{
180        signal::{Signal, SignalExt},
181        signal_vec::{SignalVec, SignalVecExt, VecDiff},
182    };
183
184    use crate::{SignalSpawn, SignalVecSpawn};
185
186    impl<A, S> SignalSpawn<A> for S
187    where
188        S: Signal<Item = A> + 'static,
189    {
190        #[cfg(feature = "spawn")]
191        fn spawn<F>(self, _: F)
192        where
193            Self: Send,
194            F: Fn(A) + Send + 'static,
195        {
196            const { unimplemented!() }
197        }
198
199        #[cfg(feature = "spawn")]
200        fn spawn_fut<F, W>(self, _: F)
201        where
202            Self: Send,
203            F: Fn(A) -> W + Send + 'static,
204            W: Future<Output = ()> + Send + 'static,
205        {
206            const { unimplemented!() }
207        }
208
209        #[cfg(feature = "spawn-local")]
210        fn spawn_local<F>(self, f: F)
211        where
212            F: Fn(A) + 'static,
213        {
214            self.spawn_local_fut(move |new| {
215                f(new);
216                ready(())
217            });
218        }
219
220        #[cfg(feature = "spawn-local")]
221        fn spawn_local_fut<F, W>(self, f: F)
222        where
223            F: Fn(A) -> W + 'static,
224            W: Future<Output = ()> + 'static,
225        {
226            artwrap::spawn_local(self.for_each(move |new| f(new)));
227        }
228    }
229
230    impl<A, S> SignalVecSpawn<A> for S
231    where
232        S: SignalVec<Item = A> + 'static,
233    {
234        #[cfg(feature = "spawn")]
235        fn spawn<F>(self, _: F)
236        where
237            Self: Send,
238            F: Fn(VecDiff<A>) + Send + 'static,
239        {
240            const { unimplemented!() }
241        }
242
243        #[cfg(feature = "spawn-local")]
244        fn spawn_local<F>(self, f: F)
245        where
246            F: Fn(VecDiff<A>) + 'static,
247        {
248            artwrap::spawn_local(self.for_each(move |new| {
249                f(new);
250                ready(())
251            }));
252        }
253    }
254}