Files
ab_glyph_rasterizer
adler
adler32
andrew
bitflags
bytemuck
byteorder
calloop
cfg_if
color_quant
crc32fast
crossbeam_channel
crossbeam_deque
crossbeam_epoch
crossbeam_utils
deflate
dlib
downcast_rs
draw_state
either
event_loop
float
fnv
gfx
gfx_core
gfx_device_gl
gfx_gl
gfx_graphics
gfx_texture
gif
gl
glutin
glutin_egl_sys
glutin_glx_sys
glutin_window
graphics
graphics_api_version
image
input
instant
interpolation
iovec
jpeg_decoder
lazy_static
lazycell
libc
libloading
lock_api
log
maybe_uninit
memchr
memmap2
memoffset
miniz_oxide
mio
mio_extras
net2
nix
nom
num_cpus
num_integer
num_iter
num_rational
num_traits
once_cell
osmesa_sys
owned_ttf_parser
parking_lot
parking_lot_core
percent_encoding
piston
piston_window
png
proc_macro2
quote
raw_window_handle
rayon
rayon_core
read_color
rusttype
same_file
scoped_threadpool
scoped_tls
scopeguard
serde
serde_derive
shader_version
shaders_graphics2d
colored
textured
textured_color
shared_library
slab
smallvec
smithay_client_toolkit
spin_sleep
syn
texture
tiff
ttf_parser
unicode_xid
vecmath
viewport
walkdir
wayland_client
wayland_commons
wayland_cursor
wayland_egl
wayland_protocols
wayland_sys
weezl
window
winit
x11_dl
xcursor
xdg
xml
  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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use super::plumbing::*;
use super::*;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;

/// `PanicFuse` is an adaptor that wraps an iterator with a fuse in case
/// of panics, to halt all threads as soon as possible.
///
/// This struct is created by the [`panic_fuse()`] method on [`ParallelIterator`]
///
/// [`panic_fuse()`]: trait.ParallelIterator.html#method.panic_fuse
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[derive(Debug, Clone)]
pub struct PanicFuse<I: ParallelIterator> {
    base: I,
}

/// Helper that sets a bool to `true` if dropped while unwinding.
#[derive(Clone)]
struct Fuse<'a>(&'a AtomicBool);

impl<'a> Drop for Fuse<'a> {
    #[inline]
    fn drop(&mut self) {
        if thread::panicking() {
            self.0.store(true, Ordering::Relaxed);
        }
    }
}

impl<'a> Fuse<'a> {
    #[inline]
    fn panicked(&self) -> bool {
        self.0.load(Ordering::Relaxed)
    }
}

impl<I> PanicFuse<I>
where
    I: ParallelIterator,
{
    /// Creates a new `PanicFuse` iterator.
    pub(super) fn new(base: I) -> PanicFuse<I> {
        PanicFuse { base }
    }
}

impl<I> ParallelIterator for PanicFuse<I>
where
    I: ParallelIterator,
{
    type Item = I::Item;

    fn drive_unindexed<C>(self, consumer: C) -> C::Result
    where
        C: UnindexedConsumer<Self::Item>,
    {
        let panicked = AtomicBool::new(false);
        let consumer1 = PanicFuseConsumer {
            base: consumer,
            fuse: Fuse(&panicked),
        };
        self.base.drive_unindexed(consumer1)
    }

    fn opt_len(&self) -> Option<usize> {
        self.base.opt_len()
    }
}

impl<I> IndexedParallelIterator for PanicFuse<I>
where
    I: IndexedParallelIterator,
{
    fn drive<C>(self, consumer: C) -> C::Result
    where
        C: Consumer<Self::Item>,
    {
        let panicked = AtomicBool::new(false);
        let consumer1 = PanicFuseConsumer {
            base: consumer,
            fuse: Fuse(&panicked),
        };
        self.base.drive(consumer1)
    }

    fn len(&self) -> usize {
        self.base.len()
    }

    fn with_producer<CB>(self, callback: CB) -> CB::Output
    where
        CB: ProducerCallback<Self::Item>,
    {
        return self.base.with_producer(Callback { callback });

        struct Callback<CB> {
            callback: CB,
        }

        impl<T, CB> ProducerCallback<T> for Callback<CB>
        where
            CB: ProducerCallback<T>,
        {
            type Output = CB::Output;

            fn callback<P>(self, base: P) -> CB::Output
            where
                P: Producer<Item = T>,
            {
                let panicked = AtomicBool::new(false);
                let producer = PanicFuseProducer {
                    base,
                    fuse: Fuse(&panicked),
                };
                self.callback.callback(producer)
            }
        }
    }
}

/// ////////////////////////////////////////////////////////////////////////
/// Producer implementation

struct PanicFuseProducer<'a, P> {
    base: P,
    fuse: Fuse<'a>,
}

impl<'a, P> Producer for PanicFuseProducer<'a, P>
where
    P: Producer,
{
    type Item = P::Item;
    type IntoIter = PanicFuseIter<'a, P::IntoIter>;

    fn into_iter(self) -> Self::IntoIter {
        PanicFuseIter {
            base: self.base.into_iter(),
            fuse: self.fuse,
        }
    }

    fn min_len(&self) -> usize {
        self.base.min_len()
    }
    fn max_len(&self) -> usize {
        self.base.max_len()
    }

    fn split_at(self, index: usize) -> (Self, Self) {
        let (left, right) = self.base.split_at(index);
        (
            PanicFuseProducer {
                base: left,
                fuse: self.fuse.clone(),
            },
            PanicFuseProducer {
                base: right,
                fuse: self.fuse,
            },
        )
    }

    fn fold_with<G>(self, folder: G) -> G
    where
        G: Folder<Self::Item>,
    {
        let folder1 = PanicFuseFolder {
            base: folder,
            fuse: self.fuse,
        };
        self.base.fold_with(folder1).base
    }
}

struct PanicFuseIter<'a, I> {
    base: I,
    fuse: Fuse<'a>,
}

impl<'a, I> Iterator for PanicFuseIter<'a, I>
where
    I: Iterator,
{
    type Item = I::Item;

    fn next(&mut self) -> Option<Self::Item> {
        if self.fuse.panicked() {
            None
        } else {
            self.base.next()
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.base.size_hint()
    }
}

impl<'a, I> DoubleEndedIterator for PanicFuseIter<'a, I>
where
    I: DoubleEndedIterator,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.fuse.panicked() {
            None
        } else {
            self.base.next_back()
        }
    }
}

impl<'a, I> ExactSizeIterator for PanicFuseIter<'a, I>
where
    I: ExactSizeIterator,
{
    fn len(&self) -> usize {
        self.base.len()
    }
}

/// ////////////////////////////////////////////////////////////////////////
/// Consumer implementation

struct PanicFuseConsumer<'a, C> {
    base: C,
    fuse: Fuse<'a>,
}

impl<'a, T, C> Consumer<T> for PanicFuseConsumer<'a, C>
where
    C: Consumer<T>,
{
    type Folder = PanicFuseFolder<'a, C::Folder>;
    type Reducer = PanicFuseReducer<'a, C::Reducer>;
    type Result = C::Result;

    fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
        let (left, right, reducer) = self.base.split_at(index);
        (
            PanicFuseConsumer {
                base: left,
                fuse: self.fuse.clone(),
            },
            PanicFuseConsumer {
                base: right,
                fuse: self.fuse.clone(),
            },
            PanicFuseReducer {
                base: reducer,
                _fuse: self.fuse,
            },
        )
    }

    fn into_folder(self) -> Self::Folder {
        PanicFuseFolder {
            base: self.base.into_folder(),
            fuse: self.fuse,
        }
    }

    fn full(&self) -> bool {
        self.fuse.panicked() || self.base.full()
    }
}

impl<'a, T, C> UnindexedConsumer<T> for PanicFuseConsumer<'a, C>
where
    C: UnindexedConsumer<T>,
{
    fn split_off_left(&self) -> Self {
        PanicFuseConsumer {
            base: self.base.split_off_left(),
            fuse: self.fuse.clone(),
        }
    }

    fn to_reducer(&self) -> Self::Reducer {
        PanicFuseReducer {
            base: self.base.to_reducer(),
            _fuse: self.fuse.clone(),
        }
    }
}

struct PanicFuseFolder<'a, C> {
    base: C,
    fuse: Fuse<'a>,
}

impl<'a, T, C> Folder<T> for PanicFuseFolder<'a, C>
where
    C: Folder<T>,
{
    type Result = C::Result;

    fn consume(mut self, item: T) -> Self {
        self.base = self.base.consume(item);
        self
    }

    fn consume_iter<I>(mut self, iter: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        fn cool<'a, T>(fuse: &'a Fuse<'_>) -> impl Fn(&T) -> bool + 'a {
            move |_| !fuse.panicked()
        }

        self.base = {
            let fuse = &self.fuse;
            let iter = iter.into_iter().take_while(cool(fuse));
            self.base.consume_iter(iter)
        };
        self
    }

    fn complete(self) -> C::Result {
        self.base.complete()
    }

    fn full(&self) -> bool {
        self.fuse.panicked() || self.base.full()
    }
}

struct PanicFuseReducer<'a, C> {
    base: C,
    _fuse: Fuse<'a>,
}

impl<'a, T, C> Reducer<T> for PanicFuseReducer<'a, C>
where
    C: Reducer<T>,
{
    fn reduce(self, left: T, right: T) -> T {
        self.base.reduce(left, right)
    }
}