Reduce the number of dependencies

This commit is contained in:
Stjepan Glavina 2020-07-20 00:55:35 +02:00
parent 80461663c4
commit 7a73c22814
18 changed files with 42 additions and 35 deletions

View File

@ -46,7 +46,8 @@ async-std = "1.5.0"
async-tungstenite = { version = "0.4.2", features = ["async-native-tls"] } async-tungstenite = { version = "0.4.2", features = ["async-native-tls"] }
base64 = "0.12.0" base64 = "0.12.0"
ctrlc = "3.1.4" ctrlc = "3.1.4"
futures = "0.3.4" futures = "0.3.5"
futures-lite = { path = "../futures-lite" }
http = "0.2.1" http = "0.2.1"
http-types = "1.2.0" http-types = "1.2.0"
hyper = { version = "0.13.5", default-features = false, features = ["stream"] } hyper = { version = "0.13.5", default-features = false, features = ["stream"] }

View File

@ -11,7 +11,7 @@ use std::net::{TcpStream, ToSocketAddrs};
use anyhow::{bail, Context as _, Error, Result}; use anyhow::{bail, Context as _, Error, Result};
use async_io::Async; use async_io::Async;
use blocking::{block_on, unblock}; use blocking::{block_on, unblock};
use futures::prelude::*; use futures_lite::*;
use http_types::{Method, Request, Response}; use http_types::{Method, Request, Response};
use url::Url; use url::Url;

View File

@ -19,7 +19,7 @@ use anyhow::Result;
use async_io::Async; use async_io::Async;
use async_native_tls::{Identity, TlsAcceptor}; use async_native_tls::{Identity, TlsAcceptor};
use blocking::block_on; use blocking::block_on;
use futures::prelude::*; use futures_lite::*;
use http_types::{Request, Response, StatusCode}; use http_types::{Request, Response, StatusCode};
use smol::Task; use smol::Task;

View File

@ -16,8 +16,7 @@ use std::net::TcpStream;
use async_io::Async; use async_io::Async;
use blocking::{block_on, Unblock}; use blocking::{block_on, Unblock};
use futures::io; use futures_lite::*;
use futures::prelude::*;
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
block_on(async { block_on(async {
@ -35,10 +34,19 @@ fn main() -> io::Result<()> {
let mut writer = &stream; let mut writer = &stream;
// Wait until the standard input is closed or the connection is closed. // Wait until the standard input is closed or the connection is closed.
futures::select! { future::race(
_ = io::copy(stdin, &mut writer).fuse() => println!("Quit!"), async {
_ = io::copy(reader, &mut stdout).fuse() => println!("Server disconnected!"), let res = io::copy(stdin, &mut writer).await;
} println!("Quit!");
res
},
async {
let res = io::copy(reader, &mut stdout).await;
println!("Server disconnected!");
res
},
)
.await?;
Ok(()) Ok(())
}) })

View File

@ -19,8 +19,7 @@ use async_channel::{bounded, Receiver, Sender};
use async_dup::Arc; use async_dup::Arc;
use async_io::Async; use async_io::Async;
use blocking::block_on; use blocking::block_on;
use futures::io::{self, BufReader}; use futures_lite::*;
use futures::prelude::*;
use smol::Task; use smol::Task;
/// An event on the chat server. /// An event on the chat server.
@ -70,7 +69,7 @@ async fn dispatch(receiver: Receiver<Event>) -> io::Result<()> {
/// Reads messages from the client and forwards them to the dispatcher task. /// Reads messages from the client and forwards them to the dispatcher task.
async fn read_messages(sender: Sender<Event>, client: Arc<Async<TcpStream>>) -> io::Result<()> { async fn read_messages(sender: Sender<Event>, client: Arc<Async<TcpStream>>) -> io::Result<()> {
let addr = client.get_ref().peer_addr()?; let addr = client.get_ref().peer_addr()?;
let mut lines = BufReader::new(client).lines(); let mut lines = io::BufReader::new(client).lines();
while let Some(line) = lines.next().await { while let Some(line) = lines.next().await {
let line = line?; let line = line?;

View File

@ -7,13 +7,13 @@
//! ``` //! ```
use blocking::block_on; use blocking::block_on;
use futures::prelude::*; use futures_lite::*;
fn main() { fn main() {
// Set a handler that sends a message through a channel. // Set a handler that sends a message through a channel.
let (s, ctrl_c) = async_channel::bounded(100); let (s, ctrl_c) = async_channel::bounded(100);
let handle = move || { let handle = move || {
let _ = s.send(()).now_or_never(); let _ = future::poll_once(s.send(()));
}; };
ctrlc::set_handler(handle).unwrap(); ctrlc::set_handler(handle).unwrap();

View File

@ -16,7 +16,7 @@ use anyhow::{bail, Context as _, Error, Result};
use async_io::Async; use async_io::Async;
use async_native_tls::TlsStream; use async_native_tls::TlsStream;
use blocking::{block_on, unblock}; use blocking::{block_on, unblock};
use futures::prelude::*; use futures_lite::*;
use http::Uri; use http::Uri;
use hyper::{Body, Client, Request, Response}; use hyper::{Body, Client, Request, Response};
use smol::Task; use smol::Task;
@ -42,7 +42,7 @@ fn main() -> Result<()> {
// Read the message body. // Read the message body.
let body = resp let body = resp
.into_body() .into_body()
.try_fold(Vec::new(), |mut body, chunk| async move { .try_fold(Vec::new(), |mut body, chunk| {
body.extend_from_slice(&chunk); body.extend_from_slice(&chunk);
Ok(body) Ok(body)
}) })
@ -70,7 +70,7 @@ struct SmolConnector;
impl hyper::service::Service<Uri> for SmolConnector { impl hyper::service::Service<Uri> for SmolConnector {
type Response = SmolStream; type Response = SmolStream;
type Error = Error; type Error = Error;
type Future = future::BoxFuture<'static, Result<Self::Response, Self::Error>>; type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(())) Poll::Ready(Ok(()))

View File

@ -23,7 +23,7 @@ use anyhow::{Error, Result};
use async_io::Async; use async_io::Async;
use async_native_tls::{Identity, TlsAcceptor, TlsStream}; use async_native_tls::{Identity, TlsAcceptor, TlsStream};
use blocking::block_on; use blocking::block_on;
use futures::prelude::*; use futures_lite::*;
use hyper::service::{make_service_fn, service_fn}; use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server}; use hyper::{Body, Request, Response, Server};
use smol::Task; use smol::Task;
@ -103,7 +103,7 @@ impl hyper::server::accept::Accept for SmolListener {
cx: &mut Context, cx: &mut Context,
) -> Poll<Option<Result<Self::Conn, Self::Error>>> { ) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
let poll = Pin::new(&mut self.listener.incoming()).poll_next(cx); let poll = Pin::new(&mut self.listener.incoming()).poll_next(cx);
let stream = futures::ready!(poll).unwrap()?; let stream = ready!(poll).unwrap()?;
let stream = match &self.tls { let stream = match &self.tls {
None => SmolStream::Plain(stream), None => SmolStream::Plain(stream),
@ -132,7 +132,7 @@ enum SmolStream {
Tls(TlsStream<Async<TcpStream>>), Tls(TlsStream<Async<TcpStream>>),
/// A TCP connection that is in process of getting secured by TLS. /// A TCP connection that is in process of getting secured by TLS.
Handshake(future::BoxFuture<'static, io::Result<TlsStream<Async<TcpStream>>>>), Handshake(Pin<Box<dyn Future<Output = io::Result<TlsStream<Async<TcpStream>>>> + Send>>),
} }
impl hyper::client::connect::Connection for SmolStream { impl hyper::client::connect::Connection for SmolStream {
@ -152,7 +152,7 @@ impl tokio::io::AsyncRead for SmolStream {
SmolStream::Plain(s) => return Pin::new(s).poll_read(cx, buf), SmolStream::Plain(s) => return Pin::new(s).poll_read(cx, buf),
SmolStream::Tls(s) => return Pin::new(s).poll_read(cx, buf), SmolStream::Tls(s) => return Pin::new(s).poll_read(cx, buf),
SmolStream::Handshake(f) => { SmolStream::Handshake(f) => {
let s = futures::ready!(f.as_mut().poll(cx))?; let s = ready!(f.as_mut().poll(cx))?;
*self = SmolStream::Tls(s); *self = SmolStream::Tls(s);
} }
} }
@ -171,7 +171,7 @@ impl tokio::io::AsyncWrite for SmolStream {
SmolStream::Plain(s) => return Pin::new(s).poll_write(cx, buf), SmolStream::Plain(s) => return Pin::new(s).poll_write(cx, buf),
SmolStream::Tls(s) => return Pin::new(s).poll_write(cx, buf), SmolStream::Tls(s) => return Pin::new(s).poll_write(cx, buf),
SmolStream::Handshake(f) => { SmolStream::Handshake(f) => {
let s = futures::ready!(f.as_mut().poll(cx))?; let s = ready!(f.as_mut().poll(cx))?;
*self = SmolStream::Tls(s); *self = SmolStream::Tls(s);
} }
} }

View File

@ -11,7 +11,7 @@ use std::net::{TcpStream, ToSocketAddrs};
use anyhow::{bail, Context as _, Result}; use anyhow::{bail, Context as _, Result};
use async_io::Async; use async_io::Async;
use blocking::{block_on, unblock}; use blocking::{block_on, unblock};
use futures::prelude::*; use futures_lite::*;
use url::Url; use url::Url;
/// Sends a GET request and fetches the response. /// Sends a GET request and fetches the response.

View File

@ -19,7 +19,7 @@ use anyhow::Result;
use async_io::Async; use async_io::Async;
use async_native_tls::{Identity, TlsAcceptor}; use async_native_tls::{Identity, TlsAcceptor};
use blocking::block_on; use blocking::block_on;
use futures::prelude::*; use futures_lite::*;
use smol::Task; use smol::Task;
const RESPONSE: &[u8] = br#" const RESPONSE: &[u8] = br#"

View File

@ -16,8 +16,7 @@ use std::net::TcpStream;
use async_io::Async; use async_io::Async;
use blocking::{block_on, Unblock}; use blocking::{block_on, Unblock};
use futures::io; use futures_lite::*;
use futures::prelude::*;
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
block_on(async { block_on(async {

View File

@ -16,7 +16,7 @@ use std::net::{TcpListener, TcpStream};
use async_io::Async; use async_io::Async;
use blocking::block_on; use blocking::block_on;
use futures::io; use futures_lite::*;
use smol::Task; use smol::Task;
/// Echoes messages from the client back to it. /// Echoes messages from the client back to it.

View File

@ -18,8 +18,7 @@ use anyhow::Result;
use async_io::Async; use async_io::Async;
use async_native_tls::{Certificate, TlsConnector}; use async_native_tls::{Certificate, TlsConnector};
use blocking::{block_on, Unblock}; use blocking::{block_on, Unblock};
use futures::io; use futures_lite::*;
use futures::prelude::*;
fn main() -> Result<()> { fn main() -> Result<()> {
// Initialize TLS with the local certificate. // Initialize TLS with the local certificate.

View File

@ -18,7 +18,7 @@ use anyhow::Result;
use async_io::Async; use async_io::Async;
use async_native_tls::{Identity, TlsAcceptor, TlsStream}; use async_native_tls::{Identity, TlsAcceptor, TlsStream};
use blocking::block_on; use blocking::block_on;
use futures::io; use futures_lite::*;
use smol::Task; use smol::Task;
/// Echoes messages from the client back to it. /// Echoes messages from the client back to it.

View File

@ -12,7 +12,7 @@ fn main() -> std::io::Result<()> {
use async_io::Async; use async_io::Async;
use blocking::block_on; use blocking::block_on;
use futures::prelude::*; use futures_lite::*;
block_on(async { block_on(async {
// Create a Unix stream that receives a byte on each signal occurrence. // Create a Unix stream that receives a byte on each signal occurrence.

View File

@ -21,7 +21,8 @@ use async_io::Async;
use async_native_tls::{Certificate, TlsConnector, TlsStream}; use async_native_tls::{Certificate, TlsConnector, TlsStream};
use async_tungstenite::WebSocketStream; use async_tungstenite::WebSocketStream;
use blocking::{block_on, unblock}; use blocking::{block_on, unblock};
use futures::prelude::*; use futures::sink::{Sink, SinkExt};
use futures_lite::*;
use tungstenite::handshake::client::Response; use tungstenite::handshake::client::Response;
use tungstenite::Message; use tungstenite::Message;
use url::Url; use url::Url;

View File

@ -21,7 +21,8 @@ use async_io::Async;
use async_native_tls::{Identity, TlsAcceptor, TlsStream}; use async_native_tls::{Identity, TlsAcceptor, TlsStream};
use async_tungstenite::WebSocketStream; use async_tungstenite::WebSocketStream;
use blocking::block_on; use blocking::block_on;
use futures::prelude::*; use futures::sink::{Sink, SinkExt};
use futures_lite::*;
use smol::Task; use smol::Task;
use tungstenite::Message; use tungstenite::Message;

View File

@ -12,8 +12,7 @@ fn main() -> std::io::Result<()> {
use async_io::Async; use async_io::Async;
use blocking::{block_on, Unblock}; use blocking::{block_on, Unblock};
use futures::io; use futures_lite::*;
use futures::prelude::*;
use smol::Task; use smol::Task;
use tempfile::tempdir; use tempfile::tempdir;
use uds_windows::{UnixListener, UnixStream}; use uds_windows::{UnixListener, UnixStream};