| <!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="A TCP stream between a local and a remote socket."><meta name="keywords" content="rust, rustlang, rust-lang, TcpStream"><title>TcpStream in tokio::net - 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 struct"><!--[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="../../tokio/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="../../tokio/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">TcpStream</a></h2><div class="sidebar-elems"><section><h3><a href="#implementations">Methods</a></h3><ul class="block"><li><a href="#method.connect">connect</a></li><li><a href="#method.from_std">from_std</a></li><li><a href="#method.into_split">into_split</a></li><li><a href="#method.into_std">into_std</a></li><li><a href="#method.linger">linger</a></li><li><a href="#method.local_addr">local_addr</a></li><li><a href="#method.nodelay">nodelay</a></li><li><a href="#method.peek">peek</a></li><li><a href="#method.peer_addr">peer_addr</a></li><li><a href="#method.poll_peek">poll_peek</a></li><li><a href="#method.poll_read_ready">poll_read_ready</a></li><li><a href="#method.poll_write_ready">poll_write_ready</a></li><li><a href="#method.readable">readable</a></li><li><a href="#method.ready">ready</a></li><li><a href="#method.set_linger">set_linger</a></li><li><a href="#method.set_nodelay">set_nodelay</a></li><li><a href="#method.set_ttl">set_ttl</a></li><li><a href="#method.split">split</a></li><li><a href="#method.take_error">take_error</a></li><li><a href="#method.try_io">try_io</a></li><li><a href="#method.try_read">try_read</a></li><li><a href="#method.try_read_buf">try_read_buf</a></li><li><a href="#method.try_read_vectored">try_read_vectored</a></li><li><a href="#method.try_write">try_write</a></li><li><a href="#method.try_write_vectored">try_write_vectored</a></li><li><a href="#method.ttl">ttl</a></li><li><a href="#method.writable">writable</a></li></ul><h3><a href="#trait-implementations">Trait Implementations</a></h3><ul class="block"><li><a href="#impl-AsRawFd-for-TcpStream">AsRawFd</a></li><li><a href="#impl-AsRef%3CTcpStream%3E-for-OwnedReadHalf">AsRef<TcpStream></a></li><li><a href="#impl-AsRef%3CTcpStream%3E-for-OwnedWriteHalf">AsRef<TcpStream></a></li><li><a href="#impl-AsRef%3CTcpStream%3E-for-ReadHalf%3C%27_%3E">AsRef<TcpStream></a></li><li><a href="#impl-AsRef%3CTcpStream%3E-for-WriteHalf%3C%27_%3E">AsRef<TcpStream></a></li><li><a href="#impl-AsyncRead-for-TcpStream">AsyncRead</a></li><li><a href="#impl-AsyncWrite-for-TcpStream">AsyncWrite</a></li><li><a href="#impl-Debug-for-TcpStream">Debug</a></li><li><a href="#impl-TryFrom%3CTcpStream%3E-for-TcpStream">TryFrom<TcpStream></a></li></ul><h3><a href="#synthetic-implementations">Auto Trait Implementations</a></h3><ul class="block"><li><a href="#impl-RefUnwindSafe-for-TcpStream">RefUnwindSafe</a></li><li><a href="#impl-Send-for-TcpStream">Send</a></li><li><a href="#impl-Sync-for-TcpStream">Sync</a></li><li><a href="#impl-Unpin-for-TcpStream">Unpin</a></li><li><a href="#impl-UnwindSafe-for-TcpStream">UnwindSafe</a></li></ul><h3><a href="#blanket-implementations">Blanket Implementations</a></h3><ul class="block"><li><a href="#impl-Any-for-TcpStream">Any</a></li><li><a href="#impl-Borrow%3CT%3E-for-TcpStream">Borrow<T></a></li><li><a href="#impl-BorrowMut%3CT%3E-for-TcpStream">BorrowMut<T></a></li><li><a href="#impl-From%3CT%3E-for-TcpStream">From<T></a></li><li><a href="#impl-Into%3CU%3E-for-TcpStream">Into<U></a></li><li><a href="#impl-TryFrom%3CU%3E-for-TcpStream">TryFrom<U></a></li><li><a href="#impl-TryInto%3CU%3E-for-TcpStream">TryInto<U></a></li></ul></section><h2><a href="index.html">In tokio::net</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">Struct <a href="../index.html">tokio</a>::<wbr><a href="index.html">net</a>::<wbr><a class="struct" href="#">TcpStream</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/tokio/net/tcp/stream.rs.html#70-72">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 struct"><code>pub struct TcpStream { /* private fields */ }</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A TCP stream between a local and a remote socket.</p> |
| <p>A TCP stream can either be created by connecting to an endpoint, via the |
| <a href="struct.TcpStream.html#method.connect"><code>connect</code></a> method, or by <a href="struct.TcpListener.html#method.accept">accepting</a> a connection from a <a href="struct.TcpListener.html">listener</a>. A |
| TCP stream can also be created via the <a href="struct.TcpSocket.html"><code>TcpSocket</code></a> type.</p> |
| <p>Reading and writing to a <code>TcpStream</code> is usually done using the |
| convenience methods found on the <a href="../io/trait.AsyncReadExt.html"><code>AsyncReadExt</code></a> and <a href="../io/trait.AsyncWriteExt.html"><code>AsyncWriteExt</code></a> |
| traits.</p> |
| <h2 id="examples"><a href="#examples">Examples</a></h2> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| <span class="kw">use </span>tokio::io::AsyncWriteExt; |
| <span class="kw">use </span>std::error::Error; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span><span class="kw-2">mut </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// Write some data. |
| </span>stream.write_all(<span class="string">b"hello world!"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| <p>The <a href="../io/trait.AsyncWriteExt.html#method.write_all"><code>write_all</code></a> method is defined on the <a href="../io/trait.AsyncWriteExt.html"><code>AsyncWriteExt</code></a> trait.</p> |
| <p>To shut down the stream in the write direction, you can call the |
| <a href="../io/trait.AsyncWriteExt.html#method.shutdown"><code>shutdown()</code></a> method. This will cause the other peer to receive a read of |
| length 0, indicating that no more data will be sent. This only closes |
| the stream in one direction.</p> |
| </div></details><h2 id="implementations" class="small-section-header">Implementations<a href="#implementations" class="anchor"></a></h2><div id="implementations-list"><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-TcpStream" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#75-1263">source</a><a href="#impl-TcpStream" class="anchor"></a><h3 class="code-header">impl <a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.connect" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#115-133">source</a><h4 class="code-header">pub async fn <a href="#method.connect" class="fnname">connect</a><A: <a class="trait" href="trait.ToSocketAddrs.html" title="trait tokio::net::ToSocketAddrs">ToSocketAddrs</a>>(addr: A) -> Result<<a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a>></h4></section></summary><div class="docblock"><p>Opens a TCP connection to a remote host.</p> |
| <p><code>addr</code> is an address of the remote host. Anything which implements the |
| <a href="trait.ToSocketAddrs.html"><code>ToSocketAddrs</code></a> trait can be supplied as the address. If <code>addr</code> |
| yields multiple addresses, connect will be attempted with each of the |
| addresses until a connection is successful. If none of the addresses |
| result in a successful connection, the error returned from the last |
| connection attempt (the last address) is returned.</p> |
| <p>To configure the socket before connecting, you can use the <a href="struct.TcpSocket.html"><code>TcpSocket</code></a> |
| type.</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>tokio::net::TcpStream; |
| <span class="kw">use </span>tokio::io::AsyncWriteExt; |
| <span class="kw">use </span>std::error::Error; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span><span class="kw-2">mut </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// Write some data. |
| </span>stream.write_all(<span class="string">b"hello world!"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| <p>The <a href="../io/trait.AsyncWriteExt.html#method.write_all"><code>write_all</code></a> method is defined on the <a href="../io/trait.AsyncWriteExt.html"><code>AsyncWriteExt</code></a> trait.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.from_std" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#196-200">source</a><h4 class="code-header">pub fn <a href="#method.from_std" class="fnname">from_std</a>(stream: TcpStream) -> Result<<a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a>></h4></section></summary><div class="docblock"><p>Creates new <code>TcpStream</code> from a <code>std::net::TcpStream</code>.</p> |
| <p>This function is intended to be used to wrap a TCP stream from the |
| standard library in the Tokio equivalent. The conversion assumes nothing |
| about the underlying stream; it is left up to the user to set it in |
| non-blocking mode.</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>std::error::Error; |
| <span class="kw">use </span>tokio::net::TcpStream; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="kw">let </span>std_stream = std::net::TcpStream::connect(<span class="string">"127.0.0.1:34254"</span>)<span class="question-mark">?</span>; |
| std_stream.set_nonblocking(<span class="bool-val">true</span>)<span class="question-mark">?</span>; |
| <span class="kw">let </span>stream = TcpStream::from_std(std_stream)<span class="question-mark">?</span>; |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| <h5 id="panics"><a href="#panics">Panics</a></h5> |
| <p>This function panics if it is not called from within a runtime with |
| IO enabled.</p> |
| <p>The runtime is usually set implicitly when this function is called |
| from a future driven by a tokio runtime, otherwise runtime can be set |
| explicitly with <a href="../runtime/struct.Runtime.html#method.enter"><code>Runtime::enter</code></a> function.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.into_std" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#236-263">source</a><h4 class="code-header">pub fn <a href="#method.into_std" class="fnname">into_std</a>(self) -> Result<TcpStream></h4></section></summary><div class="docblock"><p>Turns a <a href="struct.TcpStream.html"><code>tokio::net::TcpStream</code></a> into a <a href="std::net::TcpStream"><code>std::net::TcpStream</code></a>.</p> |
| <p>The returned <a href="std::net::TcpStream"><code>std::net::TcpStream</code></a> will have nonblocking mode set as <code>true</code>. |
| Use <a href="fn@std::net::TcpStream::set_nonblocking"><code>set_nonblocking</code></a> to change the blocking mode if needed.</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>std::error::Error; |
| <span class="kw">use </span>std::io::Read; |
| <span class="kw">use </span>tokio::net::TcpListener; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="kw">let </span><span class="kw-2">mut </span>data = [<span class="number">0u8</span>; <span class="number">12</span>]; |
| <span class="kw">let </span>listener = TcpListener::bind(<span class="string">"127.0.0.1:34254"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="kw">let </span>(tokio_tcp_stream, <span class="kw">_</span>) = listener.accept().<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="kw">let </span><span class="kw-2">mut </span>std_tcp_stream = tokio_tcp_stream.into_std()<span class="question-mark">?</span>; |
| std_tcp_stream.set_nonblocking(<span class="bool-val">false</span>)<span class="question-mark">?</span>; |
| std_tcp_stream.read_exact(<span class="kw-2">&mut </span>data)<span class="question-mark">?</span>; |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.local_addr" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#279-281">source</a><h4 class="code-header">pub fn <a href="#method.local_addr" class="fnname">local_addr</a>(&self) -> Result<SocketAddr></h4></section></summary><div class="docblock"><p>Returns the local address that this stream is bound to.</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>tokio::net::TcpStream; |
| |
| <span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="macro">println!</span>(<span class="string">"{:?}"</span>, stream.local_addr()<span class="question-mark">?</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.take_error" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#284-286">source</a><h4 class="code-header">pub fn <a href="#method.take_error" class="fnname">take_error</a>(&self) -> Result<Option<Error>></h4></section></summary><div class="docblock"><p>Returns the value of the <code>SO_ERROR</code> option.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.peer_addr" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#302-304">source</a><h4 class="code-header">pub fn <a href="#method.peer_addr" class="fnname">peer_addr</a>(&self) -> Result<SocketAddr></h4></section></summary><div class="docblock"><p>Returns the remote address that this stream is connected to.</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>tokio::net::TcpStream; |
| |
| <span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="macro">println!</span>(<span class="string">"{:?}"</span>, stream.peer_addr()<span class="question-mark">?</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_peek" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#348-372">source</a><h4 class="code-header">pub fn <a href="#method.poll_peek" class="fnname">poll_peek</a>(<br> &self,<br> cx: &mut Context<'_>,<br> buf: &mut <a class="struct" href="../io/struct.ReadBuf.html" title="struct tokio::io::ReadBuf">ReadBuf</a><'_><br>) -> Poll<Result<usize>></h4></section></summary><div class="docblock"><p>Attempts to receive data on the socket, without removing that data from |
| the queue, registering the current task for wakeup if data is not yet |
| available.</p> |
| <p>Note that on multiple calls to <code>poll_peek</code>, <code>poll_read</code> or |
| <code>poll_read_ready</code>, only the <code>Waker</code> from the <code>Context</code> passed to the |
| most recent call is scheduled to receive a wakeup. (However, |
| <code>poll_write</code> retains a second, independent waker.)</p> |
| <h5 id="return-value"><a href="#return-value">Return value</a></h5> |
| <p>The function returns:</p> |
| <ul> |
| <li><code>Poll::Pending</code> if data is not yet available.</li> |
| <li><code>Poll::Ready(Ok(n))</code> if data is available. <code>n</code> is the number of bytes peeked.</li> |
| <li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li> |
| </ul> |
| <h5 id="errors"><a href="#errors">Errors</a></h5> |
| <p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</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>tokio::io::{<span class="self">self</span>, ReadBuf}; |
| <span class="kw">use </span>tokio::net::TcpStream; |
| |
| <span class="kw">use </span>futures::future::poll_fn; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8000"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0</span>; <span class="number">10</span>]; |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = ReadBuf::new(<span class="kw-2">&mut </span>buf); |
| |
| poll_fn(|cx| { |
| stream.poll_peek(cx, <span class="kw-2">&mut </span>buf) |
| }).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.ready" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#447-450">source</a><h4 class="code-header">pub async fn <a href="#method.ready" class="fnname">ready</a>(&self, interest: <a class="struct" href="../io/struct.Interest.html" title="struct tokio::io::Interest">Interest</a>) -> Result<<a class="struct" href="../io/struct.Ready.html" title="struct tokio::io::Ready">Ready</a>></h4></section></summary><div class="docblock"><p>Waits for any of the requested ready states.</p> |
| <p>This function is usually paired with <code>try_read()</code> or <code>try_write()</code>. It |
| can be used to concurrently read / write to the same socket on a single |
| task without splitting the socket.</p> |
| <p>The function may complete without the socket being ready. This is a |
| false-positive and attempting an operation will return with |
| <code>io::ErrorKind::WouldBlock</code>. The function can also return with an empty |
| <a href="../io/struct.Ready.html" title="Ready"><code>Ready</code></a> set, so you should always check the returned value and possibly |
| wait again if the requested states are not set.</p> |
| <h5 id="cancel-safety"><a href="#cancel-safety">Cancel safety</a></h5> |
| <p>This method is cancel safe. Once a readiness event occurs, the method |
| will continue to return immediately until the readiness event is |
| consumed by an attempt to read or write that fails with <code>WouldBlock</code> or |
| <code>Poll::Pending</code>.</p> |
| <h5 id="examples-7"><a href="#examples-7">Examples</a></h5> |
| <p>Concurrently read and write to the stream on the same task without |
| splitting.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::io::Interest; |
| <span class="kw">use </span>tokio::net::TcpStream; |
| <span class="kw">use </span>std::error::Error; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| <span class="kw">let </span>ready = stream.ready(Interest::READABLE | Interest::WRITABLE).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">if </span>ready.is_readable() { |
| <span class="kw">let </span><span class="kw-2">mut </span>data = <span class="macro">vec!</span>[<span class="number">0</span>; <span class="number">1024</span>]; |
| <span class="comment">// Try to read data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>stream.try_read(<span class="kw-2">&mut </span>data) { |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="macro">println!</span>(<span class="string">"read {} bytes"</span>, n); |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e.into()); |
| } |
| } |
| |
| } |
| |
| <span class="kw">if </span>ready.is_writable() { |
| <span class="comment">// Try to write data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>stream.try_write(<span class="string">b"hello world"</span>) { |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="macro">println!</span>(<span class="string">"write {} bytes"</span>, n); |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue |
| </span>} |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e.into()); |
| } |
| } |
| } |
| } |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.readable" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#502-505">source</a><h4 class="code-header">pub async fn <a href="#method.readable" class="fnname">readable</a>(&self) -> Result<()></h4></section></summary><div class="docblock"><p>Waits for the socket to become readable.</p> |
| <p>This function is equivalent to <code>ready(Interest::READABLE)</code> and is usually |
| paired with <code>try_read()</code>.</p> |
| <h5 id="cancel-safety-1"><a href="#cancel-safety-1">Cancel safety</a></h5> |
| <p>This method is cancel safe. Once a readiness event occurs, the method |
| will continue to return immediately until the readiness event is |
| consumed by an attempt to read that fails with <code>WouldBlock</code> or |
| <code>Poll::Pending</code>.</p> |
| <h5 id="examples-8"><a href="#examples-8">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| <span class="kw">use </span>std::error::Error; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>msg = <span class="macro">vec!</span>[<span class="number">0</span>; <span class="number">1024</span>]; |
| |
| <span class="kw">loop </span>{ |
| <span class="comment">// Wait for the socket to be readable |
| </span>stream.readable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// Try to read data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>stream.try_read(<span class="kw-2">&mut </span>msg) { |
| <span class="prelude-val">Ok</span>(n) => { |
| msg.truncate(n); |
| <span class="kw">break</span>; |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e.into()); |
| } |
| } |
| } |
| |
| <span class="macro">println!</span>(<span class="string">"GOT = {:?}"</span>, msg); |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_read_ready" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#536-538">source</a><h4 class="code-header">pub fn <a href="#method.poll_read_ready" class="fnname">poll_read_ready</a>(&self, cx: &mut Context<'_>) -> Poll<Result<()>></h4></section></summary><div class="docblock"><p>Polls for read readiness.</p> |
| <p>If the tcp stream is not currently ready for reading, this method will |
| store a clone of the <code>Waker</code> from the provided <code>Context</code>. When the tcp |
| stream becomes ready for reading, <code>Waker::wake</code> will be called on the |
| waker.</p> |
| <p>Note that on multiple calls to <code>poll_read_ready</code>, <code>poll_read</code> or |
| <code>poll_peek</code>, only the <code>Waker</code> from the <code>Context</code> passed to the most |
| recent call is scheduled to receive a wakeup. (However, |
| <code>poll_write_ready</code> retains a second, independent waker.)</p> |
| <p>This function is intended for cases where creating and pinning a future |
| via <a href="struct.TcpStream.html#method.readable"><code>readable</code></a> is not feasible. Where possible, using <a href="struct.TcpStream.html#method.readable"><code>readable</code></a> is |
| preferred, as this supports polling from multiple tasks at once.</p> |
| <h5 id="return-value-1"><a href="#return-value-1">Return value</a></h5> |
| <p>The function returns:</p> |
| <ul> |
| <li><code>Poll::Pending</code> if the tcp stream is not ready for reading.</li> |
| <li><code>Poll::Ready(Ok(()))</code> if the tcp stream is ready for reading.</li> |
| <li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li> |
| </ul> |
| <h5 id="errors-1"><a href="#errors-1">Errors</a></h5> |
| <p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_read" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#603-609">source</a><h4 class="code-header">pub fn <a href="#method.try_read" class="fnname">try_read</a>(&self, buf: &mut [u8]) -> Result<usize></h4></section></summary><div class="docblock"><p>Tries to read data from the stream into the provided buffer, returning how |
| many bytes were read.</p> |
| <p>Receives any pending data from the socket but does not wait for new data |
| to arrive. On success, returns the number of bytes read. Because |
| <code>try_read()</code> is non-blocking, the buffer does not have to be stored by |
| the async task and can exist entirely on the stack.</p> |
| <p>Usually, <a href="struct.TcpStream.html#method.readable"><code>readable()</code></a> or <a href="struct.TcpStream.html#method.ready"><code>ready()</code></a> is used with this function.</p> |
| <h5 id="return"><a href="#return">Return</a></h5> |
| <p>If data is successfully read, <code>Ok(n)</code> is returned, where <code>n</code> is the |
| number of bytes read. If <code>n</code> is <code>0</code>, then it can indicate one of two scenarios:</p> |
| <ol> |
| <li>The stream’s read half is closed and will no longer yield data.</li> |
| <li>The specified buffer was 0 bytes in length.</li> |
| </ol> |
| <p>If the stream is not ready to read data, |
| <code>Err(io::ErrorKind::WouldBlock)</code> is returned.</p> |
| <h5 id="examples-9"><a href="#examples-9">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| <span class="kw">use </span>std::error::Error; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| <span class="comment">// Wait for the socket to be readable |
| </span>stream.readable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// Creating the buffer **after** the `await` prevents it from |
| // being stored in the async task. |
| </span><span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0</span>; <span class="number">4096</span>]; |
| |
| <span class="comment">// Try to read data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>stream.try_read(<span class="kw-2">&mut </span>buf) { |
| <span class="prelude-val">Ok</span>(<span class="number">0</span>) => <span class="kw">break</span>, |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="macro">println!</span>(<span class="string">"read {} bytes"</span>, n); |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e.into()); |
| } |
| } |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_read_vectored" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#681-687">source</a><h4 class="code-header">pub fn <a href="#method.try_read_vectored" class="fnname">try_read_vectored</a>(&self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize></h4></section></summary><div class="docblock"><p>Tries to read data from the stream into the provided buffers, returning |
| how many bytes were read.</p> |
| <p>Data is copied to fill each buffer in order, with the final buffer |
| written to possibly being only partially filled. This method behaves |
| equivalently to a single call to <a href="struct.TcpStream.html#method.try_read"><code>try_read()</code></a> with concatenated |
| buffers.</p> |
| <p>Receives any pending data from the socket but does not wait for new data |
| to arrive. On success, returns the number of bytes read. Because |
| <code>try_read_vectored()</code> is non-blocking, the buffer does not have to be |
| stored by the async task and can exist entirely on the stack.</p> |
| <p>Usually, <a href="struct.TcpStream.html#method.readable"><code>readable()</code></a> or <a href="struct.TcpStream.html#method.ready"><code>ready()</code></a> is used with this function.</p> |
| <h5 id="return-1"><a href="#return-1">Return</a></h5> |
| <p>If data is successfully read, <code>Ok(n)</code> is returned, where <code>n</code> is the |
| number of bytes read. <code>Ok(0)</code> indicates the stream’s read half is closed |
| and will no longer yield data. If the stream is not ready to read data |
| <code>Err(io::ErrorKind::WouldBlock)</code> is returned.</p> |
| <h5 id="examples-10"><a href="#examples-10">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| <span class="kw">use </span>std::error::Error; |
| <span class="kw">use </span>std::io::{<span class="self">self</span>, IoSliceMut}; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| <span class="comment">// Wait for the socket to be readable |
| </span>stream.readable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// Creating the buffer **after** the `await` prevents it from |
| // being stored in the async task. |
| </span><span class="kw">let </span><span class="kw-2">mut </span>buf_a = [<span class="number">0</span>; <span class="number">512</span>]; |
| <span class="kw">let </span><span class="kw-2">mut </span>buf_b = [<span class="number">0</span>; <span class="number">1024</span>]; |
| <span class="kw">let </span><span class="kw-2">mut </span>bufs = [ |
| IoSliceMut::new(<span class="kw-2">&mut </span>buf_a), |
| IoSliceMut::new(<span class="kw-2">&mut </span>buf_b), |
| ]; |
| |
| <span class="comment">// Try to read data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>stream.try_read_vectored(<span class="kw-2">&mut </span>bufs) { |
| <span class="prelude-val">Ok</span>(<span class="number">0</span>) => <span class="kw">break</span>, |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="macro">println!</span>(<span class="string">"read {} bytes"</span>, n); |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e.into()); |
| } |
| } |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_read_buf" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#747-765">source</a><h4 class="code-header">pub fn <a href="#method.try_read_buf" class="fnname">try_read_buf</a><B: <a class="trait" href="../../bytes/buf/buf_mut/trait.BufMut.html" title="trait bytes::buf::buf_mut::BufMut">BufMut</a>>(&self, buf: &mut B) -> Result<usize></h4></section></summary><div class="docblock"><p>Tries to read data from the stream into the provided buffer, advancing the |
| buffer’s internal cursor, returning how many bytes were read.</p> |
| <p>Receives any pending data from the socket but does not wait for new data |
| to arrive. On success, returns the number of bytes read. Because |
| <code>try_read_buf()</code> is non-blocking, the buffer does not have to be stored by |
| the async task and can exist entirely on the stack.</p> |
| <p>Usually, <a href="struct.TcpStream.html#method.readable"><code>readable()</code></a> or <a href="struct.TcpStream.html#method.ready"><code>ready()</code></a> is used with this function.</p> |
| <h5 id="return-2"><a href="#return-2">Return</a></h5> |
| <p>If data is successfully read, <code>Ok(n)</code> is returned, where <code>n</code> is the |
| number of bytes read. <code>Ok(0)</code> indicates the stream’s read half is closed |
| and will no longer yield data. If the stream is not ready to read data |
| <code>Err(io::ErrorKind::WouldBlock)</code> is returned.</p> |
| <h5 id="examples-11"><a href="#examples-11">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| <span class="kw">use </span>std::error::Error; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| <span class="comment">// Wait for the socket to be readable |
| </span>stream.readable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = Vec::with_capacity(<span class="number">4096</span>); |
| |
| <span class="comment">// Try to read data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>stream.try_read_buf(<span class="kw-2">&mut </span>buf) { |
| <span class="prelude-val">Ok</span>(<span class="number">0</span>) => <span class="kw">break</span>, |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="macro">println!</span>(<span class="string">"read {} bytes"</span>, n); |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e.into()); |
| } |
| } |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.writable" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#814-817">source</a><h4 class="code-header">pub async fn <a href="#method.writable" class="fnname">writable</a>(&self) -> Result<()></h4></section></summary><div class="docblock"><p>Waits for the socket to become writable.</p> |
| <p>This function is equivalent to <code>ready(Interest::WRITABLE)</code> and is usually |
| paired with <code>try_write()</code>.</p> |
| <h5 id="cancel-safety-2"><a href="#cancel-safety-2">Cancel safety</a></h5> |
| <p>This method is cancel safe. Once a readiness event occurs, the method |
| will continue to return immediately until the readiness event is |
| consumed by an attempt to write that fails with <code>WouldBlock</code> or |
| <code>Poll::Pending</code>.</p> |
| <h5 id="examples-12"><a href="#examples-12">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| <span class="kw">use </span>std::error::Error; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| <span class="comment">// Wait for the socket to be writable |
| </span>stream.writable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// Try to write data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>stream.try_write(<span class="string">b"hello world"</span>) { |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="kw">break</span>; |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e.into()); |
| } |
| } |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_write_ready" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#848-850">source</a><h4 class="code-header">pub fn <a href="#method.poll_write_ready" class="fnname">poll_write_ready</a>(&self, cx: &mut Context<'_>) -> Poll<Result<()>></h4></section></summary><div class="docblock"><p>Polls for write readiness.</p> |
| <p>If the tcp stream is not currently ready for writing, this method will |
| store a clone of the <code>Waker</code> from the provided <code>Context</code>. When the tcp |
| stream becomes ready for writing, <code>Waker::wake</code> will be called on the |
| waker.</p> |
| <p>Note that on multiple calls to <code>poll_write_ready</code> or <code>poll_write</code>, only |
| the <code>Waker</code> from the <code>Context</code> passed to the most recent call is |
| scheduled to receive a wakeup. (However, <code>poll_read_ready</code> retains a |
| second, independent waker.)</p> |
| <p>This function is intended for cases where creating and pinning a future |
| via <a href="struct.TcpStream.html#method.writable"><code>writable</code></a> is not feasible. Where possible, using <a href="struct.TcpStream.html#method.writable"><code>writable</code></a> is |
| preferred, as this supports polling from multiple tasks at once.</p> |
| <h5 id="return-value-2"><a href="#return-value-2">Return value</a></h5> |
| <p>The function returns:</p> |
| <ul> |
| <li><code>Poll::Pending</code> if the tcp stream is not ready for writing.</li> |
| <li><code>Poll::Ready(Ok(()))</code> if the tcp stream is ready for writing.</li> |
| <li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li> |
| </ul> |
| <h5 id="errors-2"><a href="#errors-2">Errors</a></h5> |
| <p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_write" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#900-906">source</a><h4 class="code-header">pub fn <a href="#method.try_write" class="fnname">try_write</a>(&self, buf: &[u8]) -> Result<usize></h4></section></summary><div class="docblock"><p>Try to write a buffer to the stream, returning how many bytes were |
| written.</p> |
| <p>The function will attempt to write the entire contents of <code>buf</code>, but |
| only part of the buffer may be written.</p> |
| <p>This function is usually paired with <code>writable()</code>.</p> |
| <h5 id="return-3"><a href="#return-3">Return</a></h5> |
| <p>If data is successfully written, <code>Ok(n)</code> is returned, where <code>n</code> is the |
| number of bytes written. If the stream is not ready to write data, |
| <code>Err(io::ErrorKind::WouldBlock)</code> is returned.</p> |
| <h5 id="examples-13"><a href="#examples-13">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| <span class="kw">use </span>std::error::Error; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| <span class="comment">// Wait for the socket to be writable |
| </span>stream.writable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// Try to write data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>stream.try_write(<span class="string">b"hello world"</span>) { |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="kw">break</span>; |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e.into()); |
| } |
| } |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_write_vectored" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#962-968">source</a><h4 class="code-header">pub fn <a href="#method.try_write_vectored" class="fnname">try_write_vectored</a>(&self, bufs: &[IoSlice<'_>]) -> Result<usize></h4></section></summary><div class="docblock"><p>Tries to write several buffers to the stream, returning how many bytes |
| were written.</p> |
| <p>Data is written from each buffer in order, with the final buffer read |
| from possible being only partially consumed. This method behaves |
| equivalently to a single call to <a href="struct.TcpStream.html#method.try_write"><code>try_write()</code></a> with concatenated |
| buffers.</p> |
| <p>This function is usually paired with <code>writable()</code>.</p> |
| <h5 id="return-4"><a href="#return-4">Return</a></h5> |
| <p>If data is successfully written, <code>Ok(n)</code> is returned, where <code>n</code> is the |
| number of bytes written. If the stream is not ready to write data, |
| <code>Err(io::ErrorKind::WouldBlock)</code> is returned.</p> |
| <h5 id="examples-14"><a href="#examples-14">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| <span class="kw">use </span>std::error::Error; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">let </span>bufs = [io::IoSlice::new(<span class="string">b"hello "</span>), io::IoSlice::new(<span class="string">b"world"</span>)]; |
| |
| <span class="kw">loop </span>{ |
| <span class="comment">// Wait for the socket to be writable |
| </span>stream.writable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// Try to write data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>stream.try_write_vectored(<span class="kw-2">&</span>bufs) { |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="kw">break</span>; |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e.into()); |
| } |
| } |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_io" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1002-1010">source</a><h4 class="code-header">pub fn <a href="#method.try_io" class="fnname">try_io</a><R>(<br> &self,<br> interest: <a class="struct" href="../io/struct.Interest.html" title="struct tokio::io::Interest">Interest</a>,<br> f: impl FnOnce() -> Result<R><br>) -> Result<R></h4></section></summary><div class="docblock"><p>Tries to read or write from the socket using a user-provided IO operation.</p> |
| <p>If the socket is ready, the provided closure is called. The closure |
| should attempt to perform IO operation on the socket by manually |
| calling the appropriate syscall. If the operation fails because the |
| socket is not actually ready, then the closure should return a |
| <code>WouldBlock</code> error and the readiness flag is cleared. The return value |
| of the closure is then returned by <code>try_io</code>.</p> |
| <p>If the socket is not ready, then the closure is not called |
| and a <code>WouldBlock</code> error is returned.</p> |
| <p>The closure should only return a <code>WouldBlock</code> error if it has performed |
| an IO operation on the socket that failed due to the socket not being |
| ready. Returning a <code>WouldBlock</code> error in any other situation will |
| incorrectly clear the readiness flag, which can cause the socket to |
| behave incorrectly.</p> |
| <p>The closure should not perform the IO operation using any of the methods |
| defined on the Tokio <code>TcpStream</code> type, as this will mess with the |
| readiness flag and can cause the socket to behave incorrectly.</p> |
| <p>This method is not intended to be used with combined interests. |
| The closure should perform only one type of IO operation, so it should not |
| require more than one ready state. This method may panic or sleep forever |
| if it is called with a combined interest.</p> |
| <p>Usually, <a href="struct.TcpStream.html#method.readable"><code>readable()</code></a>, <a href="struct.TcpStream.html#method.writable"><code>writable()</code></a> or <a href="struct.TcpStream.html#method.ready"><code>ready()</code></a> is used with this function.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.peek" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1049-1054">source</a><h4 class="code-header">pub async fn <a href="#method.peek" class="fnname">peek</a>(&self, buf: &mut [u8]) -> Result<usize></h4></section></summary><div class="docblock"><p>Receives data on the socket from the remote address to which it is |
| connected, without removing that data from the queue. On success, |
| returns the number of bytes peeked.</p> |
| <p>Successive calls return the same data. This is accomplished by passing |
| <code>MSG_PEEK</code> as a flag to the underlying recv system call.</p> |
| <h5 id="examples-15"><a href="#examples-15">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| <span class="kw">use </span>tokio::io::AsyncReadExt; |
| <span class="kw">use </span>std::error::Error; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span><span class="kw-2">mut </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>b1 = [<span class="number">0</span>; <span class="number">10</span>]; |
| <span class="kw">let </span><span class="kw-2">mut </span>b2 = [<span class="number">0</span>; <span class="number">10</span>]; |
| |
| <span class="comment">// Peek at the data |
| </span><span class="kw">let </span>n = stream.peek(<span class="kw-2">&mut </span>b1).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// Read the data |
| </span><span class="macro">assert_eq!</span>(n, stream.read(<span class="kw-2">&mut </span>b2[..n]).<span class="kw">await</span><span class="question-mark">?</span>); |
| <span class="macro">assert_eq!</span>(<span class="kw-2">&</span>b1[..n], <span class="kw-2">&</span>b2[..n]); |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| <p>The <a href="../io/trait.AsyncReadExt.html#method.read"><code>read</code></a> method is defined on the <a href="../io/trait.AsyncReadExt.html"><code>AsyncReadExt</code></a> trait.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.nodelay" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1083-1085">source</a><h4 class="code-header">pub fn <a href="#method.nodelay" class="fnname">nodelay</a>(&self) -> Result<bool></h4></section></summary><div class="docblock"><p>Gets the value of the <code>TCP_NODELAY</code> option on this socket.</p> |
| <p>For more information about this option, see <a href="struct.TcpStream.html#method.set_nodelay"><code>set_nodelay</code></a>.</p> |
| <h5 id="examples-16"><a href="#examples-16">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| |
| <span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="macro">println!</span>(<span class="string">"{:?}"</span>, stream.nodelay()<span class="question-mark">?</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.set_nodelay" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1107-1109">source</a><h4 class="code-header">pub fn <a href="#method.set_nodelay" class="fnname">set_nodelay</a>(&self, nodelay: bool) -> Result<()></h4></section></summary><div class="docblock"><p>Sets the value of the <code>TCP_NODELAY</code> option on this socket.</p> |
| <p>If set, this option disables the Nagle algorithm. This means that |
| segments are always sent as soon as possible, even if there is only a |
| small amount of data. When not set, data is buffered until there is a |
| sufficient amount to send out, thereby avoiding the frequent sending of |
| small packets.</p> |
| <h5 id="examples-17"><a href="#examples-17">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| |
| <span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| stream.set_nodelay(<span class="bool-val">true</span>)<span class="question-mark">?</span>;</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.linger" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1131-1133">source</a><h4 class="code-header">pub fn <a href="#method.linger" class="fnname">linger</a>(&self) -> Result<Option<Duration>></h4></section></summary><div class="docblock"><p>Reads the linger duration for this socket by getting the <code>SO_LINGER</code> |
| option.</p> |
| <p>For more information about this option, see <a href="struct.TcpStream.html#method.set_linger"><code>set_linger</code></a>.</p> |
| <h5 id="examples-18"><a href="#examples-18">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| |
| <span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="macro">println!</span>(<span class="string">"{:?}"</span>, stream.linger()<span class="question-mark">?</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.set_linger" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1156-1158">source</a><h4 class="code-header">pub fn <a href="#method.set_linger" class="fnname">set_linger</a>(&self, dur: Option<Duration>) -> Result<()></h4></section></summary><div class="docblock"><p>Sets the linger duration of this socket by setting the SO_LINGER option.</p> |
| <p>This option controls the action taken when a stream has unsent messages and the stream is |
| closed. If SO_LINGER is set, the system shall block the process until it can transmit the |
| data or until the time expires.</p> |
| <p>If SO_LINGER is not specified, and the stream is closed, the system handles the call in a |
| way that allows the process to continue as quickly as possible.</p> |
| <h5 id="examples-19"><a href="#examples-19">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| |
| <span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| stream.set_linger(<span class="prelude-val">None</span>)<span class="question-mark">?</span>;</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.ttl" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1179-1181">source</a><h4 class="code-header">pub fn <a href="#method.ttl" class="fnname">ttl</a>(&self) -> Result<u32></h4></section></summary><div class="docblock"><p>Gets the value of the <code>IP_TTL</code> option for this socket.</p> |
| <p>For more information about this option, see <a href="struct.TcpStream.html#method.set_ttl"><code>set_ttl</code></a>.</p> |
| <h5 id="examples-20"><a href="#examples-20">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| |
| <span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="macro">println!</span>(<span class="string">"{:?}"</span>, stream.ttl()<span class="question-mark">?</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.set_ttl" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1200-1202">source</a><h4 class="code-header">pub fn <a href="#method.set_ttl" class="fnname">set_ttl</a>(&self, ttl: u32) -> Result<()></h4></section></summary><div class="docblock"><p>Sets the value for the <code>IP_TTL</code> option on this socket.</p> |
| <p>This value sets the time-to-live field that is used in every packet sent |
| from this socket.</p> |
| <h5 id="examples-21"><a href="#examples-21">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpStream; |
| |
| <span class="kw">let </span>stream = TcpStream::connect(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| stream.set_ttl(<span class="number">123</span>)<span class="question-mark">?</span>;</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.split" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1214-1216">source</a><h4 class="code-header">pub fn <a href="#method.split" class="fnname">split</a><'a>(&'a mut self) -> (<a class="struct" href="tcp/struct.ReadHalf.html" title="struct tokio::net::tcp::ReadHalf">ReadHalf</a><'a>, <a class="struct" href="tcp/struct.WriteHalf.html" title="struct tokio::net::tcp::WriteHalf">WriteHalf</a><'a>)</h4></section></summary><div class="docblock"><p>Splits a <code>TcpStream</code> into a read half and a write half, which can be used |
| to read and write the stream concurrently.</p> |
| <p>This method is more efficient than <a href="struct.TcpStream.html#method.into_split"><code>into_split</code></a>, but the halves cannot be |
| moved into independently spawned tasks.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.into_split" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1229-1231">source</a><h4 class="code-header">pub fn <a href="#method.into_split" class="fnname">into_split</a>(self) -> (<a class="struct" href="tcp/struct.OwnedReadHalf.html" title="struct tokio::net::tcp::OwnedReadHalf">OwnedReadHalf</a>, <a class="struct" href="tcp/struct.OwnedWriteHalf.html" title="struct tokio::net::tcp::OwnedWriteHalf">OwnedWriteHalf</a>)</h4></section></summary><div class="docblock"><p>Splits a <code>TcpStream</code> into a read half and a write half, which can be used |
| to read and write the stream concurrently.</p> |
| <p>Unlike <a href="struct.TcpStream.html#method.split"><code>split</code></a>, the owned halves can be moved to separate tasks, however |
| this comes at the cost of a heap allocation.</p> |
| <p><strong>Note:</strong> Dropping the write half will shut down the write half of the TCP |
| stream. This is equivalent to calling <a href="../io/trait.AsyncWriteExt.html#method.shutdown"><code>shutdown()</code></a> on the <code>TcpStream</code>.</p> |
| </div></details></div></details></div><h2 id="trait-implementations" class="small-section-header">Trait Implementations<a href="#trait-implementations" class="anchor"></a></h2><div id="trait-implementations-list"><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-AsRawFd-for-TcpStream" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1333-1337">source</a><a href="#impl-AsRawFd-for-TcpStream" class="anchor"></a><h3 class="code-header">impl AsRawFd for <a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.as_raw_fd" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1334-1336">source</a><a href="#method.as_raw_fd" class="anchor"></a><h4 class="code-header">fn <a class="fnname">as_raw_fd</a>(&self) -> RawFd</h4></section></summary><div class='docblock'>Extracts the raw file descriptor. <a>Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-AsRef%3CTcpStream%3E-for-OwnedReadHalf" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/split_owned.rs.html#491-495">source</a><a href="#impl-AsRef%3CTcpStream%3E-for-OwnedReadHalf" class="anchor"></a><h3 class="code-header">impl AsRef<<a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a>> for <a class="struct" href="tcp/struct.OwnedReadHalf.html" title="struct tokio::net::tcp::OwnedReadHalf">OwnedReadHalf</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.as_ref-2" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/split_owned.rs.html#492-494">source</a><a href="#method.as_ref-2" class="anchor"></a><h4 class="code-header">fn <a class="fnname">as_ref</a>(&self) -> &<a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h4></section></summary><div class='docblock'>Converts this type into a shared reference of the (usually inferred) input type.</div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-AsRef%3CTcpStream%3E-for-OwnedWriteHalf" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/split_owned.rs.html#497-501">source</a><a href="#impl-AsRef%3CTcpStream%3E-for-OwnedWriteHalf" class="anchor"></a><h3 class="code-header">impl AsRef<<a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a>> for <a class="struct" href="tcp/struct.OwnedWriteHalf.html" title="struct tokio::net::tcp::OwnedWriteHalf">OwnedWriteHalf</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.as_ref-3" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/split_owned.rs.html#498-500">source</a><a href="#method.as_ref-3" class="anchor"></a><h4 class="code-header">fn <a class="fnname">as_ref</a>(&self) -> &<a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h4></section></summary><div class='docblock'>Converts this type into a shared reference of the (usually inferred) input type.</div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-AsRef%3CTcpStream%3E-for-ReadHalf%3C%27_%3E" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/split.rs.html#407-411">source</a><a href="#impl-AsRef%3CTcpStream%3E-for-ReadHalf%3C%27_%3E" class="anchor"></a><h3 class="code-header">impl AsRef<<a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a>> for <a class="struct" href="tcp/struct.ReadHalf.html" title="struct tokio::net::tcp::ReadHalf">ReadHalf</a><'_></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.as_ref" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/split.rs.html#408-410">source</a><a href="#method.as_ref" class="anchor"></a><h4 class="code-header">fn <a class="fnname">as_ref</a>(&self) -> &<a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h4></section></summary><div class='docblock'>Converts this type into a shared reference of the (usually inferred) input type.</div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-AsRef%3CTcpStream%3E-for-WriteHalf%3C%27_%3E" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/split.rs.html#413-417">source</a><a href="#impl-AsRef%3CTcpStream%3E-for-WriteHalf%3C%27_%3E" class="anchor"></a><h3 class="code-header">impl AsRef<<a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a>> for <a class="struct" href="tcp/struct.WriteHalf.html" title="struct tokio::net::tcp::WriteHalf">WriteHalf</a><'_></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.as_ref-1" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/split.rs.html#414-416">source</a><a href="#method.as_ref-1" class="anchor"></a><h4 class="code-header">fn <a class="fnname">as_ref</a>(&self) -> &<a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h4></section></summary><div class='docblock'>Converts this type into a shared reference of the (usually inferred) input type.</div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-AsyncRead-for-TcpStream" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1279-1287">source</a><a href="#impl-AsyncRead-for-TcpStream" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="../io/trait.AsyncRead.html" title="trait tokio::io::AsyncRead">AsyncRead</a> for <a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_read" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1280-1286">source</a><a href="#method.poll_read" class="anchor"></a><h4 class="code-header">fn <a href="../io/trait.AsyncRead.html#tymethod.poll_read" class="fnname">poll_read</a>(<br> self: Pin<&mut Self>,<br> cx: &mut Context<'_>,<br> buf: &mut <a class="struct" href="../io/struct.ReadBuf.html" title="struct tokio::io::ReadBuf">ReadBuf</a><'_><br>) -> Poll<Result<()>></h4></section></summary><div class='docblock'>Attempts to read from the <code>AsyncRead</code> into <code>buf</code>. <a href="../io/trait.AsyncRead.html#tymethod.poll_read">Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-AsyncWrite-for-TcpStream" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1289-1320">source</a><a href="#impl-AsyncWrite-for-TcpStream" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="../io/trait.AsyncWrite.html" title="trait tokio::io::AsyncWrite">AsyncWrite</a> for <a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_write" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1290-1296">source</a><a href="#method.poll_write" class="anchor"></a><h4 class="code-header">fn <a href="../io/trait.AsyncWrite.html#tymethod.poll_write" class="fnname">poll_write</a>(<br> self: Pin<&mut Self>,<br> cx: &mut Context<'_>,<br> buf: &[u8]<br>) -> Poll<Result<usize>></h4></section></summary><div class='docblock'>Attempt to write bytes from <code>buf</code> into the object. <a href="../io/trait.AsyncWrite.html#tymethod.poll_write">Read more</a></div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_write_vectored" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1298-1304">source</a><a href="#method.poll_write_vectored" class="anchor"></a><h4 class="code-header">fn <a href="../io/trait.AsyncWrite.html#method.poll_write_vectored" class="fnname">poll_write_vectored</a>(<br> self: Pin<&mut Self>,<br> cx: &mut Context<'_>,<br> bufs: &[IoSlice<'_>]<br>) -> Poll<Result<usize>></h4></section></summary><div class='docblock'>Like <a href="../io/trait.AsyncWrite.html#tymethod.poll_write"><code>poll_write</code></a>, except that it writes from a slice of buffers. <a href="../io/trait.AsyncWrite.html#method.poll_write_vectored">Read more</a></div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.is_write_vectored" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1306-1308">source</a><a href="#method.is_write_vectored" class="anchor"></a><h4 class="code-header">fn <a href="../io/trait.AsyncWrite.html#method.is_write_vectored" class="fnname">is_write_vectored</a>(&self) -> bool</h4></section></summary><div class='docblock'>Determines if this writer has an efficient <a href="../io/trait.AsyncWrite.html#method.poll_write_vectored"><code>poll_write_vectored</code></a> |
| implementation. <a href="../io/trait.AsyncWrite.html#method.is_write_vectored">Read more</a></div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_flush" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1311-1314">source</a><a href="#method.poll_flush" class="anchor"></a><h4 class="code-header">fn <a href="../io/trait.AsyncWrite.html#tymethod.poll_flush" class="fnname">poll_flush</a>(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>></h4></section></summary><div class='docblock'>Attempts to flush the object, ensuring that any buffered data reach |
| their destination. <a href="../io/trait.AsyncWrite.html#tymethod.poll_flush">Read more</a></div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_shutdown" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1316-1319">source</a><a href="#method.poll_shutdown" class="anchor"></a><h4 class="code-header">fn <a href="../io/trait.AsyncWrite.html#tymethod.poll_shutdown" class="fnname">poll_shutdown</a>(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>></h4></section></summary><div class='docblock'>Initiates or attempts to shut down this writer, returning success when |
| the I/O connection has completely shut down. <a href="../io/trait.AsyncWrite.html#tymethod.poll_shutdown">Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-Debug-for-TcpStream" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1322-1326">source</a><a href="#impl-Debug-for-TcpStream" class="anchor"></a><h3 class="code-header">impl Debug for <a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.fmt" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1323-1325">source</a><a href="#method.fmt" class="anchor"></a><h4 class="code-header">fn <a class="fnname">fmt</a>(&self, f: &mut Formatter<'_>) -> Result</h4></section></summary><div class='docblock'>Formats the value using the given formatter. <a>Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-TryFrom%3CTcpStream%3E-for-TcpStream" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1265-1275">source</a><a href="#impl-TryFrom%3CTcpStream%3E-for-TcpStream" class="anchor"></a><h3 class="code-header">impl TryFrom<TcpStream> for <a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_from" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/tcp/stream.rs.html#1272-1274">source</a><a href="#method.try_from" class="anchor"></a><h4 class="code-header">fn <a class="fnname">try_from</a>(stream: TcpStream) -> Result<Self, Self::Error></h4></section></summary><div class="docblock"><p>Consumes stream, returning the tokio I/O object.</p> |
| <p>This is equivalent to |
| <a href="struct.TcpStream.html#method.from_std"><code>TcpStream::from_std(stream)</code></a>.</p> |
| </div></details><details class="rustdoc-toggle" open><summary><section id="associatedtype.Error" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Error" class="anchor"></a><h4 class="code-header">type <a class="associatedtype">Error</a> = Error</h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details></div></details></div><h2 id="synthetic-implementations" class="small-section-header">Auto Trait Implementations<a href="#synthetic-implementations" class="anchor"></a></h2><div id="synthetic-implementations-list"><section id="impl-RefUnwindSafe-for-TcpStream" class="impl has-srclink"><a href="#impl-RefUnwindSafe-for-TcpStream" class="anchor"></a><h3 class="code-header">impl RefUnwindSafe for <a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h3></section><section id="impl-Send-for-TcpStream" class="impl has-srclink"><a href="#impl-Send-for-TcpStream" class="anchor"></a><h3 class="code-header">impl Send for <a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h3></section><section id="impl-Sync-for-TcpStream" class="impl has-srclink"><a href="#impl-Sync-for-TcpStream" class="anchor"></a><h3 class="code-header">impl Sync for <a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h3></section><section id="impl-Unpin-for-TcpStream" class="impl has-srclink"><a href="#impl-Unpin-for-TcpStream" class="anchor"></a><h3 class="code-header">impl Unpin for <a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h3></section><section id="impl-UnwindSafe-for-TcpStream" class="impl has-srclink"><a href="#impl-UnwindSafe-for-TcpStream" class="anchor"></a><h3 class="code-header">impl UnwindSafe for <a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a></h3></section></div><h2 id="blanket-implementations" class="small-section-header">Blanket Implementations<a href="#blanket-implementations" class="anchor"></a></h2><div id="blanket-implementations-list"><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Any-for-TcpStream" class="impl has-srclink"><a href="#impl-Any-for-TcpStream" class="anchor"></a><h3 class="code-header">impl<T> Any for T<span class="where fmt-newline">where<br> T: 'static + ?Sized,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.type_id" class="method trait-impl has-srclink"><a href="#method.type_id" class="anchor"></a><h4 class="code-header">fn <a class="fnname">type_id</a>(&self) -> TypeId</h4></section></summary><div class='docblock'>Gets the <code>TypeId</code> of <code>self</code>. <a>Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Borrow%3CT%3E-for-TcpStream" class="impl has-srclink"><a href="#impl-Borrow%3CT%3E-for-TcpStream" class="anchor"></a><h3 class="code-header">impl<T> Borrow<T> for T<span class="where fmt-newline">where<br> T: ?Sized,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.borrow" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/91522" title="Tracking issue for const_borrow">unstable</a></span><a href="#method.borrow" class="anchor"></a><h4 class="code-header">fn <a class="fnname">borrow</a>(&self) -> &T</h4></section></summary><div class='docblock'>Immutably borrows from an owned value. <a>Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-BorrowMut%3CT%3E-for-TcpStream" class="impl has-srclink"><a href="#impl-BorrowMut%3CT%3E-for-TcpStream" class="anchor"></a><h3 class="code-header">impl<T> BorrowMut<T> for T<span class="where fmt-newline">where<br> T: ?Sized,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.borrow_mut" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/91522" title="Tracking issue for const_borrow">unstable</a></span><a href="#method.borrow_mut" class="anchor"></a><h4 class="code-header">fn <a class="fnname">borrow_mut</a>(&mut self) -> &mut T</h4></section></summary><div class='docblock'>Mutably borrows from an owned value. <a>Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-From%3CT%3E-for-TcpStream" class="impl has-srclink"><a href="#impl-From%3CT%3E-for-TcpStream" class="anchor"></a><h3 class="code-header">impl<T> From<T> for T</h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.from" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span><a href="#method.from" class="anchor"></a><h4 class="code-header">fn <a class="fnname">from</a>(t: T) -> T</h4></section></summary><div class="docblock"><p>Returns the argument unchanged.</p> |
| </div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Into%3CU%3E-for-TcpStream" class="impl has-srclink"><a href="#impl-Into%3CU%3E-for-TcpStream" class="anchor"></a><h3 class="code-header">impl<T, U> Into<U> for T<span class="where fmt-newline">where<br> U: From<T>,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.into" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span><a href="#method.into" class="anchor"></a><h4 class="code-header">fn <a class="fnname">into</a>(self) -> U</h4></section></summary><div class="docblock"><p>Calls <code>U::from(self)</code>.</p> |
| <p>That is, this conversion is whatever the implementation of |
| <code>[From]<T> for U</code> chooses to do.</p> |
| </div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-TryFrom%3CU%3E-for-TcpStream" class="impl has-srclink"><a href="#impl-TryFrom%3CU%3E-for-TcpStream" class="anchor"></a><h3 class="code-header">impl<T, U> TryFrom<U> for T<span class="where fmt-newline">where<br> U: Into<T>,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle" open><summary><section id="associatedtype.Error-2" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Error-2" class="anchor"></a><h4 class="code-header">type <a class="associatedtype">Error</a> = Infallible</h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_from-1" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span><a href="#method.try_from-1" class="anchor"></a><h4 class="code-header">fn <a class="fnname">try_from</a>(value: U) -> Result<T, <T as TryFrom<U>>::Error></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-TryInto%3CU%3E-for-TcpStream" class="impl has-srclink"><a href="#impl-TryInto%3CU%3E-for-TcpStream" class="anchor"></a><h3 class="code-header">impl<T, U> TryInto<U> for T<span class="where fmt-newline">where<br> U: TryFrom<T>,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle" open><summary><section id="associatedtype.Error-1" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Error-1" class="anchor"></a><h4 class="code-header">type <a class="associatedtype">Error</a> = <U as TryFrom<T>>::Error</h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_into" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span><a href="#method.try_into" class="anchor"></a><h4 class="code-header">fn <a class="fnname">try_into</a>(self) -> Result<U, <U as TryFrom<T>>::Error></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details></div></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="tokio" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html> |