@@ -4,6 +4,7 @@ for general purposes crossing over many modules of `fpRust`.
4
4
*/
5
5
6
6
use std:: cmp:: PartialEq ;
7
+ use std:: collections:: LinkedList ;
7
8
use std:: marker:: PhantomData ;
8
9
use std:: sync:: { Arc , Mutex } ;
9
10
use std:: thread;
@@ -14,8 +15,6 @@ use futures::executor::ThreadPool;
14
15
#[ cfg( feature = "for_futures" ) ]
15
16
use futures:: stream:: Stream ;
16
17
#[ cfg( feature = "for_futures" ) ]
17
- use std:: collections:: LinkedList ;
18
- #[ cfg( feature = "for_futures" ) ]
19
18
use std:: mem;
20
19
#[ cfg( feature = "for_futures" ) ]
21
20
use std:: pin:: Pin ;
@@ -133,6 +132,37 @@ pub fn get_mut<'a, T>(v: &'a mut Vec<T>, index: usize) -> Option<&'a mut T> {
133
132
None
134
133
}
135
134
135
+ #[ derive( Debug , Clone ) ]
136
+ pub struct LinkedListAsync < T > {
137
+ inner : Arc < Mutex < LinkedList < T > > > ,
138
+
139
+ _t : PhantomData < T > ,
140
+ }
141
+
142
+ impl < T > LinkedListAsync < T > {
143
+ pub fn new ( ) -> Self {
144
+ Self {
145
+ inner : Arc :: new ( Mutex :: new ( LinkedList :: new ( ) ) ) ,
146
+
147
+ _t : PhantomData ,
148
+ }
149
+ }
150
+
151
+ pub fn push_back ( & self , input : T ) {
152
+ self . inner . lock ( ) . unwrap ( ) . push_back ( input)
153
+ }
154
+
155
+ pub fn pop_front ( & self ) -> Option < T > {
156
+ self . inner . lock ( ) . unwrap ( ) . pop_front ( )
157
+ }
158
+ }
159
+
160
+ impl < T > Default for LinkedListAsync < T > {
161
+ fn default ( ) -> Self {
162
+ Self :: new ( )
163
+ }
164
+ }
165
+
136
166
/**
137
167
`Observable` memorizes all `Subscription` and send notifications.
138
168
@@ -252,7 +282,7 @@ pub struct SubscriptionFunc<T> {
252
282
pub receiver : RawReceiver < T > ,
253
283
254
284
#[ cfg( feature = "for_futures" ) ]
255
- cached : Option < Arc < Mutex < LinkedList < Arc < T > > > > > ,
285
+ cached : Option < LinkedListAsync < Arc < T > > > ,
256
286
#[ cfg( feature = "for_futures" ) ]
257
287
alive : Option < Arc < Mutex < AtomicBool > > > ,
258
288
#[ cfg( feature = "for_futures" ) ]
@@ -345,7 +375,7 @@ where
345
375
}
346
376
347
377
if self . cached . is_none ( ) {
348
- self . cached = Some ( Arc :: new ( Mutex :: new ( LinkedList :: new ( ) ) ) ) ;
378
+ self . cached = Some ( LinkedListAsync :: new ( ) ) ;
349
379
}
350
380
}
351
381
@@ -378,10 +408,8 @@ impl<T: Send + Sync + 'static> Subscription<T> for SubscriptionFunc<T> {
378
408
if let Some ( cached) = & self . cached {
379
409
let alive = { alive. lock ( ) . unwrap ( ) . load ( Ordering :: SeqCst ) } ;
380
410
if alive {
381
- {
382
- let mut cached = cached. lock ( ) . unwrap ( ) ;
383
- cached. push_back ( x. clone ( ) )
384
- } ;
411
+ cached. push_back ( x. clone ( ) ) ;
412
+
385
413
{
386
414
if let Some ( waker) = self . waker . lock ( ) . unwrap ( ) . take ( ) {
387
415
waker. wake ( )
@@ -432,7 +460,7 @@ where
432
460
if let Some ( cached) = & self . 0 . cached {
433
461
let picked: Option < Arc < T > > ;
434
462
{
435
- picked = cached. lock ( ) . unwrap ( ) . pop_front ( ) ;
463
+ picked = cached. pop_front ( ) ;
436
464
}
437
465
if picked. is_some ( ) {
438
466
return Poll :: Ready ( picked) ;
@@ -448,7 +476,7 @@ where
448
476
if let Some ( cached) = & self . 0 . cached {
449
477
let picked: Option < Arc < T > > ;
450
478
{
451
- picked = cached. lock ( ) . unwrap ( ) . pop_front ( ) ;
479
+ picked = cached. pop_front ( ) ;
452
480
}
453
481
454
482
// Check Pending(None) or Ready(Some(item))
0 commit comments