mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-25 00:00:18 +02:00
io: add convenience method AsyncSeekExt::rewind. (#4107)
This commit is contained in:
@@ -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
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user