Files
google-apis-rs/futures/io/trait.AsyncReadExt.html
2024-03-05 21:06:01 +01:00

166 lines
35 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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::io - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../../static.files/rustdoc-ac92e1bbe349e143.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="futures" data-themes="" data-resource-suffix="" data-rustdoc-version="1.76.0 (07dca489a 2024-02-04)" data-channel="1.76.0" data-search-js="search-2b6ce74ff89ae146.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../../static.files/storage-f2adc0d6ca4d09fb.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-305769736d49e732.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-feafe1bb7466e4bd.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-2c020d218678b618.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]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../futures/index.html">futures</a><span class="version">0.3.28</span></h2></div><h2 class="location"><a href="#">AsyncReadExt</a></h2><div class="sidebar-elems"><section><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.chain">chain</a></li><li><a href="#method.read">read</a></li><li><a href="#method.read_exact">read_exact</a></li><li><a href="#method.read_to_end">read_to_end</a></li><li><a href="#method.read_to_string">read_to_string</a></li><li><a href="#method.read_vectored">read_vectored</a></li><li><a href="#method.split">split</a></li><li><a href="#method.take">take</a></li></ul><h3><a href="#object-safety">Object Safety</a></h3><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In futures::io</a></h2></div></nav><div class="sidebar-resizer"></div>
<main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><div id="sidebar-button" tabindex="-1"><a href="../../futures/all.html" title="show sidebar"></a></div><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"><div id="help-button" tabindex="-1"><a href="../../help.html" title="help">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>Trait <a href="../index.html">futures</a>::<wbr><a href="index.html">io</a>::<wbr><a class="trait" href="#">AsyncReadExt</a><button id="copy-path" title="Copy item path to clipboard"><img src="../../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="src" href="../../src/futures_util/io/mod.rs.html#148">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><pre class="rust item-decl"><code>pub trait AsyncReadExt: <a class="trait" href="trait.AsyncRead.html" title="trait futures::io::AsyncRead">AsyncRead</a> {
// Provided methods
fn <a href="#method.chain" class="fn">chain</a>&lt;R&gt;(self, next: R) -&gt; <a class="struct" href="struct.Chain.html" title="struct futures::io::Chain">Chain</a>&lt;Self, R&gt;
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.76.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
R: <a class="trait" href="trait.AsyncRead.html" title="trait futures::io::AsyncRead">AsyncRead</a></span> { ... }
<span class="item-spacer"></span> fn <a href="#method.read" class="fn">read</a>&lt;'a&gt;(&amp;'a mut self, buf: &amp;'a mut [<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.u8.html">u8</a>]) -&gt; <a class="struct" href="struct.Read.html" title="struct futures::io::Read">Read</a>&lt;'a, Self&gt; <a href="#" class="tooltip" data-notable-ty="Read&lt;&#39;a, Self&gt;"></a>
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.76.0/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>&lt;'a&gt;(
&amp;'a mut self,
bufs: &amp;'a mut [<a class="struct" href="struct.IoSliceMut.html" title="struct futures::io::IoSliceMut">IoSliceMut</a>&lt;'a&gt;]
) -&gt; <a class="struct" href="struct.ReadVectored.html" title="struct futures::io::ReadVectored">ReadVectored</a>&lt;'a, Self&gt; <a href="#" class="tooltip" data-notable-ty="ReadVectored&lt;&#39;a, Self&gt;"></a>
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.76.0/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>&lt;'a&gt;(&amp;'a mut self, buf: &amp;'a mut [<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.u8.html">u8</a>]) -&gt; <a class="struct" href="struct.ReadExact.html" title="struct futures::io::ReadExact">ReadExact</a>&lt;'a, Self&gt; <a href="#" class="tooltip" data-notable-ty="ReadExact&lt;&#39;a, Self&gt;"></a>
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.76.0/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>&lt;'a&gt;(
&amp;'a mut self,
buf: &amp;'a mut <a class="struct" href="https://doc.rust-lang.org/1.76.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.u8.html">u8</a>&gt;
) -&gt; <a class="struct" href="struct.ReadToEnd.html" title="struct futures::io::ReadToEnd">ReadToEnd</a>&lt;'a, Self&gt; <a href="#" class="tooltip" data-notable-ty="ReadToEnd&lt;&#39;a, Self&gt;"></a>
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.76.0/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>&lt;'a&gt;(
&amp;'a mut self,
buf: &amp;'a mut <a class="struct" href="https://doc.rust-lang.org/1.76.0/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>
) -&gt; <a class="struct" href="struct.ReadToString.html" title="struct futures::io::ReadToString">ReadToString</a>&lt;'a, Self&gt; <a href="#" class="tooltip" data-notable-ty="ReadToString&lt;&#39;a, Self&gt;"></a>
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.76.0/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) -&gt; (<a class="struct" href="struct.ReadHalf.html" title="struct futures::io::ReadHalf">ReadHalf</a>&lt;Self&gt;, <a class="struct" href="struct.WriteHalf.html" title="struct futures::io::WriteHalf">WriteHalf</a>&lt;Self&gt;)
<span class="where">where Self: <a class="trait" href="trait.AsyncWrite.html" title="trait futures::io::AsyncWrite">AsyncWrite</a> + <a class="trait" href="https://doc.rust-lang.org/1.76.0/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.76.0/std/primitive.u64.html">u64</a>) -&gt; <a class="struct" href="struct.Take.html" title="struct futures::io::Take">Take</a>&lt;Self&gt;
<span class="where">where Self: <a class="trait" href="https://doc.rust-lang.org/1.76.0/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#172-175">source</a><h4 class="code-header">fn <a href="#method.chain" class="fn">chain</a>&lt;R&gt;(self, next: R) -&gt; <a class="struct" href="struct.Chain.html" title="struct futures::io::Chain">Chain</a>&lt;Self, R&gt;<div class="where">where
Self: <a class="trait" href="https://doc.rust-lang.org/1.76.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
R: <a class="trait" href="trait.AsyncRead.html" title="trait futures::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 href="#examples">Examples</a></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">&amp;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#204-206">source</a><h4 class="code-header">fn <a href="#method.read" class="fn">read</a>&lt;'a&gt;(&amp;'a mut self, buf: &amp;'a mut [<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.u8.html">u8</a>]) -&gt; <a class="struct" href="struct.Read.html" title="struct futures::io::Read">Read</a>&lt;'a, Self&gt; <a href="#" class="tooltip" data-notable-ty="Read&lt;&#39;a, Self&gt;"></a><div class="where">where
Self: <a class="trait" href="https://doc.rust-lang.org/1.76.0/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 href="#examples-1">Examples</a></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">&amp;mut </span>output[..]).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="comment">// This is only guaranteed to be 4 because `&amp;[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#216-218">source</a><h4 class="code-header">fn <a href="#method.read_vectored" class="fn">read_vectored</a>&lt;'a&gt;(
&amp;'a mut self,
bufs: &amp;'a mut [<a class="struct" href="struct.IoSliceMut.html" title="struct futures::io::IoSliceMut">IoSliceMut</a>&lt;'a&gt;]
) -&gt; <a class="struct" href="struct.ReadVectored.html" title="struct futures::io::ReadVectored">ReadVectored</a>&lt;'a, Self&gt; <a href="#" class="tooltip" data-notable-ty="ReadVectored&lt;&#39;a, Self&gt;"></a><div class="where">where
Self: <a class="trait" href="https://doc.rust-lang.org/1.76.0/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#260-262">source</a><h4 class="code-header">fn <a href="#method.read_exact" class="fn">read_exact</a>&lt;'a&gt;(&amp;'a mut self, buf: &amp;'a mut [<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.u8.html">u8</a>]) -&gt; <a class="struct" href="struct.ReadExact.html" title="struct futures::io::ReadExact">ReadExact</a>&lt;'a, Self&gt; <a href="#" class="tooltip" data-notable-ty="ReadExact&lt;&#39;a, Self&gt;"></a><div class="where">where
Self: <a class="trait" href="https://doc.rust-lang.org/1.76.0/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 href="#examples-2">Examples</a></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">&amp;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 href="#eof-is-hit-before-buf-is-filled">EOF is hit before <code>buf</code> is filled</a></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">&amp;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#286-288">source</a><h4 class="code-header">fn <a href="#method.read_to_end" class="fn">read_to_end</a>&lt;'a&gt;(&amp;'a mut self, buf: &amp;'a mut <a class="struct" href="https://doc.rust-lang.org/1.76.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.u8.html">u8</a>&gt;) -&gt; <a class="struct" href="struct.ReadToEnd.html" title="struct futures::io::ReadToEnd">ReadToEnd</a>&lt;'a, Self&gt; <a href="#" class="tooltip" data-notable-ty="ReadToEnd&lt;&#39;a, Self&gt;"></a><div class="where">where
Self: <a class="trait" href="https://doc.rust-lang.org/1.76.0/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 href="#examples-3">Examples</a></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">&amp;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#312-314">source</a><h4 class="code-header">fn <a href="#method.read_to_string" class="fn">read_to_string</a>&lt;'a&gt;(
&amp;'a mut self,
buf: &amp;'a mut <a class="struct" href="https://doc.rust-lang.org/1.76.0/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>
) -&gt; <a class="struct" href="struct.ReadToString.html" title="struct futures::io::ReadToString">ReadToString</a>&lt;'a, Self&gt; <a href="#" class="tooltip" data-notable-ty="ReadToString&lt;&#39;a, Self&gt;"></a><div class="where">where
Self: <a class="trait" href="https://doc.rust-lang.org/1.76.0/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 href="#examples-4">Examples</a></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">&amp;</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">&amp;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#348-350">source</a><h4 class="code-header">fn <a href="#method.split" class="fn">split</a>(self) -&gt; (<a class="struct" href="struct.ReadHalf.html" title="struct futures::io::ReadHalf">ReadHalf</a>&lt;Self&gt;, <a class="struct" href="struct.WriteHalf.html" title="struct futures::io::WriteHalf">WriteHalf</a>&lt;Self&gt;)<div class="where">where
Self: <a class="trait" href="trait.AsyncWrite.html" title="trait futures::io::AsyncWrite">AsyncWrite</a> + <a class="trait" href="https://doc.rust-lang.org/1.76.0/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 href="#examples-5">Examples</a></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">&amp;mut </span>buffer).split();
io::copy(reader, <span class="kw-2">&amp;mut </span>buffer_writer).<span class="kw">await</span><span class="question-mark">?</span>;
io::copy(buffer_reader, <span class="kw-2">&amp;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#375-377">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.76.0/std/primitive.u64.html">u64</a>) -&gt; <a class="struct" href="struct.Take.html" title="struct futures::io::Take">Take</a>&lt;Self&gt;<div class="where">where
Self: <a class="trait" href="https://doc.rust-lang.org/1.76.0/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 href="#examples-6">Examples</a></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">&amp;</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">&amp;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">&amp;</span>buffer, <span class="string">b"1234\0"</span>);</code></pre></div>
</div></details></div><h2 id="object-safety" class="section-header">Object Safety<a href="#object-safety" class="anchor">§</a></h2><div class="object-safety-info">This trait is <b>not</b> <a href="https://doc.rust-lang.org/1.76.0/reference/items/traits.html#object-safety">object safe</a>.</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#398">source</a><a href="#impl-AsyncReadExt-for-R" class="anchor">§</a><h3 class="code-header">impl&lt;R&gt; <a class="trait" href="trait.AsyncReadExt.html" title="trait futures::io::AsyncReadExt">AsyncReadExt</a> for R<div class="where">where
R: <a class="trait" href="trait.AsyncRead.html" title="trait futures::io::AsyncRead">AsyncRead</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.76.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></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::io::Read\">Read</a>&lt;'_, R&gt;</code></h3><pre><code><div class=\"where\">impl&lt;R&gt; <a class=\"trait\" href=\"../future/trait.Future.html\" title=\"trait futures::future::Future\">Future</a> for <a class=\"struct\" href=\"struct.Read.html\" title=\"struct futures::io::Read\">Read</a>&lt;'_, R&gt;<div class=\"where\">where\n R: <a class=\"trait\" href=\"trait.AsyncRead.html\" title=\"trait futures::io::AsyncRead\">AsyncRead</a> + <a class=\"trait\" href=\"https://doc.rust-lang.org/1.76.0/core/marker/trait.Unpin.html\" title=\"trait core::marker::Unpin\">Unpin</a> + ?<a class=\"trait\" href=\"https://doc.rust-lang.org/1.76.0/core/marker/trait.Sized.html\" title=\"trait core::marker::Sized\">Sized</a>,</div></div><div class=\"where\"> type <a href=\"../future/trait.Future.html#associatedtype.Output\" class=\"associatedtype\">Output</a> = <a class=\"enum\" href=\"https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html\" title=\"enum core::result::Result\">Result</a>&lt;<a class=\"primitive\" href=\"https://doc.rust-lang.org/1.76.0/std/primitive.usize.html\">usize</a>, <a class=\"struct\" href=\"struct.Error.html\" title=\"struct futures::io::Error\">Error</a>&gt;;</div>","ReadExact<'a, Self>":"<h3>Notable traits for <code><a class=\"struct\" href=\"struct.ReadExact.html\" title=\"struct futures::io::ReadExact\">ReadExact</a>&lt;'_, R&gt;</code></h3><pre><code><div class=\"where\">impl&lt;R&gt; <a class=\"trait\" href=\"../future/trait.Future.html\" title=\"trait futures::future::Future\">Future</a> for <a class=\"struct\" href=\"struct.ReadExact.html\" title=\"struct futures::io::ReadExact\">ReadExact</a>&lt;'_, R&gt;<div class=\"where\">where\n R: <a class=\"trait\" href=\"trait.AsyncRead.html\" title=\"trait futures::io::AsyncRead\">AsyncRead</a> + <a class=\"trait\" href=\"https://doc.rust-lang.org/1.76.0/core/marker/trait.Unpin.html\" title=\"trait core::marker::Unpin\">Unpin</a> + ?<a class=\"trait\" href=\"https://doc.rust-lang.org/1.76.0/core/marker/trait.Sized.html\" title=\"trait core::marker::Sized\">Sized</a>,</div></div><div class=\"where\"> type <a href=\"../future/trait.Future.html#associatedtype.Output\" class=\"associatedtype\">Output</a> = <a class=\"enum\" href=\"https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html\" title=\"enum core::result::Result\">Result</a>&lt;<a class=\"primitive\" href=\"https://doc.rust-lang.org/1.76.0/std/primitive.unit.html\">()</a>, <a class=\"struct\" href=\"struct.Error.html\" title=\"struct futures::io::Error\">Error</a>&gt;;</div>","ReadToEnd<'a, Self>":"<h3>Notable traits for <code><a class=\"struct\" href=\"struct.ReadToEnd.html\" title=\"struct futures::io::ReadToEnd\">ReadToEnd</a>&lt;'_, A&gt;</code></h3><pre><code><div class=\"where\">impl&lt;A&gt; <a class=\"trait\" href=\"../future/trait.Future.html\" title=\"trait futures::future::Future\">Future</a> for <a class=\"struct\" href=\"struct.ReadToEnd.html\" title=\"struct futures::io::ReadToEnd\">ReadToEnd</a>&lt;'_, A&gt;<div class=\"where\">where\n A: <a class=\"trait\" href=\"trait.AsyncRead.html\" title=\"trait futures::io::AsyncRead\">AsyncRead</a> + <a class=\"trait\" href=\"https://doc.rust-lang.org/1.76.0/core/marker/trait.Unpin.html\" title=\"trait core::marker::Unpin\">Unpin</a> + ?<a class=\"trait\" href=\"https://doc.rust-lang.org/1.76.0/core/marker/trait.Sized.html\" title=\"trait core::marker::Sized\">Sized</a>,</div></div><div class=\"where\"> type <a href=\"../future/trait.Future.html#associatedtype.Output\" class=\"associatedtype\">Output</a> = <a class=\"enum\" href=\"https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html\" title=\"enum core::result::Result\">Result</a>&lt;<a class=\"primitive\" href=\"https://doc.rust-lang.org/1.76.0/std/primitive.usize.html\">usize</a>, <a class=\"struct\" href=\"struct.Error.html\" title=\"struct futures::io::Error\">Error</a>&gt;;</div>","ReadToString<'a, Self>":"<h3>Notable traits for <code><a class=\"struct\" href=\"struct.ReadToString.html\" title=\"struct futures::io::ReadToString\">ReadToString</a>&lt;'_, A&gt;</code></h3><pre><code><div class=\"where\">impl&lt;A&gt; <a class=\"trait\" href=\"../future/trait.Future.html\" title=\"trait futures::future::Future\">Future</a> for <a class=\"struct\" href=\"struct.ReadToString.html\" title=\"struct futures::io::ReadToString\">ReadToString</a>&lt;'_, A&gt;<div class=\"where\">where\n A: <a class=\"trait\" href=\"trait.AsyncRead.html\" title=\"trait futures::io::AsyncRead\">AsyncRead</a> + <a class=\"trait\" href=\"https://doc.rust-lang.org/1.76.0/core/marker/trait.Unpin.html\" title=\"trait core::marker::Unpin\">Unpin</a> + ?<a class=\"trait\" href=\"https://doc.rust-lang.org/1.76.0/core/marker/trait.Sized.html\" title=\"trait core::marker::Sized\">Sized</a>,</div></div><div class=\"where\"> type <a href=\"../future/trait.Future.html#associatedtype.Output\" class=\"associatedtype\">Output</a> = <a class=\"enum\" href=\"https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html\" title=\"enum core::result::Result\">Result</a>&lt;<a class=\"primitive\" href=\"https://doc.rust-lang.org/1.76.0/std/primitive.usize.html\">usize</a>, <a class=\"struct\" href=\"struct.Error.html\" title=\"struct futures::io::Error\">Error</a>&gt;;</div>","ReadVectored<'a, Self>":"<h3>Notable traits for <code><a class=\"struct\" href=\"struct.ReadVectored.html\" title=\"struct futures::io::ReadVectored\">ReadVectored</a>&lt;'_, R&gt;</code></h3><pre><code><div class=\"where\">impl&lt;R&gt; <a class=\"trait\" href=\"../future/trait.Future.html\" title=\"trait futures::future::Future\">Future</a> for <a class=\"struct\" href=\"struct.ReadVectored.html\" title=\"struct futures::io::ReadVectored\">ReadVectored</a>&lt;'_, R&gt;<div class=\"where\">where\n R: <a class=\"trait\" href=\"trait.AsyncRead.html\" title=\"trait futures::io::AsyncRead\">AsyncRead</a> + <a class=\"trait\" href=\"https://doc.rust-lang.org/1.76.0/core/marker/trait.Unpin.html\" title=\"trait core::marker::Unpin\">Unpin</a> + ?<a class=\"trait\" href=\"https://doc.rust-lang.org/1.76.0/core/marker/trait.Sized.html\" title=\"trait core::marker::Sized\">Sized</a>,</div></div><div class=\"where\"> type <a href=\"../future/trait.Future.html#associatedtype.Output\" class=\"associatedtype\">Output</a> = <a class=\"enum\" href=\"https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html\" title=\"enum core::result::Result\">Result</a>&lt;<a class=\"primitive\" href=\"https://doc.rust-lang.org/1.76.0/std/primitive.usize.html\">usize</a>, <a class=\"struct\" href=\"struct.Error.html\" title=\"struct futures::io::Error\">Error</a>&gt;;</div>"}</script></section></div></main></body></html>