Skip to content

Commit

Permalink
Merge pull request #1006 from nnethercote/rm-extension_trait
Browse files Browse the repository at this point in the history
Remove `extension_trait`
  • Loading branch information
yoshuawuyts committed Mar 16, 2022
2 parents 8b812d5 + cca0f3e commit 7e455db
Show file tree
Hide file tree
Showing 8 changed files with 2,652 additions and 3,413 deletions.
612 changes: 234 additions & 378 deletions src/future/future/mod.rs

Large diffs are not rendered by default.

472 changes: 185 additions & 287 deletions src/io/buf_read/mod.rs

Large diffs are not rendered by default.

639 changes: 273 additions & 366 deletions src/io/read/mod.rs

Large diffs are not rendered by default.

130 changes: 30 additions & 100 deletions src/io/seek/mod.rs
Expand Up @@ -4,117 +4,47 @@ use seek::SeekFuture;

use crate::io::SeekFrom;

extension_trait! {
use std::ops::{Deref, DerefMut};
use std::pin::Pin;
pub use futures_io::AsyncSeek as Seek;

use crate::io;
use crate::task::{Context, Poll};
#[doc = r#"
Extension methods for [`Seek`].
[`Seek`]: ../trait.Seek.html
"#]
pub trait SeekExt: Seek {
#[doc = r#"
Allows seeking through a byte stream.
Seeks to a new position in a byte stream.
This trait is a re-export of [`futures::io::AsyncSeek`] and is an async version of
[`std::io::Seek`].
Returns the new position in the byte stream.
The [provided methods] do not really exist in the trait itself, but they become
available when [`SeekExt`] the [prelude] is imported:
A seek beyond the end of stream is allowed, but behavior is defined by the
implementation.
```
# #[allow(unused_imports)]
# Examples
```no_run
# fn main() -> std::io::Result<()> { async_std::task::block_on(async {
#
use async_std::fs::File;
use async_std::io::SeekFrom;
use async_std::prelude::*;
```
[`std::io::Seek`]: https://doc.rust-lang.org/std/io/trait.Seek.html
[`futures::io::AsyncSeek`]:
https://docs.rs/futures/0.3/futures/io/trait.AsyncSeek.html
[provided methods]: #provided-methods
[`SeekExt`]: ../io/prelude/trait.SeekExt.html
[prelude]: ../prelude/index.html
"#]
pub trait Seek {
#[doc = r#"
Attempt to seek to an offset, in bytes, in a stream.
"#]
fn poll_seek(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
pos: SeekFrom,
) -> Poll<io::Result<u64>>;
}
let mut file = File::open("a.txt").await?;
#[doc = r#"
Extension methods for [`Seek`].
[`Seek`]: ../trait.Seek.html
let file_len = file.seek(SeekFrom::End(0)).await?;
#
# Ok(()) }) }
```
"#]
pub trait SeekExt: futures_io::AsyncSeek {
#[doc = r#"
Seeks to a new position in a byte stream.
Returns the new position in the byte stream.
A seek beyond the end of stream is allowed, but behavior is defined by the
implementation.
# Examples
```no_run
# fn main() -> std::io::Result<()> { async_std::task::block_on(async {
#
use async_std::fs::File;
use async_std::io::SeekFrom;
use async_std::prelude::*;
let mut file = File::open("a.txt").await?;
let file_len = file.seek(SeekFrom::End(0)).await?;
#
# Ok(()) }) }
```
"#]
fn seek(
&mut self,
pos: SeekFrom,
) -> impl Future<Output = io::Result<u64>> + '_ [SeekFuture<'_, Self>]
where
Self: Unpin,
{
SeekFuture { seeker: self, pos }
}
}

impl<T: Seek + Unpin + ?Sized> Seek for Box<T> {
fn poll_seek(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
pos: SeekFrom,
) -> Poll<io::Result<u64>> {
unreachable!("this impl only appears in the rendered docs")
}
}

impl<T: Seek + Unpin + ?Sized> Seek for &mut T {
fn poll_seek(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
pos: SeekFrom,
) -> Poll<io::Result<u64>> {
unreachable!("this impl only appears in the rendered docs")
}
}

impl<P> Seek for Pin<P>
fn seek(
&mut self,
pos: SeekFrom,
) -> SeekFuture<'_, Self>
where
P: DerefMut + Unpin,
<P as Deref>::Target: Seek,
Self: Unpin,
{
fn poll_seek(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
pos: SeekFrom,
) -> Poll<io::Result<u64>> {
unreachable!("this impl only appears in the rendered docs")
}
SeekFuture { seeker: self, pos }
}
}

impl<T: Seek + ?Sized> SeekExt for T {}

0 comments on commit 7e455db

Please sign in to comment.