blob: 15d027a6b3aef68d061f49cf839ace91c552911d [file] [log] [blame]
<!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="Create a new non-blocking Unix pipe."><meta name="keywords" content="rust, rustlang, rust-lang, new"><title>new in mio::unix::pipe - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></head><body class="rustdoc fn"><!--[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">&#9776;</button><a class="sidebar-logo" href="../../../mio/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../mio/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><div class="sidebar-elems"><h2><a href="index.html">In mio::unix::pipe</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Function <a href="../../index.html">mio</a>::<wbr><a href="../index.html">unix</a>::<wbr><a href="index.html">pipe</a>::<wbr><a class="fn" href="#">new</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../../src/mio/sys/unix/pipe.rs.html#147-213">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust fn"><code>pub fn new() -&gt; <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a>&lt;(<a class="struct" href="struct.Sender.html" title="struct mio::unix::pipe::Sender">Sender</a>, <a class="struct" href="struct.Receiver.html" title="struct mio::unix::pipe::Receiver">Receiver</a>)&gt;</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Create a new non-blocking Unix pipe.</p>
<p>This is a wrapper around Unix’s <a href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/pipe.html"><code>pipe(2)</code></a> system call and can be used as
inter-process or thread communication channel.</p>
<p>This channel may be created before forking the process and then one end used
in each process, e.g. the parent process has the sending end to send command
to the child process.</p>
<h2 id="events"><a href="#events">Events</a></h2>
<p>The <a href="struct.Sender.html" title="Sender"><code>Sender</code></a> can be registered with <a href="../../struct.Interest.html#associatedconstant.WRITABLE"><code>WRITABLE</code></a> interest to receive
<a href="../../event/struct.Event.html#method.is_writable">writable events</a>, the <a href="struct.Receiver.html" title="Receiver"><code>Receiver</code></a> with <a href="../../struct.Interest.html#associatedconstant.READABLE"><code>READABLE</code></a> interest. Once data is
written to the <code>Sender</code> the <code>Receiver</code> will receive an <a href="../../event/struct.Event.html#method.is_readable">readable event</a>.</p>
<p>In addition to those events, events will also be generated if the other side
is dropped. To check if the <code>Sender</code> is dropped you’ll need to check
<a href="../../event/struct.Event.html#method.is_read_closed"><code>is_read_closed</code></a> on events for the <code>Receiver</code>, if it returns true the
<code>Sender</code> is dropped. On the <code>Sender</code> end check <a href="../../event/struct.Event.html#method.is_write_closed"><code>is_write_closed</code></a>, if it
returns true the <code>Receiver</code> was dropped. Also see the second example below.</p>
<h2 id="deregistering"><a href="#deregistering">Deregistering</a></h2>
<p>Both <code>Sender</code> and <code>Receiver</code> will deregister themselves when dropped,
<strong>iff</strong> the file descriptors are not duplicated (via <a href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup.html"><code>dup(2)</code></a>).</p>
<h2 id="examples"><a href="#examples">Examples</a></h2>
<p>Simple example that writes data into the sending end and read it from the
receiving end.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::io::{<span class="self">self</span>, Read, Write};
<span class="kw">use </span>mio::{Poll, Events, Interest, Token};
<span class="kw">use </span>mio::unix::pipe;
<span class="comment">// Unique tokens for the two ends of the channel.
</span><span class="kw">const </span>PIPE_RECV: Token = Token(<span class="number">0</span>);
<span class="kw">const </span>PIPE_SEND: Token = Token(<span class="number">1</span>);
<span class="comment">// Create our `Poll` instance and the `Events` container.
</span><span class="kw">let </span><span class="kw-2">mut </span>poll = Poll::new()<span class="question-mark">?</span>;
<span class="kw">let </span><span class="kw-2">mut </span>events = Events::with_capacity(<span class="number">8</span>);
<span class="comment">// Create a new pipe.
</span><span class="kw">let </span>(<span class="kw-2">mut </span>sender, <span class="kw-2">mut </span>receiver) = pipe::new()<span class="question-mark">?</span>;
<span class="comment">// Register both ends of the channel.
</span>poll.registry().register(<span class="kw-2">&amp;mut </span>receiver, PIPE_RECV, Interest::READABLE)<span class="question-mark">?</span>;
poll.registry().register(<span class="kw-2">&amp;mut </span>sender, PIPE_SEND, Interest::WRITABLE)<span class="question-mark">?</span>;
<span class="kw">const </span>MSG: <span class="kw-2">&amp;</span>[u8; <span class="number">11</span>] = <span class="string">b&quot;Hello world&quot;</span>;
<span class="kw">loop </span>{
poll.poll(<span class="kw-2">&amp;mut </span>events, <span class="prelude-val">None</span>)<span class="question-mark">?</span>;
<span class="kw">for </span>event <span class="kw">in </span>events.iter() {
<span class="kw">match </span>event.token() {
PIPE_SEND =&gt; sender.write(MSG)
.and_then(|n| <span class="kw">if </span>n != MSG.len() {
<span class="comment">// We&#39;ll consider a short write an error in this
// example. NOTE: we can&#39;t use `write_all` with
// non-blocking I/O.
</span><span class="prelude-val">Err</span>(io::ErrorKind::WriteZero.into())
} <span class="kw">else </span>{
<span class="prelude-val">Ok</span>(())
})<span class="question-mark">?</span>,
PIPE_RECV =&gt; {
<span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0</span>; <span class="number">11</span>];
<span class="kw">let </span>n = receiver.read(<span class="kw-2">&amp;mut </span>buf)<span class="question-mark">?</span>;
<span class="macro">println!</span>(<span class="string">&quot;received: {:?}&quot;</span>, <span class="kw-2">&amp;</span>buf[<span class="number">0</span>..n]);
<span class="macro">assert_eq!</span>(n, MSG.len());
<span class="macro">assert_eq!</span>(<span class="kw-2">&amp;</span>buf, <span class="kw-2">&amp;*</span>MSG);
<span class="kw">return </span><span class="prelude-val">Ok</span>(());
},
<span class="kw">_ </span>=&gt; <span class="macro">unreachable!</span>(),
}
}
}</code></pre></div>
<p>Example that receives an event once the <code>Sender</code> is dropped.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// Same setup as in the example above.
</span><span class="kw">let </span><span class="kw-2">mut </span>poll = Poll::new()<span class="question-mark">?</span>;
<span class="kw">let </span><span class="kw-2">mut </span>events = Events::with_capacity(<span class="number">8</span>);
<span class="kw">let </span>(<span class="kw-2">mut </span>sender, <span class="kw-2">mut </span>receiver) = pipe::new()<span class="question-mark">?</span>;
poll.registry().register(<span class="kw-2">&amp;mut </span>receiver, PIPE_RECV, Interest::READABLE)<span class="question-mark">?</span>;
poll.registry().register(<span class="kw-2">&amp;mut </span>sender, PIPE_SEND, Interest::WRITABLE)<span class="question-mark">?</span>;
<span class="comment">// Drop the sender.
</span>drop(sender);
poll.poll(<span class="kw-2">&amp;mut </span>events, <span class="prelude-val">None</span>)<span class="question-mark">?</span>;
<span class="kw">for </span>event <span class="kw">in </span>events.iter() {
<span class="kw">match </span>event.token() {
PIPE_RECV <span class="kw">if </span>event.is_read_closed() =&gt; {
<span class="comment">// Detected that the sender was dropped.
</span><span class="macro">println!</span>(<span class="string">&quot;Sender dropped!&quot;</span>);
<span class="kw">return </span><span class="prelude-val">Ok</span>(());
},
<span class="kw">_ </span>=&gt; <span class="macro">unreachable!</span>(),
}
}</code></pre></div>
</div></details></section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="mio" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>