mirror of
https://github.com/n0-computer/iroh.git
synced 2026-11-30 08:01:44 +00:00
1050 lines
143 KiB
HTML
1050 lines
143 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="Wrapper around a tokio UDP socket that handles the fact that on drop `libc::close` can block for UDP sockets."><title>UdpSocket in netwatch - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-b21aa549bf6d91ff.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="netwatch" data-themes="" data-resource-suffix="" data-rustdoc-version="1.80.0-nightly (c987ad527 2024-05-01)" data-channel="nightly" data-search-js="search-bf21c90c8c1d92b1.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-e32f0c247825364d.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-09095024cf37855e.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc 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" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../netwatch/index.html">netwatch</a><span class="version">0.1.0</span></h2></div><h2 class="location"><a href="#">UdpSocket</a></h2><div class="sidebar-elems"><section><h3><a href="#implementations">Methods</a></h3><ul class="block method"><li><a href="#method.bind">bind</a></li><li><a href="#method.bind_full">bind_full</a></li><li><a href="#method.bind_local">bind_local</a></li><li><a href="#method.bind_local_v4">bind_local_v4</a></li><li><a href="#method.bind_local_v6">bind_local_v6</a></li><li><a href="#method.bind_v4">bind_v4</a></li><li><a href="#method.bind_v6">bind_v6</a></li></ul><h3><a href="#deref-methods-UdpSocket">Methods from Deref<Target=UdpSocket></a></h3><ul class="block deref-methods"><li><a href="#method.async_io">async_io</a></li><li><a href="#method.bind_device">bind_device</a></li><li><a href="#method.broadcast">broadcast</a></li><li><a href="#method.connect">connect</a></li><li><a href="#method.device">device</a></li><li><a href="#method.join_multicast_v4">join_multicast_v4</a></li><li><a href="#method.join_multicast_v6">join_multicast_v6</a></li><li><a href="#method.leave_multicast_v4">leave_multicast_v4</a></li><li><a href="#method.leave_multicast_v6">leave_multicast_v6</a></li><li><a href="#method.local_addr">local_addr</a></li><li><a href="#method.multicast_loop_v4">multicast_loop_v4</a></li><li><a href="#method.multicast_loop_v6">multicast_loop_v6</a></li><li><a href="#method.multicast_ttl_v4">multicast_ttl_v4</a></li><li><a href="#method.peek_from">peek_from</a></li><li><a href="#method.peek_sender">peek_sender</a></li><li><a href="#method.peer_addr">peer_addr</a></li><li><a href="#method.poll_peek_from">poll_peek_from</a></li><li><a href="#method.poll_peek_sender">poll_peek_sender</a></li><li><a href="#method.poll_recv">poll_recv</a></li><li><a href="#method.poll_recv_from">poll_recv_from</a></li><li><a href="#method.poll_recv_ready">poll_recv_ready</a></li><li><a href="#method.poll_send">poll_send</a></li><li><a href="#method.poll_send_ready">poll_send_ready</a></li><li><a href="#method.poll_send_to">poll_send_to</a></li><li><a href="#method.readable">readable</a></li><li><a href="#method.ready">ready</a></li><li><a href="#method.recv">recv</a></li><li><a href="#method.recv_buf">recv_buf</a></li><li><a href="#method.recv_buf_from">recv_buf_from</a></li><li><a href="#method.recv_from">recv_from</a></li><li><a href="#method.send">send</a></li><li><a href="#method.send_to">send_to</a></li><li><a href="#method.set_broadcast">set_broadcast</a></li><li><a href="#method.set_multicast_loop_v4">set_multicast_loop_v4</a></li><li><a href="#method.set_multicast_loop_v6">set_multicast_loop_v6</a></li><li><a href="#method.set_multicast_ttl_v4">set_multicast_ttl_v4</a></li><li><a href="#method.set_tos">set_tos</a></li><li><a href="#method.set_ttl">set_ttl</a></li><li><a href="#method.take_error">take_error</a></li><li><a href="#method.tos">tos</a></li><li><a href="#method.try_io">try_io</a></li><li><a href="#method.try_peek_from">try_peek_from</a></li><li><a href="#method.try_peek_sender">try_peek_sender</a></li><li><a href="#method.try_recv">try_recv</a></li><li><a href="#method.try_recv_buf">try_recv_buf</a></li><li><a href="#method.try_recv_buf_from">try_recv_buf_from</a></li><li><a href="#method.try_recv_from">try_recv_from</a></li><li><a href="#method.try_send">try_send</a></li><li><a href="#method.try_send_to">try_send_to</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 trait-implementation"><li><a href="#impl-AsFd-for-UdpSocket">AsFd</a></li><li><a href="#impl-Debug-for-UdpSocket">Debug</a></li><li><a href="#impl-Deref-for-UdpSocket">Deref</a></li><li><a href="#impl-Drop-for-UdpSocket">Drop</a></li><li><a href="#impl-From%3CUdpSocket%3E-for-UdpSocket">From<UdpSocket></a></li></ul><h3><a href="#synthetic-implementations">Auto Trait Implementations</a></h3><ul class="block synthetic-implementation"><li><a href="#impl-Freeze-for-UdpSocket">!Freeze</a></li><li><a href="#impl-RefUnwindSafe-for-UdpSocket">RefUnwindSafe</a></li><li><a href="#impl-Send-for-UdpSocket">Send</a></li><li><a href="#impl-Sync-for-UdpSocket">Sync</a></li><li><a href="#impl-Unpin-for-UdpSocket">Unpin</a></li><li><a href="#impl-UnwindSafe-for-UdpSocket">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">Any</a></li><li><a href="#impl-Borrow%3CT%3E-for-T">Borrow<T></a></li><li><a href="#impl-BorrowMut%3CT%3E-for-T">BorrowMut<T></a></li><li><a href="#impl-From%3CT%3E-for-T">From<T></a></li><li><a href="#impl-Instrument-for-T">Instrument</a></li><li><a href="#impl-Into%3CU%3E-for-T">Into<U></a></li><li><a href="#impl-TryFrom%3CU%3E-for-T">TryFrom<U></a></li><li><a href="#impl-TryInto%3CU%3E-for-T">TryInto<U></a></li><li><a href="#impl-WithSubscriber-for-T">WithSubscriber</a></li></ul></section><h2><a href="index.html">In crate netwatch</a></h2></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><div id="sidebar-button" tabindex="-1"><a href="../netwatch/all.html" title="show sidebar"></a></div><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Type ‘S’ or ‘/’ to search, ‘?’ for more options…" type="search"><div id="help-button" tabindex="-1"><a href="../help.html" title="help">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings">Settings</a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>Struct <a href="index.html">netwatch</a>::<wbr><a class="struct" href="#">UdpSocket</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="../src/netwatch/udp.rs.html#11">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><pre class="rust item-decl"><code>pub struct UdpSocket(<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>Wrapper around a tokio UDP socket that handles the fact that
|
||
on drop <code>libc::close</code> can block for UDP sockets.</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-UdpSocket" class="impl"><a class="src rightside" href="../src/netwatch/udp.rs.html#16-105">source</a><a href="#impl-UdpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="struct" href="struct.UdpSocket.html" title="struct netwatch::UdpSocket">UdpSocket</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.bind_v4" class="method"><a class="src rightside" href="../src/netwatch/udp.rs.html#18-20">source</a><h4 class="code-header">pub fn <a href="#method.bind_v4" class="fn">bind_v4</a>(port: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u16.html">u16</a>) -> <a class="type" href="https://docs.rs/anyhow/1.0.93/anyhow/type.Result.html" title="type anyhow::Result">Result</a><Self></h4></section></summary><div class="docblock"><p>Bind only Ipv4 on any interface.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.bind_v6" class="method"><a class="src rightside" href="../src/netwatch/udp.rs.html#23-25">source</a><h4 class="code-header">pub fn <a href="#method.bind_v6" class="fn">bind_v6</a>(port: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u16.html">u16</a>) -> <a class="type" href="https://docs.rs/anyhow/1.0.93/anyhow/type.Result.html" title="type anyhow::Result">Result</a><Self></h4></section></summary><div class="docblock"><p>Bind only Ipv6 on any interface.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.bind_local_v4" class="method"><a class="src rightside" href="../src/netwatch/udp.rs.html#28-30">source</a><h4 class="code-header">pub fn <a href="#method.bind_local_v4" class="fn">bind_local_v4</a>(port: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u16.html">u16</a>) -> <a class="type" href="https://docs.rs/anyhow/1.0.93/anyhow/type.Result.html" title="type anyhow::Result">Result</a><Self></h4></section></summary><div class="docblock"><p>Bind only Ipv4 on localhost.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.bind_local_v6" class="method"><a class="src rightside" href="../src/netwatch/udp.rs.html#33-35">source</a><h4 class="code-header">pub fn <a href="#method.bind_local_v6" class="fn">bind_local_v6</a>(port: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u16.html">u16</a>) -> <a class="type" href="https://docs.rs/anyhow/1.0.93/anyhow/type.Result.html" title="type anyhow::Result">Result</a><Self></h4></section></summary><div class="docblock"><p>Bind only Ipv6 on localhost.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.bind_local" class="method"><a class="src rightside" href="../src/netwatch/udp.rs.html#38-41">source</a><h4 class="code-header">pub fn <a href="#method.bind_local" class="fn">bind_local</a>(network: <a class="enum" href="enum.IpFamily.html" title="enum netwatch::IpFamily">IpFamily</a>, port: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u16.html">u16</a>) -> <a class="type" href="https://docs.rs/anyhow/1.0.93/anyhow/type.Result.html" title="type anyhow::Result">Result</a><Self></h4></section></summary><div class="docblock"><p>Bind to the given port only on localhost.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.bind" class="method"><a class="src rightside" href="../src/netwatch/udp.rs.html#44-47">source</a><h4 class="code-header">pub fn <a href="#method.bind" class="fn">bind</a>(network: <a class="enum" href="enum.IpFamily.html" title="enum netwatch::IpFamily">IpFamily</a>, port: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u16.html">u16</a>) -> <a class="type" href="https://docs.rs/anyhow/1.0.93/anyhow/type.Result.html" title="type anyhow::Result">Result</a><Self></h4></section></summary><div class="docblock"><p>Bind to the given port and listen on all interfaces.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.bind_full" class="method"><a class="src rightside" href="../src/netwatch/udp.rs.html#50-52">source</a><h4 class="code-header">pub fn <a href="#method.bind_full" class="fn">bind_full</a>(addr: impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>>) -> <a class="type" href="https://docs.rs/anyhow/1.0.93/anyhow/type.Result.html" title="type anyhow::Result">Result</a><Self></h4></section></summary><div class="docblock"><p>Bind to any provided <a href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr"><code>SocketAddr</code></a>.</p>
|
||
</div></details></div></details></div><h2 id="deref-methods-UdpSocket" class="section-header"><span>Methods from <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/deref/trait.Deref.html" title="trait core::ops::deref::Deref">Deref</a><Target = UdpSocket></span><a href="#deref-methods-UdpSocket" class="anchor">§</a></h2><div id="deref-methods-UdpSocket-1" class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.local_addr" class="method"><h4 class="code-header">pub fn <a href="#method.local_addr" class="fn">local_addr</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Returns the local address that this socket is bound to.</p>
|
||
<h5 id="example"><a class="doc-anchor" href="#example">§</a>Example</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket;
|
||
|
||
<span class="kw">let </span>addr = <span class="string">"0.0.0.0:8080"</span>.parse::<SocketAddr>().unwrap();
|
||
<span class="kw">let </span>sock = UdpSocket::bind(addr).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
<span class="comment">// the address the socket is bound to
|
||
</span><span class="kw">let </span>local_addr = sock.local_addr()<span class="question-mark">?</span>;</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.peer_addr" class="method"><h4 class="code-header">pub fn <a href="#method.peer_addr" class="fn">peer_addr</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Returns the socket address of the remote peer this socket was connected to.</p>
|
||
<h5 id="example-1"><a class="doc-anchor" href="#example-1">§</a>Example</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket;
|
||
|
||
<span class="kw">let </span>addr = <span class="string">"0.0.0.0:8080"</span>.parse::<SocketAddr>().unwrap();
|
||
<span class="kw">let </span>peer = <span class="string">"127.0.0.1:11100"</span>.parse::<SocketAddr>().unwrap();
|
||
<span class="kw">let </span>sock = UdpSocket::bind(addr).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
sock.connect(peer).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
<span class="macro">assert_eq!</span>(peer, sock.peer_addr()<span class="question-mark">?</span>);</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.connect" class="method"><h4 class="code-header">pub async fn <a href="#method.connect" class="fn">connect</a><A>(&self, addr: A) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>><div class="where">where
|
||
A: ToSocketAddrs,</div></h4></section></summary><div class="docblock"><p>Connects the UDP socket setting the default destination for send() and
|
||
limiting packets that are read via <code>recv</code> from the address specified in
|
||
<code>addr</code>.</p>
|
||
<h5 id="example-2"><a class="doc-anchor" href="#example-2">§</a>Example</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket;
|
||
|
||
<span class="kw">let </span>sock = UdpSocket::bind(<span class="string">"0.0.0.0:8080"</span>.parse::<SocketAddr>().unwrap()).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>remote_addr = <span class="string">"127.0.0.1:59600"</span>.parse::<SocketAddr>().unwrap();
|
||
sock.connect(remote_addr).<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">0u8</span>; <span class="number">32</span>];
|
||
<span class="comment">// recv from remote_addr
|
||
</span><span class="kw">let </span>len = sock.recv(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
<span class="comment">// send to remote_addr
|
||
</span><span class="kw">let </span>_len = sock.send(<span class="kw-2">&</span>buf[..len]).<span class="kw">await</span><span class="question-mark">?</span>;</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.ready" class="method"><h4 class="code-header">pub async fn <a href="#method.ready" class="fn">ready</a>(&self, interest: Interest) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Ready, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</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_recv()</code> or <code>try_send()</code>. It
|
||
can be used to concurrently <code>recv</code> / <code>send</code> 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
|
||
[<code>Ready</code>] 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 class="doc-anchor" href="#cancel-safety">§</a>Cancel safety</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"><a class="doc-anchor" href="#examples">§</a>Examples</h5>
|
||
<p>Concurrently receive from and send to the socket 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::{<span class="self">self</span>, Interest};
|
||
<span class="kw">use </span>tokio::net::UdpSocket;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
socket.connect(<span class="string">"127.0.0.1:8081"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">loop </span>{
|
||
<span class="kw">let </span>ready = socket.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="comment">// The buffer is **not** included in the async task and will only exist
|
||
// on the stack.
|
||
</span><span class="kw">let </span><span class="kw-2">mut </span>data = [<span class="number">0</span>; <span class="number">1024</span>];
|
||
<span class="kw">match </span>socket.try_recv(<span class="kw-2">&mut </span>data[..]) {
|
||
<span class="prelude-val">Ok</span>(n) => {
|
||
<span class="macro">println!</span>(<span class="string">"received {:?}"</span>, <span class="kw-2">&</span>data[..n]);
|
||
}
|
||
<span class="comment">// False-positive, continue
|
||
</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="prelude-val">Err</span>(e) => {
|
||
<span class="kw">return </span><span class="prelude-val">Err</span>(e);
|
||
}
|
||
}
|
||
}
|
||
|
||
<span class="kw">if </span>ready.is_writable() {
|
||
<span class="comment">// Write some data
|
||
</span><span class="kw">match </span>socket.try_send(<span class="string">b"hello world"</span>) {
|
||
<span class="prelude-val">Ok</span>(n) => {
|
||
<span class="macro">println!</span>(<span class="string">"sent {} bytes"</span>, n);
|
||
}
|
||
<span class="comment">// False-positive, continue
|
||
</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="prelude-val">Err</span>(e) => {
|
||
<span class="kw">return </span><span class="prelude-val">Err</span>(e);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.writable" class="method"><h4 class="code-header">pub async fn <a href="#method.writable" class="fn">writable</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></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_send()</code> or <code>try_send_to()</code>.</p>
|
||
<p>The function may complete without the socket being writable. This is a
|
||
false-positive and attempting a <code>try_send()</code> will return with
|
||
<code>io::ErrorKind::WouldBlock</code>.</p>
|
||
<h5 id="cancel-safety-1"><a class="doc-anchor" href="#cancel-safety-1">§</a>Cancel safety</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-1"><a class="doc-anchor" href="#examples-1">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket;
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="comment">// Bind socket
|
||
</span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
socket.connect(<span class="string">"127.0.0.1:8081"</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>socket.writable().<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Try to send data, this may still fail with `WouldBlock`
|
||
// if the readiness event is a false positive.
|
||
</span><span class="kw">match </span>socket.try_send(<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);
|
||
}
|
||
}
|
||
}
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.poll_send_ready" class="method"><h4 class="code-header">pub fn <a href="#method.poll_send_ready" class="fn">poll_send_ready</a>(&self, cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>>></h4></section></summary><div class="docblock"><p>Polls for write/send readiness.</p>
|
||
<p>If the udp stream is not currently ready for sending, this method will
|
||
store a clone of the <code>Waker</code> from the provided <code>Context</code>. When the udp
|
||
stream becomes ready for sending, <code>Waker::wake</code> will be called on the
|
||
waker.</p>
|
||
<p>Note that on multiple calls to <code>poll_send_ready</code> or <code>poll_send</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_recv_ready</code> retains a
|
||
second, independent waker.)</p>
|
||
<p>This function is intended for cases where creating and pinning a future
|
||
via <a href="method@Self::writable"><code>writable</code></a> is not feasible. Where possible, using <a href="method@Self::writable"><code>writable</code></a> is
|
||
preferred, as this supports polling from multiple tasks at once.</p>
|
||
<h5 id="return-value"><a class="doc-anchor" href="#return-value">§</a>Return value</h5>
|
||
<p>The function returns:</p>
|
||
<ul>
|
||
<li><code>Poll::Pending</code> if the udp stream is not ready for writing.</li>
|
||
<li><code>Poll::Ready(Ok(()))</code> if the udp stream is ready for writing.</li>
|
||
<li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li>
|
||
</ul>
|
||
<h5 id="errors"><a class="doc-anchor" href="#errors">§</a>Errors</h5>
|
||
<p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.send" class="method"><h4 class="code-header">pub async fn <a href="#method.send" class="fn">send</a>(&self, buf: &[<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Sends data on the socket to the remote address that the socket is
|
||
connected to.</p>
|
||
<p>The <a href="method@Self::connect"><code>connect</code></a> method will connect this socket to a remote address.
|
||
This method will fail if the socket is not connected.</p>
|
||
<h5 id="return"><a class="doc-anchor" href="#return">§</a>Return</h5>
|
||
<p>On success, the number of bytes sent is returned, otherwise, the
|
||
encountered error is returned.</p>
|
||
<h5 id="cancel-safety-2"><a class="doc-anchor" href="#cancel-safety-2">§</a>Cancel safety</h5>
|
||
<p>This method is cancel safe. If <code>send</code> is used as the event in a
|
||
<a href="crate::select"><code>tokio::select!</code></a> statement and some other branch
|
||
completes first, then it is guaranteed that the message was not sent.</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::io;
|
||
<span class="kw">use </span>tokio::net::UdpSocket;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="comment">// Bind socket
|
||
</span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
socket.connect(<span class="string">"127.0.0.1:8081"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Send a message
|
||
</span>socket.send(<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>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.poll_send" class="method"><h4 class="code-header">pub fn <a href="#method.poll_send" class="fn">poll_send</a>(
|
||
&self,
|
||
cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>,
|
||
buf: &[<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>>></h4></section></summary><div class="docblock"><p>Attempts to send data on the socket to the remote address to which it
|
||
was previously <code>connect</code>ed.</p>
|
||
<p>The <a href="method@Self::connect"><code>connect</code></a> method will connect this socket to a remote address.
|
||
This method will fail if the socket is not connected.</p>
|
||
<p>Note that on multiple calls to a <code>poll_*</code> method in the send direction,
|
||
only the <code>Waker</code> from the <code>Context</code> passed to the most recent call will
|
||
be scheduled to receive a wakeup.</p>
|
||
<h5 id="return-value-1"><a class="doc-anchor" href="#return-value-1">§</a>Return value</h5>
|
||
<p>The function returns:</p>
|
||
<ul>
|
||
<li><code>Poll::Pending</code> if the socket is not available to write</li>
|
||
<li><code>Poll::Ready(Ok(n))</code> <code>n</code> is the number of bytes sent</li>
|
||
<li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li>
|
||
</ul>
|
||
<h5 id="errors-1"><a class="doc-anchor" href="#errors-1">§</a>Errors</h5>
|
||
<p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_send" class="method"><h4 class="code-header">pub fn <a href="#method.try_send" class="fn">try_send</a>(&self, buf: &[<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Tries to send data on the socket to the remote address to which it is
|
||
connected.</p>
|
||
<p>When the socket buffer is full, <code>Err(io::ErrorKind::WouldBlock)</code> is
|
||
returned. This function is usually paired with <code>writable()</code>.</p>
|
||
<h5 id="returns"><a class="doc-anchor" href="#returns">§</a>Returns</h5>
|
||
<p>If successful, <code>Ok(n)</code> is returned, where <code>n</code> is the number of bytes
|
||
sent. If the socket is not ready to send data,
|
||
<code>Err(ErrorKind::WouldBlock)</code> is returned.</p>
|
||
<h5 id="examples-3"><a class="doc-anchor" href="#examples-3">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket;
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="comment">// Bind a UDP socket
|
||
</span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Connect to a peer
|
||
</span>socket.connect(<span class="string">"127.0.0.1:8081"</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>socket.writable().<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Try to send data, this may still fail with `WouldBlock`
|
||
// if the readiness event is a false positive.
|
||
</span><span class="kw">match </span>socket.try_send(<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);
|
||
}
|
||
}
|
||
}
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.readable" class="method"><h4 class="code-header">pub async fn <a href="#method.readable" class="fn">readable</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></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_recv()</code>.</p>
|
||
<p>The function may complete without the socket being readable. This is a
|
||
false-positive and attempting a <code>try_recv()</code> will return with
|
||
<code>io::ErrorKind::WouldBlock</code>.</p>
|
||
<h5 id="cancel-safety-3"><a class="doc-anchor" href="#cancel-safety-3">§</a>Cancel safety</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-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::UdpSocket;
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="comment">// Connect to a peer
|
||
</span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
socket.connect(<span class="string">"127.0.0.1:8081"</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>socket.readable().<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// The buffer is **not** included in the async task and will
|
||
// only exist on the stack.
|
||
</span><span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0</span>; <span class="number">1024</span>];
|
||
|
||
<span class="comment">// Try to recv data, this may still fail with `WouldBlock`
|
||
// if the readiness event is a false positive.
|
||
</span><span class="kw">match </span>socket.try_recv(<span class="kw-2">&mut </span>buf) {
|
||
<span class="prelude-val">Ok</span>(n) => {
|
||
<span class="macro">println!</span>(<span class="string">"GOT {:?}"</span>, <span class="kw-2">&</span>buf[..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);
|
||
}
|
||
}
|
||
}
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.poll_recv_ready" class="method"><h4 class="code-header">pub fn <a href="#method.poll_recv_ready" class="fn">poll_recv_ready</a>(&self, cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>>></h4></section></summary><div class="docblock"><p>Polls for read/receive readiness.</p>
|
||
<p>If the udp stream is not currently ready for receiving, this method will
|
||
store a clone of the <code>Waker</code> from the provided <code>Context</code>. When the udp
|
||
socket becomes ready for reading, <code>Waker::wake</code> will be called on the
|
||
waker.</p>
|
||
<p>Note that on multiple calls to <code>poll_recv_ready</code>, <code>poll_recv</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_send_ready</code> retains a second, independent waker.)</p>
|
||
<p>This function is intended for cases where creating and pinning a future
|
||
via <a href="method@Self::readable"><code>readable</code></a> is not feasible. Where possible, using <a href="method@Self::readable"><code>readable</code></a> is
|
||
preferred, as this supports polling from multiple tasks at once.</p>
|
||
<h5 id="return-value-2"><a class="doc-anchor" href="#return-value-2">§</a>Return value</h5>
|
||
<p>The function returns:</p>
|
||
<ul>
|
||
<li><code>Poll::Pending</code> if the udp stream is not ready for reading.</li>
|
||
<li><code>Poll::Ready(Ok(()))</code> if the udp stream is ready for reading.</li>
|
||
<li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li>
|
||
</ul>
|
||
<h5 id="errors-2"><a class="doc-anchor" href="#errors-2">§</a>Errors</h5>
|
||
<p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.recv" class="method"><h4 class="code-header">pub async fn <a href="#method.recv" class="fn">recv</a>(&self, buf: &mut [<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Receives a single datagram message on the socket from the remote address
|
||
to which it is connected. On success, returns the number of bytes read.</p>
|
||
<p>The function must be called with valid byte array <code>buf</code> of sufficient
|
||
size to hold the message bytes. If a message is too long to fit in the
|
||
supplied buffer, excess bytes may be discarded.</p>
|
||
<p>The <a href="method@Self::connect"><code>connect</code></a> method will connect this socket to a remote address.
|
||
This method will fail if the socket is not connected.</p>
|
||
<h5 id="cancel-safety-4"><a class="doc-anchor" href="#cancel-safety-4">§</a>Cancel safety</h5>
|
||
<p>This method is cancel safe. If <code>recv</code> is used as the event in a
|
||
<a href="crate::select"><code>tokio::select!</code></a> statement and some other branch
|
||
completes first, it is guaranteed that no messages were received on this
|
||
socket.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket;
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="comment">// Bind socket
|
||
</span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
socket.connect(<span class="string">"127.0.0.1:8081"</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="macro">vec!</span>[<span class="number">0</span>; <span class="number">10</span>];
|
||
<span class="kw">let </span>n = socket.recv(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="macro">println!</span>(<span class="string">"received {} bytes {:?}"</span>, n, <span class="kw-2">&</span>buf[..n]);
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.poll_recv" class="method"><h4 class="code-header">pub fn <a href="#method.poll_recv" class="fn">poll_recv</a>(
|
||
&self,
|
||
cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>,
|
||
buf: &mut ReadBuf<'_>
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>>></h4></section></summary><div class="docblock"><p>Attempts to receive a single datagram message on the socket from the remote
|
||
address to which it is <code>connect</code>ed.</p>
|
||
<p>The <a href="method@Self::connect"><code>connect</code></a> method will connect this socket to a remote address. This method
|
||
resolves to an error if the socket is not connected.</p>
|
||
<p>Note that on multiple calls to a <code>poll_*</code> method in the <code>recv</code> direction, only the
|
||
<code>Waker</code> from the <code>Context</code> passed to the most recent call will be scheduled to
|
||
receive a wakeup.</p>
|
||
<h5 id="return-value-3"><a class="doc-anchor" href="#return-value-3">§</a>Return value</h5>
|
||
<p>The function returns:</p>
|
||
<ul>
|
||
<li><code>Poll::Pending</code> if the socket is not ready to read</li>
|
||
<li><code>Poll::Ready(Ok(()))</code> reads data <code>ReadBuf</code> if the socket is ready</li>
|
||
<li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li>
|
||
</ul>
|
||
<h5 id="errors-3"><a class="doc-anchor" href="#errors-3">§</a>Errors</h5>
|
||
<p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_recv" class="method"><h4 class="code-header">pub fn <a href="#method.try_recv" class="fn">try_recv</a>(&self, buf: &mut [<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Tries to receive a single datagram message on the socket from the remote
|
||
address to which it is connected. On success, returns the number of
|
||
bytes read.</p>
|
||
<p>This method must be called with valid byte array <code>buf</code> of sufficient size
|
||
to hold the message bytes. If a message is too long to fit in the
|
||
supplied buffer, excess bytes may be discarded.</p>
|
||
<p>When there is no pending data, <code>Err(io::ErrorKind::WouldBlock)</code> is
|
||
returned. This function is usually paired with <code>readable()</code>.</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::UdpSocket;
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="comment">// Connect to a peer
|
||
</span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
socket.connect(<span class="string">"127.0.0.1:8081"</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>socket.readable().<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// The buffer is **not** included in the async task and will
|
||
// only exist on the stack.
|
||
</span><span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0</span>; <span class="number">1024</span>];
|
||
|
||
<span class="comment">// Try to recv data, this may still fail with `WouldBlock`
|
||
// if the readiness event is a false positive.
|
||
</span><span class="kw">match </span>socket.try_recv(<span class="kw-2">&mut </span>buf) {
|
||
<span class="prelude-val">Ok</span>(n) => {
|
||
<span class="macro">println!</span>(<span class="string">"GOT {:?}"</span>, <span class="kw-2">&</span>buf[..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);
|
||
}
|
||
}
|
||
}
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_recv_buf" class="method"><h4 class="code-header">pub fn <a href="#method.try_recv_buf" class="fn">try_recv_buf</a><B>(&self, buf: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut B</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>><div class="where">where
|
||
B: BufMut,</div></h4></section></summary><div class="docblock"><p>Tries to receive data from the stream into the provided buffer, advancing the
|
||
buffer’s internal cursor, returning how many bytes were read.</p>
|
||
<p>This method must be called with valid byte array <code>buf</code> of sufficient size
|
||
to hold the message bytes. If a message is too long to fit in the
|
||
supplied buffer, excess bytes may be discarded.</p>
|
||
<p>This method can be used even if <code>buf</code> is uninitialized.</p>
|
||
<p>When there is no pending data, <code>Err(io::ErrorKind::WouldBlock)</code> is
|
||
returned. This function is usually paired with <code>readable()</code>.</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::UdpSocket;
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="comment">// Connect to a peer
|
||
</span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
socket.connect(<span class="string">"127.0.0.1:8081"</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>socket.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">1024</span>);
|
||
|
||
<span class="comment">// Try to recv data, this may still fail with `WouldBlock`
|
||
// if the readiness event is a false positive.
|
||
</span><span class="kw">match </span>socket.try_recv_buf(<span class="kw-2">&mut </span>buf) {
|
||
<span class="prelude-val">Ok</span>(n) => {
|
||
<span class="macro">println!</span>(<span class="string">"GOT {:?}"</span>, <span class="kw-2">&</span>buf[..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);
|
||
}
|
||
}
|
||
}
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.recv_buf" class="method"><h4 class="code-header">pub async fn <a href="#method.recv_buf" class="fn">recv_buf</a><B>(&self, buf: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut B</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>><div class="where">where
|
||
B: BufMut,</div></h4></section></summary><div class="docblock"><p>Receives a single datagram message on the socket from the remote address
|
||
to which it is connected, advancing the buffer’s internal cursor,
|
||
returning how many bytes were read.</p>
|
||
<p>This method must be called with valid byte array <code>buf</code> of sufficient size
|
||
to hold the message bytes. If a message is too long to fit in the
|
||
supplied buffer, excess bytes may be discarded.</p>
|
||
<p>This method can be used even if <code>buf</code> is uninitialized.</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::UdpSocket;
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="comment">// Connect to a peer
|
||
</span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
socket.connect(<span class="string">"127.0.0.1:8081"</span>).<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">512</span>);
|
||
<span class="kw">let </span>len = socket.recv_buf(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="macro">println!</span>(<span class="string">"received {} bytes {:?}"</span>, len, <span class="kw-2">&</span>buf[..len]);
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_recv_buf_from" class="method"><h4 class="code-header">pub fn <a href="#method.try_recv_buf_from" class="fn">try_recv_buf_from</a><B>(
|
||
&self,
|
||
buf: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut B</a>
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>), <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>><div class="where">where
|
||
B: BufMut,</div></h4></section></summary><div class="docblock"><p>Tries to receive a single datagram message on the socket. On success,
|
||
returns the number of bytes read and the origin.</p>
|
||
<p>This method must be called with valid byte array <code>buf</code> of sufficient size
|
||
to hold the message bytes. If a message is too long to fit in the
|
||
supplied buffer, excess bytes may be discarded.</p>
|
||
<p>This method can be used even if <code>buf</code> is uninitialized.</p>
|
||
<p>When there is no pending data, <code>Err(io::ErrorKind::WouldBlock)</code> is
|
||
returned. This function is usually paired with <code>readable()</code>.</p>
|
||
<h5 id="notes"><a class="doc-anchor" href="#notes">§</a>Notes</h5>
|
||
<p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively
|
||
trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>.
|
||
Because UDP is stateless and does not validate the origin of a packet,
|
||
the attacker does not need to be able to intercept traffic in order to interfere.
|
||
It is important to be aware of this when designing your application-level protocol.</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::UdpSocket;
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="comment">// Connect to a peer
|
||
</span><span class="kw">let </span>socket = UdpSocket::bind(<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>socket.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">1024</span>);
|
||
|
||
<span class="comment">// Try to recv data, this may still fail with `WouldBlock`
|
||
// if the readiness event is a false positive.
|
||
</span><span class="kw">match </span>socket.try_recv_buf_from(<span class="kw-2">&mut </span>buf) {
|
||
<span class="prelude-val">Ok</span>((n, _addr)) => {
|
||
<span class="macro">println!</span>(<span class="string">"GOT {:?}"</span>, <span class="kw-2">&</span>buf[..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);
|
||
}
|
||
}
|
||
}
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.recv_buf_from" class="method"><h4 class="code-header">pub async fn <a href="#method.recv_buf_from" class="fn">recv_buf_from</a><B>(
|
||
&self,
|
||
buf: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut B</a>
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>), <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>><div class="where">where
|
||
B: BufMut,</div></h4></section></summary><div class="docblock"><p>Receives a single datagram message on the socket, advancing the
|
||
buffer’s internal cursor, returning how many bytes were read and the origin.</p>
|
||
<p>This method must be called with valid byte array <code>buf</code> of sufficient size
|
||
to hold the message bytes. If a message is too long to fit in the
|
||
supplied buffer, excess bytes may be discarded.</p>
|
||
<p>This method can be used even if <code>buf</code> is uninitialized.</p>
|
||
<h5 id="notes-1"><a class="doc-anchor" href="#notes-1">§</a>Notes</h5>
|
||
<p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively
|
||
trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>.
|
||
Because UDP is stateless and does not validate the origin of a packet,
|
||
the attacker does not need to be able to intercept traffic in order to interfere.
|
||
It is important to be aware of this when designing your application-level protocol.</p>
|
||
<h5 id="examples-9"><a class="doc-anchor" href="#examples-9">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket;
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="comment">// Connect to a peer
|
||
</span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
socket.connect(<span class="string">"127.0.0.1:8081"</span>).<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">512</span>);
|
||
<span class="kw">let </span>(len, addr) = socket.recv_buf_from(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="macro">println!</span>(<span class="string">"received {:?} bytes from {:?}"</span>, len, addr);
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.send_to" class="method"><h4 class="code-header">pub async fn <a href="#method.send_to" class="fn">send_to</a><A>(&self, buf: &[<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>], target: A) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>><div class="where">where
|
||
A: ToSocketAddrs,</div></h4></section></summary><div class="docblock"><p>Sends data on the socket to the given address. On success, returns the
|
||
number of bytes written.</p>
|
||
<p>Address type can be any implementor of <a href="crate::net::ToSocketAddrs"><code>ToSocketAddrs</code></a> trait. See its
|
||
documentation for concrete examples.</p>
|
||
<p>It is possible for <code>addr</code> to yield multiple addresses, but <code>send_to</code>
|
||
will only send data to the first address yielded by <code>addr</code>.</p>
|
||
<p>This will return an error when the IP version of the local socket does
|
||
not match that returned from <a href="crate::net::ToSocketAddrs"><code>ToSocketAddrs</code></a>.</p>
|
||
<h5 id="cancel-safety-5"><a class="doc-anchor" href="#cancel-safety-5">§</a>Cancel safety</h5>
|
||
<p>This method is cancel safe. If <code>send_to</code> is used as the event in a
|
||
<a href="crate::select"><code>tokio::select!</code></a> statement and some other branch
|
||
completes first, then it is guaranteed that the message was not sent.</p>
|
||
<h5 id="example-3"><a class="doc-anchor" href="#example-3">§</a>Example</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket;
|
||
<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>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
<span class="kw">let </span>len = socket.send_to(<span class="string">b"hello world"</span>, <span class="string">"127.0.0.1:8081"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="macro">println!</span>(<span class="string">"Sent {} bytes"</span>, len);
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.poll_send_to" class="method"><h4 class="code-header">pub fn <a href="#method.poll_send_to" class="fn">poll_send_to</a>(
|
||
&self,
|
||
cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>,
|
||
buf: &[<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>],
|
||
target: <a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>>></h4></section></summary><div class="docblock"><p>Attempts to send data on the socket to a given address.</p>
|
||
<p>Note that on multiple calls to a <code>poll_*</code> method in the send direction, only the
|
||
<code>Waker</code> from the <code>Context</code> passed to the most recent call will be scheduled to
|
||
receive a wakeup.</p>
|
||
<h5 id="return-value-4"><a class="doc-anchor" href="#return-value-4">§</a>Return value</h5>
|
||
<p>The function returns:</p>
|
||
<ul>
|
||
<li><code>Poll::Pending</code> if the socket is not ready to write</li>
|
||
<li><code>Poll::Ready(Ok(n))</code> <code>n</code> is the number of bytes sent.</li>
|
||
<li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li>
|
||
</ul>
|
||
<h5 id="errors-4"><a class="doc-anchor" href="#errors-4">§</a>Errors</h5>
|
||
<p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_send_to" class="method"><h4 class="code-header">pub fn <a href="#method.try_send_to" class="fn">try_send_to</a>(
|
||
&self,
|
||
buf: &[<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>],
|
||
target: <a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Tries to send data on the socket to the given address, but if the send is
|
||
blocked this will return right away.</p>
|
||
<p>This function is usually paired with <code>writable()</code>.</p>
|
||
<h5 id="returns-1"><a class="doc-anchor" href="#returns-1">§</a>Returns</h5>
|
||
<p>If successful, returns the number of bytes sent</p>
|
||
<p>Users should ensure that when the remote cannot receive, the
|
||
<a href="https://doc.rust-lang.org/nightly/std/io/error/enum.ErrorKind.html#variant.WouldBlock" title="variant std::io::error::ErrorKind::WouldBlock"><code>ErrorKind::WouldBlock</code></a> is properly handled. An error can also occur
|
||
if the IP version of the socket does not match that of <code>target</code>.</p>
|
||
<h5 id="example-4"><a class="doc-anchor" href="#example-4">§</a>Example</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket;
|
||
<span class="kw">use </span>std::error::Error;
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[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>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>dst = <span class="string">"127.0.0.1:8081"</span>.parse()<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">loop </span>{
|
||
socket.writable().<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">match </span>socket.try_send_to(<span class="kw-2">&</span><span class="string">b"hello world"</span>[..], dst) {
|
||
<span class="prelude-val">Ok</span>(sent) => {
|
||
<span class="macro">println!</span>(<span class="string">"sent {} bytes"</span>, sent);
|
||
<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="comment">// Writable false positive.
|
||
</span><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="toggle method-toggle" open><summary><section id="method.recv_from" class="method"><h4 class="code-header">pub async fn <a href="#method.recv_from" class="fn">recv_from</a>(
|
||
&self,
|
||
buf: &mut [<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>), <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Receives a single datagram message on the socket. On success, returns
|
||
the number of bytes read and the origin.</p>
|
||
<p>The function must be called with valid byte array <code>buf</code> of sufficient
|
||
size to hold the message bytes. If a message is too long to fit in the
|
||
supplied buffer, excess bytes may be discarded.</p>
|
||
<h5 id="cancel-safety-6"><a class="doc-anchor" href="#cancel-safety-6">§</a>Cancel safety</h5>
|
||
<p>This method is cancel safe. If <code>recv_from</code> is used as the event in a
|
||
<a href="crate::select"><code>tokio::select!</code></a> statement and some other branch
|
||
completes first, it is guaranteed that no messages were received on this
|
||
socket.</p>
|
||
<h5 id="example-5"><a class="doc-anchor" href="#example-5">§</a>Example</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket;
|
||
<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>socket = UdpSocket::bind(<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>buf = <span class="macro">vec!</span>[<span class="number">0u8</span>; <span class="number">32</span>];
|
||
<span class="kw">let </span>(len, addr) = socket.recv_from(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="macro">println!</span>(<span class="string">"received {:?} bytes from {:?}"</span>, len, addr);
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
<h5 id="notes-2"><a class="doc-anchor" href="#notes-2">§</a>Notes</h5>
|
||
<p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively
|
||
trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>.
|
||
Because UDP is stateless and does not validate the origin of a packet,
|
||
the attacker does not need to be able to intercept traffic in order to interfere.
|
||
It is important to be aware of this when designing your application-level protocol.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.poll_recv_from" class="method"><h4 class="code-header">pub fn <a href="#method.poll_recv_from" class="fn">poll_recv_from</a>(
|
||
&self,
|
||
cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>,
|
||
buf: &mut ReadBuf<'_>
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>>></h4></section></summary><div class="docblock"><p>Attempts to receive a single datagram on the socket.</p>
|
||
<p>Note that on multiple calls to a <code>poll_*</code> method in the <code>recv</code> direction, only the
|
||
<code>Waker</code> from the <code>Context</code> passed to the most recent call will be scheduled to
|
||
receive a wakeup.</p>
|
||
<h5 id="return-value-5"><a class="doc-anchor" href="#return-value-5">§</a>Return value</h5>
|
||
<p>The function returns:</p>
|
||
<ul>
|
||
<li><code>Poll::Pending</code> if the socket is not ready to read</li>
|
||
<li><code>Poll::Ready(Ok(addr))</code> reads data from <code>addr</code> into <code>ReadBuf</code> if the socket is ready</li>
|
||
<li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li>
|
||
</ul>
|
||
<h5 id="errors-5"><a class="doc-anchor" href="#errors-5">§</a>Errors</h5>
|
||
<p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p>
|
||
<h5 id="notes-3"><a class="doc-anchor" href="#notes-3">§</a>Notes</h5>
|
||
<p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively
|
||
trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>.
|
||
Because UDP is stateless and does not validate the origin of a packet,
|
||
the attacker does not need to be able to intercept traffic in order to interfere.
|
||
It is important to be aware of this when designing your application-level protocol.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_recv_from" class="method"><h4 class="code-header">pub fn <a href="#method.try_recv_from" class="fn">try_recv_from</a>(
|
||
&self,
|
||
buf: &mut [<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>), <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Tries to receive a single datagram message on the socket. On success,
|
||
returns the number of bytes read and the origin.</p>
|
||
<p>This method must be called with valid byte array <code>buf</code> of sufficient size
|
||
to hold the message bytes. If a message is too long to fit in the
|
||
supplied buffer, excess bytes may be discarded.</p>
|
||
<p>When there is no pending data, <code>Err(io::ErrorKind::WouldBlock)</code> is
|
||
returned. This function is usually paired with <code>readable()</code>.</p>
|
||
<h5 id="notes-4"><a class="doc-anchor" href="#notes-4">§</a>Notes</h5>
|
||
<p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively
|
||
trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>.
|
||
Because UDP is stateless and does not validate the origin of a packet,
|
||
the attacker does not need to be able to intercept traffic in order to interfere.
|
||
It is important to be aware of this when designing your application-level protocol.</p>
|
||
<h5 id="examples-10"><a class="doc-anchor" href="#examples-10">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket;
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="comment">// Connect to a peer
|
||
</span><span class="kw">let </span>socket = UdpSocket::bind(<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>socket.readable().<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// The buffer is **not** included in the async task and will
|
||
// only exist on the stack.
|
||
</span><span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0</span>; <span class="number">1024</span>];
|
||
|
||
<span class="comment">// Try to recv data, this may still fail with `WouldBlock`
|
||
// if the readiness event is a false positive.
|
||
</span><span class="kw">match </span>socket.try_recv_from(<span class="kw-2">&mut </span>buf) {
|
||
<span class="prelude-val">Ok</span>((n, _addr)) => {
|
||
<span class="macro">println!</span>(<span class="string">"GOT {:?}"</span>, <span class="kw-2">&</span>buf[..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);
|
||
}
|
||
}
|
||
}
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_io" class="method"><h4 class="code-header">pub fn <a href="#method.try_io" class="fn">try_io</a><R>(
|
||
&self,
|
||
interest: Interest,
|
||
f: impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><R, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>>
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><R, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></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>UdpSocket</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="UdpSocket::readable()"><code>readable()</code></a>, <a href="UdpSocket::writable()"><code>writable()</code></a> or <a href="UdpSocket::ready()"><code>ready()</code></a> is used with this function.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.async_io" class="method"><h4 class="code-header">pub async fn <a href="#method.async_io" class="fn">async_io</a><R>(
|
||
&self,
|
||
interest: Interest,
|
||
f: impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>() -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><R, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>>
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><R, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Reads or writes from the socket using a user-provided IO operation.</p>
|
||
<p>The readiness of the socket is awaited and when 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. In such case the
|
||
readiness flag is cleared and the socket readiness is awaited again.
|
||
This loop is repeated until the closure returns an <code>Ok</code> or an error
|
||
other than <code>WouldBlock</code>.</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>UdpSocket</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>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.peek_from" class="method"><h4 class="code-header">pub async fn <a href="#method.peek_from" class="fn">peek_from</a>(
|
||
&self,
|
||
buf: &mut [<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>), <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Receives data from the socket, without removing it from the input queue.
|
||
On success, returns the number of bytes read and the address from whence
|
||
the data came.</p>
|
||
<h5 id="notes-5"><a class="doc-anchor" href="#notes-5">§</a>Notes</h5>
|
||
<p>On Windows, if the data is larger than the buffer specified, the buffer
|
||
is filled with the first part of the data, and <code>peek_from</code> returns the error
|
||
<code>WSAEMSGSIZE(10040)</code>. The excess data is lost.
|
||
Make sure to always use a sufficiently large buffer to hold the
|
||
maximum UDP packet size, which can be up to 65536 bytes in size.</p>
|
||
<p>MacOS will return an error if you pass a zero-sized buffer.</p>
|
||
<p>If you’re merely interested in learning the sender of the data at the head of the queue,
|
||
try <a href="method@Self::peek_sender"><code>peek_sender</code></a>.</p>
|
||
<p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively
|
||
trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>.
|
||
Because UDP is stateless and does not validate the origin of a packet,
|
||
the attacker does not need to be able to intercept traffic in order to interfere.
|
||
It is important to be aware of this when designing your application-level protocol.</p>
|
||
<h5 id="examples-11"><a class="doc-anchor" href="#examples-11">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket;
|
||
<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>socket = UdpSocket::bind(<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>buf = <span class="macro">vec!</span>[<span class="number">0u8</span>; <span class="number">32</span>];
|
||
<span class="kw">let </span>(len, addr) = socket.peek_from(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="macro">println!</span>(<span class="string">"peeked {:?} bytes from {:?}"</span>, len, addr);
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.poll_peek_from" class="method"><h4 class="code-header">pub fn <a href="#method.poll_peek_from" class="fn">poll_peek_from</a>(
|
||
&self,
|
||
cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>,
|
||
buf: &mut ReadBuf<'_>
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>>></h4></section></summary><div class="docblock"><p>Receives data from the socket, without removing it from the input queue.
|
||
On success, returns the sending address of the datagram.</p>
|
||
<h5 id="notes-6"><a class="doc-anchor" href="#notes-6">§</a>Notes</h5>
|
||
<p>Note that on multiple calls to a <code>poll_*</code> method in the <code>recv</code> direction, only the
|
||
<code>Waker</code> from the <code>Context</code> passed to the most recent call will be scheduled to
|
||
receive a wakeup</p>
|
||
<p>On Windows, if the data is larger than the buffer specified, the buffer
|
||
is filled with the first part of the data, and peek returns the error
|
||
<code>WSAEMSGSIZE(10040)</code>. The excess data is lost.
|
||
Make sure to always use a sufficiently large buffer to hold the
|
||
maximum UDP packet size, which can be up to 65536 bytes in size.</p>
|
||
<p>MacOS will return an error if you pass a zero-sized buffer.</p>
|
||
<p>If you’re merely interested in learning the sender of the data at the head of the queue,
|
||
try <a href="method@Self::poll_peek_sender"><code>poll_peek_sender</code></a>.</p>
|
||
<p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively
|
||
trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>.
|
||
Because UDP is stateless and does not validate the origin of a packet,
|
||
the attacker does not need to be able to intercept traffic in order to interfere.
|
||
It is important to be aware of this when designing your application-level protocol.</p>
|
||
<h5 id="return-value-6"><a class="doc-anchor" href="#return-value-6">§</a>Return value</h5>
|
||
<p>The function returns:</p>
|
||
<ul>
|
||
<li><code>Poll::Pending</code> if the socket is not ready to read</li>
|
||
<li><code>Poll::Ready(Ok(addr))</code> reads data from <code>addr</code> into <code>ReadBuf</code> if the socket is ready</li>
|
||
<li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li>
|
||
</ul>
|
||
<h5 id="errors-6"><a class="doc-anchor" href="#errors-6">§</a>Errors</h5>
|
||
<p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_peek_from" class="method"><h4 class="code-header">pub fn <a href="#method.try_peek_from" class="fn">try_peek_from</a>(
|
||
&self,
|
||
buf: &mut [<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>), <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Tries to receive data on the socket without removing it from the input queue.
|
||
On success, returns the number of bytes read and the sending address of the
|
||
datagram.</p>
|
||
<p>When there is no pending data, <code>Err(io::ErrorKind::WouldBlock)</code> is
|
||
returned. This function is usually paired with <code>readable()</code>.</p>
|
||
<h5 id="notes-7"><a class="doc-anchor" href="#notes-7">§</a>Notes</h5>
|
||
<p>On Windows, if the data is larger than the buffer specified, the buffer
|
||
is filled with the first part of the data, and peek returns the error
|
||
<code>WSAEMSGSIZE(10040)</code>. The excess data is lost.
|
||
Make sure to always use a sufficiently large buffer to hold the
|
||
maximum UDP packet size, which can be up to 65536 bytes in size.</p>
|
||
<p>MacOS will return an error if you pass a zero-sized buffer.</p>
|
||
<p>If you’re merely interested in learning the sender of the data at the head of the queue,
|
||
try <a href="method@Self::try_peek_sender"><code>try_peek_sender</code></a>.</p>
|
||
<p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively
|
||
trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>.
|
||
Because UDP is stateless and does not validate the origin of a packet,
|
||
the attacker does not need to be able to intercept traffic in order to interfere.
|
||
It is important to be aware of this when designing your application-level protocol.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.peek_sender" class="method"><h4 class="code-header">pub async fn <a href="#method.peek_sender" class="fn">peek_sender</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Retrieve the sender of the data at the head of the input queue, waiting if empty.</p>
|
||
<p>This is equivalent to calling <a href="method@Self::peek_from"><code>peek_from</code></a> with a zero-sized buffer,
|
||
but suppresses the <code>WSAEMSGSIZE</code> error on Windows and the “invalid argument” error on macOS.</p>
|
||
<p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively
|
||
trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>.
|
||
Because UDP is stateless and does not validate the origin of a packet,
|
||
the attacker does not need to be able to intercept traffic in order to interfere.
|
||
It is important to be aware of this when designing your application-level protocol.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.poll_peek_sender" class="method"><h4 class="code-header">pub fn <a href="#method.poll_peek_sender" class="fn">poll_peek_sender</a>(
|
||
&self,
|
||
cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>>></h4></section></summary><div class="docblock"><p>Retrieve the sender of the data at the head of the input queue,
|
||
scheduling a wakeup if empty.</p>
|
||
<p>This is equivalent to calling <a href="method@Self::poll_peek_from"><code>poll_peek_from</code></a> with a zero-sized buffer,
|
||
but suppresses the <code>WSAEMSGSIZE</code> error on Windows and the “invalid argument” error on macOS.</p>
|
||
<h5 id="notes-8"><a class="doc-anchor" href="#notes-8">§</a>Notes</h5>
|
||
<p>Note that on multiple calls to a <code>poll_*</code> method in the <code>recv</code> direction, only the
|
||
<code>Waker</code> from the <code>Context</code> passed to the most recent call will be scheduled to
|
||
receive a wakeup.</p>
|
||
<p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively
|
||
trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>.
|
||
Because UDP is stateless and does not validate the origin of a packet,
|
||
the attacker does not need to be able to intercept traffic in order to interfere.
|
||
It is important to be aware of this when designing your application-level protocol.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_peek_sender" class="method"><h4 class="code-header">pub fn <a href="#method.try_peek_sender" class="fn">try_peek_sender</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/net/socket_addr/enum.SocketAddr.html" title="enum core::net::socket_addr::SocketAddr">SocketAddr</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Try to retrieve the sender of the data at the head of the input queue.</p>
|
||
<p>When there is no pending data, <code>Err(io::ErrorKind::WouldBlock)</code> is
|
||
returned. This function is usually paired with <code>readable()</code>.</p>
|
||
<p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively
|
||
trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>.
|
||
Because UDP is stateless and does not validate the origin of a packet,
|
||
the attacker does not need to be able to intercept traffic in order to interfere.
|
||
It is important to be aware of this when designing your application-level protocol.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.broadcast" class="method"><h4 class="code-header">pub fn <a href="#method.broadcast" class="fn">broadcast</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Gets the value of the <code>SO_BROADCAST</code> option for this socket.</p>
|
||
<p>For more information about this option, see <a href="method@Self::set_broadcast"><code>set_broadcast</code></a>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.set_broadcast" class="method"><h4 class="code-header">pub fn <a href="#method.set_broadcast" class="fn">set_broadcast</a>(&self, on: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Sets the value of the <code>SO_BROADCAST</code> option for this socket.</p>
|
||
<p>When enabled, this socket is allowed to send packets to a broadcast
|
||
address.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.multicast_loop_v4" class="method"><h4 class="code-header">pub fn <a href="#method.multicast_loop_v4" class="fn">multicast_loop_v4</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Gets the value of the <code>IP_MULTICAST_LOOP</code> option for this socket.</p>
|
||
<p>For more information about this option, see <a href="method@Self::set_multicast_loop_v4"><code>set_multicast_loop_v4</code></a>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.set_multicast_loop_v4" class="method"><h4 class="code-header">pub fn <a href="#method.set_multicast_loop_v4" class="fn">set_multicast_loop_v4</a>(&self, on: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Sets the value of the <code>IP_MULTICAST_LOOP</code> option for this socket.</p>
|
||
<p>If enabled, multicast packets will be looped back to the local socket.</p>
|
||
<h5 id="note"><a class="doc-anchor" href="#note">§</a>Note</h5>
|
||
<p>This may not have any affect on IPv6 sockets.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.multicast_ttl_v4" class="method"><h4 class="code-header">pub fn <a href="#method.multicast_ttl_v4" class="fn">multicast_ttl_v4</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Gets the value of the <code>IP_MULTICAST_TTL</code> option for this socket.</p>
|
||
<p>For more information about this option, see <a href="method@Self::set_multicast_ttl_v4"><code>set_multicast_ttl_v4</code></a>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.set_multicast_ttl_v4" class="method"><h4 class="code-header">pub fn <a href="#method.set_multicast_ttl_v4" class="fn">set_multicast_ttl_v4</a>(&self, ttl: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Sets the value of the <code>IP_MULTICAST_TTL</code> option for this socket.</p>
|
||
<p>Indicates the time-to-live value of outgoing multicast packets for
|
||
this socket. The default value is 1 which means that multicast packets
|
||
don’t leave the local network unless explicitly requested.</p>
|
||
<h5 id="note-1"><a class="doc-anchor" href="#note-1">§</a>Note</h5>
|
||
<p>This may not have any affect on IPv6 sockets.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.multicast_loop_v6" class="method"><h4 class="code-header">pub fn <a href="#method.multicast_loop_v6" class="fn">multicast_loop_v6</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Gets the value of the <code>IPV6_MULTICAST_LOOP</code> option for this socket.</p>
|
||
<p>For more information about this option, see <a href="method@Self::set_multicast_loop_v6"><code>set_multicast_loop_v6</code></a>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.set_multicast_loop_v6" class="method"><h4 class="code-header">pub fn <a href="#method.set_multicast_loop_v6" class="fn">set_multicast_loop_v6</a>(&self, on: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Sets the value of the <code>IPV6_MULTICAST_LOOP</code> option for this socket.</p>
|
||
<p>Controls whether this socket sees the multicast packets it sends itself.</p>
|
||
<h5 id="note-2"><a class="doc-anchor" href="#note-2">§</a>Note</h5>
|
||
<p>This may not have any affect on IPv4 sockets.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.ttl" class="method"><h4 class="code-header">pub fn <a href="#method.ttl" class="fn">ttl</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></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="method@Self::set_ttl"><code>set_ttl</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::UdpSocket;
|
||
|
||
<span class="kw">let </span>sock = UdpSocket::bind(<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>, sock.ttl()<span class="question-mark">?</span>);</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.set_ttl" class="method"><h4 class="code-header">pub fn <a href="#method.set_ttl" class="fn">set_ttl</a>(&self, ttl: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></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-13"><a class="doc-anchor" href="#examples-13">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket;
|
||
|
||
<span class="kw">let </span>sock = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
sock.set_ttl(<span class="number">60</span>)<span class="question-mark">?</span>;
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.tos" class="method"><h4 class="code-header">pub fn <a href="#method.tos" class="fn">tos</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</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="Self::set_tos"><code>set_tos</code></a>.</p>
|
||
<p><strong>NOTE:</strong> 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></p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.set_tos" class="method"><h4 class="code-header">pub fn <a href="#method.set_tos" class="fn">set_tos</a>(&self, tos: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</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>
|
||
<p><strong>NOTE:</strong> 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></p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.device" class="method"><h4 class="code-header">pub fn <a href="#method.device" class="fn">device</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="struct" href="https://doc.rust-lang.org/nightly/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>>>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</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-bound device’s interface name.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.bind_device" class="method"><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/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&[<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</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.join_multicast_v4" class="method"><h4 class="code-header">pub fn <a href="#method.join_multicast_v4" class="fn">join_multicast_v4</a>(
|
||
&self,
|
||
multiaddr: <a class="struct" href="https://doc.rust-lang.org/nightly/core/net/ip_addr/struct.Ipv4Addr.html" title="struct core::net::ip_addr::Ipv4Addr">Ipv4Addr</a>,
|
||
interface: <a class="struct" href="https://doc.rust-lang.org/nightly/core/net/ip_addr/struct.Ipv4Addr.html" title="struct core::net::ip_addr::Ipv4Addr">Ipv4Addr</a>
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Executes an operation of the <code>IP_ADD_MEMBERSHIP</code> type.</p>
|
||
<p>This function specifies a new multicast group for this socket to join.
|
||
The address must be a valid multicast address, and <code>interface</code> is the
|
||
address of the local interface with which the system should join the
|
||
multicast group. If it’s equal to <code>INADDR_ANY</code> then an appropriate
|
||
interface is chosen by the system.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.join_multicast_v6" class="method"><h4 class="code-header">pub fn <a href="#method.join_multicast_v6" class="fn">join_multicast_v6</a>(
|
||
&self,
|
||
multiaddr: &<a class="struct" href="https://doc.rust-lang.org/nightly/core/net/ip_addr/struct.Ipv6Addr.html" title="struct core::net::ip_addr::Ipv6Addr">Ipv6Addr</a>,
|
||
interface: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Executes an operation of the <code>IPV6_ADD_MEMBERSHIP</code> type.</p>
|
||
<p>This function specifies a new multicast group for this socket to join.
|
||
The address must be a valid multicast address, and <code>interface</code> is the
|
||
index of the interface to join/leave (or 0 to indicate any interface).</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.leave_multicast_v4" class="method"><h4 class="code-header">pub fn <a href="#method.leave_multicast_v4" class="fn">leave_multicast_v4</a>(
|
||
&self,
|
||
multiaddr: <a class="struct" href="https://doc.rust-lang.org/nightly/core/net/ip_addr/struct.Ipv4Addr.html" title="struct core::net::ip_addr::Ipv4Addr">Ipv4Addr</a>,
|
||
interface: <a class="struct" href="https://doc.rust-lang.org/nightly/core/net/ip_addr/struct.Ipv4Addr.html" title="struct core::net::ip_addr::Ipv4Addr">Ipv4Addr</a>
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Executes an operation of the <code>IP_DROP_MEMBERSHIP</code> type.</p>
|
||
<p>For more information about this option, see <a href="method@Self::join_multicast_v4"><code>join_multicast_v4</code></a>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.leave_multicast_v6" class="method"><h4 class="code-header">pub fn <a href="#method.leave_multicast_v6" class="fn">leave_multicast_v6</a>(
|
||
&self,
|
||
multiaddr: &<a class="struct" href="https://doc.rust-lang.org/nightly/core/net/ip_addr/struct.Ipv6Addr.html" title="struct core::net::ip_addr::Ipv6Addr">Ipv6Addr</a>,
|
||
interface: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>></h4></section></summary><div class="docblock"><p>Executes an operation of the <code>IPV6_DROP_MEMBERSHIP</code> type.</p>
|
||
<p>For more information about this option, see <a href="method@Self::join_multicast_v6"><code>join_multicast_v6</code></a>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.take_error" class="method"><h4 class="code-header">pub fn <a href="#method.take_error" class="fn">take_error</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>>, <a class="struct" href="https://doc.rust-lang.org/nightly/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>
|
||
<h5 id="examples-14"><a class="doc-anchor" href="#examples-14">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket;
|
||
<span class="kw">use </span>std::io;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() -> io::Result<()> {
|
||
<span class="comment">// Create a socket
|
||
</span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"0.0.0.0:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">if let </span><span class="prelude-val">Ok</span>(<span class="prelude-val">Some</span>(err)) = socket.take_error() {
|
||
<span class="macro">println!</span>(<span class="string">"Got error: {:?}"</span>, err);
|
||
}
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
</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-UdpSocket" class="impl"><a class="src rightside" href="../src/netwatch/udp.rs.html#108-112">source</a><a href="#impl-AsFd-for-UdpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/std/os/fd/owned/trait.AsFd.html" title="trait std::os::fd::owned::AsFd">AsFd</a> for <a class="struct" href="struct.UdpSocket.html" title="struct netwatch::UdpSocket">UdpSocket</a></h3></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/netwatch/udp.rs.html#109-111">source</a><a href="#method.as_fd" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/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/nightly/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/nightly/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-Debug-for-UdpSocket" class="impl"><a class="src rightside" href="../src/netwatch/udp.rs.html#10">source</a><a href="#impl-Debug-for-UdpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a> for <a class="struct" href="struct.UdpSocket.html" title="struct netwatch::UdpSocket">UdpSocket</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/netwatch/udp.rs.html#10">source</a><a href="#method.fmt" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html#tymethod.fmt" class="fn">fmt</a>(&self, f: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/fmt/struct.Formatter.html" title="struct core::fmt::Formatter">Formatter</a><'_>) -> <a class="type" href="https://doc.rust-lang.org/nightly/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/nightly/core/fmt/trait.Debug.html#tymethod.fmt">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-Deref-for-UdpSocket" class="impl"><a class="src rightside" href="../src/netwatch/udp.rs.html#127-133">source</a><a href="#impl-Deref-for-UdpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/deref/trait.Deref.html" title="trait core::ops::deref::Deref">Deref</a> for <a class="struct" href="struct.UdpSocket.html" title="struct netwatch::UdpSocket">UdpSocket</a></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Target" class="associatedtype trait-impl"><a href="#associatedtype.Target" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/nightly/core/ops/deref/trait.Deref.html#associatedtype.Target" class="associatedtype">Target</a> = UdpSocket</h4></section></summary><div class='docblock'>The resulting type after dereferencing.</div></details><details class="toggle method-toggle" open><summary><section id="method.deref" class="method trait-impl"><a class="src rightside" href="../src/netwatch/udp.rs.html#130-132">source</a><a href="#method.deref" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/ops/deref/trait.Deref.html#tymethod.deref" class="fn">deref</a>(&self) -> &Self::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/core/ops/deref/trait.Deref.html#associatedtype.Target" title="type core::ops::deref::Deref::Target">Target</a></h4></section></summary><div class='docblock'>Dereferences the value.</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-Drop-for-UdpSocket" class="impl"><a class="src rightside" href="../src/netwatch/udp.rs.html#135-147">source</a><a href="#impl-Drop-for-UdpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html" title="trait core::ops::drop::Drop">Drop</a> for <a class="struct" href="struct.UdpSocket.html" title="struct netwatch::UdpSocket">UdpSocket</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.drop" class="method trait-impl"><a class="src rightside" href="../src/netwatch/udp.rs.html#136-146">source</a><a href="#method.drop" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html#tymethod.drop" class="fn">drop</a>(&mut self)</h4></section></summary><div class='docblock'>Executes the destructor for this type. <a href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html#tymethod.drop">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-From%3CUdpSocket%3E-for-UdpSocket" class="impl"><a class="src rightside" href="../src/netwatch/udp.rs.html#121-125">source</a><a href="#impl-From%3CUdpSocket%3E-for-UdpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="trait core::convert::From">From</a><UdpSocket> for <a class="struct" href="struct.UdpSocket.html" title="struct netwatch::UdpSocket">UdpSocket</a></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="../src/netwatch/udp.rs.html#122-124">source</a><a href="#method.from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html#tymethod.from" class="fn">from</a>(socket: UdpSocket) -> Self</h4></section></summary><div class='docblock'>Converts to this type from the input type.</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-UdpSocket" class="impl"><a href="#impl-Freeze-for-UdpSocket" class="anchor">§</a><h3 class="code-header">impl !<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Freeze.html" title="trait core::marker::Freeze">Freeze</a> for <a class="struct" href="struct.UdpSocket.html" title="struct netwatch::UdpSocket">UdpSocket</a></h3></section><section id="impl-RefUnwindSafe-for-UdpSocket" class="impl"><a href="#impl-RefUnwindSafe-for-UdpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/panic/unwind_safe/trait.RefUnwindSafe.html" title="trait core::panic::unwind_safe::RefUnwindSafe">RefUnwindSafe</a> for <a class="struct" href="struct.UdpSocket.html" title="struct netwatch::UdpSocket">UdpSocket</a></h3></section><section id="impl-Send-for-UdpSocket" class="impl"><a href="#impl-Send-for-UdpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> for <a class="struct" href="struct.UdpSocket.html" title="struct netwatch::UdpSocket">UdpSocket</a></h3></section><section id="impl-Sync-for-UdpSocket" class="impl"><a href="#impl-Sync-for-UdpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> for <a class="struct" href="struct.UdpSocket.html" title="struct netwatch::UdpSocket">UdpSocket</a></h3></section><section id="impl-Unpin-for-UdpSocket" class="impl"><a href="#impl-Unpin-for-UdpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> for <a class="struct" href="struct.UdpSocket.html" title="struct netwatch::UdpSocket">UdpSocket</a></h3></section><section id="impl-UnwindSafe-for-UdpSocket" class="impl"><a href="#impl-UnwindSafe-for-UdpSocket" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/panic/unwind_safe/trait.UnwindSafe.html" title="trait core::panic::unwind_safe::UnwindSafe">UnwindSafe</a> for <a class="struct" href="struct.UdpSocket.html" title="struct netwatch::UdpSocket">UdpSocket</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/nightly/src/core/any.rs.html#140">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/nightly/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/nightly/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/nightly/src/core/any.rs.html#141">source</a><a href="#method.type_id" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/any/trait.Any.html#tymethod.type_id" class="fn">type_id</a>(&self) -> <a class="struct" href="https://doc.rust-lang.org/nightly/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/nightly/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/nightly/src/core/borrow.rs.html#208">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/nightly/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/nightly/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/nightly/src/core/borrow.rs.html#210">source</a><a href="#method.borrow" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html#tymethod.borrow" class="fn">borrow</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/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/nightly/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/nightly/src/core/borrow.rs.html#216">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/nightly/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/nightly/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/nightly/src/core/borrow.rs.html#217">source</a><a href="#method.borrow_mut" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut" class="fn">borrow_mut</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/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/nightly/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/nightly/src/core/convert/mod.rs.html#765">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/nightly/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-1" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#768">source</a><a href="#method.from-1" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/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-Instrument-for-T" class="impl"><a href="#impl-Instrument-for-T" class="anchor">§</a><h3 class="code-header">impl<T> Instrument for T</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.instrument" class="method trait-impl"><a href="#method.instrument" class="anchor">§</a><h4 class="code-header">fn <a class="fn">instrument</a>(self, span: Span) -> Instrumented<Self></h4></section></summary><div class='docblock'>Instruments this type with the provided [<code>Span</code>], returning an
|
||
<code>Instrumented</code> wrapper. <a>Read more</a></div></details><details class="toggle method-toggle" open><summary><section id="method.in_current_span" class="method trait-impl"><a href="#method.in_current_span" class="anchor">§</a><h4 class="code-header">fn <a class="fn">in_current_span</a>(self) -> Instrumented<Self></h4></section></summary><div class='docblock'>Instruments this type with the <a href="super::Span::current()">current</a> <a href="crate::Span"><code>Span</code></a>, returning an
|
||
<code>Instrumented</code> wrapper. <a>Read more</a></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/nightly/src/core/convert/mod.rs.html#748-750">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/nightly/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/nightly/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/nightly/src/core/convert/mod.rs.html#758">source</a><a href="#method.into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/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/nightly/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/nightly/src/core/convert/mod.rs.html#805-807">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/nightly/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/nightly/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 href="#associatedtype.Error-1" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error" class="associatedtype">Error</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/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/nightly/src/core/convert/mod.rs.html#812">source</a><a href="#method.try_from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#tymethod.try_from" class="fn">try_from</a>(value: U) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <T as <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><U>>::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/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/nightly/src/core/convert/mod.rs.html#790-792">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/nightly/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/nightly/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 href="#associatedtype.Error" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/nightly/core/convert/trait.TryInto.html#associatedtype.Error" class="associatedtype">Error</a> = <U as <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>>::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/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/nightly/src/core/convert/mod.rs.html#797">source</a><a href="#method.try_into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/convert/trait.TryInto.html#tymethod.try_into" class="fn">try_into</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><U, <U as <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>>::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/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-WithSubscriber-for-T" class="impl"><a href="#impl-WithSubscriber-for-T" class="anchor">§</a><h3 class="code-header">impl<T> WithSubscriber for T</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.with_subscriber" class="method trait-impl"><a href="#method.with_subscriber" class="anchor">§</a><h4 class="code-header">fn <a class="fn">with_subscriber</a><S>(self, subscriber: S) -> WithDispatch<Self><div class="where">where
|
||
S: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a><Dispatch>,</div></h4></section></summary><div class='docblock'>Attaches the provided <a href="super::Subscriber"><code>Subscriber</code></a> to this type, returning a
|
||
[<code>WithDispatch</code>] wrapper. <a>Read more</a></div></details><details class="toggle method-toggle" open><summary><section id="method.with_current_subscriber" class="method trait-impl"><a href="#method.with_current_subscriber" class="anchor">§</a><h4 class="code-header">fn <a class="fn">with_current_subscriber</a>(self) -> WithDispatch<Self></h4></section></summary><div class='docblock'>Attaches the current <a href="crate::dispatcher#setting-the-default-subscriber">default</a> <a href="super::Subscriber"><code>Subscriber</code></a> to this type, returning a
|
||
[<code>WithDispatch</code>] wrapper. <a>Read more</a></div></details></div></details></div></section></div></main></body></html> |