io: add convenience method AsyncSeekExt::rewind. (#4107)

This commit is contained in:
Thibeau Vercruyssen
2021-09-14 16:05:01 +02:00
committed by GitHub
parent 3fe1662e4b
commit 7af0f32751
2 changed files with 26 additions and 4 deletions
+10
View File
@@ -67,6 +67,16 @@ cfg_io_util! {
seek(self, pos)
}
/// Creates a future which will rewind to the beginning of the stream.
///
/// This is convenience method, equivalent to to `self.seek(SeekFrom::Start(0))`.
fn rewind(&mut self) -> Seek<'_, Self>
where
Self: Unpin,
{
self.seek(SeekFrom::Start(0))
}
/// Creates a future which will return the current seek position from the
/// start of the stream.
///
+16 -4
View File
@@ -1,12 +1,11 @@
#![warn(rust_2018_idioms)]
#![cfg(feature = "full")]
use tokio::fs::File;
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};
use tokio_test::task;
use std::io::prelude::*;
use tempfile::NamedTempFile;
use tokio::fs::File;
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt, SeekFrom};
use tokio_test::task;
const HELLO: &[u8] = b"hello world...";
@@ -50,6 +49,19 @@ async fn basic_write_and_shutdown() {
assert_eq!(file, HELLO);
}
#[tokio::test]
async fn rewind_seek_position() {
let tempfile = tempfile();
let mut file = File::create(tempfile.path()).await.unwrap();
file.seek(SeekFrom::Current(10)).await.unwrap();
file.rewind().await.unwrap();
assert_eq!(file.stream_position().await.unwrap(), 0);
}
#[tokio::test]
async fn coop() {
let mut tempfile = tempfile();