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
//! # Async trait for transmitting data to peers
//!
//! `async-transmit` crate provides `Transmit` trait which allows value to be transmit asynchronously.
//!
//! This crate relies on [async-trait][], the original definition of the `Transmit` trait is:
//!
//! [async-trait]: https://github.com/dtolnay/async-trait
//!
//! ```
//! use async_trait::async_trait;
//!
//! #[async_trait]
//! pub trait Transmit {
//! type Item;
//! type Error;
//!
//! async fn transmit(&mut self, item: Self::Item) -> Result<(), Self::Error>
//! where
//! Self::Item: 'async_trait;
//! }
//! ```
//!
//! So use `#[async_trait]` to when implement `Transmit` like:
//!
//! ```
//! use async_transmit::*;
//! use async_trait::async_trait;
//!
//! struct VoidTransmitter<I, E> {
//! phantom: std::marker::PhantomData<(I, E)>,
//! }
//!
//! #[async_trait]
//! impl<I, E> Transmit for VoidTransmitter<I, E>
//! where
//! I: Send,
//! E: Send,
//! {
//! type Item = I;
//! type Error = E;
//!
//! async fn transmit(&mut self, item: Self::Item) -> Result<(), Self::Error>
//! where
//! I: 'async_trait,
//! {
//! // Do Nothing
//! Ok(())
//! }
//! }
//! ```
//!
//! ## With async-std/async-channel
//!
//! If you'd like to play with [`async_std::channel::Sender`][] or [`async_channel::Sender`][],
//! use `with-async-channel` feature like:
//!
//! [`async_std::channel::Sender`]: https://docs.rs/async-std/1.9.0/async_std/channel/struct.Sender.html
//! [`async_channel::Sender`]: https://docs.rs/async-channel/1.6.1/async_channel/struct.Sender.html
//!
//! ```toml
//! [dependencies.async-transmit]
//! version = "0.1.0"
//! features = ["with-async-channel"]
//! ```
//!
//! Then you can use `transmit()` method through `Transmit` trait on the sender like:
//!
//! ```
//! # use anyhow::Result;
//! # use futures::executor;
//! # fn main() -> Result<()> {
//! # #[cfg(feature = "with-async-channel")]
//! # executor::block_on(async {
//! use async_transmit::*;
//!
//! let (mut s, r) = async_channel::unbounded::<&'static str>();
//!
//! s.transmit("Hello").await?;
//! s.transmit("World").await?;
//! drop(s);
//! assert_eq!(Some("Hello"), r.recv().await.ok());
//! assert_eq!(Some("World"), r.recv().await.ok());
//! assert_eq!(None, r.recv().await.ok());
//! # Ok(()) as Result<()>
//! # })?;
//! # Ok(())
//! # }
//! ```
//!
//! ## With tokio
//!
//! If you'd like to play with [`tokio::sync::mpsc::Sender`][] or [`tokio::sync::mpsc::UnboundedSender`],
//! use `with-tokio` feature like:
//!
//! [`tokio::sync::mpsc::Sender`]: https://docs.rs/tokio/1.3.0/tokio/sync/mpsc/struct.Sender.html
//! [`tokio::sync::mpsc::UnboundedSender`]: https://docs.rs/tokio/1.3.0/tokio/sync/mpsc/struct.UnboundedSender.html
//!
//! ```toml
//! [dependencies.async-transmit]
//! version = "0.1.0"
//! features = ["with-tokio"]
//! ```
//!
//! Then you can use `transmit()` method through `Transmit` trait on the sender like:
//!
//! ```
//! # use anyhow::Result;
//! # use futures::executor;
//! # fn main() -> Result<()> {
//! # #[cfg(feature = "with-tokio")]
//! # executor::block_on(async {
//! use async_transmit::*;
//!
//! let (mut s, mut r) = tokio::sync::mpsc::unbounded_channel::<&'static str>();
//!
//! s.transmit("Hello").await?;
//! s.transmit("World").await?;
//! drop(s);
//! assert_eq!(Some("Hello"), r.recv().await);
//! assert_eq!(Some("World"), r.recv().await);
//! assert_eq!(None, r.recv().await);
//! # Ok(()) as Result<()>
//! # })?;
//! # Ok(())
//! # }
//! ```
//!
//! ## With futures-rs
//!
//! If you'd like to play with [`futures::sink::Sink`], use `with-sink` feature like:
//!
//! [`futures::sink::Sink`]: https://docs.rs/futures/0.3.13/futures/sink/trait.Sink.html
//!
//! ```toml
//! [dependencies.async-transmit]
//! version = "0.1.0"
//! features = ["with-sink"]
//! ```
//!
//! Then you can use `async_transmit::from_sink()` to create a wrapper object which implements `Transmit`
//! trait like:
//!
//! ```
//! # use anyhow::Result;
//! # use futures::executor;
//! # fn main() -> Result<()> {
//! # #[cfg(feature = "with-sink")]
//! # executor::block_on(async {
//! use async_transmit::*;
//! use futures::prelude::*;
//!
//! let (s, mut r) = futures::channel::mpsc::unbounded::<&'static str>();
//! let mut s = from_sink(s);
//!
//! s.transmit("Hello").await?;
//! s.transmit("World").await?;
//! drop(s);
//! assert_eq!(Some("Hello"), r.next().await);
//! assert_eq!(Some("World"), r.next().await);
//! assert_eq!(None, r.next().await);
//! # Ok(()) as Result<()>
//! # })?;
//! # Ok(())
//! # }
//! ```
//!
pub use *;