mirror of
https://github.com/NotAShelf/mpvrc.git
synced 2026-04-17 08:19:51 +00:00
986 lines
No EOL
117 KiB
HTML
986 lines
No EOL
117 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="An I/O object representing a Unix datagram socket."><title>UnixDatagram in tokio::net - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="tokio" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.1 (e71f9a9a9 2025-01-27)" data-channel="1.84.1" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-044be391.svg"></head><body class="rustdoc struct"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../tokio/index.html">tokio</a><span class="version">1.42.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Unix<wbr>Datagram</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples">Examples</a></li></ul><h3><a href="#implementations">Methods</a></h3><ul class="block method"><li><a href="#method.async_io" title="async_io">async_io</a></li><li><a href="#method.bind" title="bind">bind</a></li><li><a href="#method.connect" title="connect">connect</a></li><li><a href="#method.from_std" title="from_std">from_std</a></li><li><a href="#method.into_std" title="into_std">into_std</a></li><li><a href="#method.local_addr" title="local_addr">local_addr</a></li><li><a href="#method.pair" title="pair">pair</a></li><li><a href="#method.peer_addr" title="peer_addr">peer_addr</a></li><li><a href="#method.poll_recv" title="poll_recv">poll_recv</a></li><li><a href="#method.poll_recv_from" title="poll_recv_from">poll_recv_from</a></li><li><a href="#method.poll_recv_ready" title="poll_recv_ready">poll_recv_ready</a></li><li><a href="#method.poll_send" title="poll_send">poll_send</a></li><li><a href="#method.poll_send_ready" title="poll_send_ready">poll_send_ready</a></li><li><a href="#method.poll_send_to" title="poll_send_to">poll_send_to</a></li><li><a href="#method.readable" title="readable">readable</a></li><li><a href="#method.ready" title="ready">ready</a></li><li><a href="#method.recv" title="recv">recv</a></li><li><a href="#method.recv_buf" title="recv_buf">recv_buf</a></li><li><a href="#method.recv_buf_from" title="recv_buf_from">recv_buf_from</a></li><li><a href="#method.recv_from" title="recv_from">recv_from</a></li><li><a href="#method.send" title="send">send</a></li><li><a href="#method.send_to" title="send_to">send_to</a></li><li><a href="#method.shutdown" title="shutdown">shutdown</a></li><li><a href="#method.take_error" title="take_error">take_error</a></li><li><a href="#method.try_io" title="try_io">try_io</a></li><li><a href="#method.try_recv" title="try_recv">try_recv</a></li><li><a href="#method.try_recv_buf" title="try_recv_buf">try_recv_buf</a></li><li><a href="#method.try_recv_buf_from" title="try_recv_buf_from">try_recv_buf_from</a></li><li><a href="#method.try_recv_from" title="try_recv_from">try_recv_from</a></li><li><a href="#method.try_send" title="try_send">try_send</a></li><li><a href="#method.try_send_to" title="try_send_to">try_send_to</a></li><li><a href="#method.unbound" title="unbound">unbound</a></li><li><a href="#method.writable" title="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-UnixDatagram" title="AsFd">AsFd</a></li><li><a href="#impl-AsRawFd-for-UnixDatagram" title="AsRawFd">AsRawFd</a></li><li><a href="#impl-Debug-for-UnixDatagram" title="Debug">Debug</a></li><li><a href="#impl-TryFrom%3CUnixDatagram%3E-for-UnixDatagram" title="TryFrom<UnixDatagram>">TryFrom<UnixDatagram></a></li></ul><h3><a href="#synthetic-implementations">Auto Trait Implementations</a></h3><ul class="block synthetic-implementation"><li><a href="#impl-Freeze-for-UnixDatagram" title="!Freeze">!Freeze</a></li><li><a href="#impl-RefUnwindSafe-for-UnixDatagram" title="RefUnwindSafe">RefUnwindSafe</a></li><li><a href="#impl-Send-for-UnixDatagram" title="Send">Send</a></li><li><a href="#impl-Sync-for-UnixDatagram" title="Sync">Sync</a></li><li><a href="#impl-Unpin-for-UnixDatagram" title="Unpin">Unpin</a></li><li><a href="#impl-UnwindSafe-for-UnixDatagram" title="UnwindSafe">UnwindSafe</a></li></ul><h3><a href="#blanket-implementations">Blanket Implementations</a></h3><ul class="block blanket-implementation"><li><a href="#impl-Any-for-T" title="Any">Any</a></li><li><a href="#impl-Borrow%3CT%3E-for-T" title="Borrow<T>">Borrow<T></a></li><li><a href="#impl-BorrowMut%3CT%3E-for-T" title="BorrowMut<T>">BorrowMut<T></a></li><li><a href="#impl-From%3CT%3E-for-T" title="From<T>">From<T></a></li><li><a href="#impl-Into%3CU%3E-for-T" title="Into<U>">Into<U></a></li><li><a href="#impl-TryFrom%3CU%3E-for-T" title="TryFrom<U>">TryFrom<U></a></li><li><a href="#impl-TryInto%3CU%3E-for-T" title="TryInto<U>">TryInto<U></a></li></ul></section><div id="rustdoc-modnav"><h2><a href="index.html">In tokio::<wbr>net</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="../index.html">tokio</a>::<wbr><a href="index.html">net</a></span><h1>Struct <span class="struct">UnixDatagram</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../../src/tokio/net/unix/datagram/socket.rs.html#93-95">Source</a> </span></div><pre class="rust item-decl"><code>pub struct UnixDatagram { <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>An I/O object representing a Unix datagram socket.</p>
|
||
<p>A socket can be either named (associated with a filesystem path) or
|
||
unnamed.</p>
|
||
<p>This type does not provide a <code>split</code> method, because this functionality
|
||
can be achieved by wrapping the socket in an <a href="https://doc.rust-lang.org/1.84.1/alloc/sync/struct.Arc.html" title="struct alloc::sync::Arc"><code>Arc</code></a>. Note that you do
|
||
not need a <code>Mutex</code> to share the <code>UnixDatagram</code> — an <code>Arc<UnixDatagram></code>
|
||
is enough. This is because all of the methods take <code>&self</code> instead of
|
||
<code>&mut self</code>.</p>
|
||
<p><strong>Note:</strong> named sockets are persisted even after the object is dropped
|
||
and the program has exited, and cannot be reconnected. It is advised
|
||
that you either check for and unlink the existing socket if it exists,
|
||
or use a temporary file that is guaranteed to not already exist.</p>
|
||
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
|
||
<p>Using named sockets, associated with a filesystem path:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
<span class="kw">use </span>tempfile::tempdir;
|
||
|
||
<span class="comment">// We use a temporary directory so that the socket
|
||
// files left by the bound sockets will get cleaned up.
|
||
</span><span class="kw">let </span>tmp = tempdir()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Bind each socket to a filesystem path
|
||
</span><span class="kw">let </span>tx_path = tmp.path().join(<span class="string">"tx"</span>);
|
||
<span class="kw">let </span>tx = UnixDatagram::bind(<span class="kw-2">&</span>tx_path)<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>rx_path = tmp.path().join(<span class="string">"rx"</span>);
|
||
<span class="kw">let </span>rx = UnixDatagram::bind(<span class="kw-2">&</span>rx_path)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>bytes = <span class="string">b"hello world"</span>;
|
||
tx.send_to(bytes, <span class="kw-2">&</span>rx_path).<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">24</span>];
|
||
<span class="kw">let </span>(size, addr) = rx.recv_from(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>dgram = <span class="kw-2">&</span>buf[..size];
|
||
<span class="macro">assert_eq!</span>(dgram, bytes);
|
||
<span class="macro">assert_eq!</span>(addr.as_pathname().unwrap(), <span class="kw-2">&</span>tx_path);
|
||
</code></pre></div>
|
||
<p>Using unnamed sockets, created as a pair</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
|
||
<span class="comment">// Create the pair of sockets
|
||
</span><span class="kw">let </span>(sock1, sock2) = UnixDatagram::pair()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Since the sockets are paired, the paired send/recv
|
||
// functions can be used
|
||
</span><span class="kw">let </span>bytes = <span class="string">b"hello world"</span>;
|
||
sock1.send(bytes).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>buff = <span class="macro">vec!</span>[<span class="number">0u8</span>; <span class="number">24</span>];
|
||
<span class="kw">let </span>size = sock2.recv(<span class="kw-2">&mut </span>buff).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>dgram = <span class="kw-2">&</span>buff[..size];
|
||
<span class="macro">assert_eq!</span>(dgram, bytes);
|
||
</code></pre></div>
|
||
</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-UnixDatagram" class="impl"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#98-1565">Source</a><a href="#impl-UnixDatagram" class="anchor">§</a><h3 class="code-header">impl <a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.ready" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#179-182">Source</a><h4 class="code-header">pub async fn <a href="#method.ready" class="fn">ready</a>(&self, interest: <a class="struct" href="../io/struct.Interest.html" title="struct tokio::io::Interest">Interest</a>) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="../io/struct.Ready.html" title="struct tokio::io::Ready">Ready</a>></h4></section></summary><div class="docblock"><p>Waits for any of the requested ready states.</p>
|
||
<p>This function is usually paired with <code>try_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
|
||
<a href="../io/struct.Ready.html" title="struct tokio::io::Ready"><code>Ready</code></a> set, so you should always check the returned value and possibly
|
||
wait again if the requested states are not set.</p>
|
||
<h5 id="cancel-safety"><a 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-1"><a class="doc-anchor" href="#examples-1">§</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::Interest;
|
||
<span class="kw">use </span>tokio::net::UnixDatagram;
|
||
<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>dir = tempfile::tempdir().unwrap();
|
||
<span class="kw">let </span>client_path = dir.path().join(<span class="string">"client.sock"</span>);
|
||
<span class="kw">let </span>server_path = dir.path().join(<span class="string">"server.sock"</span>);
|
||
<span class="kw">let </span>socket = UnixDatagram::bind(<span class="kw-2">&</span>client_path)<span class="question-mark">?</span>;
|
||
socket.connect(<span class="kw-2">&</span>server_path)<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="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"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#236-239">Source</a><h4 class="code-header">pub async fn <a href="#method.writable" class="fn">writable</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.unit.html">()</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-2"><a class="doc-anchor" href="#examples-2">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
<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>dir = tempfile::tempdir().unwrap();
|
||
<span class="kw">let </span>client_path = dir.path().join(<span class="string">"client.sock"</span>);
|
||
<span class="kw">let </span>server_path = dir.path().join(<span class="string">"server.sock"</span>);
|
||
<span class="kw">let </span>socket = UnixDatagram::bind(<span class="kw-2">&</span>client_path)<span class="question-mark">?</span>;
|
||
socket.connect(<span class="kw-2">&</span>server_path)<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"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#270-272">Source</a><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/1.84.1/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>) -> <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.unit.html">()</a>>></h4></section></summary><div class="docblock"><p>Polls for write/send readiness.</p>
|
||
<p>If the socket 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 socket
|
||
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="struct.UnixDatagram.html#method.writable" title="method tokio::net::UnixDatagram::writable"><code>writable</code></a> is not feasible. Where possible, using <a href="struct.UnixDatagram.html#method.writable" title="method tokio::net::UnixDatagram::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 socket is not ready for writing.</li>
|
||
<li><code>Poll::Ready(Ok(()))</code> if the socket 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.readable" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#332-335">Source</a><h4 class="code-header">pub async fn <a href="#method.readable" class="fn">readable</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.unit.html">()</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-2"><a class="doc-anchor" href="#cancel-safety-2">§</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-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::UnixDatagram;
|
||
<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>dir = tempfile::tempdir().unwrap();
|
||
<span class="kw">let </span>client_path = dir.path().join(<span class="string">"client.sock"</span>);
|
||
<span class="kw">let </span>server_path = dir.path().join(<span class="string">"server.sock"</span>);
|
||
<span class="kw">let </span>socket = UnixDatagram::bind(<span class="kw-2">&</span>client_path)<span class="question-mark">?</span>;
|
||
socket.connect(<span class="kw-2">&</span>server_path)<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"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#366-368">Source</a><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/1.84.1/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>) -> <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.unit.html">()</a>>></h4></section></summary><div class="docblock"><p>Polls for read/receive readiness.</p>
|
||
<p>If the socket 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
|
||
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="struct.UnixDatagram.html#method.readable" title="method tokio::net::UnixDatagram::readable"><code>readable</code></a> is not feasible. Where possible, using <a href="struct.UnixDatagram.html#method.readable" title="method tokio::net::UnixDatagram::readable"><code>readable</code></a> is
|
||
preferred, as this supports polling from multiple tasks at once.</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 ready for reading.</li>
|
||
<li><code>Poll::Ready(Ok(()))</code> if the socket is ready for reading.</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.bind" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#391-397">Source</a><h4 class="code-header">pub fn <a href="#method.bind" class="fn">bind</a><P>(path: P) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a>><div class="where">where
|
||
P: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a><<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/path/struct.Path.html" title="struct std::path::Path">Path</a>>,</div></h4></section></summary><div class="docblock"><p>Creates a new <code>UnixDatagram</code> bound to the specified path.</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::UnixDatagram;
|
||
<span class="kw">use </span>tempfile::tempdir;
|
||
|
||
<span class="comment">// We use a temporary directory so that the socket
|
||
// files left by the bound sockets will get cleaned up.
|
||
</span><span class="kw">let </span>tmp = tempdir()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Bind the socket to a filesystem path
|
||
</span><span class="kw">let </span>socket_path = tmp.path().join(<span class="string">"socket"</span>);
|
||
<span class="kw">let </span>socket = UnixDatagram::bind(<span class="kw-2">&</span>socket_path)<span class="question-mark">?</span>;
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.pair" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#428-434">Source</a><h4 class="code-header">pub fn <a href="#method.pair" class="fn">pair</a>() -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><(<a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a>, <a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a>)></h4></section></summary><div class="docblock"><p>Creates an unnamed pair of connected sockets.</p>
|
||
<p>This function will create a pair of interconnected Unix sockets for
|
||
communicating back and forth between one another.</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::UnixDatagram;
|
||
|
||
<span class="comment">// Create the pair of sockets
|
||
</span><span class="kw">let </span>(sock1, sock2) = UnixDatagram::pair()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Since the sockets are paired, the paired send/recv
|
||
// functions can be used
|
||
</span><span class="kw">let </span>bytes = <span class="string">b"hail eris"</span>;
|
||
sock1.send(bytes).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>buff = <span class="macro">vec!</span>[<span class="number">0u8</span>; <span class="number">24</span>];
|
||
<span class="kw">let </span>size = sock2.recv(<span class="kw-2">&mut </span>buff).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>dgram = <span class="kw-2">&</span>buff[..size];
|
||
<span class="macro">assert_eq!</span>(dgram, bytes);
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.from_std" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#481-485">Source</a><h4 class="code-header">pub fn <a href="#method.from_std" class="fn">from_std</a>(datagram: <a class="struct" href="https://doc.rust-lang.org/1.84.1/std/os/unix/net/datagram/struct.UnixDatagram.html" title="struct std::os::unix::net::datagram::UnixDatagram">UnixDatagram</a>) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a>></h4></section></summary><div class="docblock"><p>Creates new <a href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram"><code>UnixDatagram</code></a> from a <a href="https://doc.rust-lang.org/1.84.1/std/os/unix/net/datagram/struct.UnixDatagram.html" title="struct std::os::unix::net::datagram::UnixDatagram"><code>std::os::unix::net::UnixDatagram</code></a>.</p>
|
||
<p>This function is intended to be used to wrap a <code>UnixDatagram</code> from the
|
||
standard library in the Tokio equivalent.</p>
|
||
<h5 id="notes"><a class="doc-anchor" href="#notes">§</a>Notes</h5>
|
||
<p>The caller is responsible for ensuring that the socket is in
|
||
non-blocking mode. Otherwise all I/O operations on the socket
|
||
will block the thread, which will cause unexpected behavior.
|
||
Non-blocking mode can be set using <a href="https://doc.rust-lang.org/1.84.1/std/os/unix/net/datagram/struct.UnixDatagram.html#method.set_nonblocking" title="method std::os::unix::net::datagram::UnixDatagram::set_nonblocking"><code>set_nonblocking</code></a>.</p>
|
||
<h5 id="panics"><a class="doc-anchor" href="#panics">§</a>Panics</h5>
|
||
<p>This function panics if it is not called from within a runtime with
|
||
IO enabled.</p>
|
||
<p>The runtime is usually set implicitly when this function is called
|
||
from a future driven by a Tokio runtime, otherwise runtime can be set
|
||
explicitly with <a href="../runtime/struct.Runtime.html#method.enter" title="method tokio::runtime::Runtime::enter"><code>Runtime::enter</code></a> function.</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::UnixDatagram;
|
||
<span class="kw">use </span>std::os::unix::net::UnixDatagram <span class="kw">as </span>StdUDS;
|
||
<span class="kw">use </span>tempfile::tempdir;
|
||
|
||
<span class="comment">// We use a temporary directory so that the socket
|
||
// files left by the bound sockets will get cleaned up.
|
||
</span><span class="kw">let </span>tmp = tempdir()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Bind the socket to a filesystem path
|
||
</span><span class="kw">let </span>socket_path = tmp.path().join(<span class="string">"socket"</span>);
|
||
<span class="kw">let </span>std_socket = StdUDS::bind(<span class="kw-2">&</span>socket_path)<span class="question-mark">?</span>;
|
||
std_socket.set_nonblocking(<span class="bool-val">true</span>)<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>tokio_socket = UnixDatagram::from_std(std_socket)<span class="question-mark">?</span>;
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.into_std" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#508-513">Source</a><h4 class="code-header">pub fn <a href="#method.into_std" class="fn">into_std</a>(self) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/os/unix/net/datagram/struct.UnixDatagram.html" title="struct std::os::unix::net::datagram::UnixDatagram">UnixDatagram</a>></h4></section></summary><div class="docblock"><p>Turns a <a href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram"><code>tokio::net::UnixDatagram</code></a> into a <a href="https://doc.rust-lang.org/1.84.1/std/os/unix/net/datagram/struct.UnixDatagram.html" title="struct std::os::unix::net::datagram::UnixDatagram"><code>std::os::unix::net::UnixDatagram</code></a>.</p>
|
||
<p>The returned <a href="https://doc.rust-lang.org/1.84.1/std/os/unix/net/datagram/struct.UnixDatagram.html" title="struct std::os::unix::net::datagram::UnixDatagram"><code>std::os::unix::net::UnixDatagram</code></a> will have nonblocking
|
||
mode set as <code>true</code>. Use <a href="https://doc.rust-lang.org/1.84.1/std/os/unix/net/datagram/struct.UnixDatagram.html#method.set_nonblocking" title="method std::os::unix::net::datagram::UnixDatagram::set_nonblocking"><code>set_nonblocking</code></a> to change the blocking mode
|
||
if needed.</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">let </span>tokio_socket = tokio::net::UnixDatagram::bind(<span class="string">"/path/to/the/socket"</span>)<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>std_socket = tokio_socket.into_std()<span class="question-mark">?</span>;
|
||
std_socket.set_nonblocking(<span class="bool-val">false</span>)<span class="question-mark">?</span>;</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.unbound" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#551-554">Source</a><h4 class="code-header">pub fn <a href="#method.unbound" class="fn">unbound</a>() -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a>></h4></section></summary><div class="docblock"><p>Creates a new <code>UnixDatagram</code> which is not bound to any address.</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::UnixDatagram;
|
||
<span class="kw">use </span>tempfile::tempdir;
|
||
|
||
<span class="comment">// Create an unbound socket
|
||
</span><span class="kw">let </span>tx = UnixDatagram::unbound()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Create another, bound socket
|
||
</span><span class="kw">let </span>tmp = tempdir()<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>rx_path = tmp.path().join(<span class="string">"rx"</span>);
|
||
<span class="kw">let </span>rx = UnixDatagram::bind(<span class="kw-2">&</span>rx_path)<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Send to the bound socket
|
||
</span><span class="kw">let </span>bytes = <span class="string">b"hello world"</span>;
|
||
tx.send_to(bytes, <span class="kw-2">&</span>rx_path).<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">24</span>];
|
||
<span class="kw">let </span>(size, addr) = rx.recv_from(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>dgram = <span class="kw-2">&</span>buf[..size];
|
||
<span class="macro">assert_eq!</span>(dgram, bytes);
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.connect" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#593-595">Source</a><h4 class="code-header">pub fn <a href="#method.connect" class="fn">connect</a><P: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a><<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/path/struct.Path.html" title="struct std::path::Path">Path</a>>>(&self, path: P) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Connects the socket to the specified address.</p>
|
||
<p>The <code>send</code> method may be used to send data to the specified address.
|
||
<code>recv</code> and <code>recv_from</code> will only receive data from that address.</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::UnixDatagram;
|
||
<span class="kw">use </span>tempfile::tempdir;
|
||
|
||
<span class="comment">// Create an unbound socket
|
||
</span><span class="kw">let </span>tx = UnixDatagram::unbound()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Create another, bound socket
|
||
</span><span class="kw">let </span>tmp = tempdir()<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>rx_path = tmp.path().join(<span class="string">"rx"</span>);
|
||
<span class="kw">let </span>rx = UnixDatagram::bind(<span class="kw-2">&</span>rx_path)<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Connect to the bound socket
|
||
</span>tx.connect(<span class="kw-2">&</span>rx_path)<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Send to the bound socket
|
||
</span><span class="kw">let </span>bytes = <span class="string">b"hello world"</span>;
|
||
tx.send(bytes).<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">24</span>];
|
||
<span class="kw">let </span>(size, addr) = rx.recv_from(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>dgram = <span class="kw-2">&</span>buf[..size];
|
||
<span class="macro">assert_eq!</span>(dgram, bytes);
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.send" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#629-634">Source</a><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/1.84.1/std/primitive.u8.html">u8</a>]) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>></h4></section></summary><div class="docblock"><p>Sends data on the socket to the socket’s peer.</p>
|
||
<h5 id="cancel-safety-3"><a class="doc-anchor" href="#cancel-safety-3">§</a>Cancel safety</h5>
|
||
<p>This method is cancel safe. If <code>send</code> is used as the event in a
|
||
<a href="../macro.select.html" title="macro tokio::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-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::UnixDatagram;
|
||
|
||
<span class="comment">// Create the pair of sockets
|
||
</span><span class="kw">let </span>(sock1, sock2) = UnixDatagram::pair()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Since the sockets are paired, the paired send/recv
|
||
// functions can be used
|
||
</span><span class="kw">let </span>bytes = <span class="string">b"hello world"</span>;
|
||
sock1.send(bytes).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>buff = <span class="macro">vec!</span>[<span class="number">0u8</span>; <span class="number">24</span>];
|
||
<span class="kw">let </span>size = sock2.recv(<span class="kw-2">&mut </span>buff).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>dgram = <span class="kw-2">&</span>buff[..size];
|
||
<span class="macro">assert_eq!</span>(dgram, bytes);
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_send" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#674-678">Source</a><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/1.84.1/std/primitive.u8.html">u8</a>]) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>></h4></section></summary><div class="docblock"><p>Tries to send a datagram to the peer without waiting.</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::UnixDatagram;
|
||
<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>dir = tempfile::tempdir().unwrap();
|
||
<span class="kw">let </span>client_path = dir.path().join(<span class="string">"client.sock"</span>);
|
||
<span class="kw">let </span>server_path = dir.path().join(<span class="string">"server.sock"</span>);
|
||
<span class="kw">let </span>socket = UnixDatagram::bind(<span class="kw-2">&</span>client_path)<span class="question-mark">?</span>;
|
||
socket.connect(<span class="kw-2">&</span>server_path)<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.try_send_to" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#717-724">Source</a><h4 class="code-header">pub fn <a href="#method.try_send_to" class="fn">try_send_to</a><P>(&self, buf: &[<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.u8.html">u8</a>], target: P) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>><div class="where">where
|
||
P: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a><<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/path/struct.Path.html" title="struct std::path::Path">Path</a>>,</div></h4></section></summary><div class="docblock"><p>Tries to send a datagram to the peer without waiting.</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::UnixDatagram;
|
||
<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>dir = tempfile::tempdir().unwrap();
|
||
<span class="kw">let </span>client_path = dir.path().join(<span class="string">"client.sock"</span>);
|
||
<span class="kw">let </span>server_path = dir.path().join(<span class="string">"server.sock"</span>);
|
||
<span class="kw">let </span>socket = UnixDatagram::bind(<span class="kw-2">&</span>client_path)<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_to(<span class="string">b"hello world"</span>, <span class="kw-2">&</span>server_path) {
|
||
<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.recv" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#759-764">Source</a><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/1.84.1/std/primitive.u8.html">u8</a>]) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>></h4></section></summary><div class="docblock"><p>Receives data from the socket.</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="../macro.select.html" title="macro tokio::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="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::UnixDatagram;
|
||
|
||
<span class="comment">// Create the pair of sockets
|
||
</span><span class="kw">let </span>(sock1, sock2) = UnixDatagram::pair()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Since the sockets are paired, the paired send/recv
|
||
// functions can be used
|
||
</span><span class="kw">let </span>bytes = <span class="string">b"hello world"</span>;
|
||
sock1.send(bytes).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>buff = <span class="macro">vec!</span>[<span class="number">0u8</span>; <span class="number">24</span>];
|
||
<span class="kw">let </span>size = sock2.recv(<span class="kw-2">&mut </span>buff).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>dgram = <span class="kw-2">&</span>buff[..size];
|
||
<span class="macro">assert_eq!</span>(dgram, bytes);
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_recv" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#810-814">Source</a><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/1.84.1/std/primitive.u8.html">u8</a>]) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>></h4></section></summary><div class="docblock"><p>Tries to receive a datagram from the peer without waiting.</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::UnixDatagram;
|
||
<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>dir = tempfile::tempdir().unwrap();
|
||
<span class="kw">let </span>client_path = dir.path().join(<span class="string">"client.sock"</span>);
|
||
<span class="kw">let </span>server_path = dir.path().join(<span class="string">"server.sock"</span>);
|
||
<span class="kw">let </span>socket = UnixDatagram::bind(<span class="kw-2">&</span>client_path)<span class="question-mark">?</span>;
|
||
socket.connect(<span class="kw-2">&</span>server_path)<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_from" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#860-878">Source</a><h4 class="code-header">pub fn <a href="#method.try_recv_buf_from" class="fn">try_recv_buf_from</a><B: <a class="trait" href="../../bytes/buf/buf_mut/trait.BufMut.html" title="trait bytes::buf::buf_mut::BufMut">BufMut</a>>(
|
||
&self,
|
||
buf: <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&mut B</a>,
|
||
) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>, <a class="struct" href="unix/struct.SocketAddr.html" title="struct tokio::net::unix::SocketAddr">SocketAddr</a>)></h4></section></summary><div class="docblock"><p>Tries to receive data from the socket without waiting.</p>
|
||
<p>This method can be used even if <code>buf</code> is uninitialized.</p>
|
||
<h5 id="examples-15"><a class="doc-anchor" href="#examples-15">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
<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>dir = tempfile::tempdir().unwrap();
|
||
<span class="kw">let </span>client_path = dir.path().join(<span class="string">"client.sock"</span>);
|
||
<span class="kw">let </span>server_path = dir.path().join(<span class="string">"server.sock"</span>);
|
||
<span class="kw">let </span>socket = UnixDatagram::bind(<span class="kw-2">&</span>client_path)<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"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#916-931">Source</a><h4 class="code-header">pub async fn <a href="#method.recv_buf_from" class="fn">recv_buf_from</a><B: <a class="trait" href="../../bytes/buf/buf_mut/trait.BufMut.html" title="trait bytes::buf::buf_mut::BufMut">BufMut</a>>(
|
||
&self,
|
||
buf: <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&mut B</a>,
|
||
) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>, <a class="struct" href="unix/struct.SocketAddr.html" title="struct tokio::net::unix::SocketAddr">SocketAddr</a>)></h4></section></summary><div class="docblock"><p>Receives from the socket, advances the
|
||
buffer’s internal cursor and returns how many bytes were read and the origin.</p>
|
||
<p>This method can be used even if <code>buf</code> is uninitialized.</p>
|
||
<h5 id="examples-16"><a class="doc-anchor" href="#examples-16">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
<span class="kw">use </span>tempfile::tempdir;
|
||
|
||
<span class="comment">// We use a temporary directory so that the socket
|
||
// files left by the bound sockets will get cleaned up.
|
||
</span><span class="kw">let </span>tmp = tempdir()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Bind each socket to a filesystem path
|
||
</span><span class="kw">let </span>tx_path = tmp.path().join(<span class="string">"tx"</span>);
|
||
<span class="kw">let </span>tx = UnixDatagram::bind(<span class="kw-2">&</span>tx_path)<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>rx_path = tmp.path().join(<span class="string">"rx"</span>);
|
||
<span class="kw">let </span>rx = UnixDatagram::bind(<span class="kw-2">&</span>rx_path)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>bytes = <span class="string">b"hello world"</span>;
|
||
tx.send_to(bytes, <span class="kw-2">&</span>rx_path).<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">24</span>);
|
||
<span class="kw">let </span>(size, addr) = rx.recv_buf_from(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>dgram = <span class="kw-2">&</span>buf[..size];
|
||
<span class="macro">assert_eq!</span>(dgram, bytes);
|
||
<span class="macro">assert_eq!</span>(addr.as_pathname().unwrap(), <span class="kw-2">&</span>tx_path);
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_recv_buf" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#978-994">Source</a><h4 class="code-header">pub fn <a href="#method.try_recv_buf" class="fn">try_recv_buf</a><B: <a class="trait" href="../../bytes/buf/buf_mut/trait.BufMut.html" title="trait bytes::buf::buf_mut::BufMut">BufMut</a>>(&self, buf: <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&mut B</a>) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>></h4></section></summary><div class="docblock"><p>Tries to read data from the stream into the provided buffer, advancing the
|
||
buffer’s internal cursor, returning how many bytes were read.</p>
|
||
<p>This method can be used even if <code>buf</code> is uninitialized.</p>
|
||
<h5 id="examples-17"><a class="doc-anchor" href="#examples-17">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
<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>dir = tempfile::tempdir().unwrap();
|
||
<span class="kw">let </span>client_path = dir.path().join(<span class="string">"client.sock"</span>);
|
||
<span class="kw">let </span>server_path = dir.path().join(<span class="string">"server.sock"</span>);
|
||
<span class="kw">let </span>socket = UnixDatagram::bind(<span class="kw-2">&</span>client_path)<span class="question-mark">?</span>;
|
||
socket.connect(<span class="kw-2">&</span>server_path)<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"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1025-1040">Source</a><h4 class="code-header">pub async fn <a href="#method.recv_buf" class="fn">recv_buf</a><B: <a class="trait" href="../../bytes/buf/buf_mut/trait.BufMut.html" title="trait bytes::buf::buf_mut::BufMut">BufMut</a>>(&self, buf: <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&mut B</a>) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>></h4></section></summary><div class="docblock"><p>Receives data from the socket from the address to which it is connected,
|
||
advancing the buffer’s internal cursor, returning how many bytes were read.</p>
|
||
<p>This method can be used even if <code>buf</code> is uninitialized.</p>
|
||
<h5 id="examples-18"><a class="doc-anchor" href="#examples-18">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
|
||
<span class="comment">// Create the pair of sockets
|
||
</span><span class="kw">let </span>(sock1, sock2) = UnixDatagram::pair()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Since the sockets are paired, the paired send/recv
|
||
// functions can be used
|
||
</span><span class="kw">let </span>bytes = <span class="string">b"hello world"</span>;
|
||
sock1.send(bytes).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>buff = Vec::with_capacity(<span class="number">24</span>);
|
||
<span class="kw">let </span>size = sock2.recv_buf(<span class="kw-2">&mut </span>buff).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>dgram = <span class="kw-2">&</span>buff[..size];
|
||
<span class="macro">assert_eq!</span>(dgram, bytes);
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.send_to" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1082-1090">Source</a><h4 class="code-header">pub async fn <a href="#method.send_to" class="fn">send_to</a><P>(&self, buf: &[<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.u8.html">u8</a>], target: P) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>><div class="where">where
|
||
P: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a><<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/path/struct.Path.html" title="struct std::path::Path">Path</a>>,</div></h4></section></summary><div class="docblock"><p>Sends data on the socket to the specified address.</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="../macro.select.html" title="macro tokio::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-19"><a class="doc-anchor" href="#examples-19">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
<span class="kw">use </span>tempfile::tempdir;
|
||
|
||
<span class="comment">// We use a temporary directory so that the socket
|
||
// files left by the bound sockets will get cleaned up.
|
||
</span><span class="kw">let </span>tmp = tempdir()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Bind each socket to a filesystem path
|
||
</span><span class="kw">let </span>tx_path = tmp.path().join(<span class="string">"tx"</span>);
|
||
<span class="kw">let </span>tx = UnixDatagram::bind(<span class="kw-2">&</span>tx_path)<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>rx_path = tmp.path().join(<span class="string">"rx"</span>);
|
||
<span class="kw">let </span>rx = UnixDatagram::bind(<span class="kw-2">&</span>rx_path)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>bytes = <span class="string">b"hello world"</span>;
|
||
tx.send_to(bytes, <span class="kw-2">&</span>rx_path).<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">24</span>];
|
||
<span class="kw">let </span>(size, addr) = rx.recv_from(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>dgram = <span class="kw-2">&</span>buf[..size];
|
||
<span class="macro">assert_eq!</span>(dgram, bytes);
|
||
<span class="macro">assert_eq!</span>(addr.as_pathname().unwrap(), <span class="kw-2">&</span>tx_path);
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.recv_from" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1132-1140">Source</a><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/1.84.1/std/primitive.u8.html">u8</a>]) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>, <a class="struct" href="unix/struct.SocketAddr.html" title="struct tokio::net::unix::SocketAddr">SocketAddr</a>)></h4></section></summary><div class="docblock"><p>Receives data from the socket.</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="../macro.select.html" title="macro tokio::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="examples-20"><a class="doc-anchor" href="#examples-20">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
<span class="kw">use </span>tempfile::tempdir;
|
||
|
||
<span class="comment">// We use a temporary directory so that the socket
|
||
// files left by the bound sockets will get cleaned up.
|
||
</span><span class="kw">let </span>tmp = tempdir()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Bind each socket to a filesystem path
|
||
</span><span class="kw">let </span>tx_path = tmp.path().join(<span class="string">"tx"</span>);
|
||
<span class="kw">let </span>tx = UnixDatagram::bind(<span class="kw-2">&</span>tx_path)<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>rx_path = tmp.path().join(<span class="string">"rx"</span>);
|
||
<span class="kw">let </span>rx = UnixDatagram::bind(<span class="kw-2">&</span>rx_path)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>bytes = <span class="string">b"hello world"</span>;
|
||
tx.send_to(bytes, <span class="kw-2">&</span>rx_path).<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">24</span>];
|
||
<span class="kw">let </span>(size, addr) = rx.recv_from(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>dgram = <span class="kw-2">&</span>buf[..size];
|
||
<span class="macro">assert_eq!</span>(dgram, bytes);
|
||
<span class="macro">assert_eq!</span>(addr.as_pathname().unwrap(), <span class="kw-2">&</span>tx_path);
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.poll_recv_from" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1159-1180">Source</a><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/1.84.1/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>,
|
||
buf: &mut <a class="struct" href="../io/struct.ReadBuf.html" title="struct tokio::io::ReadBuf">ReadBuf</a><'_>,
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="unix/struct.SocketAddr.html" title="struct tokio::net::unix::SocketAddr">SocketAddr</a>>></h4></section></summary><div class="docblock"><p>Attempts to receive a single datagram on the specified address.</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-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 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-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.poll_send_to" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1199-1211">Source</a><h4 class="code-header">pub fn <a href="#method.poll_send_to" class="fn">poll_send_to</a><P>(
|
||
&self,
|
||
cx: &mut <a class="struct" href="https://doc.rust-lang.org/1.84.1/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>,
|
||
buf: &[<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.u8.html">u8</a>],
|
||
target: P,
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>>><div class="where">where
|
||
P: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a><<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/path/struct.Path.html" title="struct std::path::Path">Path</a>>,</div></h4></section></summary><div class="docblock"><p>Attempts to send data to the specified 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-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 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-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.poll_send" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1236-1240">Source</a><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/1.84.1/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>, buf: &[<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.u8.html">u8</a>]) -> <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</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="struct.UnixDatagram.html#method.connect" title="method tokio::net::UnixDatagram::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-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 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-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.poll_recv" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1265-1282">Source</a><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/1.84.1/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>,
|
||
buf: &mut <a class="struct" href="../io/struct.ReadBuf.html" title="struct tokio::io::ReadBuf">ReadBuf</a><'_>,
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.unit.html">()</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="struct.UnixDatagram.html#method.connect" title="method tokio::net::UnixDatagram::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-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(()))</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-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>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_recv_from" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1327-1334">Source</a><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/1.84.1/std/primitive.u8.html">u8</a>]) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>, <a class="struct" href="unix/struct.SocketAddr.html" title="struct tokio::net::unix::SocketAddr">SocketAddr</a>)></h4></section></summary><div class="docblock"><p>Tries to receive data from the socket without waiting.</p>
|
||
<h5 id="examples-21"><a class="doc-anchor" href="#examples-21">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
<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>dir = tempfile::tempdir().unwrap();
|
||
<span class="kw">let </span>client_path = dir.path().join(<span class="string">"client.sock"</span>);
|
||
<span class="kw">let </span>server_path = dir.path().join(<span class="string">"server.sock"</span>);
|
||
<span class="kw">let </span>socket = UnixDatagram::bind(<span class="kw-2">&</span>client_path)<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"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1368-1376">Source</a><h4 class="code-header">pub fn <a href="#method.try_io" class="fn">try_io</a><R>(
|
||
&self,
|
||
interest: <a class="struct" href="../io/struct.Interest.html" title="struct tokio::io::Interest">Interest</a>,
|
||
f: impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><R>,
|
||
) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><R></h4></section></summary><div class="docblock"><p>Tries to read or write from the socket using a user-provided IO operation.</p>
|
||
<p>If the socket is ready, the provided closure is called. The closure
|
||
should attempt to perform IO operation on the socket by manually
|
||
calling the appropriate syscall. If the operation fails because the
|
||
socket is not actually ready, then the closure should return a
|
||
<code>WouldBlock</code> error and the readiness flag is cleared. The return value
|
||
of the closure is then returned by <code>try_io</code>.</p>
|
||
<p>If the socket is not ready, then the closure is not called
|
||
and a <code>WouldBlock</code> error is returned.</p>
|
||
<p>The closure should only return a <code>WouldBlock</code> error if it has performed
|
||
an IO operation on the socket that failed due to the socket not being
|
||
ready. Returning a <code>WouldBlock</code> error in any other situation will
|
||
incorrectly clear the readiness flag, which can cause the socket to
|
||
behave incorrectly.</p>
|
||
<p>The closure should not perform the IO operation using any of the methods
|
||
defined on the Tokio <code>UnixDatagram</code> type, as this will mess with the
|
||
readiness flag and can cause the socket to behave incorrectly.</p>
|
||
<p>This method is not intended to be used with combined interests.
|
||
The closure should perform only one type of IO operation, so it should not
|
||
require more than one ready state. This method may panic or sleep forever
|
||
if it is called with a combined interest.</p>
|
||
<p>Usually, <a href="struct.UnixDatagram.html#method.readable" title="method tokio::net::UnixDatagram::readable"><code>readable()</code></a>, <a href="struct.UnixDatagram.html#method.writable" title="method tokio::net::UnixDatagram::writable"><code>writable()</code></a> or <a href="struct.UnixDatagram.html#method.ready" title="method tokio::net::UnixDatagram::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"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1403-1412">Source</a><h4 class="code-header">pub async fn <a href="#method.async_io" class="fn">async_io</a><R>(
|
||
&self,
|
||
interest: <a class="struct" href="../io/struct.Interest.html" title="struct tokio::io::Interest">Interest</a>,
|
||
f: impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>() -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><R>,
|
||
) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><R></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>UnixDatagram</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.local_addr" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1454-1456">Source</a><h4 class="code-header">pub fn <a href="#method.local_addr" class="fn">local_addr</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="unix/struct.SocketAddr.html" title="struct tokio::net::unix::SocketAddr">SocketAddr</a>></h4></section></summary><div class="docblock"><p>Returns the local address that this socket is bound to.</p>
|
||
<h5 id="examples-22"><a class="doc-anchor" href="#examples-22">§</a>Examples</h5>
|
||
<p>For a socket bound to a local path</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
<span class="kw">use </span>tempfile::tempdir;
|
||
|
||
<span class="comment">// We use a temporary directory so that the socket
|
||
// files left by the bound sockets will get cleaned up.
|
||
</span><span class="kw">let </span>tmp = tempdir()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Bind socket to a filesystem path
|
||
</span><span class="kw">let </span>socket_path = tmp.path().join(<span class="string">"socket"</span>);
|
||
<span class="kw">let </span>socket = UnixDatagram::bind(<span class="kw-2">&</span>socket_path)<span class="question-mark">?</span>;
|
||
|
||
<span class="macro">assert_eq!</span>(socket.local_addr()<span class="question-mark">?</span>.as_pathname().unwrap(), <span class="kw-2">&</span>socket_path);
|
||
</code></pre></div>
|
||
<p>For an unbound socket</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
|
||
<span class="comment">// Create an unbound socket
|
||
</span><span class="kw">let </span>socket = UnixDatagram::unbound()<span class="question-mark">?</span>;
|
||
|
||
<span class="macro">assert!</span>(socket.local_addr()<span class="question-mark">?</span>.is_unnamed());
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.peer_addr" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1503-1505">Source</a><h4 class="code-header">pub fn <a href="#method.peer_addr" class="fn">peer_addr</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="unix/struct.SocketAddr.html" title="struct tokio::net::unix::SocketAddr">SocketAddr</a>></h4></section></summary><div class="docblock"><p>Returns the address of this socket’s peer.</p>
|
||
<p>The <code>connect</code> method will connect the socket to a peer.</p>
|
||
<h5 id="examples-23"><a class="doc-anchor" href="#examples-23">§</a>Examples</h5>
|
||
<p>For a peer with a local path</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
<span class="kw">use </span>tempfile::tempdir;
|
||
|
||
<span class="comment">// Create an unbound socket
|
||
</span><span class="kw">let </span>tx = UnixDatagram::unbound()<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Create another, bound socket
|
||
</span><span class="kw">let </span>tmp = tempdir()<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>rx_path = tmp.path().join(<span class="string">"rx"</span>);
|
||
<span class="kw">let </span>rx = UnixDatagram::bind(<span class="kw-2">&</span>rx_path)<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// Connect to the bound socket
|
||
</span>tx.connect(<span class="kw-2">&</span>rx_path)<span class="question-mark">?</span>;
|
||
|
||
<span class="macro">assert_eq!</span>(tx.peer_addr()<span class="question-mark">?</span>.as_pathname().unwrap(), <span class="kw-2">&</span>rx_path);
|
||
</code></pre></div>
|
||
<p>For an unbound peer</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
|
||
<span class="comment">// Create the pair of sockets
|
||
</span><span class="kw">let </span>(sock1, sock2) = UnixDatagram::pair()<span class="question-mark">?</span>;
|
||
|
||
<span class="macro">assert!</span>(sock1.peer_addr()<span class="question-mark">?</span>.is_unnamed());
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.take_error" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1526-1528">Source</a><h4 class="code-header">pub fn <a href="#method.take_error" class="fn">take_error</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/1.84.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>>></h4></section></summary><div class="docblock"><p>Returns the value of the <code>SO_ERROR</code> option.</p>
|
||
<h5 id="examples-24"><a class="doc-anchor" href="#examples-24">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
|
||
<span class="comment">// Create an unbound socket
|
||
</span><span class="kw">let </span>socket = UnixDatagram::unbound()<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);
|
||
}
|
||
</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.shutdown" class="method"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1562-1564">Source</a><h4 class="code-header">pub fn <a href="#method.shutdown" class="fn">shutdown</a>(&self, how: <a class="enum" href="https://doc.rust-lang.org/1.84.1/std/net/enum.Shutdown.html" title="enum std::net::Shutdown">Shutdown</a>) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Shuts down the read, write, or both halves of this connection.</p>
|
||
<p>This function will cause all pending and future I/O calls on the
|
||
specified portions to immediately return with an appropriate value
|
||
(see the documentation of <code>Shutdown</code>).</p>
|
||
<h5 id="examples-25"><a class="doc-anchor" href="#examples-25">§</a>Examples</h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UnixDatagram;
|
||
<span class="kw">use </span>std::net::Shutdown;
|
||
|
||
<span class="comment">// Create an unbound socket
|
||
</span><span class="kw">let </span>(socket, other) = UnixDatagram::pair()<span class="question-mark">?</span>;
|
||
|
||
socket.shutdown(Shutdown::Both)<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// NOTE: the following commented out code does NOT work as expected.
|
||
// Due to an underlying issue, the recv call will block indefinitely.
|
||
// See: https://github.com/tokio-rs/tokio/issues/1679
|
||
//let mut buff = vec![0u8; 24];
|
||
//let size = socket.recv(&mut buff).await?;
|
||
//assert_eq!(size, 0);
|
||
|
||
</span><span class="kw">let </span>send_result = socket.send(<span class="string">b"hello world"</span>).<span class="kw">await</span>;
|
||
<span class="macro">assert!</span>(send_result.is_err());
|
||
</code></pre></div>
|
||
</div></details></div></details></div><h2 id="trait-implementations" class="section-header">Trait Implementations<a href="#trait-implementations" class="anchor">§</a></h2><div id="trait-implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-AsFd-for-UnixDatagram" class="impl"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1591-1595">Source</a><a href="#impl-AsFd-for-UnixDatagram" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/std/os/fd/owned/trait.AsFd.html" title="trait std::os::fd::owned::AsFd">AsFd</a> for <a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</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/tokio/net/unix/datagram/socket.rs.html#1592-1594">Source</a><a href="#method.as_fd" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/std/os/fd/owned/trait.AsFd.html#tymethod.as_fd" class="fn">as_fd</a>(&self) -> <a class="struct" href="https://doc.rust-lang.org/1.84.1/std/os/fd/owned/struct.BorrowedFd.html" title="struct std::os::fd::owned::BorrowedFd">BorrowedFd</a><'_></h4></section></summary><div class='docblock'>Borrows the file descriptor. <a href="https://doc.rust-lang.org/1.84.1/std/os/fd/owned/trait.AsFd.html#tymethod.as_fd">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-AsRawFd-for-UnixDatagram" class="impl"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1585-1589">Source</a><a href="#impl-AsRawFd-for-UnixDatagram" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/std/os/fd/raw/trait.AsRawFd.html" title="trait std::os::fd::raw::AsRawFd">AsRawFd</a> for <a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.as_raw_fd" class="method trait-impl"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1586-1588">Source</a><a href="#method.as_raw_fd" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/std/os/fd/raw/trait.AsRawFd.html#tymethod.as_raw_fd" class="fn">as_raw_fd</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/std/os/fd/raw/type.RawFd.html" title="type std::os::fd::raw::RawFd">RawFd</a></h4></section></summary><div class='docblock'>Extracts the raw file descriptor. <a href="https://doc.rust-lang.org/1.84.1/std/os/fd/raw/trait.AsRawFd.html#tymethod.as_raw_fd">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-Debug-for-UnixDatagram" class="impl"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1579-1583">Source</a><a href="#impl-Debug-for-UnixDatagram" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a> for <a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.fmt" class="method trait-impl"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1580-1582">Source</a><a href="#method.fmt" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/fmt/trait.Debug.html#tymethod.fmt" class="fn">fmt</a>(&self, f: &mut <a class="struct" href="https://doc.rust-lang.org/1.84.1/core/fmt/struct.Formatter.html" title="struct core::fmt::Formatter">Formatter</a><'_>) -> <a class="type" href="https://doc.rust-lang.org/1.84.1/core/fmt/type.Result.html" title="type core::fmt::Result">Result</a></h4></section></summary><div class='docblock'>Formats the value using the given formatter. <a href="https://doc.rust-lang.org/1.84.1/core/fmt/trait.Debug.html#tymethod.fmt">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-TryFrom%3CUnixDatagram%3E-for-UnixDatagram" class="impl"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1567-1577">Source</a><a href="#impl-TryFrom%3CUnixDatagram%3E-for-UnixDatagram" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/os/unix/net/datagram/struct.UnixDatagram.html" title="struct std::os::unix::net::datagram::UnixDatagram">UnixDatagram</a>> for <a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.try_from" class="method trait-impl"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1574-1576">Source</a><a href="#method.try_from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html#tymethod.try_from" class="fn">try_from</a>(stream: <a class="struct" href="https://doc.rust-lang.org/1.84.1/std/os/unix/net/datagram/struct.UnixDatagram.html" title="struct std::os::unix::net::datagram::UnixDatagram">UnixDatagram</a>) -> <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self, Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>></h4></section></summary><div class="docblock"><p>Consumes stream, returning the Tokio I/O object.</p>
|
||
<p>This is equivalent to
|
||
<a href="struct.UnixDatagram.html#method.from_std" title="associated function tokio::net::UnixDatagram::from_std"><code>UnixDatagram::from_std(stream)</code></a>.</p>
|
||
</div></details><details class="toggle" open><summary><section id="associatedtype.Error" class="associatedtype trait-impl"><a class="src rightside" href="../../src/tokio/net/unix/datagram/socket.rs.html#1568">Source</a><a href="#associatedtype.Error" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html#associatedtype.Error" class="associatedtype">Error</a> = <a class="struct" href="https://doc.rust-lang.org/1.84.1/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details></div></details></div><h2 id="synthetic-implementations" class="section-header">Auto Trait Implementations<a href="#synthetic-implementations" class="anchor">§</a></h2><div id="synthetic-implementations-list"><section id="impl-Freeze-for-UnixDatagram" class="impl"><a href="#impl-Freeze-for-UnixDatagram" class="anchor">§</a><h3 class="code-header">impl !<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Freeze.html" title="trait core::marker::Freeze">Freeze</a> for <a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a></h3></section><section id="impl-RefUnwindSafe-for-UnixDatagram" class="impl"><a href="#impl-RefUnwindSafe-for-UnixDatagram" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/panic/unwind_safe/trait.RefUnwindSafe.html" title="trait core::panic::unwind_safe::RefUnwindSafe">RefUnwindSafe</a> for <a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a></h3></section><section id="impl-Send-for-UnixDatagram" class="impl"><a href="#impl-Send-for-UnixDatagram" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> for <a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a></h3></section><section id="impl-Sync-for-UnixDatagram" class="impl"><a href="#impl-Sync-for-UnixDatagram" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> for <a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a></h3></section><section id="impl-Unpin-for-UnixDatagram" class="impl"><a href="#impl-Unpin-for-UnixDatagram" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> for <a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a></h3></section><section id="impl-UnwindSafe-for-UnixDatagram" class="impl"><a href="#impl-UnwindSafe-for-UnixDatagram" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/panic/unwind_safe/trait.UnwindSafe.html" title="trait core::panic::unwind_safe::UnwindSafe">UnwindSafe</a> for <a class="struct" href="struct.UnixDatagram.html" title="struct tokio::net::UnixDatagram">UnixDatagram</a></h3></section></div><h2 id="blanket-implementations" class="section-header">Blanket Implementations<a href="#blanket-implementations" class="anchor">§</a></h2><div id="blanket-implementations-list"><details class="toggle implementors-toggle"><summary><section id="impl-Any-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/any.rs.html#138">Source</a><a href="#impl-Any-for-T" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/any/trait.Any.html" title="trait core::any::Any">Any</a> for T<div class="where">where
|
||
T: 'static + ?<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.type_id" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/any.rs.html#139">Source</a><a href="#method.type_id" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/any/trait.Any.html#tymethod.type_id" class="fn">type_id</a>(&self) -> <a class="struct" href="https://doc.rust-lang.org/1.84.1/core/any/struct.TypeId.html" title="struct core::any::TypeId">TypeId</a></h4></section></summary><div class='docblock'>Gets the <code>TypeId</code> of <code>self</code>. <a href="https://doc.rust-lang.org/1.84.1/core/any/trait.Any.html#tymethod.type_id">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Borrow%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/borrow.rs.html#209">Source</a><a href="#impl-Borrow%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/borrow/trait.Borrow.html" title="trait core::borrow::Borrow">Borrow</a><T> for T<div class="where">where
|
||
T: ?<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/borrow.rs.html#211">Source</a><a href="#method.borrow" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/borrow/trait.Borrow.html#tymethod.borrow" class="fn">borrow</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&T</a></h4></section></summary><div class='docblock'>Immutably borrows from an owned value. <a href="https://doc.rust-lang.org/1.84.1/core/borrow/trait.Borrow.html#tymethod.borrow">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-BorrowMut%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/borrow.rs.html#217">Source</a><a href="#impl-BorrowMut%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/borrow/trait.BorrowMut.html" title="trait core::borrow::BorrowMut">BorrowMut</a><T> for T<div class="where">where
|
||
T: ?<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow_mut" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/borrow.rs.html#218">Source</a><a href="#method.borrow_mut" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut" class="fn">borrow_mut</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&mut T</a></h4></section></summary><div class='docblock'>Mutably borrows from an owned value. <a href="https://doc.rust-lang.org/1.84.1/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-From%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/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/1.84.1/core/convert/trait.From.html" title="trait core::convert::From">From</a><T> for T</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/convert/mod.rs.html#768">Source</a><a href="#method.from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.From.html#tymethod.from" class="fn">from</a>(t: T) -> T</h4></section></summary><div class="docblock"><p>Returns the argument unchanged.</p>
|
||
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Into%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/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/1.84.1/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a><U> for T<div class="where">where
|
||
U: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.From.html" title="trait core::convert::From">From</a><T>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/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/1.84.1/core/convert/trait.Into.html#tymethod.into" class="fn">into</a>(self) -> U</h4></section></summary><div class="docblock"><p>Calls <code>U::from(self)</code>.</p>
|
||
<p>That is, this conversion is whatever the implementation of
|
||
<code><a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.From.html" title="trait core::convert::From">From</a><T> for U</code> chooses to do.</p>
|
||
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryFrom%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/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/1.84.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><U> for T<div class="where">where
|
||
U: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a><T>,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error-2" class="associatedtype trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/convert/mod.rs.html#809">Source</a><a href="#associatedtype.Error-2" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html#associatedtype.Error" class="associatedtype">Error</a> = <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/convert/enum.Infallible.html" title="enum core::convert::Infallible">Infallible</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_from-1" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/convert/mod.rs.html#812">Source</a><a href="#method.try_from-1" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html#tymethod.try_from" class="fn">try_from</a>(value: U) -> <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <T as <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><U>>::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryInto%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/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/1.84.1/core/convert/trait.TryInto.html" title="trait core::convert::TryInto">TryInto</a><U> for T<div class="where">where
|
||
U: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error-1" class="associatedtype trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/convert/mod.rs.html#794">Source</a><a href="#associatedtype.Error-1" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryInto.html#associatedtype.Error" class="associatedtype">Error</a> = <U as <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>>::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/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/1.84.1/core/convert/trait.TryInto.html#tymethod.try_into" class="fn">try_into</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a><U, <U as <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>>::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details></div></section></div></main></body></html> |