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 4 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
13 changes: 6 additions & 7 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 @@ -69,7 +69,7 @@ But we wanted to abstract over *computation* and let someone else choose how to
```rust,edition2018
# use std::{fs::File, io::{self, Read}};
#
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
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