安裝
捨棄 Future 表示 Future 無法再供輪詢。這稱為「取消」,可能發生在任何 await 時間點。即使 Future 遭到取消,也需審慎確保系統正常運作,例如不應出現死結或遺失資料。
use std::io::{self, ErrorKind};
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt, DuplexStream};
struct LinesReader {
stream: DuplexStream,
}
impl LinesReader {
fn new(stream: DuplexStream) -> Self {
Self { stream }
}
async fn next(&mut self) -> io::Result<Option<String>> {
let mut bytes = Vec::new();
let mut buf = [0];
while self.stream.read(&mut buf[..]).await? != 0 {
bytes.push(buf[0]);
if buf[0] == b'\n' {
break;
}
}
if bytes.is_empty() {
return Ok(None);
}
let s = String::from_utf8(bytes)
.map_err(|_| io::Error::new(ErrorKind::InvalidData, "非 UTF-8"))?;
Ok(Some(s))
}
}
async fn slow_copy(source: String, mut dest: DuplexStream) -> std::io::Result<()> {
for b in source.bytes() {
dest.write_u8(b).await?;
tokio::time::sleep(Duration::from_millis(10)).await
}
Ok(())
}
#[tokio::main]
async fn main() -> std::io::Result<()> {
let (client, server) = tokio::io::duplex(5);
let handle = tokio::spawn(slow_copy("hi\nthere\n".to_owned(), client));
let mut lines = LinesReader::new(server);
let mut interval = tokio::time::interval(Duration::from_millis(60));
loop {
tokio::select! {
_ = interval.tick() => println!("tick!"),
line = lines.next() => if let Some(l) = line? {
print!("{}", l)
} else {
break
},
}
}
handle.await.unwrap()?;
Ok(())
}
-
編譯器無法確保安全的取消作業。您需要閱讀 API 說明文件,並考量
async fn保留的狀態。 -
與
panic和?不同,取消是正常控制流程的一部分 (相較於錯誤處理)。 -
此範例失去字串的某些部分。
-
每當
tick()分支版本先完成時,next()和其buf都會遭到捨棄。 -
LinesReader可讓buf成為結構體的一部分,確保安全的取消作業:#![allow(unused)] fn main() { struct LinesReader { stream: DuplexStream, bytes: Vec<u8>, buf: [u8; 1], } impl LinesReader { fn new(stream: DuplexStream) -> Self { Self { stream, bytes: Vec::new(), buf: [0] } } async fn next(&mut self) -> io::Result<Option<String>> { // prefix buf and bytes with self. // ... let raw = std::mem::take(&mut self.bytes); let s = String::from_utf8(raw) // ... } } }
-
-
Interval::tick會追蹤滴答是否「已送達」,因此可確保安全的取消作業。 -
AsyncReadExt::readod.read) 只會傳回資料或不讀取資料,因此可確保安全的取消作業。 -
AsyncBufReadExt::read_line與本範例相似,「無法」確保安全的取消作業。如要瞭解詳情和替代方案,請參閱說明文件。