156 lines
34 KiB
HTML
156 lines
34 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="An extension trait which adds utility methods to `AsyncRead` types."><title>AsyncReadExt in futures_util::io - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="futures_util" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../../static.files/storage-e2aeef58.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-044be391.svg"></head><body class="rustdoc trait"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">AsyncReadExt</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../futures_util/index.html">futures_<wbr>util</a><span class="version">0.3.32</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Async<wbr>Read<wbr>Ext</a></h2><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.chain" title="chain">chain</a></li><li><a href="#method.read" title="read">read</a></li><li><a href="#method.read_exact" title="read_exact">read_exact</a></li><li><a href="#method.read_to_end" title="read_to_end">read_to_end</a></li><li><a href="#method.read_to_string" title="read_to_string">read_to_string</a></li><li><a href="#method.read_vectored" title="read_vectored">read_vectored</a></li><li><a href="#method.split" title="split">split</a></li><li><a href="#method.take" title="take">take</a></li></ul><h3><a href="#dyn-compatibility">Dyn Compatibility</a></h3><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2><a href="index.html">In futures_<wbr>util::<wbr>io</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="../index.html">futures_util</a>::<wbr><a href="index.html">io</a></div><h1>Trait <span class="trait">Async<wbr>Read<wbr>Ext</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../../src/futures_util/io/mod.rs.html#140-388">Source</a> </span></div><pre class="rust item-decl"><code>pub trait AsyncReadExt: <a class="trait" href="trait.AsyncRead.html" title="trait futures_util::io::AsyncRead">AsyncRead</a> {
|
|
// Provided methods
|
|
fn <a href="#method.chain" class="fn">chain</a><R>(self, next: R) -> <a class="struct" href="struct.Chain.html" title="struct futures_util::io::Chain">Chain</a><Self, R>
|
|
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
|
|
R: <a class="trait" href="trait.AsyncRead.html" title="trait futures_util::io::AsyncRead">AsyncRead</a></span> { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.read" class="fn">read</a><'a>(&'a mut self, buf: &'a mut [<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u8.html">u8</a>]) -> <a class="struct" href="struct.Read.html" title="struct futures_util::io::Read">Read</a><'a, Self> <a href="#" class="tooltip" data-notable-ty="Read<'a, Self>">ⓘ</a>
|
|
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a></span> { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.read_vectored" class="fn">read_vectored</a><'a>(
|
|
&'a mut self,
|
|
bufs: &'a mut [<a class="struct" href="https://doc.rust-lang.org/1.93.1/std/io/struct.IoSliceMut.html" title="struct std::io::IoSliceMut">IoSliceMut</a><'a>],
|
|
) -> <a class="struct" href="struct.ReadVectored.html" title="struct futures_util::io::ReadVectored">ReadVectored</a><'a, Self> <a href="#" class="tooltip" data-notable-ty="ReadVectored<'a, Self>">ⓘ</a>
|
|
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a></span> { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.read_exact" class="fn">read_exact</a><'a>(&'a mut self, buf: &'a mut [<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u8.html">u8</a>]) -> <a class="struct" href="struct.ReadExact.html" title="struct futures_util::io::ReadExact">ReadExact</a><'a, Self> <a href="#" class="tooltip" data-notable-ty="ReadExact<'a, Self>">ⓘ</a>
|
|
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a></span> { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.read_to_end" class="fn">read_to_end</a><'a>(
|
|
&'a mut self,
|
|
buf: &'a mut <a class="struct" href="https://doc.rust-lang.org/1.93.1/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u8.html">u8</a>>,
|
|
) -> <a class="struct" href="struct.ReadToEnd.html" title="struct futures_util::io::ReadToEnd">ReadToEnd</a><'a, Self> <a href="#" class="tooltip" data-notable-ty="ReadToEnd<'a, Self>">ⓘ</a>
|
|
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a></span> { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.read_to_string" class="fn">read_to_string</a><'a>(
|
|
&'a mut self,
|
|
buf: &'a mut <a class="struct" href="https://doc.rust-lang.org/1.93.1/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>,
|
|
) -> <a class="struct" href="struct.ReadToString.html" title="struct futures_util::io::ReadToString">ReadToString</a><'a, Self> <a href="#" class="tooltip" data-notable-ty="ReadToString<'a, Self>">ⓘ</a>
|
|
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a></span> { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.split" class="fn">split</a>(self) -> (<a class="struct" href="struct.ReadHalf.html" title="struct futures_util::io::ReadHalf">ReadHalf</a><Self>, <a class="struct" href="struct.WriteHalf.html" title="struct futures_util::io::WriteHalf">WriteHalf</a><Self>)
|
|
<span class="where">where Self: <a class="trait" href="trait.AsyncWrite.html" title="trait futures_util::io::AsyncWrite">AsyncWrite</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.take" class="fn">take</a>(self, limit: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u64.html">u64</a>) -> <a class="struct" href="struct.Take.html" title="struct futures_util::io::Take">Take</a><Self>
|
|
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
|
|
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>An extension trait which adds utility methods to <code>AsyncRead</code> types.</p>
|
|
</div></details><h2 id="provided-methods" class="section-header">Provided Methods<a href="#provided-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="method.chain" class="method"><a class="src rightside" href="../../src/futures_util/io/mod.rs.html#164-170">Source</a><h4 class="code-header">fn <a href="#method.chain" class="fn">chain</a><R>(self, next: R) -> <a class="struct" href="struct.Chain.html" title="struct futures_util::io::Chain">Chain</a><Self, R><div class="where">where
|
|
Self: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
|
|
R: <a class="trait" href="trait.AsyncRead.html" title="trait futures_util::io::AsyncRead">AsyncRead</a>,</div></h4></section></summary><div class="docblock"><p>Creates an adaptor which will chain this stream with another.</p>
|
|
<p>The returned <code>AsyncRead</code> instance will first read all bytes from this object
|
|
until EOF is encountered. Afterwards the output is equivalent to the
|
|
output of <code>next</code>.</p>
|
|
<h5 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h5>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::io::{AsyncReadExt, Cursor};
|
|
|
|
<span class="kw">let </span>reader1 = Cursor::new([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);
|
|
<span class="kw">let </span>reader2 = Cursor::new([<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]);
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>reader = reader1.chain(reader2);
|
|
<span class="kw">let </span><span class="kw-2">mut </span>buffer = Vec::new();
|
|
|
|
<span class="comment">// read the value into a Vec.
|
|
</span>reader.read_to_end(<span class="kw-2">&mut </span>buffer).<span class="kw">await</span><span class="question-mark">?</span>;
|
|
<span class="macro">assert_eq!</span>(buffer, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]);</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.read" class="method"><a class="src rightside" href="../../src/futures_util/io/mod.rs.html#196-201">Source</a><h4 class="code-header">fn <a href="#method.read" class="fn">read</a><'a>(&'a mut self, buf: &'a mut [<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u8.html">u8</a>]) -> <a class="struct" href="struct.Read.html" title="struct futures_util::io::Read">Read</a><'a, Self> <a href="#" class="tooltip" data-notable-ty="Read<'a, Self>">ⓘ</a><div class="where">where
|
|
Self: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a>,</div></h4></section></summary><div class="docblock"><p>Tries to read some bytes directly into the given <code>buf</code> in asynchronous
|
|
manner, returning a future type.</p>
|
|
<p>The returned future will resolve to the number of bytes read once the read
|
|
operation is completed.</p>
|
|
<h5 id="examples-1"><a class="doc-anchor" href="#examples-1">§</a>Examples</h5>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::io::{AsyncReadExt, Cursor};
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>reader = Cursor::new([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);
|
|
<span class="kw">let </span><span class="kw-2">mut </span>output = [<span class="number">0u8</span>; <span class="number">5</span>];
|
|
|
|
<span class="kw">let </span>bytes = reader.read(<span class="kw-2">&mut </span>output[..]).<span class="kw">await</span><span class="question-mark">?</span>;
|
|
|
|
<span class="comment">// This is only guaranteed to be 4 because `&[u8]` is a synchronous
|
|
// reader. In a real system you could get anywhere from 1 to
|
|
// `output.len()` bytes in a single read.
|
|
</span><span class="macro">assert_eq!</span>(bytes, <span class="number">4</span>);
|
|
<span class="macro">assert_eq!</span>(output, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">0</span>]);</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.read_vectored" class="method"><a class="src rightside" href="../../src/futures_util/io/mod.rs.html#208-213">Source</a><h4 class="code-header">fn <a href="#method.read_vectored" class="fn">read_vectored</a><'a>(
|
|
&'a mut self,
|
|
bufs: &'a mut [<a class="struct" href="https://doc.rust-lang.org/1.93.1/std/io/struct.IoSliceMut.html" title="struct std::io::IoSliceMut">IoSliceMut</a><'a>],
|
|
) -> <a class="struct" href="struct.ReadVectored.html" title="struct futures_util::io::ReadVectored">ReadVectored</a><'a, Self> <a href="#" class="tooltip" data-notable-ty="ReadVectored<'a, Self>">ⓘ</a><div class="where">where
|
|
Self: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a>,</div></h4></section></summary><div class="docblock"><p>Creates a future which will read from the <code>AsyncRead</code> into <code>bufs</code> using vectored
|
|
IO operations.</p>
|
|
<p>The returned future will resolve to the number of bytes read once the read
|
|
operation is completed.</p>
|
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.read_exact" class="method"><a class="src rightside" href="../../src/futures_util/io/mod.rs.html#252-257">Source</a><h4 class="code-header">fn <a href="#method.read_exact" class="fn">read_exact</a><'a>(&'a mut self, buf: &'a mut [<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u8.html">u8</a>]) -> <a class="struct" href="struct.ReadExact.html" title="struct futures_util::io::ReadExact">ReadExact</a><'a, Self> <a href="#" class="tooltip" data-notable-ty="ReadExact<'a, Self>">ⓘ</a><div class="where">where
|
|
Self: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a>,</div></h4></section></summary><div class="docblock"><p>Creates a future which will read exactly enough bytes to fill <code>buf</code>,
|
|
returning an error if end of file (EOF) is hit sooner.</p>
|
|
<p>The returned future will resolve once the read operation is completed.</p>
|
|
<p>In the case of an error the buffer and the object will be discarded, with
|
|
the error yielded.</p>
|
|
<h5 id="examples-2"><a class="doc-anchor" href="#examples-2">§</a>Examples</h5>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::io::{AsyncReadExt, Cursor};
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>reader = Cursor::new([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);
|
|
<span class="kw">let </span><span class="kw-2">mut </span>output = [<span class="number">0u8</span>; <span class="number">4</span>];
|
|
|
|
reader.read_exact(<span class="kw-2">&mut </span>output).<span class="kw">await</span><span class="question-mark">?</span>;
|
|
|
|
<span class="macro">assert_eq!</span>(output, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);</code></pre></div><h6 id="eof-is-hit-before-buf-is-filled"><a class="doc-anchor" href="#eof-is-hit-before-buf-is-filled">§</a>EOF is hit before <code>buf</code> is filled</h6>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::io::{<span class="self">self</span>, AsyncReadExt, Cursor};
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>reader = Cursor::new([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);
|
|
<span class="kw">let </span><span class="kw-2">mut </span>output = [<span class="number">0u8</span>; <span class="number">5</span>];
|
|
|
|
<span class="kw">let </span>result = reader.read_exact(<span class="kw-2">&mut </span>output).<span class="kw">await</span>;
|
|
|
|
<span class="macro">assert_eq!</span>(result.unwrap_err().kind(), io::ErrorKind::UnexpectedEof);</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.read_to_end" class="method"><a class="src rightside" href="../../src/futures_util/io/mod.rs.html#278-283">Source</a><h4 class="code-header">fn <a href="#method.read_to_end" class="fn">read_to_end</a><'a>(&'a mut self, buf: &'a mut <a class="struct" href="https://doc.rust-lang.org/1.93.1/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u8.html">u8</a>>) -> <a class="struct" href="struct.ReadToEnd.html" title="struct futures_util::io::ReadToEnd">ReadToEnd</a><'a, Self> <a href="#" class="tooltip" data-notable-ty="ReadToEnd<'a, Self>">ⓘ</a><div class="where">where
|
|
Self: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a>,</div></h4></section></summary><div class="docblock"><p>Creates a future which will read all the bytes from this <code>AsyncRead</code>.</p>
|
|
<p>On success the total number of bytes read is returned.</p>
|
|
<h5 id="examples-3"><a class="doc-anchor" href="#examples-3">§</a>Examples</h5>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::io::{AsyncReadExt, Cursor};
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>reader = Cursor::new([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);
|
|
<span class="kw">let </span><span class="kw-2">mut </span>output = Vec::with_capacity(<span class="number">4</span>);
|
|
|
|
<span class="kw">let </span>bytes = reader.read_to_end(<span class="kw-2">&mut </span>output).<span class="kw">await</span><span class="question-mark">?</span>;
|
|
|
|
<span class="macro">assert_eq!</span>(bytes, <span class="number">4</span>);
|
|
<span class="macro">assert_eq!</span>(output, <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.read_to_string" class="method"><a class="src rightside" href="../../src/futures_util/io/mod.rs.html#304-309">Source</a><h4 class="code-header">fn <a href="#method.read_to_string" class="fn">read_to_string</a><'a>(
|
|
&'a mut self,
|
|
buf: &'a mut <a class="struct" href="https://doc.rust-lang.org/1.93.1/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>,
|
|
) -> <a class="struct" href="struct.ReadToString.html" title="struct futures_util::io::ReadToString">ReadToString</a><'a, Self> <a href="#" class="tooltip" data-notable-ty="ReadToString<'a, Self>">ⓘ</a><div class="where">where
|
|
Self: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a>,</div></h4></section></summary><div class="docblock"><p>Creates a future which will read all the bytes from this <code>AsyncRead</code>.</p>
|
|
<p>On success the total number of bytes read is returned.</p>
|
|
<h5 id="examples-4"><a class="doc-anchor" href="#examples-4">§</a>Examples</h5>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::io::{AsyncReadExt, Cursor};
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>reader = Cursor::new(<span class="kw-2">&</span><span class="string">b"1234"</span>[..]);
|
|
<span class="kw">let </span><span class="kw-2">mut </span>buffer = String::with_capacity(<span class="number">4</span>);
|
|
|
|
<span class="kw">let </span>bytes = reader.read_to_string(<span class="kw-2">&mut </span>buffer).<span class="kw">await</span><span class="question-mark">?</span>;
|
|
|
|
<span class="macro">assert_eq!</span>(bytes, <span class="number">4</span>);
|
|
<span class="macro">assert_eq!</span>(buffer, String::from(<span class="string">"1234"</span>));</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.split" class="method"><a class="src rightside" href="../../src/futures_util/io/mod.rs.html#340-346">Source</a><h4 class="code-header">fn <a href="#method.split" class="fn">split</a>(self) -> (<a class="struct" href="struct.ReadHalf.html" title="struct futures_util::io::ReadHalf">ReadHalf</a><Self>, <a class="struct" href="struct.WriteHalf.html" title="struct futures_util::io::WriteHalf">WriteHalf</a><Self>)<div class="where">where
|
|
Self: <a class="trait" href="trait.AsyncWrite.html" title="trait futures_util::io::AsyncWrite">AsyncWrite</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h4></section></summary><div class="docblock"><p>Helper method for splitting this read/write object into two halves.</p>
|
|
<p>The two halves returned implement the <code>AsyncRead</code> and <code>AsyncWrite</code>
|
|
traits, respectively.</p>
|
|
<h5 id="examples-5"><a class="doc-anchor" href="#examples-5">§</a>Examples</h5>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::io::{<span class="self">self</span>, AsyncReadExt, Cursor};
|
|
|
|
<span class="comment">// Note that for `Cursor` the read and write halves share a single
|
|
// seek position. This may or may not be true for other types that
|
|
// implement both `AsyncRead` and `AsyncWrite`.
|
|
|
|
</span><span class="kw">let </span>reader = Cursor::new([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);
|
|
<span class="kw">let </span><span class="kw-2">mut </span>buffer = Cursor::new(<span class="macro">vec!</span>[<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]);
|
|
<span class="kw">let </span><span class="kw-2">mut </span>writer = Cursor::new(<span class="macro">vec!</span>[<span class="number">0u8</span>; <span class="number">5</span>]);
|
|
|
|
{
|
|
<span class="kw">let </span>(buffer_reader, <span class="kw-2">mut </span>buffer_writer) = (<span class="kw-2">&mut </span>buffer).split();
|
|
io::copy(reader, <span class="kw-2">&mut </span>buffer_writer).<span class="kw">await</span><span class="question-mark">?</span>;
|
|
io::copy(buffer_reader, <span class="kw-2">&mut </span>writer).<span class="kw">await</span><span class="question-mark">?</span>;
|
|
}
|
|
|
|
<span class="macro">assert_eq!</span>(buffer.into_inner(), [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]);
|
|
<span class="macro">assert_eq!</span>(writer.into_inner(), [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">0</span>]);</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.take" class="method"><a class="src rightside" href="../../src/futures_util/io/mod.rs.html#367-372">Source</a><h4 class="code-header">fn <a href="#method.take" class="fn">take</a>(self, limit: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u64.html">u64</a>) -> <a class="struct" href="struct.Take.html" title="struct futures_util::io::Take">Take</a><Self><div class="where">where
|
|
Self: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h4></section></summary><div class="docblock"><p>Creates an AsyncRead adapter which will read at most <code>limit</code> bytes
|
|
from the underlying reader.</p>
|
|
<h5 id="examples-6"><a class="doc-anchor" href="#examples-6">§</a>Examples</h5>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::io::{AsyncReadExt, Cursor};
|
|
|
|
<span class="kw">let </span>reader = Cursor::new(<span class="kw-2">&</span><span class="string">b"12345678"</span>[..]);
|
|
<span class="kw">let </span><span class="kw-2">mut </span>buffer = [<span class="number">0</span>; <span class="number">5</span>];
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>take = reader.take(<span class="number">4</span>);
|
|
<span class="kw">let </span>n = take.read(<span class="kw-2">&mut </span>buffer).<span class="kw">await</span><span class="question-mark">?</span>;
|
|
|
|
<span class="macro">assert_eq!</span>(n, <span class="number">4</span>);
|
|
<span class="macro">assert_eq!</span>(<span class="kw-2">&</span>buffer, <span class="string">b"1234\0"</span>);</code></pre></div></div></details></div><h2 id="dyn-compatibility" class="section-header">Dyn Compatibility<a href="#dyn-compatibility" class="anchor">§</a></h2><div class="dyn-compatibility-info"><p>This trait is <b>not</b> <a href="https://doc.rust-lang.org/1.93.1/reference/items/traits.html#dyn-compatibility">dyn compatible</a>.</p><p><i>In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.</i></p></div><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><section id="impl-AsyncReadExt-for-R" class="impl"><a class="src rightside" href="../../src/futures_util/io/mod.rs.html#390">Source</a><a href="#impl-AsyncReadExt-for-R" class="anchor">§</a><h3 class="code-header">impl<R: <a class="trait" href="trait.AsyncRead.html" title="trait futures_util::io::AsyncRead">AsyncRead</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> <a class="trait" href="trait.AsyncReadExt.html" title="trait futures_util::io::AsyncReadExt">AsyncReadExt</a> for R</h3></section></div><script src="../../trait.impl/futures_util/io/trait.AsyncReadExt.js" async></script><script type="text/json" id="notable-traits-data">{"Read<'a, Self>":"<h3>Notable traits for <code><a class=\"struct\" href=\"struct.Read.html\" title=\"struct futures_util::io::Read\">Read</a><'_, R></code></h3><pre><code><div class=\"where\">impl<R: <a class=\"trait\" href=\"trait.AsyncRead.html\" title=\"trait futures_util::io::AsyncRead\">AsyncRead</a> + ?<a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html\" title=\"trait core::marker::Sized\">Sized</a> + <a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html\" title=\"trait core::marker::Unpin\">Unpin</a>> <a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html\" title=\"trait core::future::future::Future\">Future</a> for <a class=\"struct\" href=\"struct.Read.html\" title=\"struct futures_util::io::Read\">Read</a><'_, R></div><div class=\"where\"> type <a href=\"https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html#associatedtype.Output\" class=\"associatedtype\">Output</a> = <a class=\"type\" href=\"https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html\" title=\"type std::io::error::Result\">Result</a><<a class=\"primitive\" href=\"https://doc.rust-lang.org/1.93.1/std/primitive.usize.html\">usize</a>>;</div>","ReadExact<'a, Self>":"<h3>Notable traits for <code><a class=\"struct\" href=\"struct.ReadExact.html\" title=\"struct futures_util::io::ReadExact\">ReadExact</a><'_, R></code></h3><pre><code><div class=\"where\">impl<R: <a class=\"trait\" href=\"trait.AsyncRead.html\" title=\"trait futures_util::io::AsyncRead\">AsyncRead</a> + ?<a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html\" title=\"trait core::marker::Sized\">Sized</a> + <a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html\" title=\"trait core::marker::Unpin\">Unpin</a>> <a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html\" title=\"trait core::future::future::Future\">Future</a> for <a class=\"struct\" href=\"struct.ReadExact.html\" title=\"struct futures_util::io::ReadExact\">ReadExact</a><'_, R></div><div class=\"where\"> type <a href=\"https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html#associatedtype.Output\" class=\"associatedtype\">Output</a> = <a class=\"type\" href=\"https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html\" title=\"type std::io::error::Result\">Result</a><<a class=\"primitive\" href=\"https://doc.rust-lang.org/1.93.1/std/primitive.unit.html\">()</a>>;</div>","ReadToEnd<'a, Self>":"<h3>Notable traits for <code><a class=\"struct\" href=\"struct.ReadToEnd.html\" title=\"struct futures_util::io::ReadToEnd\">ReadToEnd</a><'_, A></code></h3><pre><code><div class=\"where\">impl<A> <a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html\" title=\"trait core::future::future::Future\">Future</a> for <a class=\"struct\" href=\"struct.ReadToEnd.html\" title=\"struct futures_util::io::ReadToEnd\">ReadToEnd</a><'_, A><div class=\"where\">where\n A: <a class=\"trait\" href=\"trait.AsyncRead.html\" title=\"trait futures_util::io::AsyncRead\">AsyncRead</a> + ?<a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html\" title=\"trait core::marker::Sized\">Sized</a> + <a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html\" title=\"trait core::marker::Unpin\">Unpin</a>,</div></div><div class=\"where\"> type <a href=\"https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html#associatedtype.Output\" class=\"associatedtype\">Output</a> = <a class=\"type\" href=\"https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html\" title=\"type std::io::error::Result\">Result</a><<a class=\"primitive\" href=\"https://doc.rust-lang.org/1.93.1/std/primitive.usize.html\">usize</a>>;</div>","ReadToString<'a, Self>":"<h3>Notable traits for <code><a class=\"struct\" href=\"struct.ReadToString.html\" title=\"struct futures_util::io::ReadToString\">ReadToString</a><'_, A></code></h3><pre><code><div class=\"where\">impl<A> <a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html\" title=\"trait core::future::future::Future\">Future</a> for <a class=\"struct\" href=\"struct.ReadToString.html\" title=\"struct futures_util::io::ReadToString\">ReadToString</a><'_, A><div class=\"where\">where\n A: <a class=\"trait\" href=\"trait.AsyncRead.html\" title=\"trait futures_util::io::AsyncRead\">AsyncRead</a> + ?<a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html\" title=\"trait core::marker::Sized\">Sized</a> + <a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html\" title=\"trait core::marker::Unpin\">Unpin</a>,</div></div><div class=\"where\"> type <a href=\"https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html#associatedtype.Output\" class=\"associatedtype\">Output</a> = <a class=\"type\" href=\"https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html\" title=\"type std::io::error::Result\">Result</a><<a class=\"primitive\" href=\"https://doc.rust-lang.org/1.93.1/std/primitive.usize.html\">usize</a>>;</div>","ReadVectored<'a, Self>":"<h3>Notable traits for <code><a class=\"struct\" href=\"struct.ReadVectored.html\" title=\"struct futures_util::io::ReadVectored\">ReadVectored</a><'_, R></code></h3><pre><code><div class=\"where\">impl<R: <a class=\"trait\" href=\"trait.AsyncRead.html\" title=\"trait futures_util::io::AsyncRead\">AsyncRead</a> + ?<a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html\" title=\"trait core::marker::Sized\">Sized</a> + <a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html\" title=\"trait core::marker::Unpin\">Unpin</a>> <a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html\" title=\"trait core::future::future::Future\">Future</a> for <a class=\"struct\" href=\"struct.ReadVectored.html\" title=\"struct futures_util::io::ReadVectored\">ReadVectored</a><'_, R></div><div class=\"where\"> type <a href=\"https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html#associatedtype.Output\" class=\"associatedtype\">Output</a> = <a class=\"type\" href=\"https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html\" title=\"type std::io::error::Result\">Result</a><<a class=\"primitive\" href=\"https://doc.rust-lang.org/1.93.1/std/primitive.usize.html\">usize</a>>;</div>"}</script></section></div></main></body></html> |