2个版本
0.1.1 | 2020年2月26日 |
---|---|
0.1.0 | 2020年2月26日 |
#1565 in 异步
6KB
incoming
此crate提供了一种处理Stream
的传入连接的特例。
为什么?
tokio提供的类型,tokio::net::tcp::Incoming
和tokio::net::unix::Incoming
,都与其各自的Listener的生命周期相关联。之前提供的用于消耗自身的.incoming()
方法,但现在已更改。
示例
async fn use_owned_stream<S, C, E>(s: S)
where
S: Stream<Item = Result<C, E>> + Send + 'static,
C: AsyncRead + AsyncWrite + Unpin + Send + 'static,
E: Into<Box<dyn Error + Sync + Send + 'static>> + std::fmt::Debug + Unpin + Send + 'static,
{
tokio::spawn(s.for_each_concurrent(None, |st| async move { handle_this_conn(st) }));
}
fn main() -> Result<(), Box<dyn Error>> {
let mut rt = tokio::runtime::Runtime::new()?;
use incoming::IntoIncoming;
rt.block_on(async move {
let addr: std::net::SocketAddr = "0.0.0.0:4242".parse()?;
let st = tokio::net::TcpListener::bind(addr).await?;
use_owned_stream(st.into_incoming()).await;
Ok(())
})
}
依赖项
~5.5MB
~80K SLoC