跳到主要内容

AsyncSeekExt

搜索

特性 AsyncSeekExt 

Source
pub trait AsyncSeekExt: AsyncSeek {
    // Provided methods
    fn seek(&mut self, pos: SeekFrom) -> Seek<'_, Self>
       where Self: Unpin { ... }
    fn rewind(&mut self) -> Seek<'_, Self>
       where Self: Unpin { ... }
    fn stream_position(&mut self) -> Seek<'_, Self>
       where Self: Unpin { ... }
}
展开描述

一个为 AsyncSeek 类型添加实用方法的扩展 trait。

§示例

use std::io::{self, Cursor, SeekFrom};
use tokio::io::{AsyncSeekExt, AsyncReadExt};

let mut cursor = Cursor::new(b"abcdefg");

// the `seek` method is defined by this trait
cursor.seek(SeekFrom::Start(3)).await?;

let mut buf = [0; 1];
let n = cursor.read(&mut buf).await?;
assert_eq!(n, 1);
assert_eq!(buf, [b'd']);

Ok(())

更多详情请参阅 module 文档。

提供方法§

Source

fn seek(&mut self, pos: SeekFrom) -> Seek<'_, Self>
where Self: Unpin,

创建一个 future,对 IO 对象进行定位,然后产出该对象的新位置以及该对象本身。

Equivalent to:

async fn seek(&mut self, pos: SeekFrom) -> io::Result<u64>;

如果出现错误,缓冲区和对象将被丢弃,并产出错误。

§示例
use tokio::fs::File;
use tokio::io::{AsyncSeekExt, AsyncReadExt};

use std::io::SeekFrom;

let mut file = File::open("foo.txt").await?;
file.seek(SeekFrom::Start(6)).await?;

let mut contents = vec![0u8; 10];
file.read_exact(&mut contents).await?;
Source

fn rewind(&mut self) -> Seek<'_, Self>
where Self: Unpin,

创建一个 future,将倒回到流的开头。

这是一个便捷方法,等价于 self.seek(SeekFrom::Start(0))

Source

fn stream_position(&mut self) -> Seek<'_, Self>
where Self: Unpin,

创建一个 future,返回从流开头算起的当前定位位置。

这等价于 self.seek(SeekFrom::Current(0))

动态兼容性§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

实现者§