Skip to content

Re-export IO traits from futures #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from Sep 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ matrix:
- mdbook test -L ./target/debug/deps docs

script:
- cargo check --all --benches --bins --examples --tests
- cargo check --features unstable --all --benches --bins --examples --tests
- cargo test --all --doc --features unstable
15 changes: 7 additions & 8 deletions docs/src/concepts/futures.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ Remember the talk about "deferred computation" in the intro? That's all it is. I
Let's have a look at a simple function, specifically the return value:

```rust,edition2018
# use std::{fs::File, io::{self, Read}};
# use std::{fs::File, io, io::prelude::*};
#
fn read_file(path: &str) -> Result<String, io::Error> {
fn read_file(path: &str) -> io::Result<String> {
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Expand All @@ -67,9 +67,9 @@ Note that this return value talks about the past. The past has a drawback: all d
But we wanted to abstract over *computation* and let someone else choose how to run it. That's fundamentally incompatible with looking at the results of previous computation all the time. So, let's find a type that *describes* a computation without running it. Let's look at the function again:

```rust,edition2018
# use std::{fs::File, io::{self, Read}};
# use std::{fs::File, io, io::prelude::*};
#
fn read_file(path: &str) -> Result<String, io::Error> {
fn read_file(path: &str) -> io::Result<String> {
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Expand Down Expand Up @@ -112,10 +112,9 @@ While the `Future` trait has existed in Rust for a while, it was inconvenient to

```rust,edition2018
# extern crate async_std;
# use async_std::{fs::File, io::Read};
# use std::io;
# use async_std::{fs::File, io, io::prelude::*};
#
async fn read_file(path: &str) -> Result<String, io::Error> {
async fn read_file(path: &str) -> io::Result<String> {
let mut file = File::open(path).await?;
let mut contents = String::new();
file.read_to_string(&mut contents).await?;
Expand All @@ -125,7 +124,7 @@ async fn read_file(path: &str) -> Result<String, io::Error> {

Amazingly little difference, right? All we did is label the function `async` and insert 2 special commands: `.await`.

This `async` function sets up a deferred computation. When this function is called, it will produce a `Future<Output=Result<String, io::Error>>` instead of immediately returning a `Result<String, io::Error>`. (Or, more precisely, generate a type for you that implements `Future<Output=Result<String, io::Error>>`.)
This `async` function sets up a deferred computation. When this function is called, it will produce a `Future<Output = io::Result<String>>` instead of immediately returning a `io::Result<String>`. (Or, more precisely, generate a type for you that implements `Future<Output = io::Result<String>>`.)

## What does `.await` do?

Expand Down
9 changes: 4 additions & 5 deletions docs/src/concepts/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ In `async-std`, the [`tasks`][tasks] module is responsible for this. The simples

```rust,edition2018
# extern crate async_std;
use async_std::{io, task, fs::File, io::Read};
use async_std::{fs::File, io, prelude::*, task};

async fn read_file(path: &str) -> Result<String, io::Error> {
async fn read_file(path: &str) -> io::Result<String> {
let mut file = File::open(path).await?;
let mut contents = String::new();
file.read_to_string(&mut contents).await?;
Expand All @@ -33,10 +33,9 @@ This asks the runtime baked into `async_std` to execute the code that reads a fi

```rust,edition2018
# extern crate async_std;
# use async_std::{fs::File, io::Read, task};
# use std::io;
# use async_std::{fs::File, io, prelude::*, task};
#
# async fn read_file(path: &str) -> Result<String, io::Error> {
# async fn read_file(path: &str) -> io::Result<String> {
# let mut file = File::open(path).await?;
# let mut contents = String::new();
# file.read_to_string(&mut contents).await?;
Expand Down
4 changes: 2 additions & 2 deletions docs/src/tutorial/accept_loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Now we can write the server's accept loop:
# extern crate async_std;
# use async_std::{
# net::{TcpListener, ToSocketAddrs},
# prelude::Stream,
# prelude::*,
# };
#
# type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
Expand Down Expand Up @@ -66,7 +66,7 @@ Finally, let's add main:
# extern crate async_std;
# use async_std::{
# net::{TcpListener, ToSocketAddrs},
# prelude::Stream,
# prelude::*,
# task,
# };
#
Expand Down
3 changes: 1 addition & 2 deletions docs/src/tutorial/connecting_readers_and_writers.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ The order of events "Bob sends message to Alice" and "Alice joins" is determined
# extern crate futures_channel;
# extern crate futures_util;
# use async_std::{
# io::{Write},
# net::TcpStream,
# prelude::{Future, Stream},
# prelude::*,
# task,
# };
# use futures_channel::mpsc;
Expand Down
21 changes: 11 additions & 10 deletions docs/src/tutorial/handling_disconnection.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ We use the `select` macro for this purpose:
# extern crate async_std;
# extern crate futures_channel;
# extern crate futures_util;
# use async_std::{io::Write, net::TcpStream};
# use async_std::{net::TcpStream, prelude::*};
use futures_channel::mpsc;
use futures_util::{select, FutureExt, StreamExt};
use futures_util::{select, FutureExt};
# use std::sync::Arc;

# type Receiver<T> = mpsc::UnboundedReceiver<T>;
Expand All @@ -94,11 +94,11 @@ async fn client_writer(
let mut shutdown = shutdown.fuse();
loop { // 2
select! {
msg = messages.next() => match msg {
msg = messages.next().fuse() => match msg {
Some(msg) => stream.write_all(msg.as_bytes()).await?,
None => break,
},
void = shutdown.next() => match void {
void = shutdown.next().fuse() => match void {
Some(void) => match void {}, // 3
None => break,
}
Expand All @@ -125,12 +125,13 @@ The final code looks like this:
# extern crate futures_channel;
# extern crate futures_util;
use async_std::{
io::{BufReader, BufRead, Write},
io::BufReader,
net::{TcpListener, TcpStream, ToSocketAddrs},
prelude::*,
task,
};
use futures_channel::mpsc;
use futures_util::{select, FutureExt, SinkExt, StreamExt};
use futures_util::{select, FutureExt, SinkExt};
use std::{
collections::hash_map::{Entry, HashMap},
future::Future,
Expand Down Expand Up @@ -209,11 +210,11 @@ async fn client_writer(
let mut shutdown = shutdown.fuse();
loop {
select! {
msg = messages.next() => match msg {
msg = messages.next().fuse() => match msg {
Some(msg) => stream.write_all(msg.as_bytes()).await?,
None => break,
},
void = shutdown.next() => match void {
void = shutdown.next().fuse() => match void {
Some(void) => match void {},
None => break,
}
Expand Down Expand Up @@ -243,11 +244,11 @@ async fn broker(events: Receiver<Event>) {
let mut events = events.fuse();
loop {
let event = select! {
event = events.next() => match event {
event = events.next().fuse() => match event {
None => break, // 2
Some(event) => event,
},
disconnect = disconnect_receiver.next() => {
disconnect = disconnect_receiver.next().fuse() => {
let (name, _pending_messages) = disconnect.unwrap(); // 3
assert!(peers.remove(&name).is_some());
continue;
Expand Down
9 changes: 5 additions & 4 deletions docs/src/tutorial/implementing_a_client.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ With async, we can just use the `select!` macro.
# extern crate async_std;
# extern crate futures_util;
use async_std::{
io::{stdin, BufRead, BufReader, Write},
io::{stdin, BufReader},
net::{TcpStream, ToSocketAddrs},
prelude::*,
task,
};
use futures_util::{select, FutureExt, StreamExt};
use futures_util::{select, FutureExt};

type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

Expand All @@ -38,14 +39,14 @@ async fn try_run(addr: impl ToSocketAddrs) -> Result<()> {
let mut lines_from_stdin = BufReader::new(stdin()).lines().fuse(); // 2
loop {
select! { // 3
line = lines_from_server.next() => match line {
line = lines_from_server.next().fuse() => match line {
Some(line) => {
let line = line?;
println!("{}", line);
},
None => break,
},
line = lines_from_stdin.next() => match line {
line = lines_from_stdin.next().fuse() => match line {
Some(line) => {
let line = line?;
writer.write_all(line.as_bytes()).await?;
Expand Down
10 changes: 5 additions & 5 deletions docs/src/tutorial/receiving_messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ We need to:
```rust,edition2018
# extern crate async_std;
# use async_std::{
# io::{BufRead, BufReader},
# io::BufReader,
# net::{TcpListener, TcpStream, ToSocketAddrs},
# prelude::Stream,
# prelude::*,
# task,
# };
#
Expand Down Expand Up @@ -75,9 +75,9 @@ We can "fix" it by waiting for the task to be joined, like this:
# #![feature(async_closure)]
# extern crate async_std;
# use async_std::{
# io::{BufRead, BufReader},
# io::BufReader,
# net::{TcpListener, TcpStream, ToSocketAddrs},
# prelude::Stream,
# prelude::*,
# task,
# };
#
Expand Down Expand Up @@ -125,7 +125,7 @@ So let's use a helper function for this:
# extern crate async_std;
# use async_std::{
# io,
# prelude::Future,
# prelude::*,
# task,
# };
fn spawn_and_log_error<F>(fut: F) -> task::JoinHandle<()>
Expand Down
3 changes: 1 addition & 2 deletions docs/src/tutorial/sending_messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ if Alice and Charley send two messages to Bob at the same time, Bob will see the
# extern crate futures_channel;
# extern crate futures_util;
# use async_std::{
# io::Write,
# net::TcpStream,
# prelude::Stream,
# prelude::*,
# };
use futures_channel::mpsc; // 1
use futures_util::sink::SinkExt;
Expand Down
26 changes: 8 additions & 18 deletions src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};

use cfg_if::cfg_if;
use futures_io::{AsyncRead, AsyncSeek, AsyncWrite, Initializer};

use crate::fs::{Metadata, Permissions};
use crate::future;
use crate::io::{self, SeekFrom, Write};
use crate::io::{self, Read, Seek, SeekFrom, Write};
use crate::prelude::*;
use crate::task::{self, blocking, Context, Poll, Waker};

/// An open file on the filesystem.
Expand Down Expand Up @@ -302,22 +302,17 @@ impl fmt::Debug for File {
}
}

impl AsyncRead for File {
impl Read for File {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut &*self).poll_read(cx, buf)
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
}
}

impl AsyncRead for &File {
impl Read for &File {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand All @@ -326,14 +321,9 @@ impl AsyncRead for &File {
let state = futures_core::ready!(self.lock.poll_lock(cx));
state.poll_read(cx, buf)
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
}
}

impl AsyncWrite for File {
impl Write for File {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand All @@ -351,7 +341,7 @@ impl AsyncWrite for File {
}
}

impl AsyncWrite for &File {
impl Write for &File {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand All @@ -372,7 +362,7 @@ impl AsyncWrite for &File {
}
}

impl AsyncSeek for File {
impl Seek for File {
fn poll_seek(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand All @@ -382,7 +372,7 @@ impl AsyncSeek for File {
}
}

impl AsyncSeek for &File {
impl Seek for &File {
fn poll_seek(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
32 changes: 0 additions & 32 deletions src/io/buf_read/fill_buf.rs

This file was deleted.

8 changes: 3 additions & 5 deletions src/io/buf_read/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use std::mem;
use std::pin::Pin;
use std::str;

use futures_io::AsyncBufRead;

use super::read_until_internal;
use crate::io;
use crate::io::{self, BufRead};
use crate::task::{Context, Poll};

/// A stream of lines in a byte stream.
Expand All @@ -25,7 +23,7 @@ pub struct Lines<R> {
pub(crate) read: usize,
}

impl<R: AsyncBufRead> futures_core::stream::Stream for Lines<R> {
impl<R: BufRead> futures_core::stream::Stream for Lines<R> {
type Item = io::Result<String>;

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Expand All @@ -50,7 +48,7 @@ impl<R: AsyncBufRead> futures_core::stream::Stream for Lines<R> {
}
}

pub fn read_line_internal<R: AsyncBufRead + ?Sized>(
pub fn read_line_internal<R: BufRead + ?Sized>(
reader: Pin<&mut R>,
cx: &mut Context<'_>,
buf: &mut String,
Expand Down
Loading