aboutsummaryrefslogtreecommitdiff
path: root/carboxyl/struct.Stream.html
blob: e51f5acb111547727932c6324b5a1931c617c85b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<!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="API documentation for the Rust `Stream` struct in crate `carboxyl`.">
    <meta name="keywords" content="rust, rustlang, rust-lang, Stream">

    <title>carboxyl::Stream - Rust</title>

    <link rel="stylesheet" type="text/css" href="../main.css">

    
    
</head>
<body class="rustdoc">
    <!--[if lte IE 8]>
    <div class="warning">
        This old browser is unsupported and will most likely display funky
        things.
    </div>
    <![endif]-->

    

    <section class="sidebar">
        
        <p class='location'><a href='index.html'>carboxyl</a></p><script>window.sidebarCurrent = {name: 'Stream', ty: 'struct', relpath: ''};</script><script defer src="sidebar-items.js"></script>
    </section>

    <nav class="sub">
        <form class="search-form js-only">
            <div class="search-container">
                <input class="search-input" name="search"
                       autocomplete="off"
                       placeholder="Click or press 'S' to search, '?' for more options..."
                       type="search">
            </div>
        </form>
    </nav>

    <section id='main' class="content struct">
<h1 class='fqn'><span class='in-band'>Struct <a href='index.html'>carboxyl</a>::<wbr><a class='struct' href=''>Stream</a></span><span class='out-of-band'><span id='render-detail'>
            <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
                [<span class='inner'>&#x2212;</span>]
            </a>
        </span><a id='src-1394' class='srclink' href='../src/carboxyl/stream.rs.html#186-190' title='goto source code'>[src]</a></span></h1>
<pre class='rust struct'>pub struct Stream&lt;A&gt; {
    // some fields omitted
}</pre><div class='docblock'><p>A stream of events.</p>

<p>Conceptually a stream can be thought of as a series of discrete events that
occur at specific times. They are ordered by a transaction system. This
means that firings of disjoint events can not interfere with each other. The
consequences of one event are atomically reflected in dependent quantities.</p>

<p>Streams provide a number of primitive operations. These can be used to
compose streams and combine them with signals. For instance, streams can be
mapped over with a function, merged with another stream of the same type or
filtered by some predicate.</p>

<h1 id="algebraic-laws" class='section-header'><a
                           href="#algebraic-laws">Algebraic laws</a></h1>
<p>Furthermore, streams satisfy certain algebraic properties that are useful to
reason about them.</p>

<h2 id="monoid" class='section-header'><a
                           href="#monoid">Monoid</a></h2>
<p>For once, streams of the same type form a <strong>monoid</strong> under merging. The
neutral element in this context is <code>Stream::never()</code>. So the following laws
always hold for streams <code>a</code>, <code>b</code> and <code>c</code> of the same type:</p>

<ul>
<li>Left identity: <code>Stream::never().merge(&amp;a) == a</code>,</li>
<li>Right identity: <code>a.merge(&amp;Stream::never()) == a</code>,</li>
<li>Associativity: <code>a.merge(&amp;b).merge(&amp;c) == a.merge(&amp;b.merge(&amp;c))</code>.</li>
</ul>

<p><em>Note that equality in this context is not actually implemented as such,
since comparing two (potentially infinite) streams is a prohibitive
operation. Instead, the expressions above can be used interchangably and
behave identically.</em></p>

<h2 id="functor" class='section-header'><a
                           href="#functor">Functor</a></h2>
<p>Under the mapping operation streams also become a functor. A functor is a
generic type like <code>Stream</code> with some mapping operation that takes a function
<code>Fn(A) -&gt; B</code> to map a <code>Stream&lt;A&gt;</code> to a <code>Stream&lt;B&gt;</code>. Algebraically it
satisfies the following laws:</p>

<ul>
<li>The identity function is preserved: <code>a.map(|x| x) == a</code>,</li>
<li>Function composition is respected: <code>a.map(f).map(g) == a.map(|x| g(f(x)))</code>.</li>
</ul>
</div><h2 id='methods'>Methods</h2><h3 class='impl'><code>impl&lt;A: <a class='trait' href='http://doc.rust-lang.org/nightly/core/clone/trait.Clone.html' title='core::clone::Clone'>Clone</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Sync.html' title='core::marker::Sync'>Sync</a> + 'static&gt; <a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;A&gt;</code></h3><div class='impl-items'><h4 id='method.never' class='method'><code>fn <a href='#method.never' class='fnname'>never</a>() -&gt; <a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;A&gt;</code></h4>
<div class='docblock'><p>Create a stream that never fires. This can be useful in certain
situations, where a stream is logically required, but no events are
expected.</p>
</div><h4 id='method.map' class='method'><code>fn <a href='#method.map' class='fnname'>map</a>&lt;B, F&gt;(&amp;self, f: F) -&gt; <a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;B&gt; <span class='where'>where B: <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Sync.html' title='core::marker::Sync'>Sync</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/clone/trait.Clone.html' title='core::clone::Clone'>Clone</a> + 'static, F: <a class='trait' href='http://doc.rust-lang.org/nightly/core/ops/trait.Fn.html' title='core::ops::Fn'>Fn</a>(A) -&gt; B + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Sync.html' title='core::marker::Sync'>Sync</a> + 'static</span></code></h4>
<div class='docblock'><p>Map the stream to another stream using a function.</p>

<p><code>map</code> applies a function to every event fired in this stream to create a
new stream of type <code>B</code>.</p>
<pre class='rust rust-example-rendered'>
<span class='kw'>let</span> <span class='ident'>sink</span>: <span class='ident'>Sink</span><span class='op'>&lt;</span><span class='ident'>i32</span><span class='op'>&gt;</span> <span class='op'>=</span> <span class='ident'>Sink</span>::<span class='ident'>new</span>();
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>events</span> <span class='op'>=</span> <span class='ident'>sink</span>.<span class='ident'>stream</span>().<span class='ident'>map</span>(<span class='op'>|</span><span class='ident'>x</span><span class='op'>|</span> <span class='ident'>x</span> <span class='op'>+</span> <span class='number'>4</span>).<span class='ident'>events</span>();
<span class='ident'>sink</span>.<span class='ident'>send</span>(<span class='number'>3</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>events</span>.<span class='ident'>next</span>(), <span class='prelude-val'>Some</span>(<span class='number'>7</span>));
</pre>
</div><h4 id='method.filter' class='method'><code>fn <a href='#method.filter' class='fnname'>filter</a>&lt;F&gt;(&amp;self, f: F) -&gt; <a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;A&gt; <span class='where'>where F: <a class='trait' href='http://doc.rust-lang.org/nightly/core/ops/trait.Fn.html' title='core::ops::Fn'>Fn</a>(&amp;A) -&gt; <a href='http://doc.rust-lang.org/nightly/std/primitive.bool.html'>bool</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Sync.html' title='core::marker::Sync'>Sync</a> + 'static</span></code></h4>
<div class='docblock'><p>Filter a stream according to a predicate.</p>

<p><code>filter</code> creates a new stream that only fires those events from the
original stream that satisfy the predicate.</p>
<pre class='rust rust-example-rendered'>
<span class='kw'>let</span> <span class='ident'>sink</span>: <span class='ident'>Sink</span><span class='op'>&lt;</span><span class='ident'>i32</span><span class='op'>&gt;</span> <span class='op'>=</span> <span class='ident'>Sink</span>::<span class='ident'>new</span>();
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>events</span> <span class='op'>=</span> <span class='ident'>sink</span>.<span class='ident'>stream</span>()
    .<span class='ident'>filter</span>(<span class='op'>|</span><span class='kw-2'>&amp;</span><span class='ident'>x</span><span class='op'>|</span> (<span class='ident'>x</span> <span class='op'>&gt;=</span> <span class='number'>4</span>) <span class='op'>&amp;&amp;</span> (<span class='ident'>x</span> <span class='op'>&lt;=</span> <span class='number'>10</span>))
    .<span class='ident'>events</span>();
<span class='ident'>sink</span>.<span class='ident'>send</span>(<span class='number'>2</span>); <span class='comment'>// won&#39;t arrive</span>
<span class='ident'>sink</span>.<span class='ident'>send</span>(<span class='number'>5</span>); <span class='comment'>// will arrive</span>
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>events</span>.<span class='ident'>next</span>(), <span class='prelude-val'>Some</span>(<span class='number'>5</span>));
</pre>
</div><h4 id='method.filter_map' class='method'><code>fn <a href='#method.filter_map' class='fnname'>filter_map</a>&lt;B, F&gt;(&amp;self, f: F) -&gt; <a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;B&gt; <span class='where'>where B: <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Sync.html' title='core::marker::Sync'>Sync</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/clone/trait.Clone.html' title='core::clone::Clone'>Clone</a> + 'static, F: <a class='trait' href='http://doc.rust-lang.org/nightly/core/ops/trait.Fn.html' title='core::ops::Fn'>Fn</a>(A) -&gt; <a class='enum' href='http://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a>&lt;B&gt; + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Sync.html' title='core::marker::Sync'>Sync</a> + 'static</span></code></h4>
<div class='docblock'><p>Both filter and map a stream.</p>

<p>This is equivalent to <code>.map(f).filter_some()</code>.</p>
<pre class='rust rust-example-rendered'>
<span class='kw'>let</span> <span class='ident'>sink</span> <span class='op'>=</span> <span class='ident'>Sink</span>::<span class='ident'>new</span>();
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>events</span> <span class='op'>=</span> <span class='ident'>sink</span>.<span class='ident'>stream</span>()
    .<span class='ident'>filter_map</span>(<span class='op'>|</span><span class='ident'>i</span><span class='op'>|</span> <span class='kw'>if</span> <span class='ident'>i</span> <span class='op'>&gt;</span> <span class='number'>3</span> { <span class='prelude-val'>Some</span>(<span class='ident'>i</span> <span class='op'>+</span> <span class='number'>2</span>) } <span class='kw'>else</span> { <span class='prelude-val'>None</span> })
    .<span class='ident'>events</span>();
<span class='ident'>sink</span>.<span class='ident'>send</span>(<span class='number'>2</span>);
<span class='ident'>sink</span>.<span class='ident'>send</span>(<span class='number'>4</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>events</span>.<span class='ident'>next</span>(), <span class='prelude-val'>Some</span>(<span class='number'>6</span>));
</pre>
</div><h4 id='method.merge' class='method'><code>fn <a href='#method.merge' class='fnname'>merge</a>(&amp;self, other: &amp;<a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;A&gt;) -&gt; <a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;A&gt;</code></h4>
<div class='docblock'><p>Merge with another stream.</p>

<p><code>merge</code> takes two streams and creates a new stream that fires events
from both input streams.</p>
<pre class='rust rust-example-rendered'>
<span class='kw'>let</span> <span class='ident'>sink_1</span> <span class='op'>=</span> <span class='ident'>Sink</span>::<span class='op'>&lt;</span><span class='ident'>i32</span><span class='op'>&gt;</span>::<span class='ident'>new</span>();
<span class='kw'>let</span> <span class='ident'>sink_2</span> <span class='op'>=</span> <span class='ident'>Sink</span>::<span class='op'>&lt;</span><span class='ident'>i32</span><span class='op'>&gt;</span>::<span class='ident'>new</span>();
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>events</span> <span class='op'>=</span> <span class='ident'>sink_1</span>.<span class='ident'>stream</span>().<span class='ident'>merge</span>(<span class='kw-2'>&amp;</span><span class='ident'>sink_2</span>.<span class='ident'>stream</span>()).<span class='ident'>events</span>();
<span class='ident'>sink_1</span>.<span class='ident'>send</span>(<span class='number'>2</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>events</span>.<span class='ident'>next</span>(), <span class='prelude-val'>Some</span>(<span class='number'>2</span>));
<span class='ident'>sink_2</span>.<span class='ident'>send</span>(<span class='number'>4</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>events</span>.<span class='ident'>next</span>(), <span class='prelude-val'>Some</span>(<span class='number'>4</span>));
</pre>
</div><h4 id='method.coalesce' class='method'><code>fn <a href='#method.coalesce' class='fnname'>coalesce</a>&lt;F&gt;(&amp;self, f: F) -&gt; <a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;A&gt; <span class='where'>where F: <a class='trait' href='http://doc.rust-lang.org/nightly/core/ops/trait.Fn.html' title='core::ops::Fn'>Fn</a>(A, A) -&gt; A + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Sync.html' title='core::marker::Sync'>Sync</a> + 'static</span></code></h4>
<div class='docblock'><p>Coalesce multiple event firings within the same transaction into a
single event.</p>

<p>The function should ideally commute, as the order of events within a
transaction is not well-defined.</p>
</div><h4 id='method.hold' class='method'><code>fn <a href='#method.hold' class='fnname'>hold</a>(&amp;self, initial: A) -&gt; <a class='struct' href='../carboxyl/struct.Signal.html' title='carboxyl::Signal'>Signal</a>&lt;A&gt;</code></h4>
<div class='docblock'><p>Hold an event in a signal.</p>

<p>The resulting signal <code>hold</code>s the value of the last event fired by the
stream.</p>
<pre class='rust rust-example-rendered'>
<span class='kw'>let</span> <span class='ident'>sink</span> <span class='op'>=</span> <span class='ident'>Sink</span>::<span class='ident'>new</span>();
<span class='kw'>let</span> <span class='ident'>signal</span> <span class='op'>=</span> <span class='ident'>sink</span>.<span class='ident'>stream</span>().<span class='ident'>hold</span>(<span class='number'>0</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>signal</span>.<span class='ident'>sample</span>(), <span class='number'>0</span>);
<span class='ident'>sink</span>.<span class='ident'>send</span>(<span class='number'>2</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>signal</span>.<span class='ident'>sample</span>(), <span class='number'>2</span>);
</pre>
</div><h4 id='method.events' class='method'><code>fn <a href='#method.events' class='fnname'>events</a>(&amp;self) -&gt; Events&lt;A&gt;</code></h4>
<div class='docblock'><p>A blocking iterator over the stream.</p>
</div><h4 id='method.scan' class='method'><code>fn <a href='#method.scan' class='fnname'>scan</a>&lt;B, F&gt;(&amp;self, initial: B, f: F) -&gt; <a class='struct' href='../carboxyl/struct.Signal.html' title='carboxyl::Signal'>Signal</a>&lt;B&gt; <span class='where'>where B: <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Sync.html' title='core::marker::Sync'>Sync</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/clone/trait.Clone.html' title='core::clone::Clone'>Clone</a> + 'static, F: <a class='trait' href='http://doc.rust-lang.org/nightly/core/ops/trait.Fn.html' title='core::ops::Fn'>Fn</a>(B, A) -&gt; B + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Sync.html' title='core::marker::Sync'>Sync</a> + 'static</span></code></h4>
<div class='docblock'><p>Scan a stream and accumulate its event firings in a signal.</p>

<p>Starting at some initial value, each new event changes the value of the
resulting signal as prescribed by the supplied function.</p>
<pre class='rust rust-example-rendered'>
<span class='kw'>let</span> <span class='ident'>sink</span> <span class='op'>=</span> <span class='ident'>Sink</span>::<span class='ident'>new</span>();
<span class='kw'>let</span> <span class='ident'>sum</span> <span class='op'>=</span> <span class='ident'>sink</span>.<span class='ident'>stream</span>().<span class='ident'>scan</span>(<span class='number'>0</span>, <span class='op'>|</span><span class='ident'>a</span>, <span class='ident'>b</span><span class='op'>|</span> <span class='ident'>a</span> <span class='op'>+</span> <span class='ident'>b</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>sum</span>.<span class='ident'>sample</span>(), <span class='number'>0</span>);
<span class='ident'>sink</span>.<span class='ident'>send</span>(<span class='number'>2</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>sum</span>.<span class='ident'>sample</span>(), <span class='number'>2</span>);
<span class='ident'>sink</span>.<span class='ident'>send</span>(<span class='number'>4</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>sum</span>.<span class='ident'>sample</span>(), <span class='number'>6</span>);
</pre>
</div><h4 id='method.scan_mut' class='method'><code>fn <a href='#method.scan_mut' class='fnname'>scan_mut</a>&lt;B, F&gt;(&amp;self, initial: B, f: F) -&gt; <a class='struct' href='../carboxyl/struct.SignalMut.html' title='carboxyl::SignalMut'>SignalMut</a>&lt;B&gt; <span class='where'>where B: <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Sync.html' title='core::marker::Sync'>Sync</a> + 'static, F: <a class='trait' href='http://doc.rust-lang.org/nightly/core/ops/trait.Fn.html' title='core::ops::Fn'>Fn</a>(&amp;mut B, A) + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Sync.html' title='core::marker::Sync'>Sync</a> + 'static</span></code></h4>
<div class='docblock'><p>Scan a stream and accumulate its event firings in some mutable state.</p>

<p>Semantically this is equivalent to <code>scan</code>. However, it allows one to use
a non-Clone type as an accumulator and update it with efficient in-place
operations.</p>

<p>The resulting <code>SignalMut</code> does have a slightly different API from a
regular <code>Signal</code> as it does not allow clones.</p>

<h1 id="example" class='section-header'><a
                           href="#example">Example</a></h1><pre class='rust rust-example-rendered'>
<span class='kw'>let</span> <span class='ident'>sink</span>: <span class='ident'>Sink</span><span class='op'>&lt;</span><span class='ident'>i32</span><span class='op'>&gt;</span> <span class='op'>=</span> <span class='ident'>Sink</span>::<span class='ident'>new</span>();
<span class='kw'>let</span> <span class='ident'>sum</span> <span class='op'>=</span> <span class='ident'>sink</span>.<span class='ident'>stream</span>()
    .<span class='ident'>scan_mut</span>(<span class='number'>0</span>, <span class='op'>|</span><span class='ident'>sum</span>, <span class='ident'>a</span><span class='op'>|</span> <span class='op'>*</span><span class='ident'>sum</span> <span class='op'>+=</span> <span class='ident'>a</span>)
    .<span class='ident'>combine</span>(<span class='kw-2'>&amp;</span><span class='ident'>Signal</span>::<span class='ident'>new</span>(()), <span class='op'>|</span><span class='ident'>sum</span>, ()<span class='op'>|</span> <span class='op'>*</span><span class='ident'>sum</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>sum</span>.<span class='ident'>sample</span>(), <span class='number'>0</span>);
<span class='ident'>sink</span>.<span class='ident'>send</span>(<span class='number'>2</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>sum</span>.<span class='ident'>sample</span>(), <span class='number'>2</span>);
<span class='ident'>sink</span>.<span class='ident'>send</span>(<span class='number'>4</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>sum</span>.<span class='ident'>sample</span>(), <span class='number'>6</span>);
</pre>
</div></div><h3 class='impl'><code>impl&lt;A: <a class='trait' href='http://doc.rust-lang.org/nightly/core/clone/trait.Clone.html' title='core::clone::Clone'>Clone</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Sync.html' title='core::marker::Sync'>Sync</a> + 'static&gt; <a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;<a class='enum' href='http://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a>&lt;A&gt;&gt;</code></h3><div class='impl-items'><h4 id='method.filter_some' class='method'><code>fn <a href='#method.filter_some' class='fnname'>filter_some</a>(&amp;self) -&gt; <a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;A&gt;</code></h4>
<div class='docblock'><p>Filter a stream of options.</p>

<p><code>filter_some</code> creates a new stream that only fires the unwrapped
<code>Some(…)</code> events from the original stream omitting any <code>None</code> events.</p>
<pre class='rust rust-example-rendered'>
<span class='kw'>let</span> <span class='ident'>sink</span> <span class='op'>=</span> <span class='ident'>Sink</span>::<span class='ident'>new</span>();
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>events</span> <span class='op'>=</span> <span class='ident'>sink</span>.<span class='ident'>stream</span>().<span class='ident'>filter_some</span>().<span class='ident'>events</span>();
<span class='ident'>sink</span>.<span class='ident'>send</span>(<span class='prelude-val'>None</span>); <span class='comment'>// won&#39;t arrive</span>
<span class='ident'>sink</span>.<span class='ident'>send</span>(<span class='prelude-val'>Some</span>(<span class='number'>5</span>)); <span class='comment'>// will arrive</span>
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>events</span>.<span class='ident'>next</span>(), <span class='prelude-val'>Some</span>(<span class='number'>5</span>));
</pre>
</div></div><h3 class='impl'><code>impl&lt;A: <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/marker/trait.Sync.html' title='core::marker::Sync'>Sync</a> + <a class='trait' href='http://doc.rust-lang.org/nightly/core/clone/trait.Clone.html' title='core::clone::Clone'>Clone</a> + 'static&gt; <a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;<a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;A&gt;&gt;</code></h3><div class='impl-items'><h4 id='method.switch' class='method'><code>fn <a href='#method.switch' class='fnname'>switch</a>(&amp;self) -&gt; <a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;A&gt;</code></h4>
<div class='docblock'><p>Switch between streams.</p>

<p>This takes a stream of streams and maps it to a new stream, which fires
all events from the most recent stream fired into it.</p>

<h1 id="example" class='section-header'><a
                           href="#example">Example</a></h1><pre class='rust rust-example-rendered'>
<span class='comment'>// Create sinks</span>
<span class='kw'>let</span> <span class='ident'>stream_sink</span>: <span class='ident'>Sink</span><span class='op'>&lt;</span><span class='ident'>Stream</span><span class='op'>&lt;</span><span class='ident'>i32</span><span class='op'>&gt;&gt;</span> <span class='op'>=</span> <span class='ident'>Sink</span>::<span class='ident'>new</span>();
<span class='kw'>let</span> <span class='ident'>sink1</span>: <span class='ident'>Sink</span><span class='op'>&lt;</span><span class='ident'>i32</span><span class='op'>&gt;</span> <span class='op'>=</span> <span class='ident'>Sink</span>::<span class='ident'>new</span>();
<span class='kw'>let</span> <span class='ident'>sink2</span>: <span class='ident'>Sink</span><span class='op'>&lt;</span><span class='ident'>i32</span><span class='op'>&gt;</span> <span class='op'>=</span> <span class='ident'>Sink</span>::<span class='ident'>new</span>();

<span class='comment'>// Switch and listen</span>
<span class='kw'>let</span> <span class='ident'>switched</span> <span class='op'>=</span> <span class='ident'>stream_sink</span>.<span class='ident'>stream</span>().<span class='ident'>switch</span>();
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>events</span> <span class='op'>=</span> <span class='ident'>switched</span>.<span class='ident'>events</span>();

<span class='comment'>// Should not receive events from either sink</span>
<span class='ident'>sink1</span>.<span class='ident'>send</span>(<span class='number'>1</span>); <span class='ident'>sink2</span>.<span class='ident'>send</span>(<span class='number'>2</span>);

<span class='comment'>// Now switch to sink 2</span>
<span class='ident'>stream_sink</span>.<span class='ident'>send</span>(<span class='ident'>sink2</span>.<span class='ident'>stream</span>());
<span class='ident'>sink1</span>.<span class='ident'>send</span>(<span class='number'>3</span>); <span class='ident'>sink2</span>.<span class='ident'>send</span>(<span class='number'>4</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>events</span>.<span class='ident'>next</span>(), <span class='prelude-val'>Some</span>(<span class='number'>4</span>));

<span class='comment'>// And then to sink 1</span>
<span class='ident'>stream_sink</span>.<span class='ident'>send</span>(<span class='ident'>sink1</span>.<span class='ident'>stream</span>());
<span class='ident'>sink1</span>.<span class='ident'>send</span>(<span class='number'>5</span>); <span class='ident'>sink2</span>.<span class='ident'>send</span>(<span class='number'>6</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>events</span>.<span class='ident'>next</span>(), <span class='prelude-val'>Some</span>(<span class='number'>5</span>));
</pre>
</div></div><h2 id='implementations'>Trait Implementations</h2><h3 class='impl'><code>impl&lt;A&gt; <a class='trait' href='http://doc.rust-lang.org/nightly/core/clone/trait.Clone.html' title='core::clone::Clone'>Clone</a> for <a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;A&gt;</code></h3><div class='impl-items'><h4 id='method.clone' class='method'><code>fn <a href='http://doc.rust-lang.org/nightly/core/clone/trait.Clone.html#method.clone' class='fnname'>clone</a>(&amp;self) -&gt; <a class='struct' href='../carboxyl/struct.Stream.html' title='carboxyl::Stream'>Stream</a>&lt;A&gt;</code></h4>
<h4 id='method.clone_from' class='method'><code>fn <a href='http://doc.rust-lang.org/nightly/core/clone/trait.Clone.html#method.clone_from' class='fnname'>clone_from</a>(&amp;mut self, source: &amp;Self)</code></h4>
</div></section>
    <section id='search' class="content hidden"></section>

    <section class="footer"></section>

    <div id="help" class="hidden">
        <div class="shortcuts">
            <h1>Keyboard shortcuts</h1>
            <dl>
                <dt>?</dt>
                <dd>Show this help dialog</dd>
                <dt>S</dt>
                <dd>Focus the search field</dd>
                <dt>&larrb;</dt>
                <dd>Move up in search results</dd>
                <dt>&rarrb;</dt>
                <dd>Move down in search results</dd>
                <dt>&#9166;</dt>
                <dd>Go to active search result</dd>
            </dl>
        </div>
        <div class="infos">
            <h1>Search tricks</h1>
            <p>
                Prefix searches with a type followed by a colon (e.g.
                <code>fn:</code>) to restrict the search to a given type.
            </p>
            <p>
                Accepted types are: <code>fn</code>, <code>mod</code>,
                <code>struct</code>, <code>enum</code>,
                <code>trait</code>, <code>typedef</code> (or
                <code>tdef</code>).
            </p>
            <p>
                Search functions by type signature (e.g.
                <code>vec -> usize</code>)
            </p>
        </div>
    </div>

    

    <script>
        window.rootPath = "../";
        window.currentCrate = "carboxyl";
        window.playgroundUrl = "";
    </script>
    <script src="../jquery.js"></script>
    <script src="../main.js"></script>
    
    <script async src="../search-index.js"></script>
</body>
</html>