402 lines
72 KiB
HTML
402 lines
72 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="A TCP socket that has not yet been converted to a `TcpStream` or `TcpListener`."><title>TcpSocket in tokio::net - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="tokio" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../../static.files/storage-e2aeef58.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-044be391.svg"></head><body class="rustdoc struct"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">TcpSocket</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../tokio/index.html">tokio</a><span class="version">1.49.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">TcpSocket</a></h2><h3><a href="#implementations">Methods</a></h3><ul class="block method"><li><a href="#method.bind" title="bind">bind</a></li><li><a href="#method.bind_device" title="bind_device">bind_device</a></li><li><a href="#method.connect" title="connect">connect</a></li><li><a href="#method.device" title="device">device</a></li><li><a href="#method.from_std_stream" title="from_std_stream">from_std_stream</a></li><li><a href="#method.keepalive" title="keepalive">keepalive</a></li><li><a href="#method.linger" title="linger">linger</a></li><li><a href="#method.listen" title="listen">listen</a></li><li><a href="#method.local_addr" title="local_addr">local_addr</a></li><li><a href="#method.new_v4" title="new_v4">new_v4</a></li><li><a href="#method.new_v6" title="new_v6">new_v6</a></li><li><a href="#method.nodelay" title="nodelay">nodelay</a></li><li><a href="#method.recv_buffer_size" title="recv_buffer_size">recv_buffer_size</a></li><li><a href="#method.reuseaddr" title="reuseaddr">reuseaddr</a></li><li><a href="#method.reuseport" title="reuseport">reuseport</a></li><li><a href="#method.send_buffer_size" title="send_buffer_size">send_buffer_size</a></li><li><a href="#method.set_keepalive" title="set_keepalive">set_keepalive</a></li><li><a href="#method.set_linger" title="set_linger">set_linger</a></li><li><a href="#method.set_nodelay" title="set_nodelay">set_nodelay</a></li><li><a href="#method.set_recv_buffer_size" title="set_recv_buffer_size">set_recv_buffer_size</a></li><li><a href="#method.set_reuseaddr" title="set_reuseaddr">set_reuseaddr</a></li><li><a href="#method.set_reuseport" title="set_reuseport">set_reuseport</a></li><li><a href="#method.set_send_buffer_size" title="set_send_buffer_size">set_send_buffer_size</a></li><li><a href="#method.set_tclass_v6" title="set_tclass_v6">set_tclass_v6</a></li><li><a href="#method.set_tos_v4" title="set_tos_v4">set_tos_v4</a></li><li><a href="#method.take_error" title="take_error">take_error</a></li><li><a href="#method.tclass_v6" title="tclass_v6">tclass_v6</a></li><li><a href="#method.tos_v4" title="tos_v4">tos_v4</a></li></ul><h3><a href="#trait-implementations">Trait Implementations</a></h3><ul class="block trait-implementation"><li><a href="#impl-AsFd-for-TcpSocket" title="AsFd">AsFd</a></li><li><a href="#impl-AsRawFd-for-TcpSocket" title="AsRawFd">AsRawFd</a></li><li><a href="#impl-Debug-for-TcpSocket" title="Debug">Debug</a></li><li><a href="#impl-FromRawFd-for-TcpSocket" title="FromRawFd">FromRawFd</a></li><li><a href="#impl-IntoRawFd-for-TcpSocket" title="IntoRawFd">IntoRawFd</a></li></ul><h3><a href="#synthetic-implementations">Auto Trait Implementations</a></h3><ul class="block synthetic-implementation"><li><a href="#impl-Freeze-for-TcpSocket" title="Freeze">Freeze</a></li><li><a href="#impl-RefUnwindSafe-for-TcpSocket" title="RefUnwindSafe">RefUnwindSafe</a></li><li><a href="#impl-Send-for-TcpSocket" title="Send">Send</a></li><li><a href="#impl-Sync-for-TcpSocket" title="Sync">Sync</a></li><li><a href="#impl-Unpin-for-TcpSocket" title="Unpin">Unpin</a></li><li><a href="#impl-UnwindSafe-for-TcpSocket" title="UnwindSafe">UnwindSafe</a></li></ul><h3><a href="#blanket-implementations">Blanket Implementations</a></h3><ul class="block blanket-implementation"><li><a href="#impl-Any-for-T" title="Any">Any</a></li><li><a href="#impl-Borrow%3CT%3E-for-T" title="Borrow<T>">Borrow<T></a></li><li><a href="#impl-BorrowMut%3CT%3E-for-T" title="BorrowMut<T>">BorrowMut<T></a></li><li><a href="#impl-From%3CT%3E-for-T" title="From<T>">From<T></a></li><li><a href="#impl-Into%3CU%3E-for-T" title="Into<U>">Into<U></a></li><li><a href="#impl-TryFrom%3CU%3E-for-T" title="TryFrom<U>">TryFrom<U></a></li><li><a href="#impl-TryInto%3CU%3E-for-T" title="TryInto<U>">TryInto<U></a></li></ul></section><div id="rustdoc-modnav"><h2><a href="index.html">In tokio::<wbr>net</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="../index.html">tokio</a>::<wbr><a href="index.html">net</a></div><h1>Struct <span class="struct">TcpSocket</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../../src/tokio/net/tcp/socket.rs.html#91-93">Source</a> </span></div><pre class="rust item-decl"><code>pub struct TcpSocket { <span class="comment">/* private fields */</span> }</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A TCP socket that has not yet been converted to a <code>TcpStream</code> or
|
||
<code>TcpListener</code>.</p>
|
||
<p><code>TcpSocket</code> wraps an operating system socket and enables the caller to
|
||
configure the socket before establishing a TCP connection or accepting
|
||
inbound connections. The caller is able to set socket option and explicitly
|
||
bind the socket with a socket address.</p>
|
||
<p>The underlying socket is closed when the <code>TcpSocket</code> value is dropped.</p>
|
||
<p><code>TcpSocket</code> should only be used directly if the default configuration used
|
||
by <code>TcpStream::connect</code> and <code>TcpListener::bind</code> does not meet the required
|
||
use case.</p>
|
||
<p>Calling <code>TcpStream::connect("127.0.0.1:8080")</code> is equivalent to:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="kw">let </span>addr = <span class="string">"127.0.0.1:8080"</span>.parse().unwrap();
|
||
|
||
<span class="kw">let </span>socket = TcpSocket::new_v4()<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>stream = socket.connect(addr).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
<p>Calling <code>TcpListener::bind("127.0.0.1:8080")</code> is equivalent to:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="kw">let </span>addr = <span class="string">"127.0.0.1:8080"</span>.parse().unwrap();
|
||
|
||
<span class="kw">let </span>socket = TcpSocket::new_v4()<span class="question-mark">?</span>;
|
||
<span class="comment">// On platforms with Berkeley-derived sockets, this allows to quickly
|
||
// rebind a socket, without needing to wait for the OS to clean up the
|
||
// previous one.
|
||
//
|
||
// On Windows, this allows rebinding sockets which are actively in use,
|
||
// which allows "socket hijacking", so we explicitly don't set it here.
|
||
// https://docs.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse
|
||
</span>socket.set_reuseaddr(<span class="bool-val">true</span>)<span class="question-mark">?</span>;
|
||
socket.bind(addr)<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Note: the actual backlog used by `TcpListener::bind` is platform-dependent,
|
||
// as Tokio relies on Mio's default backlog value configuration. The `1024` here is only
|
||
// illustrative and does not reflect the real value used.
|
||
</span><span class="kw">let </span>listener = socket.listen(<span class="number">1024</span>)<span class="question-mark">?</span>;
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
<p>Setting socket options not explicitly provided by <code>TcpSocket</code> may be done by
|
||
accessing the <code>RawFd</code>/<code>RawSocket</code> using <a href="https://doc.rust-lang.org/std/os/fd/trait.AsRawFd.html"><code>AsRawFd</code></a>/<a href="https://doc.rust-lang.org/std/os/windows/io/trait.AsRawSocket.html"><code>AsRawSocket</code></a> and
|
||
setting the option with a crate like <a href="https://docs.rs/socket2/"><code>socket2</code></a>.</p>
|
||
</div></details><h2 id="implementations" class="section-header">Implementations<a href="#implementations" class="anchor">§</a></h2><div id="implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-TcpSocket" class="impl"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#96-940">Source</a><a href="#impl-TcpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.new_v4" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#126-128">Source</a><h4 class="code-header">pub fn <a href="#method.new_v4" class="fn">new_v4</a>() -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a>></h4></section></summary><div class="docblock"><p>Creates a new socket configured for IPv4.</p>
|
||
<p>Calls <code>socket(2)</code> with <code>AF_INET</code> and <code>SOCK_STREAM</code>.</p>
|
||
<h5 id="returns"><a class="doc-anchor" href="#returns">§</a>Returns</h5>
|
||
<p>On success, the newly created <code>TcpSocket</code> is returned. If an error is
|
||
encountered, it is returned instead.</p>
|
||
<h5 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h5>
|
||
<p>Create a new IPv4 socket and start listening.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="kw">let </span>addr = <span class="string">"127.0.0.1:8080"</span>.parse().unwrap();
|
||
<span class="kw">let </span>socket = TcpSocket::new_v4()<span class="question-mark">?</span>;
|
||
socket.bind(addr)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>listener = socket.listen(<span class="number">128</span>)<span class="question-mark">?</span>;
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.new_v6" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#159-161">Source</a><h4 class="code-header">pub fn <a href="#method.new_v6" class="fn">new_v6</a>() -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a>></h4></section></summary><div class="docblock"><p>Creates a new socket configured for IPv6.</p>
|
||
<p>Calls <code>socket(2)</code> with <code>AF_INET6</code> and <code>SOCK_STREAM</code>.</p>
|
||
<h5 id="returns-1"><a class="doc-anchor" href="#returns-1">§</a>Returns</h5>
|
||
<p>On success, the newly created <code>TcpSocket</code> is returned. If an error is
|
||
encountered, it is returned instead.</p>
|
||
<h5 id="examples-1"><a class="doc-anchor" href="#examples-1">§</a>Examples</h5>
|
||
<p>Create a new IPv6 socket and start listening.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="kw">let </span>addr = <span class="string">"[::1]:8080"</span>.parse().unwrap();
|
||
<span class="kw">let </span>socket = TcpSocket::new_v6()<span class="question-mark">?</span>;
|
||
socket.bind(addr)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>listener = socket.listen(<span class="number">128</span>)<span class="question-mark">?</span>;
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.set_keepalive" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#192-194">Source</a><h4 class="code-header">pub fn <a href="#method.set_keepalive" class="fn">set_keepalive</a>(&self, keepalive: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.bool.html">bool</a>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Sets value for the <code>SO_KEEPALIVE</code> option on this socket.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.keepalive" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#197-199">Source</a><h4 class="code-header">pub fn <a href="#method.keepalive" class="fn">keepalive</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.bool.html">bool</a>></h4></section></summary><div class="docblock"><p>Gets the value of the <code>SO_KEEPALIVE</code> option on this socket.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.set_reuseaddr" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#227-229">Source</a><h4 class="code-header">pub fn <a href="#method.set_reuseaddr" class="fn">set_reuseaddr</a>(&self, reuseaddr: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.bool.html">bool</a>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Allows the socket to bind to an in-use address.</p>
|
||
<p>Behavior is platform specific. Refer to the target platform’s
|
||
documentation for more details.</p>
|
||
<h5 id="examples-2"><a class="doc-anchor" href="#examples-2">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="kw">let </span>addr = <span class="string">"127.0.0.1:8080"</span>.parse().unwrap();
|
||
|
||
<span class="kw">let </span>socket = TcpSocket::new_v4()<span class="question-mark">?</span>;
|
||
socket.set_reuseaddr(<span class="bool-val">true</span>)<span class="question-mark">?</span>;
|
||
socket.bind(addr)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>listener = socket.listen(<span class="number">1024</span>)<span class="question-mark">?</span>;
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.reuseaddr" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#253-255">Source</a><h4 class="code-header">pub fn <a href="#method.reuseaddr" class="fn">reuseaddr</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.bool.html">bool</a>></h4></section></summary><div class="docblock"><p>Retrieves the value set for <code>SO_REUSEADDR</code> on this socket.</p>
|
||
<h5 id="examples-3"><a class="doc-anchor" href="#examples-3">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="kw">let </span>addr = <span class="string">"127.0.0.1:8080"</span>.parse().unwrap();
|
||
|
||
<span class="kw">let </span>socket = TcpSocket::new_v4()<span class="question-mark">?</span>;
|
||
socket.set_reuseaddr(<span class="bool-val">true</span>)<span class="question-mark">?</span>;
|
||
<span class="macro">assert!</span>(socket.reuseaddr().unwrap());
|
||
socket.bind(addr)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>listener = socket.listen(<span class="number">1024</span>)<span class="question-mark">?</span>;
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.set_reuseport" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#297-299">Source</a><h4 class="code-header">pub fn <a href="#method.set_reuseport" class="fn">set_reuseport</a>(&self, reuseport: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.bool.html">bool</a>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Allows the socket to bind to an in-use port. Only available for unix systems
|
||
(excluding Solaris, Illumos, and Cygwin).</p>
|
||
<p>Behavior is platform specific. Refer to the target platform’s
|
||
documentation for more details.</p>
|
||
<h5 id="examples-4"><a class="doc-anchor" href="#examples-4">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="kw">let </span>addr = <span class="string">"127.0.0.1:8080"</span>.parse().unwrap();
|
||
|
||
<span class="kw">let </span>socket = TcpSocket::new_v4()<span class="question-mark">?</span>;
|
||
socket.set_reuseport(<span class="bool-val">true</span>)<span class="question-mark">?</span>;
|
||
socket.bind(addr)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>listener = socket.listen(<span class="number">1024</span>)<span class="question-mark">?</span>;
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.reuseport" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#342-344">Source</a><h4 class="code-header">pub fn <a href="#method.reuseport" class="fn">reuseport</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.bool.html">bool</a>></h4></section></summary><div class="docblock"><p>Allows the socket to bind to an in-use port. Only available for unix systems
|
||
(excluding Solaris, Illumos, and Cygwin).</p>
|
||
<p>Behavior is platform specific. Refer to the target platform’s
|
||
documentation for more details.</p>
|
||
<h5 id="examples-5"><a class="doc-anchor" href="#examples-5">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="kw">let </span>addr = <span class="string">"127.0.0.1:8080"</span>.parse().unwrap();
|
||
|
||
<span class="kw">let </span>socket = TcpSocket::new_v4()<span class="question-mark">?</span>;
|
||
socket.set_reuseport(<span class="bool-val">true</span>)<span class="question-mark">?</span>;
|
||
<span class="macro">assert!</span>(socket.reuseport().unwrap());
|
||
socket.bind(addr)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>listener = socket.listen(<span class="number">1024</span>)<span class="question-mark">?</span>;
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.set_send_buffer_size" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#349-351">Source</a><h4 class="code-header">pub fn <a href="#method.set_send_buffer_size" class="fn">set_send_buffer_size</a>(&self, size: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Sets the size of the TCP send buffer on this socket.</p>
|
||
<p>On most operating systems, this sets the <code>SO_SNDBUF</code> socket option.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.send_buffer_size" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#376-378">Source</a><h4 class="code-header">pub fn <a href="#method.send_buffer_size" class="fn">send_buffer_size</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>></h4></section></summary><div class="docblock"><p>Returns the size of the TCP send buffer for this socket.</p>
|
||
<p>On most operating systems, this is the value of the <code>SO_SNDBUF</code> socket
|
||
option.</p>
|
||
<p>Note that if <a href="#method.set_send_buffer_size"><code>set_send_buffer_size</code></a> has been called on this socket
|
||
previously, the value returned by this function may not be the same as
|
||
the argument provided to <code>set_send_buffer_size</code>. This is for the
|
||
following reasons:</p>
|
||
<ul>
|
||
<li>Most operating systems have minimum and maximum allowed sizes for the
|
||
send buffer, and will clamp the provided value if it is below the
|
||
minimum or above the maximum. The minimum and maximum buffer sizes are
|
||
OS-dependent.</li>
|
||
<li>Linux will double the buffer size to account for internal bookkeeping
|
||
data, and returns the doubled value from <code>getsockopt(2)</code>. As per <code>man 7 socket</code>:
|
||
<blockquote>
|
||
<p>Sets or gets the maximum socket send buffer in bytes. The
|
||
kernel doubles this value (to allow space for bookkeeping
|
||
overhead) when it is set using <code>setsockopt(2)</code>, and this doubled
|
||
value is returned by <code>getsockopt(2)</code>.</p>
|
||
</blockquote>
|
||
</li>
|
||
</ul>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.set_recv_buffer_size" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#383-385">Source</a><h4 class="code-header">pub fn <a href="#method.set_recv_buffer_size" class="fn">set_recv_buffer_size</a>(&self, size: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Sets the size of the TCP receive buffer on this socket.</p>
|
||
<p>On most operating systems, this sets the <code>SO_RCVBUF</code> socket option.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.recv_buffer_size" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#410-412">Source</a><h4 class="code-header">pub fn <a href="#method.recv_buffer_size" class="fn">recv_buffer_size</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>></h4></section></summary><div class="docblock"><p>Returns the size of the TCP receive buffer for this socket.</p>
|
||
<p>On most operating systems, this is the value of the <code>SO_RCVBUF</code> socket
|
||
option.</p>
|
||
<p>Note that if <a href="#method.set_recv_buffer_size"><code>set_recv_buffer_size</code></a> has been called on this socket
|
||
previously, the value returned by this function may not be the same as
|
||
the argument provided to <code>set_recv_buffer_size</code>. This is for the
|
||
following reasons:</p>
|
||
<ul>
|
||
<li>Most operating systems have minimum and maximum allowed sizes for the
|
||
receive buffer, and will clamp the provided value if it is below the
|
||
minimum or above the maximum. The minimum and maximum buffer sizes are
|
||
OS-dependent.</li>
|
||
<li>Linux will double the buffer size to account for internal bookkeeping
|
||
data, and returns the doubled value from <code>getsockopt(2)</code>. As per <code>man 7 socket</code>:
|
||
<blockquote>
|
||
<p>Sets or gets the maximum socket send buffer in bytes. The
|
||
kernel doubles this value (to allow space for bookkeeping
|
||
overhead) when it is set using <code>setsockopt(2)</code>, and this doubled
|
||
value is returned by <code>getsockopt(2)</code>.</p>
|
||
</blockquote>
|
||
</li>
|
||
</ul>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.set_linger" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#433-435">Source</a><h4 class="code-header">pub fn <a href="#method.set_linger" class="fn">set_linger</a>(&self, dur: <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="struct" href="https://doc.rust-lang.org/1.93.1/core/time/struct.Duration.html" title="struct core::time::Duration">Duration</a>>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.unit.html">()</a>></h4></section><span class="item-info"><div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated: <code>SO_LINGER</code> causes the socket to block the thread on drop</span></div></span></summary><div class="docblock"><p>Sets the linger duration of this socket by setting the <code>SO_LINGER</code> option.</p>
|
||
<p>This option controls the action taken when a stream has unsent messages and the stream is
|
||
closed. If <code>SO_LINGER</code> is set, the system shall block the process until it can transmit the
|
||
data or until the time expires.</p>
|
||
<p>If <code>SO_LINGER</code> is not specified, and the socket is closed, the system handles the call in a
|
||
way that allows the process to continue as quickly as possible.</p>
|
||
<p>This option is deprecated because setting <code>SO_LINGER</code> on a socket used with Tokio is always
|
||
incorrect as it leads to blocking the thread when the socket is closed. For more details,
|
||
please see:</p>
|
||
<blockquote>
|
||
<p>Volumes of communications have been devoted to the intricacies of <code>SO_LINGER</code> versus
|
||
non-blocking (<code>O_NONBLOCK</code>) sockets. From what I can tell, the final word is: don’t do
|
||
it. Rely on the <code>shutdown()</code>-followed-by-<code>read()</code>-eof technique instead.</p>
|
||
<p>From <a href="https://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable">The ultimate <code>SO_LINGER</code> page, or: why is my tcp not reliable</a></p>
|
||
</blockquote>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.linger" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#443-445">Source</a><h4 class="code-header">pub fn <a href="#method.linger" class="fn">linger</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="struct" href="https://doc.rust-lang.org/1.93.1/core/time/struct.Duration.html" title="struct core::time::Duration">Duration</a>>></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.TcpSocket.html#method.set_linger" title="method tokio::net::TcpSocket::set_linger"><code>set_linger</code></a>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.set_nodelay" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#466-468">Source</a><h4 class="code-header">pub fn <a href="#method.set_nodelay" class="fn">set_nodelay</a>(&self, nodelay: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.bool.html">bool</a>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.unit.html">()</a>></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-6"><a class="doc-anchor" href="#examples-6">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
|
||
<span class="kw">let </span>socket = TcpSocket::new_v4()<span class="question-mark">?</span>;
|
||
|
||
socket.set_nodelay(<span class="bool-val">true</span>)<span class="question-mark">?</span>;</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.nodelay" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#488-490">Source</a><h4 class="code-header">pub fn <a href="#method.nodelay" class="fn">nodelay</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.bool.html">bool</a>></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.TcpSocket.html#method.set_nodelay" title="method tokio::net::TcpSocket::set_nodelay"><code>set_nodelay</code></a>.</p>
|
||
<h5 id="examples-7"><a class="doc-anchor" href="#examples-7">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
|
||
<span class="kw">let </span>socket = TcpSocket::new_v4()<span class="question-mark">?</span>;
|
||
|
||
<span class="macro">println!</span>(<span class="string">"{:?}"</span>, socket.nodelay()<span class="question-mark">?</span>);</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.tclass_v6" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#523-525">Source</a><h4 class="code-header">pub fn <a href="#method.tclass_v6" class="fn">tclass_v6</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>></h4></section></summary><div class="docblock"><p>Gets the value of the <code>IPV6_TCLASS</code> option for this socket.</p>
|
||
<p>For more information about this option, see <a href="struct.TcpSocket.html#method.set_tclass_v6" title="method tokio::net::TcpSocket::set_tclass_v6"><code>set_tclass_v6</code></a>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.set_tclass_v6" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#561-563">Source</a><h4 class="code-header">pub fn <a href="#method.set_tclass_v6" class="fn">set_tclass_v6</a>(&self, tclass: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Sets the value for the <code>IPV6_TCLASS</code> option on this socket.</p>
|
||
<p>Specifies the traffic class field that is used in every packet
|
||
sent from this socket.</p>
|
||
<h5 id="note"><a class="doc-anchor" href="#note">§</a>Note</h5>
|
||
<p>This may not have any effect on IPv4 sockets.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.tos_v4" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#588-590">Source</a><h4 class="code-header">pub fn <a href="#method.tos_v4" class="fn">tos_v4</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>></h4></section></summary><div class="docblock"><p>Gets the value of the <code>IP_TOS</code> option for this socket.</p>
|
||
<p>For more information about this option, see <a href="struct.TcpSocket.html#method.set_tos_v4" title="method tokio::net::TcpSocket::set_tos_v4"><code>set_tos_v4</code></a>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.set_tos_v4" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#648-650">Source</a><h4 class="code-header">pub fn <a href="#method.set_tos_v4" class="fn">set_tos_v4</a>(&self, tos: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Sets the value for the <code>IP_TOS</code> option on this socket.</p>
|
||
<p>This value sets the type-of-service field that is used in every packet
|
||
sent from this socket.</p>
|
||
<h5 id="note-1"><a class="doc-anchor" href="#note-1">§</a>Note</h5>
|
||
<ul>
|
||
<li>This may not have any effect on IPv6 sockets.</li>
|
||
<li>On Windows, <code>IP_TOS</code> is only supported on <a href="https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options">Windows 8+ or
|
||
Windows Server 2012+.</a></li>
|
||
</ul>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.device" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#688-690">Source</a><h4 class="code-header">pub fn <a href="#method.device" class="fn">device</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="struct" href="https://doc.rust-lang.org/1.93.1/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u8.html">u8</a>>>></h4></section></summary><div class="docblock"><p>Gets the value for the <code>SO_BINDTODEVICE</code> option on this socket</p>
|
||
<p>This value gets the socket binded device’s interface name.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.bind_device" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#704-706">Source</a><h4 class="code-header">pub fn <a href="#method.bind_device" class="fn">bind_device</a>(&self, interface: <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&[<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u8.html">u8</a>]>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Sets the value for the <code>SO_BINDTODEVICE</code> option on this socket</p>
|
||
<p>If a socket is bound to an interface, only packets received from that
|
||
particular interface are processed by the socket. Note that this only
|
||
works for some socket types, particularly <code>AF_INET</code> sockets.</p>
|
||
<p>If <code>interface</code> is <code>None</code> or an empty string it removes the binding.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.local_addr" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#730-732">Source</a><h4 class="code-header">pub fn <a href="#method.local_addr" class="fn">local_addr</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/1.93.1/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>></h4></section></summary><div class="docblock"><p>Gets the local address of this socket.</p>
|
||
<p>Will fail on windows if called before <code>bind</code>.</p>
|
||
<h5 id="examples-8"><a class="doc-anchor" href="#examples-8">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="kw">let </span>addr = <span class="string">"127.0.0.1:8080"</span>.parse().unwrap();
|
||
|
||
<span class="kw">let </span>socket = TcpSocket::new_v4()<span class="question-mark">?</span>;
|
||
socket.bind(addr)<span class="question-mark">?</span>;
|
||
<span class="macro">assert_eq!</span>(socket.local_addr().unwrap().to_string(), <span class="string">"127.0.0.1:8080"</span>);
|
||
<span class="kw">let </span>listener = socket.listen(<span class="number">1024</span>)<span class="question-mark">?</span>;
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.take_error" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#735-737">Source</a><h4 class="code-header">pub fn <a href="#method.take_error" class="fn">take_error</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="struct" href="https://doc.rust-lang.org/1.93.1/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>>></h4></section></summary><div class="docblock"><p>Returns the value of the <code>SO_ERROR</code> option.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.bind" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#767-769">Source</a><h4 class="code-header">pub fn <a href="#method.bind" class="fn">bind</a>(&self, addr: <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Binds the socket to the given address.</p>
|
||
<p>This calls the <code>bind(2)</code> operating-system function. Behavior is
|
||
platform specific. Refer to the target platform’s documentation for more
|
||
details.</p>
|
||
<h5 id="examples-9"><a class="doc-anchor" href="#examples-9">§</a>Examples</h5>
|
||
<p>Bind a socket before listening.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="kw">let </span>addr = <span class="string">"127.0.0.1:8080"</span>.parse().unwrap();
|
||
|
||
<span class="kw">let </span>socket = TcpSocket::new_v4()<span class="question-mark">?</span>;
|
||
socket.bind(addr)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>listener = socket.listen(<span class="number">1024</span>)<span class="question-mark">?</span>;
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.connect" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#803-831">Source</a><h4 class="code-header">pub async fn <a href="#method.connect" class="fn">connect</a>(self, addr: <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="struct.TcpStream.html" title="struct tokio::net::TcpStream">TcpStream</a>></h4></section></summary><div class="docblock"><p>Establishes a TCP connection with a peer at the specified socket address.</p>
|
||
<p>The <code>TcpSocket</code> is consumed. Once the connection is established, a
|
||
connected <a href="struct.TcpStream.html" title="struct tokio::net::TcpStream"><code>TcpStream</code></a> is returned. If the connection fails, the
|
||
encountered error is returned.</p>
|
||
<p>This calls the <code>connect(2)</code> operating-system function. Behavior is
|
||
platform specific. Refer to the target platform’s documentation for more
|
||
details.</p>
|
||
<h5 id="examples-10"><a class="doc-anchor" href="#examples-10">§</a>Examples</h5>
|
||
<p>Connecting to a peer.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="kw">let </span>addr = <span class="string">"127.0.0.1:8080"</span>.parse().unwrap();
|
||
|
||
<span class="kw">let </span>socket = TcpSocket::new_v4()<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>stream = socket.connect(addr).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.listen" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#868-887">Source</a><h4 class="code-header">pub fn <a href="#method.listen" class="fn">listen</a>(self, backlog: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="struct.TcpListener.html" title="struct tokio::net::TcpListener">TcpListener</a>></h4></section></summary><div class="docblock"><p>Converts the socket into a <code>TcpListener</code>.</p>
|
||
<p><code>backlog</code> defines the maximum number of pending connections are queued
|
||
by the operating system at any given time. Connection are removed from
|
||
the queue with <a href="struct.TcpListener.html#method.accept" title="method tokio::net::TcpListener::accept"><code>TcpListener::accept</code></a>. When the queue is full, the
|
||
operating-system will start rejecting connections.</p>
|
||
<p>This calls the <code>listen(2)</code> operating-system function, marking the socket
|
||
as a passive socket. Behavior is platform specific. Refer to the target
|
||
platform’s documentation for more details.</p>
|
||
<h5 id="examples-11"><a class="doc-anchor" href="#examples-11">§</a>Examples</h5>
|
||
<p>Create a <code>TcpListener</code>.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="kw">let </span>addr = <span class="string">"127.0.0.1:8080"</span>.parse().unwrap();
|
||
|
||
<span class="kw">let </span>socket = TcpSocket::new_v4()<span class="question-mark">?</span>;
|
||
socket.bind(addr)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>listener = socket.listen(<span class="number">1024</span>)<span class="question-mark">?</span>;
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.from_std_stream" class="method"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#923-939">Source</a><h4 class="code-header">pub fn <a href="#method.from_std_stream" class="fn">from_std_stream</a>(std_stream: <a class="struct" href="https://doc.rust-lang.org/1.93.1/std/net/tcp/struct.TcpStream.html" title="struct std::net::tcp::TcpStream">TcpStream</a>) -> <a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a></h4></section></summary><div class="docblock"><p>Converts a <a href="https://doc.rust-lang.org/1.93.1/std/net/tcp/struct.TcpStream.html" title="struct std::net::tcp::TcpStream"><code>std::net::TcpStream</code></a> into a <code>TcpSocket</code>. The provided
|
||
socket must not have been connected prior to calling this function. This
|
||
function is typically used together with crates such as <a href="https://docs.rs/socket2/"><code>socket2</code></a> to
|
||
configure socket options that are not available on <code>TcpSocket</code>.</p>
|
||
<h5 id="notes"><a class="doc-anchor" href="#notes">§</a>Notes</h5>
|
||
<p>The caller is responsible for ensuring that the socket is in
|
||
non-blocking mode. Otherwise all I/O operations on the socket
|
||
will block the thread, which will cause unexpected behavior.
|
||
Non-blocking mode can be set using <a href="https://doc.rust-lang.org/1.93.1/std/net/tcp/struct.TcpStream.html#method.set_nonblocking" title="method std::net::tcp::TcpStream::set_nonblocking"><code>set_nonblocking</code></a>.</p>
|
||
<h5 id="examples-12"><a class="doc-anchor" href="#examples-12">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::TcpSocket;
|
||
<span class="kw">use </span>socket2::{Domain, Socket, Type};
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> std::io::Result<()> {
|
||
<span class="kw">let </span>socket2_socket = Socket::new(Domain::IPV4, Type::STREAM, <span class="prelude-val">None</span>)<span class="question-mark">?</span>;
|
||
socket2_socket.set_nonblocking(<span class="bool-val">true</span>)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>socket = TcpSocket::from_std_stream(socket2_socket.into());
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div></div></details></div></details></div><h2 id="trait-implementations" class="section-header">Trait Implementations<a href="#trait-implementations" class="anchor">§</a></h2><div id="trait-implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-AsFd-for-TcpSocket" class="impl"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#968-972">Source</a><a href="#impl-AsFd-for-TcpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.93.1/std/os/fd/owned/trait.AsFd.html" title="trait std::os::fd::owned::AsFd">AsFd</a> for <a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a></h3><span class="item-info"><div class="stab portability">Available on <strong><code>docsrs</code>, or Unix</strong> only.</div></span></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.as_fd" class="method trait-impl"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#969-971">Source</a><a href="#method.as_fd" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/std/os/fd/owned/trait.AsFd.html#tymethod.as_fd" class="fn">as_fd</a>(&self) -> <a class="struct" href="https://doc.rust-lang.org/1.93.1/std/os/fd/owned/struct.BorrowedFd.html" title="struct std::os::fd::owned::BorrowedFd">BorrowedFd</a><'_></h4></section></summary><div class='docblock'>Borrows the file descriptor. <a href="https://doc.rust-lang.org/1.93.1/std/os/fd/owned/trait.AsFd.html#tymethod.as_fd">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-AsRawFd-for-TcpSocket" class="impl"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#962-966">Source</a><a href="#impl-AsRawFd-for-TcpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.93.1/std/os/fd/raw/trait.AsRawFd.html" title="trait std::os::fd::raw::AsRawFd">AsRawFd</a> for <a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a></h3><span class="item-info"><div class="stab portability">Available on <strong><code>docsrs</code>, or Unix</strong> only.</div></span></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.as_raw_fd" class="method trait-impl"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#963-965">Source</a><a href="#method.as_raw_fd" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/std/os/fd/raw/trait.AsRawFd.html#tymethod.as_raw_fd" class="fn">as_raw_fd</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/os/fd/raw/type.RawFd.html" title="type std::os::fd::raw::RawFd">RawFd</a></h4></section></summary><div class='docblock'>Extracts the raw file descriptor. <a href="https://doc.rust-lang.org/1.93.1/std/os/fd/raw/trait.AsRawFd.html#tymethod.as_raw_fd">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-Debug-for-TcpSocket" class="impl"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#952-956">Source</a><a href="#impl-Debug-for-TcpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a> for <a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.fmt" class="method trait-impl"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#953-955">Source</a><a href="#method.fmt" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Debug.html#tymethod.fmt" class="fn">fmt</a>(&self, fmt: &mut <a class="struct" href="https://doc.rust-lang.org/1.93.1/core/fmt/struct.Formatter.html" title="struct core::fmt::Formatter">Formatter</a><'_>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/core/fmt/type.Result.html" title="type core::fmt::Result">Result</a></h4></section></summary><div class='docblock'>Formats the value using the given formatter. <a href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Debug.html#tymethod.fmt">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-FromRawFd-for-TcpSocket" class="impl"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#974-987">Source</a><a href="#impl-FromRawFd-for-TcpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.93.1/std/os/fd/raw/trait.FromRawFd.html" title="trait std::os::fd::raw::FromRawFd">FromRawFd</a> for <a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a></h3><span class="item-info"><div class="stab portability">Available on <strong><code>docsrs</code>, or Unix</strong> only.</div></span></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from_raw_fd" class="method trait-impl"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#981-986">Source</a><a href="#method.from_raw_fd" class="anchor">§</a><h4 class="code-header">unsafe fn <a href="https://doc.rust-lang.org/1.93.1/std/os/fd/raw/trait.FromRawFd.html#tymethod.from_raw_fd" class="fn">from_raw_fd</a>(fd: <a class="type" href="https://doc.rust-lang.org/1.93.1/std/os/fd/raw/type.RawFd.html" title="type std::os::fd::raw::RawFd">RawFd</a>) -> <a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a></h4></section></summary><div class="docblock"><p>Converts a <code>RawFd</code> to a <code>TcpSocket</code>.</p>
|
||
<h5 id="notes-1"><a class="doc-anchor" href="#notes-1">§</a>Notes</h5>
|
||
<p>The caller is responsible for ensuring that the socket is in
|
||
non-blocking mode.</p>
|
||
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-IntoRawFd-for-TcpSocket" class="impl"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#989-993">Source</a><a href="#impl-IntoRawFd-for-TcpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.93.1/std/os/fd/raw/trait.IntoRawFd.html" title="trait std::os::fd::raw::IntoRawFd">IntoRawFd</a> for <a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a></h3><span class="item-info"><div class="stab portability">Available on <strong><code>docsrs</code>, or Unix</strong> only.</div></span></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.into_raw_fd" class="method trait-impl"><a class="src rightside" href="../../src/tokio/net/tcp/socket.rs.html#990-992">Source</a><a href="#method.into_raw_fd" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/std/os/fd/raw/trait.IntoRawFd.html#tymethod.into_raw_fd" class="fn">into_raw_fd</a>(self) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/std/os/fd/raw/type.RawFd.html" title="type std::os::fd::raw::RawFd">RawFd</a></h4></section></summary><div class='docblock'>Consumes this object, returning the raw underlying file descriptor. <a href="https://doc.rust-lang.org/1.93.1/std/os/fd/raw/trait.IntoRawFd.html#tymethod.into_raw_fd">Read more</a></div></details></div></details></div><h2 id="synthetic-implementations" class="section-header">Auto Trait Implementations<a href="#synthetic-implementations" class="anchor">§</a></h2><div id="synthetic-implementations-list"><section id="impl-Freeze-for-TcpSocket" class="impl"><a href="#impl-Freeze-for-TcpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Freeze.html" title="trait core::marker::Freeze">Freeze</a> for <a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a></h3></section><section id="impl-RefUnwindSafe-for-TcpSocket" class="impl"><a href="#impl-RefUnwindSafe-for-TcpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/panic/unwind_safe/trait.RefUnwindSafe.html" title="trait core::panic::unwind_safe::RefUnwindSafe">RefUnwindSafe</a> for <a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a></h3></section><section id="impl-Send-for-TcpSocket" class="impl"><a href="#impl-Send-for-TcpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> for <a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a></h3></section><section id="impl-Sync-for-TcpSocket" class="impl"><a href="#impl-Sync-for-TcpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> for <a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a></h3></section><section id="impl-Unpin-for-TcpSocket" class="impl"><a href="#impl-Unpin-for-TcpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> for <a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a></h3></section><section id="impl-UnwindSafe-for-TcpSocket" class="impl"><a href="#impl-UnwindSafe-for-TcpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/panic/unwind_safe/trait.UnwindSafe.html" title="trait core::panic::unwind_safe::UnwindSafe">UnwindSafe</a> for <a class="struct" href="struct.TcpSocket.html" title="struct tokio::net::TcpSocket">TcpSocket</a></h3></section></div><h2 id="blanket-implementations" class="section-header">Blanket Implementations<a href="#blanket-implementations" class="anchor">§</a></h2><div id="blanket-implementations-list"><details class="toggle implementors-toggle"><summary><section id="impl-Any-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/any.rs.html#138">Source</a><a href="#impl-Any-for-T" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/any/trait.Any.html" title="trait core::any::Any">Any</a> for T<div class="where">where
|
||
T: 'static + ?<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.type_id" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/any.rs.html#139">Source</a><a href="#method.type_id" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/any/trait.Any.html#tymethod.type_id" class="fn">type_id</a>(&self) -> <a class="struct" href="https://doc.rust-lang.org/1.93.1/core/any/struct.TypeId.html" title="struct core::any::TypeId">TypeId</a></h4></section></summary><div class='docblock'>Gets the <code>TypeId</code> of <code>self</code>. <a href="https://doc.rust-lang.org/1.93.1/core/any/trait.Any.html#tymethod.type_id">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Borrow%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/borrow.rs.html#212">Source</a><a href="#impl-Borrow%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/borrow/trait.Borrow.html" title="trait core::borrow::Borrow">Borrow</a><T> for T<div class="where">where
|
||
T: ?<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/borrow.rs.html#214">Source</a><a href="#method.borrow" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/borrow/trait.Borrow.html#tymethod.borrow" class="fn">borrow</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&T</a></h4></section></summary><div class='docblock'>Immutably borrows from an owned value. <a href="https://doc.rust-lang.org/1.93.1/core/borrow/trait.Borrow.html#tymethod.borrow">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-BorrowMut%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/borrow.rs.html#221">Source</a><a href="#impl-BorrowMut%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/borrow/trait.BorrowMut.html" title="trait core::borrow::BorrowMut">BorrowMut</a><T> for T<div class="where">where
|
||
T: ?<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow_mut" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/borrow.rs.html#222">Source</a><a href="#method.borrow_mut" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut" class="fn">borrow_mut</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&mut T</a></h4></section></summary><div class='docblock'>Mutably borrows from an owned value. <a href="https://doc.rust-lang.org/1.93.1/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-From%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#785">Source</a><a href="#impl-From%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.From.html" title="trait core::convert::From">From</a><T> for T</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#788">Source</a><a href="#method.from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/convert/trait.From.html#tymethod.from" class="fn">from</a>(t: T) -> T</h4></section></summary><div class="docblock"><p>Returns the argument unchanged.</p>
|
||
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Into%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#767-769">Source</a><a href="#impl-Into%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T, U> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a><U> for T<div class="where">where
|
||
U: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.From.html" title="trait core::convert::From">From</a><T>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#777">Source</a><a href="#method.into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/convert/trait.Into.html#tymethod.into" class="fn">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><a href="https://doc.rust-lang.org/1.93.1/core/convert/trait.From.html" title="trait core::convert::From">From</a><T> for U</code> chooses to do.</p>
|
||
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryFrom%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#827-829">Source</a><a href="#impl-TryFrom%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T, U> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><U> for T<div class="where">where
|
||
U: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a><T>,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error-1" class="associatedtype trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#831">Source</a><a href="#associatedtype.Error-1" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html#associatedtype.Error" class="associatedtype">Error</a> = <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/convert/enum.Infallible.html" title="enum core::convert::Infallible">Infallible</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_from" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#834">Source</a><a href="#method.try_from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html#tymethod.try_from" class="fn">try_from</a>(value: U) -> <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <T as <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><U>>::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryInto%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#811-813">Source</a><a href="#impl-TryInto%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T, U> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryInto.html" title="trait core::convert::TryInto">TryInto</a><U> for T<div class="where">where
|
||
U: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error" class="associatedtype trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#815">Source</a><a href="#associatedtype.Error" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryInto.html#associatedtype.Error" class="associatedtype">Error</a> = <U as <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>>::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#818">Source</a><a href="#method.try_into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryInto.html#tymethod.try_into" class="fn">try_into</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a><U, <U as <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>>::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details></div></section></div></main></body></html> |