blob: 138aff780ff395ba6633f1df97d6b3313f7f43f0 [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="An event source that may be registered with `Registry`."><meta name="keywords" content="rust, rustlang, rust-lang, Source"><title>Source in mio::event - 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 trait"><!--[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><h2 class="location"><a href="#">Source</a></h2><div class="sidebar-elems"><section><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.deregister">deregister</a></li><li><a href="#tymethod.register">register</a></li><li><a href="#tymethod.reregister">reregister</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-Source-for-Box%3CT%3E">Box&lt;T&gt;</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In mio::event</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">Trait <a href="../index.html">mio</a>::<wbr><a href="index.html">event</a>::<wbr><a class="trait" href="#">Source</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/event/source.rs.html#75-112">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 trait"><code>pub trait Source {
fn <a href="#tymethod.register" class="fnname">register</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;mut self,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;registry: &amp;<a class="struct" href="../struct.Registry.html" title="struct mio::Registry">Registry</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;token: <a class="struct" href="../struct.Token.html" title="struct mio::Token">Token</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;interests: <a class="struct" href="../struct.Interest.html" title="struct mio::Interest">Interest</a><br>&nbsp;&nbsp;&nbsp;&nbsp;) -&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="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt;;
<span class="item-spacer"></span> fn <a href="#tymethod.reregister" class="fnname">reregister</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;mut self,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;registry: &amp;<a class="struct" href="../struct.Registry.html" title="struct mio::Registry">Registry</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;token: <a class="struct" href="../struct.Token.html" title="struct mio::Token">Token</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;interests: <a class="struct" href="../struct.Interest.html" title="struct mio::Interest">Interest</a><br>&nbsp;&nbsp;&nbsp;&nbsp;) -&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="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt;;
<span class="item-spacer"></span> fn <a href="#tymethod.deregister" class="fnname">deregister</a>(&amp;mut self, registry: &amp;<a class="struct" href="../struct.Registry.html" title="struct mio::Registry">Registry</a>) -&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="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt;;
}</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>An event source that may be registered with <a href="../struct.Registry.html"><code>Registry</code></a>.</p>
<p>Types that implement <code>event::Source</code> can be registered with
<code>Registry</code>. Users of Mio <strong>should not</strong> use the <code>event::Source</code> trait
functions directly. Instead, the equivalent functions on <code>Registry</code> should
be used.</p>
<p>See <a href="../struct.Registry.html"><code>Registry</code></a> for more details.</p>
<h2 id="implementing-eventsource"><a href="#implementing-eventsource">Implementing <code>event::Source</code></a></h2>
<p>Event sources are always backed by system handles, such as sockets or other
system handles. These <code>event::Source</code>s will be monitored by the system
selector. An implementation of <code>Source</code> will almost always delegates to a
lower level handle. Examples of this are <a href="../net/struct.TcpStream.html"><code>TcpStream</code></a>s, or the <em>unix only</em>
<a href="../unix/struct.SourceFd.html"><code>SourceFd</code></a>.</p>
<h2 id="dropping-eventsources"><a href="#dropping-eventsources">Dropping <code>event::Source</code>s</a></h2>
<p>All <code>event::Source</code>s, unless otherwise specified, need to be <a href="../struct.Registry.html#method.deregister">deregistered</a>
before being dropped for them to not leak resources. This goes against the
normal drop behaviour of types in Rust which cleanup after themselves, e.g.
a <code>File</code> will close itself. However since deregistering needs access to
<a href="../struct.Registry.html"><code>Registry</code></a> this cannot be done while being dropped.</p>
<h2 id="examples"><a href="#examples">Examples</a></h2>
<p>Implementing <code>Source</code> on a struct containing a socket:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>mio::{Interest, Registry, Token};
<span class="kw">use </span>mio::event::Source;
<span class="kw">use </span>mio::net::TcpStream;
<span class="kw">use </span>std::io;
<span class="kw">pub struct </span>MySource {
socket: TcpStream,
}
<span class="kw">impl </span>Source <span class="kw">for </span>MySource {
<span class="kw">fn </span>register(<span class="kw-2">&amp;mut </span><span class="self">self</span>, registry: <span class="kw-2">&amp;</span>Registry, token: Token, interests: Interest)
-&gt; io::Result&lt;()&gt;
{
<span class="comment">// Delegate the `register` call to `socket`
</span><span class="self">self</span>.socket.register(registry, token, interests)
}
<span class="kw">fn </span>reregister(<span class="kw-2">&amp;mut </span><span class="self">self</span>, registry: <span class="kw-2">&amp;</span>Registry, token: Token, interests: Interest)
-&gt; io::Result&lt;()&gt;
{
<span class="comment">// Delegate the `reregister` call to `socket`
</span><span class="self">self</span>.socket.reregister(registry, token, interests)
}
<span class="kw">fn </span>deregister(<span class="kw-2">&amp;mut </span><span class="self">self</span>, registry: <span class="kw-2">&amp;</span>Registry) -&gt; io::Result&lt;()&gt; {
<span class="comment">// Delegate the `deregister` call to `socket`
</span><span class="self">self</span>.socket.deregister(registry)
}
}</code></pre></div>
</div></details><h2 id="required-methods" class="small-section-header">Required Methods<a href="#required-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="tymethod.register" class="method has-srclink"><a class="srclink rightside" href="../../src/mio/event/source.rs.html#83-88">source</a><h4 class="code-header">fn <a href="#tymethod.register" class="fnname">register</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&amp;mut self,<br>&nbsp;&nbsp;&nbsp;&nbsp;registry: &amp;<a class="struct" href="../struct.Registry.html" title="struct mio::Registry">Registry</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;token: <a class="struct" href="../struct.Token.html" title="struct mio::Token">Token</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;interests: <a class="struct" href="../struct.Interest.html" title="struct mio::Interest">Interest</a><br>) -&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="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt;</h4></section></summary><div class="docblock"><p>Register <code>self</code> with the given <code>Registry</code> instance.</p>
<p>This function should not be called directly. Use <a href="../struct.Registry.html#method.register"><code>Registry::register</code></a>
instead. Implementors should handle registration by delegating the call
to another <code>Source</code> type.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="tymethod.reregister" class="method has-srclink"><a class="srclink rightside" href="../../src/mio/event/source.rs.html#97-102">source</a><h4 class="code-header">fn <a href="#tymethod.reregister" class="fnname">reregister</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&amp;mut self,<br>&nbsp;&nbsp;&nbsp;&nbsp;registry: &amp;<a class="struct" href="../struct.Registry.html" title="struct mio::Registry">Registry</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;token: <a class="struct" href="../struct.Token.html" title="struct mio::Token">Token</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;interests: <a class="struct" href="../struct.Interest.html" title="struct mio::Interest">Interest</a><br>) -&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="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt;</h4></section></summary><div class="docblock"><p>Re-register <code>self</code> with the given <code>Registry</code> instance.</p>
<p>This function should not be called directly. Use
<a href="../struct.Registry.html#method.reregister"><code>Registry::reregister</code></a> instead. Implementors should handle
re-registration by either delegating the call to another <code>Source</code> type.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="tymethod.deregister" class="method has-srclink"><a class="srclink rightside" href="../../src/mio/event/source.rs.html#111">source</a><h4 class="code-header">fn <a href="#tymethod.deregister" class="fnname">deregister</a>(&amp;mut self, registry: &amp;<a class="struct" href="../struct.Registry.html" title="struct mio::Registry">Registry</a>) -&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="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt;</h4></section></summary><div class="docblock"><p>Deregister <code>self</code> from the given <code>Registry</code> instance.</p>
<p>This function should not be called directly. Use
<a href="../struct.Registry.html#method.deregister"><code>Registry::deregister</code></a> instead. Implementors should handle
deregistration by delegating the call to another <code>Source</code> type.</p>
</div></details></div><h2 id="foreign-impls" class="small-section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor"></a></h2><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Source-for-Box%3CT%3E" class="impl has-srclink"><a class="srclink rightside" href="../../src/mio/event/source.rs.html#114-139">source</a><a href="#impl-Source-for-Box%3CT%3E" class="anchor"></a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="trait.Source.html" title="trait mio::event::Source">Source</a> for <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a>&lt;T&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="trait.Source.html" title="trait mio::event::Source">Source</a> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></h3></section></summary><div class="impl-items"><section id="method.register" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/mio/event/source.rs.html#118-125">source</a><a href="#method.register" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.register" class="fnname">register</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&amp;mut self,<br>&nbsp;&nbsp;&nbsp;&nbsp;registry: &amp;<a class="struct" href="../struct.Registry.html" title="struct mio::Registry">Registry</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;token: <a class="struct" href="../struct.Token.html" title="struct mio::Token">Token</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;interests: <a class="struct" href="../struct.Interest.html" title="struct mio::Interest">Interest</a><br>) -&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="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt;</h4></section><section id="method.reregister" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/mio/event/source.rs.html#127-134">source</a><a href="#method.reregister" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.reregister" class="fnname">reregister</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&amp;mut self,<br>&nbsp;&nbsp;&nbsp;&nbsp;registry: &amp;<a class="struct" href="../struct.Registry.html" title="struct mio::Registry">Registry</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;token: <a class="struct" href="../struct.Token.html" title="struct mio::Token">Token</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;interests: <a class="struct" href="../struct.Interest.html" title="struct mio::Interest">Interest</a><br>) -&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="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt;</h4></section><section id="method.deregister" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/mio/event/source.rs.html#136-138">source</a><a href="#method.deregister" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.deregister" class="fnname">deregister</a>(&amp;mut self, registry: &amp;<a class="struct" href="../struct.Registry.html" title="struct mio::Registry">Registry</a>) -&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="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt;</h4></section></div></details><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div id="implementors-list"><section id="impl-Source-for-TcpListener" class="impl has-srclink"><a class="srclink rightside" href="../../src/mio/net/tcp/listener.rs.html#139-161">source</a><a href="#impl-Source-for-TcpListener" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="trait.Source.html" title="trait mio::event::Source">Source</a> for <a class="struct" href="../net/struct.TcpListener.html" title="struct mio::net::TcpListener">TcpListener</a></h3></section><section id="impl-Source-for-TcpStream" class="impl has-srclink"><a class="srclink rightside" href="../../src/mio/net/tcp/stream.rs.html#318-340">source</a><a href="#impl-Source-for-TcpStream" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="trait.Source.html" title="trait mio::event::Source">Source</a> for <a class="struct" href="../net/struct.TcpStream.html" title="struct mio::net::TcpStream">TcpStream</a></h3></section><section id="impl-Source-for-UdpSocket" class="impl has-srclink"><a class="srclink rightside" href="../../src/mio/net/udp.rs.html#615-637">source</a><a href="#impl-Source-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="trait.Source.html" title="trait mio::event::Source">Source</a> for <a class="struct" href="../net/struct.UdpSocket.html" title="struct mio::net::UdpSocket">UdpSocket</a></h3></section><section id="impl-Source-for-UnixDatagram" class="impl has-srclink"><a class="srclink rightside" href="../../src/mio/net/uds/datagram.rs.html#184-206">source</a><a href="#impl-Source-for-UnixDatagram" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="trait.Source.html" title="trait mio::event::Source">Source</a> for <a class="struct" href="../net/struct.UnixDatagram.html" title="struct mio::net::UnixDatagram">UnixDatagram</a></h3></section><section id="impl-Source-for-UnixListener" class="impl has-srclink"><a class="srclink rightside" href="../../src/mio/net/uds/listener.rs.html#52-74">source</a><a href="#impl-Source-for-UnixListener" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="trait.Source.html" title="trait mio::event::Source">Source</a> for <a class="struct" href="../net/struct.UnixListener.html" title="struct mio::net::UnixListener">UnixListener</a></h3></section><section id="impl-Source-for-UnixStream" class="impl has-srclink"><a class="srclink rightside" href="../../src/mio/net/uds/stream.rs.html#193-215">source</a><a href="#impl-Source-for-UnixStream" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="trait.Source.html" title="trait mio::event::Source">Source</a> for <a class="struct" href="../net/struct.UnixStream.html" title="struct mio::net::UnixStream">UnixStream</a></h3></section><section id="impl-Source-for-Receiver" class="impl has-srclink"><a class="srclink rightside" href="../../src/mio/sys/unix/pipe.rs.html#463-485">source</a><a href="#impl-Source-for-Receiver" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="trait.Source.html" title="trait mio::event::Source">Source</a> for <a class="struct" href="../unix/pipe/struct.Receiver.html" title="struct mio::unix::pipe::Receiver">Receiver</a></h3></section><section id="impl-Source-for-Sender" class="impl has-srclink"><a class="srclink rightside" href="../../src/mio/sys/unix/pipe.rs.html#298-320">source</a><a href="#impl-Source-for-Sender" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="trait.Source.html" title="trait mio::event::Source">Source</a> for <a class="struct" href="../unix/pipe/struct.Sender.html" title="struct mio::unix::pipe::Sender">Sender</a></h3></section><section id="impl-Source-for-SourceFd%3C%27a%3E" class="impl has-srclink"><a class="srclink rightside" href="../../src/mio/sys/unix/sourcefd.rs.html#94-116">source</a><a href="#impl-Source-for-SourceFd%3C%27a%3E" class="anchor"></a><h3 class="code-header">impl&lt;'a&gt; <a class="trait" href="trait.Source.html" title="trait mio::event::Source">Source</a> for <a class="struct" href="../unix/struct.SourceFd.html" title="struct mio::unix::SourceFd">SourceFd</a>&lt;'a&gt;</h3></section></div><script src="../../implementors/mio/event/source/trait.Source.js" data-ignore-extern-crates="alloc" async></script></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>