rxrust 1.0.0-rc.4

A Rust implementation of Reactive Extensions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
//! FromStream observable implementation
//!
//! This module provides the `FromStream` observable which converts a
//! `futures_core::stream::Stream` into an Observable that emits each item from
//! the stream and then completes.
//!
//! ## Direct futures_core::stream::Stream Support
//!
//! This implementation directly accepts `futures_core::stream::Stream` for
//! simplicity and ecosystem compatibility. The `futures_core::stream::Stream`
//! trait is the standard async iterator in Rust:
//!
//! ```rust,ignore
//! pub trait Stream {
//!     type Item;
//!     fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
//! }
//! ```
//!
//! Key semantics:
//! - `Poll::Ready(Some(value))` - A value is available, can poll again for more
//! - `Poll::Ready(None)` - Stream exhausted, no more values
//! - `Poll::Pending` - No value yet, will wake when ready
//!
//! ## Scheduler Compatibility
//!
//! `FromStream` works seamlessly with all scheduler types through generic trait
//! bounds:
//!
//! - **LocalScheduler**: Single-threaded execution, no `Send` requirement
//! - **SharedScheduler**: Multi-threaded execution with `Send` bounds
//! - **Custom schedulers**: Any type implementing the `Scheduler` trait
//!
//! ## Examples
//!
//! ```rust,no_run
//! use futures::stream;
//! use rxrust::prelude::*;
//!
//! // Create an observable from a futures_core::Stream
//! let stream = stream::iter(vec![1, 2, 3]);
//! Local::from_stream(stream)
//!   .map(|x| x * 2)
//!   .subscribe(|v| println!("{}", v));
//! // Emits: 2, 4, 6, then completes
//! ```
//!
//! ## Error Handling and Cancellation
//!
//! - **Cancellation**: Unsubscribing stops polling the stream and cleans up
//!   resources
//! - **Panic safety**: The scheduler's `CatchUnwind` wrapper prevents panic
//!   propagation
//! - **Resource cleanup**: Observers are properly dropped on completion or
//!   cancellation

use std::{
  convert::Infallible,
  future::Future,
  pin::Pin,
  task::{Context as TaskContext, Poll},
};

use futures_core::stream::Stream;
use pin_project_lite::pin_project;

use crate::{
  context::Context,
  observable::{CoreObservable, ObservableType},
  observer::Observer,
  scheduler::Scheduler,
};

/// An observable that converts a `futures_core::stream::Stream` into an
/// Observable sequence.
///
/// `FromStream` creates an observable that, upon subscription, polls the stream
/// and emits each item. When the stream is exhausted (returns `Ready(None)`),
/// the observable completes.
///
/// # Type Parameters
///
/// * `St` - The stream type implementing `futures_core::stream::Stream`
/// * `S` - The scheduler type used to spawn the async task
///
/// # Polling Behavior
///
/// When subscribed, `FromStream` creates a `FromStreamTask` future that:
/// 1. Polls the stream using `poll_next()`
/// 2. On `Ready(Some(value))`: emit value, continue polling
/// 3. On `Ready(None)`: stream exhausted, complete
/// 4. On `Pending`: yield control, scheduler will retry
#[derive(Clone)]
pub struct FromStream<St, S> {
  /// The stream to convert
  pub stream: St,
  /// The scheduler used to spawn the task
  pub scheduler: S,
}

impl<St, S> ObservableType for FromStream<St, S>
where
  St: Stream,
{
  type Item<'a>
    = St::Item
  where
    Self: 'a;
  type Err = Infallible;
}

pin_project! {
  /// A future that drives the stream and emits values to the observer.
  ///
  /// `FromStreamTask` implements the core polling logic for `futures_core::stream::Stream`.
  ///
  /// # Polling Algorithm
  ///
  /// 1. **Check cancellation**: If observer is None, return Ready(()) immediately
  /// 2. **Poll stream**: Call `poll_next()` on the stream
  ///    - If `Ready(Some(value))`: emit value, continue loop
  ///    - If `Ready(None)`: stream exhausted, complete observer, return Ready(())
  ///    - If `Pending`: return Pending (yield control to scheduler)
  ///
  /// # Type Parameters
  ///
  /// * `St` - The stream type implementing `futures_core::stream::Stream`
  /// * `O` - The observer type that receives emitted values
  pub struct FromStreamTask<St, O> {
    #[pin]
    stream: St,
    observer: Option<O>,
  }
}

impl<St, O> Future for FromStreamTask<St, O>
where
  St: Stream,
  O: Observer<St::Item, Infallible>,
{
  type Output = ();

  fn poll(self: Pin<&mut Self>, cx: &mut TaskContext<'_>) -> Poll<Self::Output> {
    let mut this = self.project();

    loop {
      // Check if observer has been dropped (cancellation)
      if this.observer.is_none() {
        return Poll::Ready(());
      }

      // Poll the stream
      match this.stream.as_mut().poll_next(cx) {
        Poll::Ready(Some(value)) => {
          // Emit value to observer
          if let Some(observer) = this.observer.as_mut() {
            observer.next(value);
          }
          // Continue loop to poll for next value immediately
        }
        Poll::Ready(None) => {
          // Stream exhausted, complete
          if let Some(observer) = this.observer.take() {
            observer.complete();
          }
          return Poll::Ready(());
        }
        Poll::Pending => {
          // No value available yet, yield control
          return Poll::Pending;
        }
      }
    }
  }
}

/// Unified generic implementation that works with any scheduler type.
///
/// # Requirements Satisfied
///
/// - **1.1**: Creates Observable from `futures_core::stream::Stream`
/// - **1.4**: Generic scheduler integration with trait bounds
/// - **2.1**: LocalScheduler support without Send requirements
/// - **2.2**: SharedScheduler support with automatic Send enforcement
impl<St, S, C> CoreObservable<C> for FromStream<St, S>
where
  C: Context,
  C::Inner: Observer<St::Item, Infallible>,
  St: Stream,
  S: Scheduler<FromStreamTask<St, C::Inner>> + Clone,
{
  type Unsub = crate::scheduler::TaskHandle;

  fn subscribe(self, context: C) -> Self::Unsub {
    let observer = context.into_inner();
    let task = FromStreamTask { stream: self.stream, observer: Some(observer) };
    self.scheduler.schedule(task, None)
  }
}

// ============================================================================
// FromStreamResult - For streams that emit Result types
// ============================================================================

/// An observable that converts a `Stream<Item = Result<Item, Err>>` into an
/// Observable sequence.
///
/// Unlike `FromStream`, this observable handles `Result` types specially:
/// - `Ok(value)` is emitted via `next()`
/// - `Err(error)` is emitted via `error()` and terminates the stream
///
/// # Type Parameters
///
/// * `St` - The stream type that outputs `Result<Item, Err>`
/// * `S` - The scheduler type used to spawn the async task
///
/// # Examples
///
/// ```rust,no_run
/// use std::convert::Infallible;
///
/// use futures::stream;
/// use rxrust::prelude::*;
///
/// // Success case - emits values and completes
/// let stream = stream::iter(vec![Ok::<i32, Infallible>(1), Ok(2), Ok(3)]);
/// Local::from_stream_result(stream)
///   .on_error(|_e| {})
///   .subscribe(|v| println!("Got: {}", v));
///
/// // Error case - emits values until error, then terminates
/// let stream = stream::iter(vec![Ok(1), Err("error"), Ok(3)]);
/// Local::from_stream_result(stream)
///   .on_error(|e| println!("Error: {}", e))
///   .subscribe(|v| println!("Got: {}", v));
/// // Output: Got: 1, Error: error
/// ```
#[derive(Clone)]
pub struct FromStreamResult<St, S> {
  /// The stream to convert (must output Result types)
  pub stream: St,
  /// The scheduler used to spawn the task
  pub scheduler: S,
}

impl<St, Item, Err, S> ObservableType for FromStreamResult<St, S>
where
  St: Stream<Item = Result<Item, Err>>,
{
  type Item<'a>
    = Item
  where
    Self: 'a;
  type Err = Err;
}

pin_project! {
  /// A future that drives the stream and emits values to the observer,
  /// handling Result types by routing Ok to next() and Err to error().
  pub struct FromStreamResultTask<St, O> {
    #[pin]
    stream: St,
    observer: Option<O>,
  }
}

impl<St, O, Item, Err> Future for FromStreamResultTask<St, O>
where
  St: Stream<Item = Result<Item, Err>>,
  O: Observer<Item, Err>,
{
  type Output = ();

  fn poll(self: Pin<&mut Self>, cx: &mut TaskContext<'_>) -> Poll<Self::Output> {
    let mut this = self.project();

    loop {
      // Check if observer has been dropped (cancellation)
      if this.observer.is_none() {
        return Poll::Ready(());
      }

      // Poll the stream
      match this.stream.as_mut().poll_next(cx) {
        Poll::Ready(Some(result)) => {
          match result {
            Ok(value) => {
              // Emit value to observer
              if let Some(observer) = this.observer.as_mut() {
                observer.next(value);
              }
              // Continue loop to poll for next value immediately
            }
            Err(err) => {
              // Emit error and terminate
              if let Some(observer) = this.observer.take() {
                observer.error(err);
              }
              return Poll::Ready(());
            }
          }
        }
        Poll::Ready(None) => {
          // Stream exhausted, complete
          if let Some(observer) = this.observer.take() {
            observer.complete();
          }
          return Poll::Ready(());
        }
        Poll::Pending => {
          // No value available yet, yield control
          return Poll::Pending;
        }
      }
    }
  }
}

impl<St, C, S, Item, Err> CoreObservable<C> for FromStreamResult<St, S>
where
  C: Context,
  C::Inner: Observer<Item, Err>,
  St: Stream<Item = Result<Item, Err>>,
  S: Scheduler<FromStreamResultTask<St, C::Inner>> + Clone,
{
  type Unsub = crate::scheduler::TaskHandle;

  fn subscribe(self, context: C) -> Self::Unsub {
    let observer = context.into_inner();
    let task = FromStreamResultTask { stream: self.stream, observer: Some(observer) };
    self.scheduler.schedule(task, None)
  }
}

#[cfg(test)]
mod tests {
  use std::sync::{Arc, Mutex};

  use futures::stream;

  use crate::prelude::*;
  #[rxrust_macro::test(local)]
  async fn test_from_stream_basic() {
    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();
    let completed = Arc::new(Mutex::new(false));
    let completed_clone = completed.clone();

    let handle = Local::from_stream(stream::iter(vec![1, 2, 3]))
      .on_complete(move || *completed_clone.lock().unwrap() = true)
      .subscribe(move |v| result_clone.lock().unwrap().push(v));

    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![1, 2, 3]);
    assert!(*completed.lock().unwrap());
  }

  #[rxrust_macro::test(local)]
  async fn test_from_stream_empty() {
    let next_count = Arc::new(Mutex::new(0));
    let next_count_clone = next_count.clone();
    let completed = Arc::new(Mutex::new(false));
    let completed_clone = completed.clone();

    let handle = Local::from_stream(stream::iter(Vec::<i32>::new()))
      .on_complete(move || *completed_clone.lock().unwrap() = true)
      .subscribe(move |_| *next_count_clone.lock().unwrap() += 1);

    handle.await;

    assert_eq!(*next_count.lock().unwrap(), 0);
    assert!(*completed.lock().unwrap());
  }

  #[cfg(not(target_arch = "wasm32"))]
  #[rxrust_macro::test]
  async fn test_from_stream_shared() {
    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();
    let completed = Arc::new(Mutex::new(false));
    let completed_clone = completed.clone();

    let handle = Shared::from_stream(stream::iter(vec![10, 20, 30]))
      .on_complete(move || *completed_clone.lock().unwrap() = true)
      .subscribe(move |v| result_clone.lock().unwrap().push(v));

    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![10, 20, 30]);
    assert!(*completed.lock().unwrap());
  }

  #[rxrust_macro::test(local)]
  async fn test_from_stream_unfold() {
    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();

    let stream =
      stream::unfold(
        1,
        |state| async move { if state <= 3 { Some((state, state + 1)) } else { None } },
      );

    let handle =
      Local::from_stream(stream).subscribe(move |v| result_clone.lock().unwrap().push(v));

    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![1, 2, 3]);
  }

  #[cfg(not(target_arch = "wasm32"))]
  #[rxrust_macro::test]
  async fn test_from_stream_channel() {
    use futures::{SinkExt, channel::mpsc};

    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();

    let (mut sender, receiver) = mpsc::channel::<i32>(8);

    let handle = Shared::from_stream(receiver).subscribe(move |v| {
      result_clone.lock().unwrap().push(v);
    });

    sender.send(10).await.unwrap();
    sender.send(20).await.unwrap();
    sender.send(30).await.unwrap();
    drop(sender);

    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![10, 20, 30]);
  }

  #[rxrust_macro::test(local)]
  async fn test_from_stream_with_map() {
    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();

    let handle = Local::from_stream(stream::iter(vec![1, 2, 3]))
      .map(|v| v * 2)
      .subscribe(move |v| result_clone.lock().unwrap().push(v));

    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![2, 4, 6]);
  }

  #[rxrust_macro::test(local)]
  async fn test_from_stream_complex_chain() {
    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();
    let completed = Arc::new(Mutex::new(false));
    let completed_clone = completed.clone();

    let handle = Local::from_stream(stream::iter(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
      .map(|v| v * 2)
      .filter(|v| *v > 5)
      .take(4)
      .on_complete(move || *completed_clone.lock().unwrap() = true)
      .subscribe(move |v| result_clone.lock().unwrap().push(v));

    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![6, 8, 10, 12]);
    assert!(*completed.lock().unwrap());
  }
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod cancellation_tests {
  use std::sync::{Arc, Mutex};

  use futures::{SinkExt, channel::mpsc, stream};

  use crate::{prelude::*, subscription::Subscription};
  /// 测试 unsubscribe 停止轮询
  #[cfg(not(target_arch = "wasm32"))]
  #[rxrust_macro::test]
  async fn test_unsubscribe_stops_polling() {
    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();

    let (mut sender, receiver) = mpsc::channel::<i32>(8);

    let handle = Shared::from_stream(receiver).subscribe(move |v| {
      result_clone.lock().unwrap().push(v);
    });

    sender.send(1).await.unwrap();
    tokio::time::sleep(Duration::from_millis(50)).await;

    let handle_clone = handle.clone();
    handle.unsubscribe();

    assert!(handle_clone.is_closed());

    let _ = sender.send(2).await;
    let _ = sender.send(3).await;
    tokio::time::sleep(Duration::from_millis(50)).await;

    let values = result.lock().unwrap().clone();
    assert_eq!(values, vec![1], "Only values before unsubscribe should be received");
  }

  #[cfg(not(target_arch = "wasm32"))]
  #[rxrust_macro::test]
  async fn test_cancellation_prevents_completion() {
    let completed = Arc::new(Mutex::new(false));
    let completed_clone = completed.clone();

    let (mut sender, receiver) = mpsc::channel::<i32>(8);

    let handle = Shared::from_stream(receiver)
      .on_complete(move || *completed_clone.lock().unwrap() = true)
      .subscribe(|_| {});

    sender.send(1).await.unwrap();
    tokio::time::sleep(Duration::from_millis(50)).await;

    handle.unsubscribe();
    drop(sender);

    tokio::time::sleep(Duration::from_millis(50)).await;

    assert!(!*completed.lock().unwrap(), "Completion should not be called on cancellation");
  }

  #[cfg(not(target_arch = "wasm32"))]
  #[rxrust_macro::test]
  async fn test_resource_cleanup_on_completion() {
    let resource_active = Arc::new(Mutex::new(true));
    let resource_active_clone = resource_active.clone();

    struct ResourceObserver {
      resource_active: Arc<Mutex<bool>>,
    }

    impl Drop for ResourceObserver {
      fn drop(&mut self) { *self.resource_active.lock().unwrap() = false; }
    }

    let observer = Arc::new(ResourceObserver { resource_active: resource_active_clone });
    let observer_clone = observer.clone();

    let handle = Shared::from_stream(stream::iter(vec![1, 2, 3])).subscribe(move |_v| {
      let _ = &observer_clone;
    });

    drop(observer);
    handle.await;
    tokio::time::sleep(Duration::from_millis(50)).await;

    assert!(!*resource_active.lock().unwrap(), "Resource should be cleaned up after completion");
  }
}

#[cfg(test)]
mod pending_tests {
  use std::sync::{Arc, Mutex};

  use futures::stream;

  use crate::prelude::*;

  #[cfg(not(target_arch = "wasm32"))]
  #[rxrust_macro::test]
  async fn test_pending_does_not_cause_premature_completion() {
    use futures::{SinkExt, channel::mpsc};

    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();
    let completed = Arc::new(Mutex::new(false));
    let completed_clone = completed.clone();

    let (mut sender, receiver) = mpsc::channel::<i32>(8);

    let handle = Shared::from_stream(receiver)
      .on_complete(move || *completed_clone.lock().unwrap() = true)
      .subscribe(move |v| result_clone.lock().unwrap().push(v));

    for _ in 0..3 {
      tokio::time::sleep(Duration::from_millis(20)).await;
      assert!(!*completed.lock().unwrap(), "Stream should NOT complete while pending");
    }

    sender.send(10).await.unwrap();
    sender.send(20).await.unwrap();
    tokio::time::sleep(Duration::from_millis(20)).await;
    assert!(!*completed.lock().unwrap(), "Stream should NOT complete while sender is open");

    drop(sender);
    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![10, 20]);
    assert!(*completed.lock().unwrap());
  }

  #[rxrust_macro::test(local)]
  async fn test_explicit_pending_control() {
    use std::task::Poll;

    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();
    let completed = Arc::new(Mutex::new(false));
    let completed_clone = completed.clone();

    let mut state = 0;
    let stream = stream::poll_fn(move |_cx| {
      state += 1;
      match state {
        1 => Poll::Ready(Some(1)),
        2 => Poll::Ready(Some(2)),
        3 => Poll::Ready(Some(3)),
        _ => Poll::Ready(None),
      }
    });

    let handle = Local::from_stream(stream)
      .on_complete(move || *completed_clone.lock().unwrap() = true)
      .subscribe(move |v| result_clone.lock().unwrap().push(v));

    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![1, 2, 3]);
    assert!(*completed.lock().unwrap());
  }
}

#[cfg(test)]
mod from_stream_result_tests {
  use std::sync::{Arc, Mutex};

  use futures::stream;

  use crate::prelude::*;
  #[rxrust_macro::test(local)]
  async fn test_from_stream_result_all_ok() {
    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();
    let completed = Arc::new(Mutex::new(false));
    let completed_clone = completed.clone();
    let error_received = Arc::new(Mutex::new(false));
    let error_clone = error_received.clone();

    let stream = stream::iter(vec![Ok::<_, String>(1), Ok(2), Ok(3)]);

    let handle = Local::from_stream_result(stream)
      .on_complete(move || *completed_clone.lock().unwrap() = true)
      .on_error(move |_| *error_clone.lock().unwrap() = true)
      .subscribe(move |v| result_clone.lock().unwrap().push(v));

    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![1, 2, 3]);
    assert!(*completed.lock().unwrap());
    assert!(!*error_received.lock().unwrap());
  }

  #[rxrust_macro::test(local)]
  async fn test_from_stream_result_with_error() {
    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();
    let completed = Arc::new(Mutex::new(false));
    let completed_clone = completed.clone();
    let error_received = Arc::new(Mutex::new(None));
    let error_clone = error_received.clone();

    let stream = stream::iter(vec![
      Ok(1),
      Ok(2),
      Err("test error".to_string()),
      Ok(3), // This should not be emitted
    ]);

    let handle = Local::from_stream_result(stream)
      .on_complete(move || *completed_clone.lock().unwrap() = true)
      .on_error(move |e| *error_clone.lock().unwrap() = Some(e))
      .subscribe(move |v| result_clone.lock().unwrap().push(v));

    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![1, 2]);
    assert!(!*completed.lock().unwrap());
    assert_eq!(*error_received.lock().unwrap(), Some("test error".to_string()));
  }

  #[rxrust_macro::test(local)]
  async fn test_from_stream_result_immediate_error() {
    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();
    let completed = Arc::new(Mutex::new(false));
    let completed_clone = completed.clone();
    let error_received = Arc::new(Mutex::new(None));
    let error_clone = error_received.clone();

    let stream = stream::iter(vec![Err::<i32, _>("immediate error".to_string())]);

    let handle = Local::from_stream_result(stream)
      .on_complete(move || *completed_clone.lock().unwrap() = true)
      .on_error(move |e| *error_clone.lock().unwrap() = Some(e))
      .subscribe(move |v| result_clone.lock().unwrap().push(v));

    handle.await;

    assert!(result.lock().unwrap().is_empty());
    assert!(!*completed.lock().unwrap());
    assert_eq!(*error_received.lock().unwrap(), Some("immediate error".to_string()));
  }

  #[rxrust_macro::test(local)]
  async fn test_from_stream_result_empty() {
    let next_count = Arc::new(Mutex::new(0));
    let next_count_clone = next_count.clone();
    let completed = Arc::new(Mutex::new(false));
    let completed_clone = completed.clone();
    let error_received = Arc::new(Mutex::new(false));
    let error_clone = error_received.clone();

    let stream = stream::iter(Vec::<Result<i32, String>>::new());

    let handle = Local::from_stream_result(stream)
      .on_complete(move || *completed_clone.lock().unwrap() = true)
      .on_error(move |_| *error_clone.lock().unwrap() = true)
      .subscribe(move |_| *next_count_clone.lock().unwrap() += 1);

    handle.await;

    assert_eq!(*next_count.lock().unwrap(), 0);
    assert!(*completed.lock().unwrap());
    assert!(!*error_received.lock().unwrap());
  }

  #[cfg(not(target_arch = "wasm32"))]
  #[rxrust_macro::test]
  async fn test_from_stream_result_shared_ok() {
    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();
    let completed = Arc::new(Mutex::new(false));
    let completed_clone = completed.clone();
    let error_received = Arc::new(Mutex::new(false));
    let error_clone = error_received.clone();

    let stream = stream::iter(vec![Ok::<_, String>(10), Ok(20), Ok(30)]);

    let handle = Shared::from_stream_result(stream)
      .on_complete(move || *completed_clone.lock().unwrap() = true)
      .on_error(move |_| *error_clone.lock().unwrap() = true)
      .subscribe(move |v| result_clone.lock().unwrap().push(v));

    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![10, 20, 30]);
    assert!(*completed.lock().unwrap());
    assert!(!*error_received.lock().unwrap());
  }

  #[cfg(not(target_arch = "wasm32"))]
  #[rxrust_macro::test]
  async fn test_from_stream_result_shared_error() {
    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();
    let completed = Arc::new(Mutex::new(false));
    let completed_clone = completed.clone();
    let error_received = Arc::new(Mutex::new(None));
    let error_clone = error_received.clone();

    let stream = stream::iter(vec![Ok(1), Err("shared error".to_string()), Ok(2)]);

    let handle = Shared::from_stream_result(stream)
      .on_complete(move || *completed_clone.lock().unwrap() = true)
      .on_error(move |e| *error_clone.lock().unwrap() = Some(e))
      .subscribe(move |v| result_clone.lock().unwrap().push(v));

    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![1]);
    assert!(!*completed.lock().unwrap());
    assert_eq!(*error_received.lock().unwrap(), Some("shared error".to_string()));
  }

  #[cfg(not(target_arch = "wasm32"))]
  #[rxrust_macro::test]
  async fn test_from_stream_result_channel() {
    use futures::{SinkExt, channel::mpsc};

    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();
    let error_received = Arc::new(Mutex::new(None));
    let error_clone = error_received.clone();

    let (mut sender, receiver) = mpsc::channel::<Result<i32, String>>(8);

    let handle = Shared::from_stream_result(receiver)
      .on_error(move |e| *error_clone.lock().unwrap() = Some(e))
      .subscribe(move |v| result_clone.lock().unwrap().push(v));

    sender.send(Ok(1)).await.unwrap();
    sender.send(Ok(2)).await.unwrap();
    sender
      .send(Err("channel error".to_string()))
      .await
      .unwrap();
    // These should not be received
    let _ = sender.send(Ok(3)).await;

    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![1, 2]);
    assert_eq!(*error_received.lock().unwrap(), Some("channel error".to_string()));
  }

  #[rxrust_macro::test(local)]
  async fn test_from_stream_result_with_map() {
    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();

    let stream = stream::iter(vec![Ok::<_, String>(1), Ok(2), Ok(3)]);

    let handle = Local::from_stream_result(stream)
      .map(|v| v * 2)
      .on_error(|_| {})
      .subscribe(move |v| result_clone.lock().unwrap().push(v));

    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![2, 4, 6]);
  }

  #[rxrust_macro::test(local)]
  async fn test_from_stream_result_try_unfold() {
    let result = Arc::new(Mutex::new(Vec::new()));
    let result_clone = result.clone();
    let error_received = Arc::new(Mutex::new(None));
    let error_clone = error_received.clone();

    // Create a stream that emits 1, 2, 3 then errors at 4
    let stream = stream::try_unfold(1, |state| async move {
      if state == 4 { Err("invalid value".to_string()) } else { Ok(Some((state, state + 1))) }
    });

    let handle = Local::from_stream_result(stream)
      .on_error(move |e| *error_clone.lock().unwrap() = Some(e))
      .subscribe(move |v| result_clone.lock().unwrap().push(v));

    handle.await;

    assert_eq!(*result.lock().unwrap(), vec![1, 2, 3]);
    assert_eq!(*error_received.lock().unwrap(), Some("invalid value".to_string()));
  }
}