mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-28 12:10:37 +00:00
remove io workarounds from example (#1891)
This PR removes no longer needed io workarounds from connect example.
This commit is contained in:
parent
6efe07c3fb
commit
8bcbe78dbe
@ -17,27 +17,14 @@
|
|||||||
#![warn(rust_2018_idioms)]
|
#![warn(rust_2018_idioms)]
|
||||||
|
|
||||||
use tokio::io;
|
use tokio::io;
|
||||||
use tokio::sync::{mpsc, oneshot};
|
|
||||||
use tokio_util::codec::{FramedRead, FramedWrite};
|
use tokio_util::codec::{FramedRead, FramedWrite};
|
||||||
|
|
||||||
use futures::{Stream, StreamExt};
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let (tx, rx) = oneshot::channel();
|
|
||||||
tokio::spawn(async move {
|
|
||||||
run().await.unwrap();
|
|
||||||
tx.send(()).unwrap();
|
|
||||||
});
|
|
||||||
|
|
||||||
rx.await.map_err(Into::into)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Currently, we need to spawn the initial future due to https://github.com/tokio-rs/tokio/issues/1356
|
|
||||||
async fn run() -> Result<(), Box<dyn Error>> {
|
|
||||||
// Determine if we're going to run in TCP or UDP mode
|
// Determine if we're going to run in TCP or UDP mode
|
||||||
let mut args = env::args().skip(1).collect::<Vec<_>>();
|
let mut args = env::args().skip(1).collect::<Vec<_>>();
|
||||||
let tcp = match args.iter().position(|a| a == "--udp") {
|
let tcp = match args.iter().position(|a| a == "--udp") {
|
||||||
@ -55,7 +42,7 @@ async fn run() -> Result<(), Box<dyn Error>> {
|
|||||||
};
|
};
|
||||||
let addr = addr.parse::<SocketAddr>()?;
|
let addr = addr.parse::<SocketAddr>()?;
|
||||||
|
|
||||||
let stdin = stdin();
|
let stdin = FramedRead::new(io::stdin(), codec::Bytes);
|
||||||
let stdout = FramedWrite::new(io::stdout(), codec::Bytes);
|
let stdout = FramedWrite::new(io::stdout(), codec::Bytes);
|
||||||
|
|
||||||
if tcp {
|
if tcp {
|
||||||
@ -67,21 +54,6 @@ async fn run() -> Result<(), Box<dyn Error>> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Temporary work around for stdin blocking the stream
|
|
||||||
fn stdin() -> impl Stream<Item = Result<Vec<u8>, io::Error>> + Unpin {
|
|
||||||
let mut stdin = FramedRead::new(io::stdin(), codec::Bytes);
|
|
||||||
|
|
||||||
let (tx, rx) = mpsc::unbounded_channel();
|
|
||||||
|
|
||||||
tokio::spawn(async move {
|
|
||||||
while let Some(res) = stdin.next().await {
|
|
||||||
let _ = tx.send(res);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
rx
|
|
||||||
}
|
|
||||||
|
|
||||||
mod tcp {
|
mod tcp {
|
||||||
use super::codec;
|
use super::codec;
|
||||||
use futures::{future, Sink, SinkExt, Stream, StreamExt};
|
use futures::{future, Sink, SinkExt, Stream, StreamExt};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user