| <!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 `AsyncBufRead` types."><meta name="keywords" content="rust, rustlang, rust-lang, AsyncBufReadExt"><title>AsyncBufReadExt in futures::io - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.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">☰</button><a class="sidebar-logo" href="../../futures/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../futures/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">AsyncBufReadExt</a></h2><div class="sidebar-elems"><section><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.consume_unpin">consume_unpin</a></li><li><a href="#method.fill_buf">fill_buf</a></li><li><a href="#method.lines">lines</a></li><li><a href="#method.read_line">read_line</a></li><li><a href="#method.read_until">read_until</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In futures::io</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Trait <a href="../index.html">futures</a>::<wbr><a href="index.html">io</a>::<wbr><a class="trait" href="#">AsyncBufReadExt</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../src/futures_util/io/mod.rs.html#623">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="item-decl"><pre class="rust trait"><code>pub trait AsyncBufReadExt: <a class="trait" href="trait.AsyncBufRead.html" title="trait futures::io::AsyncBufRead">AsyncBufRead</a> { |
| fn <a href="#method.fill_buf" class="fnname">fill_buf</a>(&mut self) -> <a class="struct" href="struct.FillBuf.html" title="struct futures::io::FillBuf">FillBuf</a><'_, Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.FillBuf.html" title="struct futures::io::FillBuf">FillBuf</a><'a, R></span><code class="content"><span class="where fmt-newline">impl<'a, R> <a class="trait" href="../future/trait.Future.html" title="trait futures::future::Future">Future</a> for <a class="struct" href="struct.FillBuf.html" title="struct futures::io::FillBuf">FillBuf</a><'a, R><span class="where fmt-newline">where<br> R: <a class="trait" href="trait.AsyncBufRead.html" title="trait futures::io::AsyncBufRead">AsyncBufRead</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></span><span class="where fmt-newline"> type <a href="../future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&'a [<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>], <a class="struct" href="struct.Error.html" title="struct futures::io::Error">Error</a>>;</span></code></span></span></span></span><br> <span class="where">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a></span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.consume_unpin" class="fnname">consume_unpin</a>(&mut self, amt: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>)<br> <span class="where">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a></span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.read_until" class="fnname">read_until</a>(<br> &'a mut self,<br> byte: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>,<br> buf: &'a mut <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/alloc/struct.Global.html" title="struct alloc::alloc::Global">Global</a>><br> ) -> <a class="struct" href="struct.ReadUntil.html" title="struct futures::io::ReadUntil">ReadUntil</a><'a, Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.ReadUntil.html" title="struct futures::io::ReadUntil">ReadUntil</a><'_, R></span><code class="content"><span class="where fmt-newline">impl<R> <a class="trait" href="../future/trait.Future.html" title="trait futures::future::Future">Future</a> for <a class="struct" href="struct.ReadUntil.html" title="struct futures::io::ReadUntil">ReadUntil</a><'_, R><span class="where fmt-newline">where<br> R: <a class="trait" href="trait.AsyncBufRead.html" title="trait futures::io::AsyncBufRead">AsyncBufRead</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></span><span class="where fmt-newline"> type <a href="../future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="struct" href="struct.Error.html" title="struct futures::io::Error">Error</a>>;</span></code></span></span></span></span><br> <span class="where">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a></span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.read_line" class="fnname">read_line</a>(&'a mut self, buf: &'a mut <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>) -> <a class="struct" href="struct.ReadLine.html" title="struct futures::io::ReadLine">ReadLine</a><'a, Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.ReadLine.html" title="struct futures::io::ReadLine">ReadLine</a><'_, R></span><code class="content"><span class="where fmt-newline">impl<R> <a class="trait" href="../future/trait.Future.html" title="trait futures::future::Future">Future</a> for <a class="struct" href="struct.ReadLine.html" title="struct futures::io::ReadLine">ReadLine</a><'_, R><span class="where fmt-newline">where<br> R: <a class="trait" href="trait.AsyncBufRead.html" title="trait futures::io::AsyncBufRead">AsyncBufRead</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></span><span class="where fmt-newline"> type <a href="../future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="struct" href="struct.Error.html" title="struct futures::io::Error">Error</a>>;</span></code></span></span></span></span><br> <span class="where">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a></span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.lines" class="fnname">lines</a>(self) -> <a class="struct" href="struct.Lines.html" title="struct futures::io::Lines">Lines</a><Self> { ... } |
| }</code></pre></div><details class="rustdoc-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>AsyncBufRead</code> types.</p> |
| </div></details><h2 id="provided-methods" class="small-section-header">Provided Methods<a href="#provided-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.fill_buf" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/io/mod.rs.html#647-649">source</a><h4 class="code-header">fn <a href="#method.fill_buf" class="fnname">fill_buf</a>(&mut self) -> <a class="struct" href="struct.FillBuf.html" title="struct futures::io::FillBuf">FillBuf</a><'_, Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.FillBuf.html" title="struct futures::io::FillBuf">FillBuf</a><'a, R></span><code class="content"><span class="where fmt-newline">impl<'a, R> <a class="trait" href="../future/trait.Future.html" title="trait futures::future::Future">Future</a> for <a class="struct" href="struct.FillBuf.html" title="struct futures::io::FillBuf">FillBuf</a><'a, R><span class="where fmt-newline">where<br> R: <a class="trait" href="trait.AsyncBufRead.html" title="trait futures::io::AsyncBufRead">AsyncBufRead</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></span><span class="where fmt-newline"> type <a href="../future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&'a [<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>], <a class="struct" href="struct.Error.html" title="struct futures::io::Error">Error</a>>;</span></code></span></span></span></span><span class="where fmt-newline">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a>,</span></h4></section></summary><div class="docblock"><p>Creates a future which will wait for a non-empty buffer to be available from this I/O |
| object or EOF to be reached.</p> |
| <p>This method is the async equivalent to <a href="https://doc.rust-lang.org/nightly/std/io/trait.BufRead.html#tymethod.fill_buf"><code>BufRead::fill_buf</code></a>.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::{io::AsyncBufReadExt <span class="kw">as _</span>, stream::{iter, TryStreamExt <span class="kw">as _</span>}}; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>stream = iter(<span class="macro">vec!</span>[<span class="prelude-val">Ok</span>(<span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]), <span class="prelude-val">Ok</span>(<span class="macro">vec!</span>[<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>])]).into_async_read(); |
| |
| <span class="macro">assert_eq!</span>(stream.fill_buf().<span class="kw">await</span><span class="question-mark">?</span>, <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]); |
| stream.consume_unpin(<span class="number">2</span>); |
| |
| <span class="macro">assert_eq!</span>(stream.fill_buf().<span class="kw">await</span><span class="question-mark">?</span>, <span class="macro">vec!</span>[<span class="number">3</span>]); |
| stream.consume_unpin(<span class="number">1</span>); |
| |
| <span class="macro">assert_eq!</span>(stream.fill_buf().<span class="kw">await</span><span class="question-mark">?</span>, <span class="macro">vec!</span>[<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]); |
| stream.consume_unpin(<span class="number">3</span>); |
| |
| <span class="macro">assert_eq!</span>(stream.fill_buf().<span class="kw">await</span><span class="question-mark">?</span>, <span class="macro">vec!</span>[]);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.consume_unpin" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/io/mod.rs.html#671-673">source</a><h4 class="code-header">fn <a href="#method.consume_unpin" class="fnname">consume_unpin</a>(&mut self, amt: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>)<span class="where fmt-newline">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a>,</span></h4></section></summary><div class="docblock"><p>A convenience for calling <a href="trait.AsyncBufRead.html#tymethod.consume" title="AsyncBufRead::consume"><code>AsyncBufRead::consume</code></a> on <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="Unpin"><code>Unpin</code></a> IO types.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::{io::AsyncBufReadExt <span class="kw">as _</span>, stream::{iter, TryStreamExt <span class="kw">as _</span>}}; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>stream = iter(<span class="macro">vec!</span>[<span class="prelude-val">Ok</span>(<span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])]).into_async_read(); |
| |
| <span class="macro">assert_eq!</span>(stream.fill_buf().<span class="kw">await</span><span class="question-mark">?</span>, <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]); |
| stream.consume_unpin(<span class="number">2</span>); |
| |
| <span class="macro">assert_eq!</span>(stream.fill_buf().<span class="kw">await</span><span class="question-mark">?</span>, <span class="macro">vec!</span>[<span class="number">3</span>]); |
| stream.consume_unpin(<span class="number">1</span>); |
| |
| <span class="macro">assert_eq!</span>(stream.fill_buf().<span class="kw">await</span><span class="question-mark">?</span>, <span class="macro">vec!</span>[]);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.read_until" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/io/mod.rs.html#719-721">source</a><h4 class="code-header">fn <a href="#method.read_until" class="fnname">read_until</a>(<br> &'a mut self,<br> byte: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>,<br> buf: &'a mut <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/alloc/struct.Global.html" title="struct alloc::alloc::Global">Global</a>><br>) -> <a class="struct" href="struct.ReadUntil.html" title="struct futures::io::ReadUntil">ReadUntil</a><'a, Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.ReadUntil.html" title="struct futures::io::ReadUntil">ReadUntil</a><'_, R></span><code class="content"><span class="where fmt-newline">impl<R> <a class="trait" href="../future/trait.Future.html" title="trait futures::future::Future">Future</a> for <a class="struct" href="struct.ReadUntil.html" title="struct futures::io::ReadUntil">ReadUntil</a><'_, R><span class="where fmt-newline">where<br> R: <a class="trait" href="trait.AsyncBufRead.html" title="trait futures::io::AsyncBufRead">AsyncBufRead</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></span><span class="where fmt-newline"> type <a href="../future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="struct" href="struct.Error.html" title="struct futures::io::Error">Error</a>>;</span></code></span></span></span></span><span class="where fmt-newline">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a>,</span></h4></section></summary><div class="docblock"><p>Creates a future which will read all the bytes associated with this I/O |
| object into <code>buf</code> until the delimiter <code>byte</code> or EOF is reached. |
| This method is the async equivalent to <a href="https://doc.rust-lang.org/nightly/std/io/trait.BufRead.html#method.read_until"><code>BufRead::read_until</code></a>.</p> |
| <p>This function will read bytes from the underlying stream until the |
| delimiter or EOF is found. Once found, all bytes up to, and including, |
| the delimiter (if found) will be appended to <code>buf</code>.</p> |
| <p>The returned future will resolve to the number of bytes read 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"><a href="#examples">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::io::{AsyncBufReadExt, Cursor}; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>cursor = Cursor::new(<span class="string">b"lorem-ipsum"</span>); |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = <span class="macro">vec!</span>[]; |
| |
| <span class="comment">// cursor is at 'l' |
| </span><span class="kw">let </span>num_bytes = cursor.read_until(<span class="string">b'-'</span>, <span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(num_bytes, <span class="number">6</span>); |
| <span class="macro">assert_eq!</span>(buf, <span class="string">b"lorem-"</span>); |
| buf.clear(); |
| |
| <span class="comment">// cursor is at 'i' |
| </span><span class="kw">let </span>num_bytes = cursor.read_until(<span class="string">b'-'</span>, <span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(num_bytes, <span class="number">5</span>); |
| <span class="macro">assert_eq!</span>(buf, <span class="string">b"ipsum"</span>); |
| buf.clear(); |
| |
| <span class="comment">// cursor is at EOF |
| </span><span class="kw">let </span>num_bytes = cursor.read_until(<span class="string">b'-'</span>, <span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(num_bytes, <span class="number">0</span>); |
| <span class="macro">assert_eq!</span>(buf, <span class="string">b""</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.read_line" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/io/mod.rs.html#777-779">source</a><h4 class="code-header">fn <a href="#method.read_line" class="fnname">read_line</a>(&'a mut self, buf: &'a mut <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>) -> <a class="struct" href="struct.ReadLine.html" title="struct futures::io::ReadLine">ReadLine</a><'a, Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.ReadLine.html" title="struct futures::io::ReadLine">ReadLine</a><'_, R></span><code class="content"><span class="where fmt-newline">impl<R> <a class="trait" href="../future/trait.Future.html" title="trait futures::future::Future">Future</a> for <a class="struct" href="struct.ReadLine.html" title="struct futures::io::ReadLine">ReadLine</a><'_, R><span class="where fmt-newline">where<br> R: <a class="trait" href="trait.AsyncBufRead.html" title="trait futures::io::AsyncBufRead">AsyncBufRead</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></span><span class="where fmt-newline"> type <a href="../future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="struct" href="struct.Error.html" title="struct futures::io::Error">Error</a>>;</span></code></span></span></span></span><span class="where fmt-newline">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a>,</span></h4></section></summary><div class="docblock"><p>Creates a future which will read all the bytes associated with this I/O |
| object into <code>buf</code> until a newline (the 0xA byte) or EOF is reached, |
| This method is the async equivalent to <a href="https://doc.rust-lang.org/nightly/std/io/trait.BufRead.html#method.read_line"><code>BufRead::read_line</code></a>.</p> |
| <p>This function will read bytes from the underlying stream until the |
| newline delimiter (the 0xA byte) or EOF is found. Once found, all bytes |
| up to, and including, the delimiter (if found) will be appended to |
| <code>buf</code>.</p> |
| <p>The returned future will resolve to the number of bytes read 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="errors"><a href="#errors">Errors</a></h5> |
| <p>This function has the same error semantics as <a href="trait.AsyncBufReadExt.html#method.read_until"><code>read_until</code></a> and will |
| also return an error if the read bytes are not valid UTF-8. If an I/O |
| error is encountered then <code>buf</code> may contain some bytes already read in |
| the event that all data read so far was valid UTF-8.</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::{AsyncBufReadExt, Cursor}; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>cursor = Cursor::new(<span class="string">b"foo\nbar"</span>); |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = String::new(); |
| |
| <span class="comment">// cursor is at 'f' |
| </span><span class="kw">let </span>num_bytes = cursor.read_line(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(num_bytes, <span class="number">4</span>); |
| <span class="macro">assert_eq!</span>(buf, <span class="string">"foo\n"</span>); |
| buf.clear(); |
| |
| <span class="comment">// cursor is at 'b' |
| </span><span class="kw">let </span>num_bytes = cursor.read_line(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(num_bytes, <span class="number">3</span>); |
| <span class="macro">assert_eq!</span>(buf, <span class="string">"bar"</span>); |
| buf.clear(); |
| |
| <span class="comment">// cursor is at EOF |
| </span><span class="kw">let </span>num_bytes = cursor.read_line(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(num_bytes, <span class="number">0</span>); |
| <span class="macro">assert_eq!</span>(buf, <span class="string">""</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.lines" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/io/mod.rs.html#816-818">source</a><h4 class="code-header">fn <a href="#method.lines" class="fnname">lines</a>(self) -> <a class="struct" href="struct.Lines.html" title="struct futures::io::Lines">Lines</a><Self></h4></section></summary><div class="docblock"><p>Returns a stream over the lines of this reader. |
| This method is the async equivalent to <a href="https://doc.rust-lang.org/nightly/std/io/trait.BufRead.html#method.lines"><code>BufRead::lines</code></a>.</p> |
| <p>The stream returned from this function will yield instances of |
| <a href="type.Result.html"><code>io::Result</code></a><code><</code><a href="https://doc.rust-lang.org/nightly/alloc/string/struct.String.html"><code>String</code></a><code>></code>. Each string returned will <em>not</em> have a newline |
| byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end.</p> |
| <h5 id="errors-1"><a href="#errors-1">Errors</a></h5> |
| <p>Each line of the stream has the same error semantics as <a href="trait.AsyncBufReadExt.html#method.read_line"><code>AsyncBufReadExt::read_line</code></a>.</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::{AsyncBufReadExt, Cursor}; |
| <span class="kw">use </span>futures::stream::StreamExt; |
| |
| <span class="kw">let </span>cursor = Cursor::new(<span class="string">b"lorem\nipsum\r\ndolor"</span>); |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>lines_stream = cursor.lines().map(|l| l.unwrap()); |
| <span class="macro">assert_eq!</span>(lines_stream.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(String::from(<span class="string">"lorem"</span>))); |
| <span class="macro">assert_eq!</span>(lines_stream.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(String::from(<span class="string">"ipsum"</span>))); |
| <span class="macro">assert_eq!</span>(lines_stream.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(String::from(<span class="string">"dolor"</span>))); |
| <span class="macro">assert_eq!</span>(lines_stream.next().<span class="kw">await</span>, <span class="prelude-val">None</span>);</code></pre></div> |
| </div></details></div><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div id="implementors-list"><section id="impl-AsyncBufReadExt-for-R" class="impl has-srclink"><a class="srclink rightside" href="../../src/futures_util/io/mod.rs.html#824">source</a><a href="#impl-AsyncBufReadExt-for-R" class="anchor"></a><h3 class="code-header">impl<R> <a class="trait" href="trait.AsyncBufReadExt.html" title="trait futures::io::AsyncBufReadExt">AsyncBufReadExt</a> for R<span class="where fmt-newline">where<br> R: <a class="trait" href="trait.AsyncBufRead.html" title="trait futures::io::AsyncBufRead">AsyncBufRead</a> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></h3></section></div><script src="../../implementors/futures_util/io/trait.AsyncBufReadExt.js" async></script></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="futures" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html> |