139 lines
20 KiB
HTML
139 lines
20 KiB
HTML
|
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `io` mod in crate `tokio`."><meta name="keywords" content="rust, rustlang, rust-lang, io"><title>tokio::io - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../../ayu.css" disabled ><script id="default-settings"></script><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="icon" type="image/svg+xml" href="../../favicon.svg">
|
|||
|
<link rel="alternate icon" type="image/png" href="../../favicon-16x16.png">
|
|||
|
<link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../tokio/index.html'><div class='logo-container rust-logo'><img src='../../rust-logo.png' alt='logo'></div></a><p class="location">Module io</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#reexports">Re-exports</a></li><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></div><p class="location"><a href="../index.html">tokio</a></p><script>window.sidebarCurrent = {name: "io", ty: "mod", relpath: "../"};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices" role="menu"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><span class="help-button">?</span>
|
|||
|
<a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class="fqn"><span class="out-of-band"><span id="render-detail"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span><a class="srclink" href="../../src/tokio/io/mod.rs.html#1-267" title="goto source code">[src]</a></span><span class="in-band">Module <a href="../index.html">tokio</a>::<wbr><a class="mod" href="">io</a></span></h1><div class="docblock"><p>Traits, helpers, and type definitions for asynchronous I/O functionality.</p>
|
|||
|
<p>This module is the asynchronous version of <code>std::io</code>. Primarily, it
|
|||
|
defines two traits, <a href="../../tokio/io/trait.AsyncRead.html"><code>AsyncRead</code></a> and <a href="../../tokio/io/trait.AsyncWrite.html"><code>AsyncWrite</code></a>, which are asynchronous
|
|||
|
versions of the <a href="https://doc.rust-lang.org/nightly/std/io/trait.Read.html"><code>Read</code></a> and <a href="https://doc.rust-lang.org/nightly/std/io/trait.Write.html"><code>Write</code></a> traits in the standard library.</p>
|
|||
|
<h1 id="asyncread-and-asyncwrite" class="section-header"><a href="#asyncread-and-asyncwrite">AsyncRead and AsyncWrite</a></h1>
|
|||
|
<p>Like the standard library's <a href="https://doc.rust-lang.org/nightly/std/io/trait.Read.html"><code>Read</code></a> and <a href="https://doc.rust-lang.org/nightly/std/io/trait.Write.html"><code>Write</code></a> traits, <a href="../../tokio/io/trait.AsyncRead.html"><code>AsyncRead</code></a> and
|
|||
|
<a href="../../tokio/io/trait.AsyncWrite.html"><code>AsyncWrite</code></a> provide the most general interface for reading and writing
|
|||
|
input and output. Unlike the standard library's traits, however, they are
|
|||
|
<em>asynchronous</em> — meaning that reading from or writing to a <code>tokio::io</code>
|
|||
|
type will <em>yield</em> to the Tokio scheduler when IO is not ready, rather than
|
|||
|
blocking. This allows other tasks to run while waiting on IO.</p>
|
|||
|
<p>Another difference is that <code>AsyncRead</code> and <code>AsyncWrite</code> only contain
|
|||
|
core methods needed to provide asynchronous reading and writing
|
|||
|
functionality. Instead, utility methods are defined in the <a href="trait@AsyncReadExt"><code>AsyncReadExt</code></a>
|
|||
|
and <a href="trait@AsyncWriteExt"><code>AsyncWriteExt</code></a> extension traits. These traits are automatically
|
|||
|
implemented for all values that implement <code>AsyncRead</code> and <code>AsyncWrite</code>
|
|||
|
respectively.</p>
|
|||
|
<p>End users will rarely interact directly with <code>AsyncRead</code> and
|
|||
|
<code>AsyncWrite</code>. Instead, they will use the async functions defined in the
|
|||
|
extension traits. Library authors are expected to implement <code>AsyncRead</code>
|
|||
|
and <code>AsyncWrite</code> in order to provide types that behave like byte streams.</p>
|
|||
|
<p>Even with these differences, Tokio's <code>AsyncRead</code> and <code>AsyncWrite</code> traits
|
|||
|
can be used in almost exactly the same manner as the standard library's
|
|||
|
<code>Read</code> and <code>Write</code>. Most types in the standard library that implement <code>Read</code>
|
|||
|
and <code>Write</code> have asynchronous equivalents in <code>tokio</code> that implement
|
|||
|
<code>AsyncRead</code> and <code>AsyncWrite</code>, such as <a href="crate::fs::File"><code>File</code></a> and <a href="../../tokio/net/struct.TcpStream.html"><code>TcpStream</code></a>.</p>
|
|||
|
<p>For example, the standard library documentation introduces <code>Read</code> by
|
|||
|
<a href="https://doc.rust-lang.org/nightly/std/io/index.html#read-and-write">demonstrating</a> reading some bytes from a <a href="https://doc.rust-lang.org/nightly/std/fs/struct.File.html"><code>std::fs::File</code></a>. We
|
|||
|
can do the same with <a href="crate::fs::File"><code>tokio::fs::File</code></a>:</p>
|
|||
|
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">io</span>::{<span class="self">self</span>, <span class="ident">AsyncReadExt</span>};
|
|||
|
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">fs</span>::<span class="ident">File</span>;
|
|||
|
|
|||
|
<span class="attribute">#[<span class="ident">tokio</span>::<span class="ident">main</span>]</span>
|
|||
|
<span class="ident">async</span> <span class="kw">fn</span> <span class="ident">main</span>() <span class="op">-</span><span class="op">></span> <span class="ident">io</span>::<span class="prelude-ty">Result</span><span class="op"><</span>()<span class="op">></span> {
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">f</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">open</span>(<span class="string">"foo.txt"</span>).<span class="ident">await</span><span class="question-mark">?</span>;
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> [<span class="number">0</span>; <span class="number">10</span>];
|
|||
|
|
|||
|
<span class="comment">// read up to 10 bytes</span>
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="ident">f</span>.<span class="ident">read</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">buffer</span>).<span class="ident">await</span><span class="question-mark">?</span>;
|
|||
|
|
|||
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"The bytes: {:?}"</span>, <span class="kw-2">&</span><span class="ident">buffer</span>[..<span class="ident">n</span>]);
|
|||
|
<span class="prelude-val">Ok</span>(())
|
|||
|
}</pre></div>
|
|||
|
<h2 id="buffered-readers-and-writers" class="section-header"><a href="#buffered-readers-and-writers">Buffered Readers and Writers</a></h2>
|
|||
|
<p>Byte-based interfaces are unwieldy and can be inefficient, as we'd need to be
|
|||
|
making near-constant calls to the operating system. To help with this,
|
|||
|
<code>std::io</code> comes with <a href="https://doc.rust-lang.org/nightly/std/io/index.html#bufreader-and-bufwriter">support for <em>buffered</em> readers and writers</a>,
|
|||
|
and therefore, <code>tokio::io</code> does as well.</p>
|
|||
|
<p>Tokio provides an async version of the <a href="https://doc.rust-lang.org/nightly/std/io/trait.BufRead.html"><code>std::io::BufRead</code></a> trait,
|
|||
|
<a href="../../tokio/io/trait.AsyncBufRead.html"><code>AsyncBufRead</code></a>; and async <a href="crate::io::BufReader"><code>BufReader</code></a> and <a href="crate::io::BufWriter"><code>BufWriter</code></a> structs, which
|
|||
|
wrap readers and writers. These wrappers use a buffer, reducing the number
|
|||
|
of calls and providing nicer methods for accessing exactly what you want.</p>
|
|||
|
<p>For example, <a href="crate::io::BufReader"><code>BufReader</code></a> works with the <a href="../../tokio/io/trait.AsyncBufRead.html"><code>AsyncBufRead</code></a> trait to add
|
|||
|
extra methods to any async reader:</p>
|
|||
|
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">io</span>::{<span class="self">self</span>, <span class="ident">BufReader</span>, <span class="ident">AsyncBufReadExt</span>};
|
|||
|
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">fs</span>::<span class="ident">File</span>;
|
|||
|
|
|||
|
<span class="attribute">#[<span class="ident">tokio</span>::<span class="ident">main</span>]</span>
|
|||
|
<span class="ident">async</span> <span class="kw">fn</span> <span class="ident">main</span>() <span class="op">-</span><span class="op">></span> <span class="ident">io</span>::<span class="prelude-ty">Result</span><span class="op"><</span>()<span class="op">></span> {
|
|||
|
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">open</span>(<span class="string">"foo.txt"</span>).<span class="ident">await</span><span class="question-mark">?</span>;
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">reader</span> <span class="op">=</span> <span class="ident">BufReader</span>::<span class="ident">new</span>(<span class="ident">f</span>);
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">new</span>();
|
|||
|
|
|||
|
<span class="comment">// read a line into buffer</span>
|
|||
|
<span class="ident">reader</span>.<span class="ident">read_line</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">buffer</span>).<span class="ident">await</span><span class="question-mark">?</span>;
|
|||
|
|
|||
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">buffer</span>);
|
|||
|
<span class="prelude-val">Ok</span>(())
|
|||
|
}</pre></div>
|
|||
|
<p><a href="crate::io::BufWriter"><code>BufWriter</code></a> doesn't add any new ways of writing; it just buffers every call
|
|||
|
to <a href="crate::io::AsyncWriteExt::write"><code>write</code></a>. However, you <strong>must</strong> flush
|
|||
|
<a href="crate::io::BufWriter"><code>BufWriter</code></a> to ensure that any buffered data is written.</p>
|
|||
|
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">io</span>::{<span class="self">self</span>, <span class="ident">BufWriter</span>, <span class="ident">AsyncWriteExt</span>};
|
|||
|
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">fs</span>::<span class="ident">File</span>;
|
|||
|
|
|||
|
<span class="attribute">#[<span class="ident">tokio</span>::<span class="ident">main</span>]</span>
|
|||
|
<span class="ident">async</span> <span class="kw">fn</span> <span class="ident">main</span>() <span class="op">-</span><span class="op">></span> <span class="ident">io</span>::<span class="prelude-ty">Result</span><span class="op"><</span>()<span class="op">></span> {
|
|||
|
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">create</span>(<span class="string">"foo.txt"</span>).<span class="ident">await</span><span class="question-mark">?</span>;
|
|||
|
{
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">writer</span> <span class="op">=</span> <span class="ident">BufWriter</span>::<span class="ident">new</span>(<span class="ident">f</span>);
|
|||
|
|
|||
|
<span class="comment">// Write a byte to the buffer.</span>
|
|||
|
<span class="ident">writer</span>.<span class="ident">write</span>(<span class="kw-2">&</span>[<span class="number">42u8</span>]).<span class="ident">await</span><span class="question-mark">?</span>;
|
|||
|
|
|||
|
<span class="comment">// Flush the buffer before it goes out of scope.</span>
|
|||
|
<span class="ident">writer</span>.<span class="ident">flush</span>().<span class="ident">await</span><span class="question-mark">?</span>;
|
|||
|
|
|||
|
} <span class="comment">// Unless flushed or shut down, the contents of the buffer is discarded on drop.</span>
|
|||
|
|
|||
|
<span class="prelude-val">Ok</span>(())
|
|||
|
}</pre></div>
|
|||
|
<h2 id="implementing-asyncread-and-asyncwrite" class="section-header"><a href="#implementing-asyncread-and-asyncwrite">Implementing AsyncRead and AsyncWrite</a></h2>
|
|||
|
<p>Because they are traits, we can implement <a href="../../tokio/io/trait.AsyncRead.html"><code>AsyncRead</code></a> and <a href="../../tokio/io/trait.AsyncWrite.html"><code>AsyncWrite</code></a> for
|
|||
|
our own types, as well. Note that these traits must only be implemented for
|
|||
|
non-blocking I/O types that integrate with the futures type system. In
|
|||
|
other words, these types must never block the thread, and instead the
|
|||
|
current task is notified when the I/O resource is ready.</p>
|
|||
|
<h2 id="conversion-to-and-from-sinkstream" class="section-header"><a href="#conversion-to-and-from-sinkstream">Conversion to and from Sink/Stream</a></h2>
|
|||
|
<p>It is often convenient to encapsulate the reading and writing of
|
|||
|
bytes and instead work with a <a href="https://docs.rs/futures/0.3/futures/sink/trait.Sink.html"><code>Sink</code></a> or <a href="https://docs.rs/futures/0.3/futures/stream/trait.Stream.html"><code>Stream</code></a> of some data
|
|||
|
type that is encoded as bytes and/or decoded from bytes. Tokio
|
|||
|
provides some utility traits in the <a href="https://docs.rs/tokio-util/0.6/tokio_util/codec/index.html">tokio-util</a> crate that
|
|||
|
abstract the asynchronous buffering that is required and allows
|
|||
|
you to write <a href="https://docs.rs/tokio-util/0.6/tokio_util/codec/trait.Encoder.html"><code>Encoder</code></a> and <a href="https://docs.rs/tokio-util/0.6/tokio_util/codec/trait.Decoder.html"><code>Decoder</code></a> functions working with a
|
|||
|
buffer of bytes, and then use that <a href="https://docs.rs/tokio-util/0.6/tokio_util/codec/index.html">"codec"</a> to transform anything
|
|||
|
that implements <a href="../../tokio/io/trait.AsyncRead.html"><code>AsyncRead</code></a> and <a href="../../tokio/io/trait.AsyncWrite.html"><code>AsyncWrite</code></a> into a <code>Sink</code>/<code>Stream</code> of
|
|||
|
your structured data.</p>
|
|||
|
<h1 id="standard-input-and-output" class="section-header"><a href="#standard-input-and-output">Standard input and output</a></h1>
|
|||
|
<p>Tokio provides asynchronous APIs to standard <a href="fn@stdin">input</a>, <a href="fn@stdout">output</a>, and <a href="fn@stderr">error</a>.
|
|||
|
These APIs are very similar to the ones provided by <code>std</code>, but they also
|
|||
|
implement <a href="../../tokio/io/trait.AsyncRead.html"><code>AsyncRead</code></a> and <a href="../../tokio/io/trait.AsyncWrite.html"><code>AsyncWrite</code></a>.</p>
|
|||
|
<p>Note that the standard input / output APIs <strong>must</strong> be used from the
|
|||
|
context of the Tokio runtime, as they require Tokio-specific features to
|
|||
|
function. Calling these functions outside of a Tokio runtime will panic.</p>
|
|||
|
<h1 id="std-re-exports" class="section-header"><a href="#std-re-exports"><code>std</code> re-exports</a></h1>
|
|||
|
<p>Additionally, <a href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html"><code>Error</code></a>, <a href="https://doc.rust-lang.org/nightly/std/io/error/enum.ErrorKind.html"><code>ErrorKind</code></a>, <a href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html"><code>Result</code></a>, and <a href="https://doc.rust-lang.org/nightly/std/io/enum.SeekFrom.html"><code>SeekFrom</code></a> are
|
|||
|
re-exported from <code>std::io</code> for ease of use.</p>
|
|||
|
</div><h2 id="reexports" class="section-header"><a href="#reexports">Re-exports</a></h2>
|
|||
|
<table><tr><td><code>pub use std::io::<a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>;</code></td></tr><tr><td><code>pub use std::io::<a class="enum" href="https://doc.rust-lang.org/nightly/std/io/error/enum.ErrorKind.html" title="enum std::io::error::ErrorKind">ErrorKind</a>;</code></td></tr><tr><td><code>pub use std::io::<a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a>;</code></td></tr><tr><td><code>pub use std::io::<a class="enum" href="https://doc.rust-lang.org/nightly/std/io/enum.SeekFrom.html" title="enum std::io::SeekFrom">SeekFrom</a>;</code></td></tr></table><h2 id="modules" class="section-header"><a href="#modules">Modules</a></h2>
|
|||
|
<table><tr class="module-item"><td><a class="mod" href="unix/index.html" title="tokio::io::unix mod">unix</a></td><td class="docblock-short"><p>Asynchronous IO structures specific to Unix-like operating systems.</p>
|
|||
|
</td></tr></table><h2 id="structs" class="section-header"><a href="#structs">Structs</a></h2>
|
|||
|
<table><tr class="module-item"><td><a class="struct" href="struct.Interest.html" title="tokio::io::Interest struct">Interest</a></td><td class="docblock-short"><p>Readiness event interest</p>
|
|||
|
</td></tr><tr class="module-item"><td><a class="struct" href="struct.ReadBuf.html" title="tokio::io::ReadBuf struct">ReadBuf</a></td><td class="docblock-short"><p>A wrapper around a byte buffer that is incrementally filled and initialized.</p>
|
|||
|
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Ready.html" title="tokio::io::Ready struct">Ready</a></td><td class="docblock-short"><p>Describes the readiness state of an I/O resources.</p>
|
|||
|
</td></tr></table><h2 id="traits" class="section-header"><a href="#traits">Traits</a></h2>
|
|||
|
<table><tr class="module-item"><td><a class="trait" href="trait.AsyncBufRead.html" title="tokio::io::AsyncBufRead trait">AsyncBufRead</a></td><td class="docblock-short"><p>Reads bytes asynchronously.</p>
|
|||
|
</td></tr><tr class="module-item"><td><a class="trait" href="trait.AsyncRead.html" title="tokio::io::AsyncRead trait">AsyncRead</a></td><td class="docblock-short"><p>Reads bytes from a source.</p>
|
|||
|
</td></tr><tr class="module-item"><td><a class="trait" href="trait.AsyncSeek.html" title="tokio::io::AsyncSeek trait">AsyncSeek</a></td><td class="docblock-short"><p>Seek bytes asynchronously.</p>
|
|||
|
</td></tr><tr class="module-item"><td><a class="trait" href="trait.AsyncWrite.html" title="tokio::io::AsyncWrite trait">AsyncWrite</a></td><td class="docblock-short"><p>Writes bytes asynchronously.</p>
|
|||
|
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../../";window.currentCrate = "tokio";</script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>
|