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
use std::pin::Pin;
use std::task::Poll;

use futures::{Stream, TryStreamExt};
use pin_project_lite::pin_project;

use crate::flux::FluxChannel;
use wasmrs_runtime::ConditionallySafe;

pin_project! {
/// A [FluxPipe] is the result of piping one [Flux] into another.
pub struct FluxPipe<Item, Err, From>
where
    Item: ConditionallySafe,
    Err: ConditionallySafe,
{
    #[pin]
    from: From,
    to: FluxChannel<Item, Err>,
}
}

impl<Item, Err, From> FluxPipe<Item, Err, From>
where
  Item: ConditionallySafe,
  Err: ConditionallySafe,
{
  /// Create a new [FluxPipe]
  pub fn new(from: From, to: FluxChannel<Item, Err>) -> Self {
    Self { from, to }
  }
}

impl<Item, Err, From> FluxPipe<Item, Err, From>
where
  Item: ConditionallySafe,
  Err: ConditionallySafe,
  From: Stream<Item = Result<Item, Err>>,
{
}

impl<Item, Err, From> Stream for FluxPipe<Item, Err, From>
where
  Item: ConditionallySafe,
  Err: ConditionallySafe,
  From: Stream<Item = Result<Item, Err>> + Unpin,
{
  type Item = Result<Item, Err>;

  fn poll_next(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Option<Self::Item>> {
    self.get_mut().from.try_poll_next_unpin(cx)
  }
}
#[cfg(all(test, not(target_family = "wasm")))]
mod test {

  use anyhow::Result;
  use futures::StreamExt;

  use super::*;
  use crate::flux::Observer;
  use crate::Observable;

  #[tokio::test]
  async fn test_pipes() -> Result<()> {
    let (flux, observer) = FluxChannel::new_parts();

    flux.send("First".to_owned())?;

    let second_flux = FluxChannel::<String, String>::new();

    let mut pipe = observer.pipe(second_flux);

    let value = pipe.next().await;
    assert_eq!(value, Some(Ok("First".to_owned())));
    Ok(())
  }
}