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
use std::pin::Pin;
use futures::Stream;
use parking_lot::Mutex;
use wasmrs_rx::{FluxChannel, Observer};
use crate::Packet;
pub type PacketSender = FluxChannel<Packet, crate::Error>;
#[must_use]
pub struct PacketStream {
#[cfg(target_family = "wasm")]
inner: Mutex<Box<dyn Stream<Item = Result<Packet, crate::Error>> + Unpin>>,
#[cfg(not(target_family = "wasm"))]
inner: Mutex<Box<dyn Stream<Item = Result<Packet, crate::Error>> + Unpin + Send + Sync>>,
}
impl From<Vec<Packet>> for PacketStream {
fn from(iter: Vec<Packet>) -> Self {
Self::new(Box::new(futures::stream::iter(iter.into_iter().map(Ok))))
}
}
impl PacketStream {
#[cfg(target_family = "wasm")]
pub fn new(rx: Box<dyn Stream<Item = Result<Packet, crate::Error>> + Unpin>) -> Self {
Self { inner: Mutex::new(rx) }
}
#[cfg(not(target_family = "wasm"))]
pub fn new(rx: Box<dyn Stream<Item = Result<Packet, crate::Error>> + Unpin + Send + Sync>) -> Self {
Self { inner: Mutex::new(rx) }
}
pub fn new_channels() -> (PacketSender, Self) {
let flux = FluxChannel::new();
let rx = flux.take_rx().unwrap();
(flux, Self::new(Box::new(rx)))
}
}
impl Default for PacketStream {
fn default() -> Self {
let flux = FluxChannel::new();
flux.complete();
Self::new(Box::new(flux.take_rx().unwrap()))
}
}
impl std::fmt::Debug for PacketStream {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("PacketStream").finish()
}
}
impl Stream for PacketStream {
type Item = Result<Packet, crate::Error>;
fn poll_next(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Option<Self::Item>> {
cx.waker().wake_by_ref();
let mut inner = self.inner.lock();
let pinned = Pin::new(&mut *inner);
pinned.poll_next(cx)
}
}
#[cfg(test)]
mod test {
use anyhow::Result;
use super::*;
fn sync_send<T>()
where
T: Sync + Send,
{
}
#[test]
fn test_sync_send() -> Result<()> {
sync_send::<PacketStream>();
Ok(())
}
}