Crates.io | incoming |
lib.rs | incoming |
version | 0.1.1 |
source | src |
created_at | 2020-02-26 16:44:01.782256 |
updated_at | 2020-02-26 16:47:41.802179 |
description | Trait for taking ownership of a stream of incoming connections |
homepage | |
repository | https://github.com/akshayknarayan/incoming |
max_upload_size | |
id | 212741 |
size | 6,006 |
This crate provides a trait for taking ownership of a [Stream
] of incoming connections.
The types tokio provides, [tokio::net::tcp::Incoming
] and [tokio::net::unix::Incoming
], are
both tied to the lifetime of their respective Listeners [1].
The provided .incoming()
used to consume self, but this was changed
.
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(())
})
}