|
|
<!DOCTYPE HTML>
|
|
|
<html lang="en" class="light sidebar-visible" dir="ltr">
|
|
|
<head>
|
|
|
<!-- Book generated using mdBook -->
|
|
|
<meta charset="UTF-8">
|
|
|
<title>深入理解 async 相关的 traits - Rust 程序设计语言 简体中文版</title>
|
|
|
|
|
|
|
|
|
<!-- Custom HTML head -->
|
|
|
|
|
|
<meta name="description" content="">
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
<meta name="theme-color" content="#ffffff">
|
|
|
|
|
|
<link rel="icon" href="favicon-de23e50b.svg">
|
|
|
<link rel="shortcut icon" href="favicon-8114d1fc.png">
|
|
|
<link rel="stylesheet" href="css/variables-8adf115d.css">
|
|
|
<link rel="stylesheet" href="css/general-2459343d.css">
|
|
|
<link rel="stylesheet" href="css/chrome-ae938929.css">
|
|
|
<link rel="stylesheet" href="css/print-9e4910d8.css" media="print">
|
|
|
|
|
|
<!-- Fonts -->
|
|
|
<link rel="stylesheet" href="fonts/fonts-9644e21d.css">
|
|
|
|
|
|
<!-- Highlight.js Stylesheets -->
|
|
|
<link rel="stylesheet" id="mdbook-highlight-css" href="highlight-493f70e1.css">
|
|
|
<link rel="stylesheet" id="mdbook-tomorrow-night-css" href="tomorrow-night-4c0ae647.css">
|
|
|
<link rel="stylesheet" id="mdbook-ayu-highlight-css" href="ayu-highlight-3fdfc3ac.css">
|
|
|
|
|
|
<!-- Custom theme stylesheets -->
|
|
|
<link rel="stylesheet" href="ferris-d33b75bf.css">
|
|
|
<link rel="stylesheet" href="theme/2018-edition-4e126c62.css">
|
|
|
<link rel="stylesheet" href="theme/semantic-notes-9b5766c0.css">
|
|
|
<link rel="stylesheet" href="theme/listing-cab26221.css">
|
|
|
|
|
|
|
|
|
<!-- Provide site root and default themes to javascript -->
|
|
|
<script>
|
|
|
const path_to_root = "";
|
|
|
const default_light_theme = "light";
|
|
|
const default_dark_theme = "navy";
|
|
|
window.path_to_searchindex_js = "searchindex-e0ffcbc3.js";
|
|
|
</script>
|
|
|
<!-- Start loading toc.js asap -->
|
|
|
<script src="toc-fb31ca2f.js"></script>
|
|
|
</head>
|
|
|
<body>
|
|
|
<div id="mdbook-help-container">
|
|
|
<div id="mdbook-help-popup">
|
|
|
<h2 class="mdbook-help-title">Keyboard shortcuts</h2>
|
|
|
<div>
|
|
|
<p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p>
|
|
|
<p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
|
|
|
<p>Press <kbd>?</kbd> to show this help</p>
|
|
|
<p>Press <kbd>Esc</kbd> to hide this help</p>
|
|
|
</div>
|
|
|
</div>
|
|
|
</div>
|
|
|
<div id="mdbook-body-container">
|
|
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
|
<script>
|
|
|
try {
|
|
|
let theme = localStorage.getItem('mdbook-theme');
|
|
|
let sidebar = localStorage.getItem('mdbook-sidebar');
|
|
|
|
|
|
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
|
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
|
}
|
|
|
|
|
|
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
|
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
|
}
|
|
|
} catch (e) { }
|
|
|
</script>
|
|
|
|
|
|
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
|
<script>
|
|
|
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
|
|
|
let theme;
|
|
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
|
const html = document.documentElement;
|
|
|
html.classList.remove('light')
|
|
|
html.classList.add(theme);
|
|
|
html.classList.add("js");
|
|
|
</script>
|
|
|
|
|
|
<input type="checkbox" id="mdbook-sidebar-toggle-anchor" class="hidden">
|
|
|
|
|
|
<!-- Hide / unhide sidebar before it is displayed -->
|
|
|
<script>
|
|
|
let sidebar = null;
|
|
|
const sidebar_toggle = document.getElementById("mdbook-sidebar-toggle-anchor");
|
|
|
if (document.body.clientWidth >= 1080) {
|
|
|
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
|
sidebar = sidebar || 'visible';
|
|
|
} else {
|
|
|
sidebar = 'hidden';
|
|
|
sidebar_toggle.checked = false;
|
|
|
}
|
|
|
if (sidebar === 'visible') {
|
|
|
sidebar_toggle.checked = true;
|
|
|
} else {
|
|
|
html.classList.remove('sidebar-visible');
|
|
|
}
|
|
|
</script>
|
|
|
|
|
|
<nav id="mdbook-sidebar" class="sidebar" aria-label="Table of contents">
|
|
|
<!-- populated by js -->
|
|
|
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
|
|
|
<noscript>
|
|
|
<iframe class="sidebar-iframe-outer" src="toc.html"></iframe>
|
|
|
</noscript>
|
|
|
<div id="mdbook-sidebar-resize-handle" class="sidebar-resize-handle">
|
|
|
<div class="sidebar-resize-indicator"></div>
|
|
|
</div>
|
|
|
</nav>
|
|
|
|
|
|
<div id="mdbook-page-wrapper" class="page-wrapper">
|
|
|
|
|
|
<div class="page">
|
|
|
<div id="mdbook-menu-bar-hover-placeholder"></div>
|
|
|
<div id="mdbook-menu-bar" class="menu-bar sticky">
|
|
|
<div class="left-buttons">
|
|
|
<label id="mdbook-sidebar-toggle" class="icon-button" for="mdbook-sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="mdbook-sidebar">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M0 96C0 78.3 14.3 64 32 64H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32C14.3 128 0 113.7 0 96zM0 256c0-17.7 14.3-32 32-32H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32c-17.7 0-32-14.3-32-32zM448 416c0 17.7-14.3 32-32 32H32c-17.7 0-32-14.3-32-32s14.3-32 32-32H416c17.7 0 32 14.3 32 32z"/></svg></span>
|
|
|
</label>
|
|
|
<button id="mdbook-theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="mdbook-theme-list">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M371.3 367.1c27.3-3.9 51.9-19.4 67.2-42.9L600.2 74.1c12.6-19.5 9.4-45.3-7.6-61.2S549.7-4.4 531.1 9.6L294.4 187.2c-24 18-38.2 46.1-38.4 76.1L371.3 367.1zm-19.6 25.4l-116-104.4C175.9 290.3 128 339.6 128 400c0 3.9 .2 7.8 .6 11.6c1.8 17.5-10.2 36.4-27.8 36.4H96c-17.7 0-32 14.3-32 32s14.3 32 32 32H240c61.9 0 112-50.1 112-112c0-2.5-.1-5-.2-7.5z"/></svg></span>
|
|
|
</button>
|
|
|
<ul id="mdbook-theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-default_theme">Auto</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-light">Light</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-rust">Rust</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-coal">Coal</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-navy">Navy</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-ayu">Ayu</button></li>
|
|
|
</ul>
|
|
|
<button id="mdbook-search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="mdbook-searchbar">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M416 208c0 45.9-14.9 88.3-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM208 352c79.5 0 144-64.5 144-144s-64.5-144-144-144S64 128.5 64 208s64.5 144 144 144z"/></svg></span>
|
|
|
</button>
|
|
|
</div>
|
|
|
|
|
|
<h1 class="menu-title">Rust 程序设计语言 简体中文版</h1>
|
|
|
|
|
|
<div class="right-buttons">
|
|
|
<a href="print.html" title="Print this book" aria-label="Print this book">
|
|
|
<span class=fa-svg id="print-button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M128 0C92.7 0 64 28.7 64 64v96h64V64H354.7L384 93.3V160h64V93.3c0-17-6.7-33.3-18.7-45.3L400 18.7C388 6.7 371.7 0 354.7 0H128zM384 352v32 64H128V384 368 352H384zm64 32h32c17.7 0 32-14.3 32-32V256c0-35.3-28.7-64-64-64H64c-35.3 0-64 28.7-64 64v96c0 17.7 14.3 32 32 32H64v64c0 35.3 28.7 64 64 64H384c35.3 0 64-28.7 64-64V384zm-16-88c-13.3 0-24-10.7-24-24s10.7-24 24-24s24 10.7 24 24s-10.7 24-24 24z"/></svg></span>
|
|
|
</a>
|
|
|
<a href="https://github.com/KaiserY/trpl-zh-cn/tree/main" title="Git repository" aria-label="Git repository">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg></span>
|
|
|
</a>
|
|
|
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<div id="mdbook-search-wrapper" class="hidden">
|
|
|
<form id="mdbook-searchbar-outer" class="searchbar-outer">
|
|
|
<div class="search-wrapper">
|
|
|
<input type="search" id="mdbook-searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="mdbook-searchresults-outer" aria-describedby="searchresults-header">
|
|
|
<div class="spinner-wrapper">
|
|
|
<span class=fa-svg id="fa-spin"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M304 48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zm0 416c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM48 304c26.5 0 48-21.5 48-48s-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48zm464-48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM142.9 437c18.7-18.7 18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zm0-294.2c18.7-18.7 18.7-49.1 0-67.9S93.7 56.2 75 75s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zM369.1 437c18.7 18.7 49.1 18.7 67.9 0s18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9z"/></svg></span>
|
|
|
</div>
|
|
|
</div>
|
|
|
</form>
|
|
|
<div id="mdbook-searchresults-outer" class="searchresults-outer hidden">
|
|
|
<div id="mdbook-searchresults-header" class="searchresults-header"></div>
|
|
|
<ul id="mdbook-searchresults">
|
|
|
</ul>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
|
<script>
|
|
|
document.getElementById('mdbook-sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
|
document.getElementById('mdbook-sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
|
Array.from(document.querySelectorAll('#mdbook-sidebar a')).forEach(function(link) {
|
|
|
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
|
});
|
|
|
</script>
|
|
|
|
|
|
<div id="mdbook-content" class="content">
|
|
|
<main>
|
|
|
<h2 id="深入理解-async-相关的-trait"><a class="header" href="#深入理解-async-相关的-trait">深入理解 async 相关的 trait</a></h2>
|
|
|
<p><a href="https://github.com/rust-lang/book/blob/9fc2a4e8e478ee1388c2b9ba55e3f12e89808bc2/src/ch17-05-traits-for-async.md">ch17-05-traits-for-async.md</a></p>
|
|
|
<p>贯穿本章,我们以各种方式使用了 <code>Future</code>、<code>Stream</code> 和 <code>StreamExt</code> trait。不过到目前为止,我们一直刻意没有太深入它们究竟是如何工作的、又是如何彼此配合的。对日常 Rust 编程来说,这通常完全没问题。不过有时你会遇到一些场景,在那里你需要额外理解这些 trait 的更多细节,以及 <code>Pin</code> 类型和 <code>Unpin</code> trait。在这一节里,我们会适度深入,足够帮助你应对这些情况,但把<em>真正</em>深入的内容留给其他文档。</p>
|
|
|
<h3 id="future-trait"><a class="header" href="#future-trait"><code>Future</code> trait</a></h3>
|
|
|
<p>让我们先更仔细地看看 <code>Future</code> trait 是如何工作的。Rust 中它的定义如下:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2024"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>use std::pin::Pin;
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
|
pub trait Future {
|
|
|
type Output;
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>这个 trait 定义里包含了不少新类型,也有一些我们之前还没见过的语法,所以我们逐部分来看。</p>
|
|
|
<p>首先,<code>Future</code> 的关联类型 <code>Output</code> 指明了这个 future 最终会解析成什么值。这和 <code>Iterator</code> trait 里的关联类型 <code>Item</code> 是类似的。其次,<code>Future</code> 提供了一个 <code>poll</code> 方法。它接收一个特殊的 <code>Pin</code> 包裹的 <code>self</code> 引用、一个指向 <code>Context</code> 类型的可变引用,并返回 <code>Poll<Self::Output></code>。稍后我们会再讲 <code>Pin</code> 和 <code>Context</code>。现在,先聚焦到这个方法的返回值 <code>Poll</code>:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2024"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>pub enum Poll<T> {
|
|
|
Ready(T),
|
|
|
Pending,
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>这个 <code>Poll</code> 类型有点像 <code>Option</code>。它也有一个带值的变体 <code>Ready(T)</code>,以及一个不带值的变体 <code>Pending</code>。但 <code>Poll</code> 的语义和 <code>Option</code> 完全不同。<code>Pending</code> 表示这个 future 还有工作没做完,因此调用方稍后还需要再次检查。<code>Ready</code> 则表示这个 <code>Future</code> 已经完成,其结果值 <code>T</code> 现在已经可用。</p>
|
|
|
<blockquote>
|
|
|
<p>注意:直接调用 <code>poll</code> 的场景很少,但如果你真的需要这么做,请记住:对于大多数 future 来说,一旦它已经返回过 <code>Ready</code>,调用方就不应再对它调用 <code>poll</code>。很多 future 在 ready 之后再次被轮询时会 panic。那些可以安全重复轮询的 future,会在文档里明确说明。这和 <code>Iterator::next</code> 的行为有些相似。</p>
|
|
|
</blockquote>
|
|
|
<p>当你看到使用 <code>await</code> 的代码时,Rust 在底层会把它编译成调用 <code>poll</code> 的代码。如果你回头看示例 17-4,也就是在单个 URL 的标题解析完成后把它打印出来的那个例子,Rust 编译出来的代码大致会像下面这样(虽然并不完全一致):</p>
|
|
|
<pre><code class="language-rust ignore">match page_title(url).poll() {
|
|
|
Ready(page_title) => match page_title {
|
|
|
Some(title) => println!("The title for {url} was {title}"),
|
|
|
None => println!("{url} had no title"),
|
|
|
}
|
|
|
Pending => {
|
|
|
// 这里该怎么办?
|
|
|
}
|
|
|
}</code></pre>
|
|
|
<p>如果 future 仍然是 <code>Pending</code>,那我们该怎么办?我们需要一种办法不断重试,直到 future 最终准备好。换句话说,我们需要一个循环:</p>
|
|
|
<pre><code class="language-rust ignore">let mut page_title_fut = page_title(url);
|
|
|
loop {
|
|
|
match page_title_fut.poll() {
|
|
|
Ready(value) => match page_title {
|
|
|
Some(title) => println!("The title for {url} was {title}"),
|
|
|
None => println!("{url} had no title"),
|
|
|
}
|
|
|
Pending => {
|
|
|
// continue
|
|
|
}
|
|
|
}
|
|
|
}</code></pre>
|
|
|
<p>但如果 Rust 真按这段代码精确地编译,那么每个 <code>await</code> 就都会变成阻塞式的,这恰恰和我们想要的效果相反!Rust 实际上会保证:这个循环能够把控制权交给某个东西,由它暂停当前 future 的工作,去处理别的 future,然后稍后再回来重新检查当前这个。正如我们已经见过的,这个“某个东西”就是异步运行时,而调度和协调这些工作,正是运行时的核心职责之一。</p>
|
|
|
<p>在<a href="ch17-02-concurrency-with-async.html#通过消息传递在两个任务之间发送数据">“通过消息传递在两个任务之间发送数据”</a><!-- ignore -->一节中,我们描述过等待 <code>rx.recv</code> 的过程。<code>recv</code> 调用会返回一个 future,而等待这个 future 本质上就是在轮询它。我们之前提到,运行时会暂停这个 future,直到它准备好,最终要么得到 <code>Some(message)</code>,要么在信道关闭时得到 <code>None</code>。现在,借助对 <code>Future</code> trait,尤其是 <code>Future::poll</code> 的更深入理解,我们就能看清它的工作方式了:当返回 <code>Poll::Pending</code> 时,运行时知道这个 future 还没准备好;反过来,当 <code>poll</code> 返回 <code>Poll::Ready(Some(message))</code> 或 <code>Poll::Ready(None)</code> 时,运行时就知道这个 future 已经准备好,可以继续推进它。</p>
|
|
|
<p>至于运行时具体是怎么做到这一点的,已经超出了本书的范围。不过关键是看清 future 的基本机制:运行时会去<em>轮询</em>它所负责的每个 future,而当 future 还没准备好时,就让它重新休眠。</p>
|
|
|
<h3 id="pin-类型与-unpin-trait"><a class="header" href="#pin-类型与-unpin-trait"><code>Pin</code> 类型与 <code>Unpin</code> trait</a></h3>
|
|
|
<p>回到示例 17-13,我们使用过 <code>trpl::join!</code> 宏来等待三个 future。不过,更常见的情况是你会有一个集合,比如一个向量,其中包含若干个 future,而这些 future 的个数要到运行时才知道。让我们把示例 17-13 改成示例 17-23 中的代码:把这三个 future 放进一个向量里,再调用 <code>trpl::join_all</code>。不过,这段代码暂时还编译不过。</p>
|
|
|
<figure class="listing">
|
|
|
<p><span class="file-name">文件名:src/main.rs</span></p>
|
|
|
<pre><code class="language-rust ignore does_not_compile"><span class="boring">extern crate trpl; // required for mdbook test
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring">use std::time::Duration;
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span><span class="boring"> trpl::block_on(async {
|
|
|
</span><span class="boring"> let (tx, mut rx) = trpl::channel();
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring"> let tx1 = tx.clone();
|
|
|
</span><span class="boring"> let tx1_fut = async move {
|
|
|
</span><span class="boring"> let vals = vec![
|
|
|
</span><span class="boring"> String::from("hi"),
|
|
|
</span><span class="boring"> String::from("from"),
|
|
|
</span><span class="boring"> String::from("the"),
|
|
|
</span><span class="boring"> String::from("future"),
|
|
|
</span><span class="boring"> ];
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring"> for val in vals {
|
|
|
</span><span class="boring"> tx1.send(val).unwrap();
|
|
|
</span><span class="boring"> trpl::sleep(Duration::from_secs(1)).await;
|
|
|
</span><span class="boring"> }
|
|
|
</span><span class="boring"> };
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring"> let rx_fut = async {
|
|
|
</span><span class="boring"> while let Some(value) = rx.recv().await {
|
|
|
</span><span class="boring"> println!("received '{value}'");
|
|
|
</span><span class="boring"> }
|
|
|
</span><span class="boring"> };
|
|
|
</span><span class="boring">
|
|
|
</span> let tx_fut = async move {
|
|
|
// --snip--
|
|
|
<span class="boring"> let vals = vec![
|
|
|
</span><span class="boring"> String::from("more"),
|
|
|
</span><span class="boring"> String::from("messages"),
|
|
|
</span><span class="boring"> String::from("for"),
|
|
|
</span><span class="boring"> String::from("you"),
|
|
|
</span><span class="boring"> ];
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring"> for val in vals {
|
|
|
</span><span class="boring"> tx.send(val).unwrap();
|
|
|
</span><span class="boring"> trpl::sleep(Duration::from_secs(1)).await;
|
|
|
</span><span class="boring"> }
|
|
|
</span> };
|
|
|
|
|
|
let futures: Vec<Box<dyn Future<Output = ()>>> =
|
|
|
vec![Box::new(tx1_fut), Box::new(rx_fut), Box::new(tx_fut)];
|
|
|
|
|
|
trpl::join_all(futures).await;
|
|
|
<span class="boring"> });
|
|
|
</span><span class="boring">}</span></code></pre>
|
|
|
<figcaption>示例 17-23:等待一个集合中的多个 future</figcaption>
|
|
|
</figure>
|
|
|
<p>我们把每个 future 都放进了一个 <code>Box</code> 中,好把它们变成 <em>trait object</em>,就像我们在第十二章“从 <code>run</code> 返回错误”那一节做的那样。(我们会在第十八章详细讨论 trait object。)使用 trait object 后,我们就能把这些类型各不相同的匿名 future 当成同一种类型来对待,因为它们全都实现了 <code>Future</code> trait。</p>
|
|
|
<p>这也许会让人意外。毕竟,这些 async 代码块都没有返回任何值,所以它们每一个产生的都是 <code>Future<Output = ()></code>。但别忘了:<code>Future</code> 是个 trait,而编译器会为每个 async 代码块生成一个独一无二的 enum,即使它们的输出类型完全相同。就像你不能把两个不同的手写 struct 放进同一个 <code>Vec</code>,你也同样不能把这些编译器生成的不同 enum 混在一起。</p>
|
|
|
<p>然后,我们把这组 future 传给 <code>trpl::join_all</code>,再等待结果。然而,这段代码仍然无法编译。下面是报错中最关键的一部分:</p>
|
|
|
<pre><code class="language-text">error[E0277]: `dyn Future<Output = ()>` cannot be unpinned
|
|
|
--> src/main.rs:48:33
|
|
|
|
|
|
|
48 | trpl::join_all(futures).await;
|
|
|
| ^^^^^ the trait `Unpin` is not implemented for `dyn Future<Output = ()>`
|
|
|
|
|
|
|
= note: consider using the `pin!` macro
|
|
|
consider using `Box::pin` if you need to access the pinned value outside of the current scope
|
|
|
= note: required for `Box<dyn Future<Output = ()>>` to implement `Future`
|
|
|
note: required by a bound in `futures_util::future::join_all::JoinAll`
|
|
|
--> file:///home/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.30/src/future/join_all.rs:29:8
|
|
|
|
|
|
|
27 | pub struct JoinAll<F>
|
|
|
| ------- required by a bound in this struct
|
|
|
28 | where
|
|
|
29 | F: Future,
|
|
|
| ^^^^^^ required by this bound in `JoinAll`
|
|
|
</code></pre>
|
|
|
<p>这段错误信息里的 note 告诉我们,应该使用 <code>pin!</code> 宏来 <em>pin</em> 这些值,也就是把它们放进 <code>Pin</code> 类型中,以保证这些值不会在内存中移动。报错之所以说需要 pin,是因为 <code>dyn Future<Output = ()></code> 需要实现 <code>Unpin</code> trait,而它当前并没有实现。</p>
|
|
|
<p><code>trpl::join_all</code> 返回的是一个名为 <code>JoinAll</code> 的结构体。这个结构体在类型参数 <code>F</code> 上是泛型的,而 <code>F</code> 又被约束必须实现 <code>Future</code> trait。直接通过 <code>await</code> 去等待一个 future 时,Rust 会隐式地把它 pin 住。这也正是为什么我们平常不需要在每个想等待 future 的地方都显式写 <code>pin!</code>。</p>
|
|
|
<p>但这里,我们并不是直接在等待某个 future。相反,我们是通过把一组 future 传给 <code>join_all</code>,构造出了一个新的 future:<code>JoinAll</code>。而 <code>join_all</code> 的签名要求集合中的元素类型都必须实现 <code>Future</code> trait。另一方面,<code>Box<T></code> 只有在它包裹的 <code>T</code> 本身是 future 且实现了 <code>Unpin</code> trait 时,才会实现 <code>Future</code>。</p>
|
|
|
<p>这一下信息量很大!为了真正理解它,我们得再更深入一点,看清 <code>Future</code> trait 尤其是 pinning 这一部分到底是如何运作的。再看一遍 <code>Future</code> trait 的定义:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2024"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>use std::pin::Pin;
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
|
pub trait Future {
|
|
|
type Output;
|
|
|
|
|
|
// Required method
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>这里的 <code>cx</code> 参数以及它的 <code>Context</code> 类型,是运行时在保持 lazy 的同时,真正知道该在什么时候重新检查某个 future 的关键。和前面一样,这部分具体机制超出了本章范围,而且通常也只有在你自己实现 <code>Future</code> 时才需要关注。我们这里聚焦的是 <code>self</code> 的类型,因为这是我们第一次见到一个方法里的 <code>self</code> 带有类型注解。对 <code>self</code> 进行类型注解,和给其他函数参数写类型注解类似,但有两个关键区别:</p>
|
|
|
<ul>
|
|
|
<li>它告诉 Rust:要调用这个方法,<code>self</code> 必须是什么类型。</li>
|
|
|
<li>它不能随便写成任意类型。它必须是方法所实现类型本身、该类型的引用或智能指针,或者是一个包裹了该类型引用的 <code>Pin</code>。</li>
|
|
|
</ul>
|
|
|
<p>我们会在<a href="ch18-00-oop.html">第十八章</a><!-- ignore -->里看到更多相关语法。眼下,只要知道:如果我们想通过轮询 future 来检查它到底是 <code>Pending</code> 还是 <code>Ready(Output)</code>,那么就需要一个 <code>Pin</code> 包裹的、指向该类型的可变引用。</p>
|
|
|
<p><code>Pin</code> 是一种针对指针类类型的包装器,比如 <code>&</code>、<code>&mut</code>、<code>Box</code> 和 <code>Rc</code>。(严格来说,<code>Pin</code> 作用于实现了 <code>Deref</code> 或 <code>DerefMut</code> 的类型,但实际效果基本等同于“引用和智能指针”。)<code>Pin</code> 本身并不是指针,也不像 <code>Rc</code> 或 <code>Arc</code> 那样自带引用计数之类的行为;它纯粹是一个让编译器能够对指针使用方式施加约束的工具。</p>
|
|
|
<p>回忆一下:<code>await</code> 是通过调用 <code>poll</code> 实现的。理解这一点以后,前面的错误信息就已经开始变得容易理解了,不过那个报错说的是 <code>Unpin</code>,不是 <code>Pin</code>。那么,<code>Pin</code> 和 <code>Unpin</code> 究竟是什么关系?为什么 <code>Future</code> 又要求 <code>self</code> 必须放在 <code>Pin</code> 里才能调用 <code>poll</code> 呢?</p>
|
|
|
<p>记住,我们在本章前面提过,一个 future 里的多个 await 点会被编译成一个状态机,而编译器会确保这个状态机遵守 Rust 关于安全性的全部常规规则,包括借用和所有权。为了做到这一点,Rust 会分析:在某个 await 点和下一个 await 点之间,或者直到 async 代码块结束之前,哪些数据是需要保留的。然后,它会在编译出来的状态机里生成对应的变体。每个变体都会得到其对应源代码片段所需的数据访问权限,这种访问可能是获得所有权,也可能是获得可变或不可变引用。</p>
|
|
|
<p>到这里为止,一切都很好:如果你在某个 async 代码块里把所有权或引用关系写错了,借用检查器会告诉你。但当我们想要移动这个代码块对应的 future 时,比如把它放进 <code>Vec</code> 然后传给 <code>join_all</code>,事情就开始变复杂了。</p>
|
|
|
<p>当我们移动一个 future 时,无论是把它放进数据结构,以便通过 <code>join_all</code> 这种方式迭代处理,还是从函数里返回它,本质上都是在移动 Rust 为我们生成的那个状态机。与 Rust 中大多数其他类型不同的是,Rust 为 async 代码块生成的 future,可能会在某个状态变体的字段里保存指向它自身其他字段的引用,就像图 17-4 里的简化示意图那样。</p>
|
|
|
<figure>
|
|
|
<img alt="A single-column, three-row table representing a future, fut1, which has data values 0 and 1 in the first two rows and an arrow pointing from the third row back to the second row, representing an internal reference within the future." src="img/trpl17-04.svg" class="center" />
|
|
|
<figcaption>图 17-4:一个自引用的数据类型</figcaption>
|
|
|
</figure>
|
|
|
<p>但默认情况下,任何包含自引用的对象,一旦移动就是不安全的,因为引用始终指向它们所引用对象的真实内存地址(见图 17-5)。如果我们移动了这个数据结构本身,那么这些内部引用仍然会指向旧位置。然而那个内存地址现在已经失效了。一方面,你之后对数据结构做的修改不会再反映到那些旧引用上;另一方面,更严重的是,计算机此时已经可以把那块内存拿去做别的用途了。最后你很可能会读到完全无关的数据。</p>
|
|
|
<figure>
|
|
|
<img alt="Two tables, depicting two futures, fut1 and fut2, each of which has one column and three rows, representing the result of having moved a future out of fut1 into fut2. The first, fut1, is grayed out, with a question mark in each index, representing unknown memory. The second, fut2, has 0 and 1 in the first and second rows and an arrow pointing from its third row back to the second row of fut1, representing a pointer that is referencing the old location in memory of the future before it was moved." src="img/trpl17-05.svg" class="center" />
|
|
|
<figcaption>图 17-5:移动自引用数据类型后产生的不安全结果</figcaption>
|
|
|
</figure>
|
|
|
<p>理论上,Rust 编译器也可以尝试在对象被移动时更新所有引用,但这样很可能带来大量性能开销,尤其在需要更新的是一整张引用网络的时候。如果我们反过来,确保这个数据结构<em>根本不在内存中移动</em>,那就完全不需要更新任何引用。这正是 Rust 借用检查器要做的事:在安全代码里,它会阻止你移动任何仍然存在活动引用的值。</p>
|
|
|
<p><code>Pin</code> 正是在这个基础上,进一步提供了我们需要的精确保证。当我们把一个指向某值的指针包进 <code>Pin</code> 里,也就是对这个值进行 <em>pin</em> 之后,它就不能再被移动了。因此,如果你有的是 <code>Pin<Box<SomeType>></code>,那么真正被 pin 住的是 <code>SomeType</code> 这个值,而<em>不是</em> <code>Box</code> 指针本身。图 17-6 展示了这个过程。</p>
|
|
|
<figure>
|
|
|
<img alt="Three boxes laid out side by side. The first is labeled “Pin”, the second “b1”, and the third “pinned”. Within “pinned” is a table labeled “fut”, with a single column; it represents a future with cells for each part of the data structure. Its first cell has the value “0”, its second cell has an arrow coming out of it and pointing to the fourth and final cell, which has the value “1” in it, and the third cell has dashed lines and an ellipsis to indicate there may be other parts to the data structure. All together, the “fut” table represents a future which is self-referential. An arrow leaves the box labeled “Pin”, goes through the box labeled “b1” and terminates inside the “pinned” box at the “fut” table." src="img/trpl17-06.svg" class="center" />
|
|
|
<figcaption>图 17-6:把一个指向自引用 future 类型的 `Box` pin 住</figcaption>
|
|
|
</figure>
|
|
|
<p>实际上,<code>Box</code> 指针本身仍然可以自由移动。请记住:我们真正关心的是最终被引用的数据必须固定不动。如果指针移动了,<em>但它指向的数据</em>仍然留在原地,就像图 17-7 那样,那么就不会产生问题。(你可以把这当作一个独立练习:去查阅相关类型以及 <code>std::pin</code> 模块的文档,试着想清楚如果是 <code>Pin</code> 包着 <code>Box</code>,到底如何做到这一点。)关键在于:那个自引用的类型本身不能移动,因为它仍然是被 pin 住的。</p>
|
|
|
<figure>
|
|
|
<img alt="Four boxes laid out in three rough columns, identical to the previous diagram with a change to the second column. Now there are two boxes in the second column, labeled “b1” and “b2”, “b1” is grayed out, and the arrow from “Pin” goes through “b2” instead of “b1”, indicating that the pointer has moved from “b1” to “b2”, but the data in “pinned” has not moved." src="img/trpl17-07.svg" class="center" />
|
|
|
<figcaption>图 17-7:移动一个指向自引用 future 类型的 `Box`</figcaption>
|
|
|
</figure>
|
|
|
<p>不过,大多数类型即使碰巧放在 <code>Pin</code> 指针后面,也完全可以安全移动。只有当某个值内部真的包含引用时,我们才需要关心 pin。比如数字和布尔值这类基本类型显然没有内部引用,所以当然是安全的。你平时在 Rust 里处理的大多数类型也都是这样。比如一个 <code>Vec</code> 就可以自由移动而不用担心。考虑到目前为止我们看到的内容,如果你有一个 <code>Pin<Vec<String>></code>,那理论上你必须通过 <code>Pin</code> 提供的那套安全但受限的 API 来操作它,哪怕 <code>Vec<String></code> 在没有其他引用存在时始终都是可以安全移动的。因此,我们需要一种机制来告诉编译器:像这种情况,移动它完全没问题。这正是 <code>Unpin</code> 的用途。</p>
|
|
|
<p><code>Unpin</code> 是一个标记 trait(marker trait),就像我们在第十六章见过的 <code>Send</code> 和 <code>Sync</code> 一样,它本身没有任何功能。marker trait 的存在,只是为了告诉编译器:实现了该 trait 的类型,在某种特定上下文里可以被安全使用。<code>Unpin</code> 告诉编译器,某个类型<em>不需要</em>维护“这个值是否可以安全移动”方面的额外保证。</p>
|
|
|
<p>就像 <code>Send</code> 和 <code>Sync</code> 一样,只要编译器能证明某个类型这样做是安全的,它就会自动为其实现 <code>Unpin</code>。同样也存在一个特殊情况:某个类型<em>不会</em>实现 <code>Unpin</code>。这种写法是 <code>impl !Unpin for <em>SomeType</em></code>,其中 <code><em>SomeType</em></code> 表示的是:为了在被 <code>Pin</code> 指针引用时保持安全,该类型必须保证自身不会被移动。</p>
|
|
|
<p>换句话说,关于 <code>Pin</code> 和 <code>Unpin</code> 的关系,有两件事要记住。第一,<code>Unpin</code> 才是“正常情况”,<code>!Unpin</code> 才是特殊情况。第二,一个类型到底实现的是 <code>Unpin</code> 还是 <code>!Unpin</code>,<em>只有在</em>你使用像 <code>Pin<&mut <em>SomeType</em>></code> 这样指向该类型的 pin 过的指针时,才真正有意义。</p>
|
|
|
<p>为了更具体一点,想想 <code>String</code>。它内部保存的是长度以及组成它的 Unicode 字符。我们完全可以把一个 <code>String</code> 包进 <code>Pin</code>,如图 17-8 所示。不过,<code>String</code> 会自动实现 <code>Unpin</code>,Rust 中绝大多数其他类型也一样。</p>
|
|
|
<figure>
|
|
|
<img alt="A box labeled “Pin” on the left with an arrow going from it to a box labeled “String” on the right. The “String” box contains the data 5usize, representing the length of the string, and the letters “h”, “e”, “l”, “l”, and “o” representing the characters of the string “hello” stored in this String instance. A dotted rectangle surrounds the “String” box and its label, but not the “Pin” box." src="img/trpl17-08.svg" class="center" />
|
|
|
<figcaption>图 17-8:把一个 `String` pin 起来;虚线表示 `String` 实现了 `Unpin` trait,因此它实际上并没有被固定住</figcaption>
|
|
|
</figure>
|
|
|
<p>结果就是,我们可以做一些如果 <code>String</code> 实现的是 <code>!Unpin</code> 就会非法的事情,比如像图 17-9 那样,在同一块内存位置上把一个字符串直接替换成另一个完全不同的字符串。这并没有违反 <code>Pin</code> 的约定,因为 <code>String</code> 内部没有那种会让它在移动时变得不安全的自引用。也正因为如此,它实现的是 <code>Unpin</code>,而不是 <code>!Unpin</code>。</p>
|
|
|
<figure>
|
|
|
<img alt="The same “hello” string data from the previous example, now labeled “s1” and grayed out. The “Pin” box from the previous example now points to a different String instance, one that is labeled “s2”, is valid, has a length of 7usize, and contains the characters of the string “goodbye”. s2 is surrounded by a dotted rectangle because it, too, implements the Unpin trait." src="img/trpl17-09.svg" class="center" />
|
|
|
<figcaption>图 17-9:在内存中用一个完全不同的 `String` 替换原来的 `String`</figcaption>
|
|
|
</figure>
|
|
|
<p>到这里,我们已经知道得足够多,可以理解前面示例 17-23 中那个 <code>join_all</code> 调用为什么会报错了。我们最初试图把 async 代码块生成的 future 移动进 <code>Vec<Box<dyn Future<Output = ()>>></code> 中,但正如我们刚刚看到的,那些 future 可能带有内部引用,因此它们不会自动实现 <code>Unpin</code>。一旦把它们 pin 住,我们就可以放心地把得到的 <code>Pin</code> 类型放进 <code>Vec</code>,因为此时这些 future 底层的数据就<em>不会</em>再被移动。示例 17-24 展示了修复这段代码的方法:在定义每个 future 的地方调用 <code>pin!</code> 宏,并相应调整 trait object 的类型。</p>
|
|
|
<figure class="listing">
|
|
|
<p><span class="file-name">文件名:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024"><span class="boring">extern crate trpl; // required for mdbook test
|
|
|
</span><span class="boring">
|
|
|
</span>use std::pin::{Pin, pin};
|
|
|
|
|
|
// --snip--
|
|
|
|
|
|
<span class="boring">use std::time::Duration;
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span><span class="boring"> trpl::block_on(async {
|
|
|
</span><span class="boring"> let (tx, mut rx) = trpl::channel();
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring"> let tx1 = tx.clone();
|
|
|
</span> let tx1_fut = pin!(async move {
|
|
|
// --snip--
|
|
|
<span class="boring"> let vals = vec![
|
|
|
</span><span class="boring"> String::from("hi"),
|
|
|
</span><span class="boring"> String::from("from"),
|
|
|
</span><span class="boring"> String::from("the"),
|
|
|
</span><span class="boring"> String::from("future"),
|
|
|
</span><span class="boring"> ];
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring"> for val in vals {
|
|
|
</span><span class="boring"> tx1.send(val).unwrap();
|
|
|
</span><span class="boring"> trpl::sleep(Duration::from_secs(1)).await;
|
|
|
</span><span class="boring"> }
|
|
|
</span> });
|
|
|
|
|
|
let rx_fut = pin!(async {
|
|
|
// --snip--
|
|
|
<span class="boring"> while let Some(value) = rx.recv().await {
|
|
|
</span><span class="boring"> println!("received '{value}'");
|
|
|
</span><span class="boring"> }
|
|
|
</span> });
|
|
|
|
|
|
let tx_fut = pin!(async move {
|
|
|
// --snip--
|
|
|
<span class="boring"> let vals = vec![
|
|
|
</span><span class="boring"> String::from("more"),
|
|
|
</span><span class="boring"> String::from("messages"),
|
|
|
</span><span class="boring"> String::from("for"),
|
|
|
</span><span class="boring"> String::from("you"),
|
|
|
</span><span class="boring"> ];
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring"> for val in vals {
|
|
|
</span><span class="boring"> tx.send(val).unwrap();
|
|
|
</span><span class="boring"> trpl::sleep(Duration::from_secs(1)).await;
|
|
|
</span><span class="boring"> }
|
|
|
</span> });
|
|
|
|
|
|
let futures: Vec<Pin<&mut dyn Future<Output = ()>>> =
|
|
|
vec![tx1_fut, rx_fut, tx_fut];
|
|
|
<span class="boring">
|
|
|
</span><span class="boring"> trpl::join_all(futures).await;
|
|
|
</span><span class="boring"> });
|
|
|
</span><span class="boring">}</span></code></pre>
|
|
|
<figcaption>示例 17-24:将 future pin 住,以便把它们移动进向量中</figcaption>
|
|
|
</figure>
|
|
|
<p>这段代码现在已经可以编译和运行了,而且我们还能在运行时动态地从向量里增加或删除 future,再把它们全部 join 在一起。</p>
|
|
|
<h3 id="stream-trait"><a class="header" href="#stream-trait"><code>Stream</code> trait</a></h3>
|
|
|
<p>现在你已经对 <code>Future</code>、<code>Pin</code> 和 <code>Unpin</code> 有了更深入的理解,我们可以把注意力转向 <code>Stream</code> trait 了。正如你在本章前面学到的,stream 很像异步迭代器。不过和 <code>Iterator</code> 以及 <code>Future</code> 不同的是,到本书写作时,标准库里还没有 <code>Stream</code> 的定义;但 <code>futures</code> crate 提供了一个在整个生态系统中被广泛采用的通用定义。</p>
|
|
|
<p>在看 <code>Stream</code> 如何把 <code>Iterator</code> 和 <code>Future</code> 的特征结合起来之前,我们先回顾一下这两个 trait 的定义。从 <code>Iterator</code> 我们得到了“序列”这个概念:它的 <code>next</code> 方法返回 <code>Option<Self::Item></code>。从 <code>Future</code> 我们得到了“值会随着时间变得就绪”这个概念:它的 <code>poll</code> 方法返回 <code>Poll<Self::Output></code>。为了表示“一串会随着时间逐渐就绪的项”,我们就可以定义这样一个 <code>Stream</code> trait,把两者的特征合并起来:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2024"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>use std::pin::Pin;
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
|
trait Stream {
|
|
|
type Item;
|
|
|
|
|
|
fn poll_next(
|
|
|
self: Pin<&mut Self>,
|
|
|
cx: &mut Context<'_>
|
|
|
) -> Poll<Option<Self::Item>>;
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p><code>Stream</code> trait 定义了一个名为 <code>Item</code> 的关联类型,用来表示 stream 产生的条目类型。这和 <code>Iterator</code> 很像,因为它可以有零个到多个条目;而和 <code>Future</code> 不同,后者始终只有一个 <code>Output</code>,哪怕这个输出只是 unit 类型 <code>()</code>。</p>
|
|
|
<p><code>Stream</code> 还定义了一个获取这些条目的方法。它叫 <code>poll_next</code>,这个名字清楚地表明:它既像 <code>Future::poll</code> 那样进行轮询,又像 <code>Iterator::next</code> 那样生成一个接一个的条目。它的返回类型把 <code>Poll</code> 和 <code>Option</code> 组合了起来。最外层是 <code>Poll</code>,因为和 future 一样,它需要先检查是否就绪;里面那层是 <code>Option</code>,因为和迭代器一样,它还得表示“后面是否还有更多条目”。</p>
|
|
|
<p>和这个定义非常相似的版本,将来很可能会进入 Rust 标准库。在此之前,它已经是大多数运行时工具箱的一部分,因此你完全可以依赖它,而我们接下来讲的内容通常也都会成立。</p>
|
|
|
<p>不过,在我们前面[“Stream:按顺序出现的 Future”][streams]<!-- ignore -->一节中见到的那些例子里,我们并没有直接用 <code>poll_next</code> 或 <code>Stream</code>,而是用了 <code>next</code> 和 <code>StreamExt</code>。当然,我们<em>可以</em>像直接操作 future 的 <code>poll</code> 方法那样,手写自己的 <code>Stream</code> 状态机,直接基于 <code>poll_next</code> 来工作。不过,用 <code>await</code> 显然舒服得多,而 <code>StreamExt</code> trait 则为此提供了 <code>next</code> 方法:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2024"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span><span class="boring">use std::pin::Pin;
|
|
|
</span><span class="boring">use std::task::{Context, Poll};
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring">trait Stream {
|
|
|
</span><span class="boring"> type Item;
|
|
|
</span><span class="boring"> fn poll_next(
|
|
|
</span><span class="boring"> self: Pin<&mut Self>,
|
|
|
</span><span class="boring"> cx: &mut Context<'_>,
|
|
|
</span><span class="boring"> ) -> Poll<Option<Self::Item>>;
|
|
|
</span><span class="boring">}
|
|
|
</span><span class="boring">
|
|
|
</span>trait StreamExt: Stream {
|
|
|
async fn next(&mut self) -> Option<Self::Item>
|
|
|
where
|
|
|
Self: Unpin;
|
|
|
|
|
|
// other methods...
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<blockquote>
|
|
|
<p>注意:我们在本章前面实际使用到的定义,看起来会和这个稍微有点不同,因为它需要兼容那些还不支持“在 trait 中使用 async 函数”的 Rust 版本。所以它实际上更像这样:</p>
|
|
|
<pre><code class="language-rust ignore">fn next(&mut self) -> Next<'_, Self> where Self: Unpin;</code></pre>
|
|
|
<p>这里的 <code>Next</code> 类型是一个实现了 <code>Future</code> 的 <code>struct</code>,它通过 <code>Next<'_, Self></code> 的形式,把对 <code>self</code> 的引用生命周期显式命名出来,这样 <code>await</code> 才能和这个方法一起工作。</p>
|
|
|
</blockquote>
|
|
|
<p><code>StreamExt</code> trait 还是所有那些“用于 stream 的有趣方法”的所在地。任何实现了 <code>Stream</code> 的类型,都会自动获得 <code>StreamExt</code> 的实现;不过这两个 trait 之所以分开定义,是为了让社区能够在不影响底层基础 trait 的前提下,不断迭代那些更方便的高层 API。</p>
|
|
|
<p>在 <code>trpl</code> crate 使用的这个 <code>StreamExt</code> 版本里,这个 trait 不仅定义了 <code>next</code> 方法,还给 <code>next</code> 提供了一个默认实现,这个实现会正确处理 <code>Stream::poll_next</code> 的各种细节。这意味着,即便你将来需要自己写一种流式数据类型,也<em>只需要</em>实现 <code>Stream</code>;然后,任何使用你这个数据类型的人,都会自动获得 <code>StreamExt</code> 及其方法。</p>
|
|
|
<p>关于这些 trait 的底层细节,我们就讲到这里。最后,让我们来想一想:future(包括 stream)、任务和线程到底是如何一起协作的。</p>
|
|
|
|
|
|
</main>
|
|
|
|
|
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
|
<!-- Mobile navigation buttons -->
|
|
|
<a rel="prev" href="ch17-04-streams.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 246.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z"/></svg></span>
|
|
|
</a>
|
|
|
|
|
|
<a rel="next prefetch" href="ch17-06-futures-tasks-threads.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z"/></svg></span>
|
|
|
</a>
|
|
|
|
|
|
<div style="clear: both"></div>
|
|
|
</nav>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
|
<a rel="prev" href="ch17-04-streams.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 246.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z"/></svg></span>
|
|
|
</a>
|
|
|
|
|
|
<a rel="next prefetch" href="ch17-06-futures-tasks-threads.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z"/></svg></span>
|
|
|
</a>
|
|
|
</nav>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<template id=fa-eye><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M288 32c-80.8 0-145.5 36.8-192.6 80.6C48.6 156 17.3 208 2.5 243.7c-3.3 7.9-3.3 16.7 0 24.6C17.3 304 48.6 356 95.4 399.4C142.5 443.2 207.2 480 288 480s145.5-36.8 192.6-80.6c46.8-43.5 78.1-95.4 93-131.1c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C433.5 68.8 368.8 32 288 32zM432 256c0 79.5-64.5 144-144 144s-144-64.5-144-144s64.5-144 144-144s144 64.5 144 144zM288 192c0 35.3-28.7 64-64 64c-11.5 0-22.3-3-31.6-8.4c-.2 2.8-.4 5.5-.4 8.4c0 53 43 96 96 96s96-43 96-96s-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6z"/></svg></span></template>
|
|
|
<template id=fa-eye-slash><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M38.8 5.1C28.4-3.1 13.3-1.2 5.1 9.2S-1.2 34.7 9.2 42.9l592 464c10.4 8.2 25.5 6.3 33.7-4.1s6.3-25.5-4.1-33.7L525.6 386.7c39.6-40.6 66.4-86.1 79.9-118.4c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C465.5 68.8 400.8 32 320 32c-68.2 0-125 26.3-169.3 60.8L38.8 5.1zM223.1 149.5C248.6 126.2 282.7 112 320 112c79.5 0 144 64.5 144 144c0 24.9-6.3 48.3-17.4 68.7L408 294.5c5.2-11.8 8-24.8 8-38.5c0-53-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6c0 10.2-2.4 19.8-6.6 28.3l-90.3-70.8zm223.1 298L373 389.9c-16.4 6.5-34.3 10.1-53 10.1c-79.5 0-144-64.5-144-144c0-6.9 .5-13.6 1.4-20.2L83.1 161.5C60.3 191.2 44 220.8 34.5 243.7c-3.3 7.9-3.3 16.7 0 24.6c14.9 35.7 46.2 87.7 93 131.1C174.5 443.2 239.2 480 320 480c47.8 0 89.9-12.9 126.2-32.5z"/></svg></span></template>
|
|
|
<template id=fa-copy><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M502.6 70.63l-61.25-61.25C435.4 3.371 427.2 0 418.7 0H255.1c-35.35 0-64 28.66-64 64l.0195 256C192 355.4 220.7 384 256 384h192c35.2 0 64-28.8 64-64V93.25C512 84.77 508.6 76.63 502.6 70.63zM464 320c0 8.836-7.164 16-16 16H255.1c-8.838 0-16-7.164-16-16L239.1 64.13c0-8.836 7.164-16 16-16h128L384 96c0 17.67 14.33 32 32 32h47.1V320zM272 448c0 8.836-7.164 16-16 16H63.1c-8.838 0-16-7.164-16-16L47.98 192.1c0-8.836 7.164-16 16-16H160V128H63.99c-35.35 0-64 28.65-64 64l.0098 256C.002 483.3 28.66 512 64 512h192c35.2 0 64-28.8 64-64v-32h-47.1L272 448z"/></svg></span></template>
|
|
|
<template id=fa-play><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80V432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"/></svg></span></template>
|
|
|
<template id=fa-clock-rotate-left><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M75 75L41 41C25.9 25.9 0 36.6 0 57.9V168c0 13.3 10.7 24 24 24H134.1c21.4 0 32.1-25.9 17-41l-30.8-30.8C155 85.5 203 64 256 64c106 0 192 86 192 192s-86 192-192 192c-40.8 0-78.6-12.7-109.7-34.4c-14.5-10.1-34.4-6.6-44.6 7.9s-6.6 34.4 7.9 44.6C151.2 495 201.7 512 256 512c141.4 0 256-114.6 256-256S397.4 0 256 0C185.3 0 121.3 28.7 75 75zm181 53c-13.3 0-24 10.7-24 24V256c0 6.4 2.5 12.5 7 17l72 72c9.4 9.4 24.6 9.4 33.9 0s9.4-24.6 0-33.9l-65-65V152c0-13.3-10.7-24-24-24z"/></svg></span></template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
window.playground_copyable = true;
|
|
|
</script>
|
|
|
|
|
|
|
|
|
<script src="elasticlunr-ef4e11c1.min.js"></script>
|
|
|
<script src="mark-09e88c2c.min.js"></script>
|
|
|
<script src="searcher-c2a407aa.js"></script>
|
|
|
|
|
|
<script src="clipboard-1626706a.min.js"></script>
|
|
|
<script src="highlight-abc7f01d.js"></script>
|
|
|
<script src="book-a0b12cfe.js"></script>
|
|
|
|
|
|
<!-- Custom JS scripts -->
|
|
|
<script src="ferris-2317480c.js"></script>
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
</body>
|
|
|
</html>
|