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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! Infrastructure for producers and consumers in a market.

//!

//! A market is a stock of goods. A producer stores goods in the market while a consumer retrieves goods from the market.

//!

//! In the rust stdlib, the primary example of a market is [`std::sync::mpsc::channel`]. [`std::sync::mpsc::Sender`] is the producer and [`std::sync::mpsc::Receiver`] is the consumer.

//!

//! [`std::sync::mpsc::channel`]: https://doc.rust-lang.org/std/sync/mpsc/fn.channel.html

//! [`std::sync::mpsc::Sender`]: https://doc.rust-lang.org/std/sync/mpsc/struct.Sender.html

//! [`std::sync::mpsc::Receiver`]: https:://doc.rust-lang.org/std/sync/mpsc/struct.Receiver.html


pub mod channel;
mod error;
pub mod io;
mod map;
pub mod process;
pub mod sync;
pub mod thread;

pub use {
    error::{ClosedMarketFault, ConsumeFailure, ProduceFailure, Recall},
    never::Never,
};

use {
    core::fmt::Debug,
    crossbeam_queue::SegQueue,
    fehler::{throw, throws},
    std::error::Error,
};

/// Retrieves goods from a market.

///

/// The order in which goods are retrieved is defined by the implementer.

pub trait Consumer {
    /// The item being consumed.

    type Good;
    /// The fault that could be thrown during consumption.

    type Fault: Error + 'static;

    /// Retrieves the next good from the market without blocking.

    ///

    /// To ensure all functionality of a `Consumer` performs as specified, the implementor MUST implement `consume` such that all of the following specifications are true:

    ///

    /// 1. `consume` returns without blocking the current process.

    /// 2. If at least one good is in stock, `consume` returns one of those goods.

    /// 3. If fault `T` is thrown during consumption, `consume` throws `ConsumeFailure::Fault(T)`.

    /// 4. If there are no goods in stock, `consume` throws `ConsumeFailure::EmptyStock`.

    #[throws(ConsumeFailure<Self::Fault>)]
    fn consume(&self) -> Self::Good;

    /// Retrieves all goods held in the market without blocking.

    ///

    /// If the stock of the market is empty, an empty list is returned. If a fault is thrown after consuming 1 or more goods, the consumption stops and the fault is ignored.

    #[inline]
    #[throws(Self::Fault)]
    fn consume_all(&self) -> Vec<Self::Good> {
        let mut goods = Vec::new();

        loop {
            match self.consume() {
                Ok(good) => {
                    goods.push(good);
                }
                Err(failure) => {
                    if let ConsumeFailure::Fault(fault) = failure {
                        if goods.is_empty() {
                            throw!(fault)
                        }
                    }

                    break goods;
                }
            }
        }
    }

    /// Retrieves the next good from the market, blocking until one is available.

    #[inline]
    #[throws(Self::Fault)]
    fn demand(&self) -> Self::Good {
        loop {
            match self.consume() {
                Ok(good) => {
                    break good;
                }
                Err(failure) => {
                    if let ConsumeFailure::Fault(fault) = failure {
                        throw!(fault);
                    }
                }
            }
        }
    }
}

/// Stores goods in a market.

#[allow(clippy::missing_inline_in_public_items)] // current issue with fehler for `fn produce()`; see https://github.com/withoutboats/fehler/issues/39

pub trait Producer {
    /// The item being produced.

    type Good;
    /// The fault that could be thrown during production.

    type Fault: Error;

    /// Stores `good` in the market without blocking.

    ///

    /// To ensure all functionality of the `Producer` performs as specified, the implementor MUST implement `produce` such that all of the following specifications are true:

    ///

    /// 1. `produce` returns without blocking the current process.

    /// 2. If the market has space available for `good`, `process` stores `good` in the market.

    /// 3. If the market has no space available for `good`, `process` throws `ProduceFailure::FullStock`.

    /// 4. If fault `T` is thrown, `produce` throws `ProduceFailure::Fault(T)`.

    #[allow(redundant_semicolons, unused_variables)] // current issue with fehler; see https://github.com/withoutboats/fehler/issues/39

    #[throws(ProduceFailure<Self::Fault>)]
    fn produce(&self, good: Self::Good);

    /// Stores `good` in the market without blocking, returning `good` on failure.

    #[throws(Recall<Self::Good, Self::Fault>)]
    fn produce_or_recall(&self, good: Self::Good)
    where
        // Debug and Dislay bounds required by Recall.

        Self::Good: Clone + Debug,
    {
        self.produce(good.clone())
            .map_err(|failure| Recall::new(good, failure))?
    }

    /// Stores each good in `goods` in the market without blocking.

    ///

    /// If a failure is thrown, all goods remaining to be produced are not attempted.

    #[throws(ProduceFailure<Self::Fault>)]
    fn produce_all(&self, goods: Vec<Self::Good>) {
        for good in goods {
            self.produce(good)?
        }
    }

    /// Stores `good` in the market, blocking until space is available.

    #[inline]
    #[throws(Self::Fault)]
    fn force(&self, mut good: Self::Good)
    where
        Self::Good: Clone + Debug,
        Self::Fault: 'static,
    {
        loop {
            match self.produce_or_recall(good) {
                Ok(()) => break,
                Err(recall) => {
                    good = recall.overstock()?;
                }
            }
        }
    }

    /// Stores every good in `goods`, blocking until space is available.

    ///

    /// If an error is thrown, all goods remaining to be produced are not attempted.

    #[throws(Self::Fault)]
    fn force_all(&self, goods: Vec<Self::Good>)
    where
        Self::Good: Clone + Debug,
        Self::Fault: 'static,
    {
        for good in goods {
            self.force(good)?
        }
    }
}

/// A [`Consumer`] that consumes goods of type `G` from multiple [`Consumer`]s.

#[derive(Default)]
pub struct Collector<G, T> {
    /// The [`Consumer`]s.

    consumers: Vec<Box<dyn Consumer<Good = G, Fault = T>>>,
}

impl<G, T> Collector<G, T>
where
    T: 'static,
{
    /// Creates a new, empty [`Collector`].

    #[must_use]
    #[inline]
    pub fn new() -> Self {
        Self {
            consumers: Vec::new(),
        }
    }

    /// Adds `consumer` to the end of the [`Consumer`]s held by `self`.

    #[inline]
    pub fn push<C>(&mut self, consumer: std::rc::Rc<C>)
    where
        C: Consumer + 'static,
        G: From<C::Good> + 'static,
        T: From<C::Fault> + Error + 'static,
    {
        self.consumers.push(Box::new(map::Adapter::new(consumer)));
    }
}

impl<G, T> Consumer for Collector<G, T>
where
    T: Error + 'static,
{
    type Good = G;
    type Fault = T;

    #[inline]
    #[throws(ConsumeFailure<Self::Fault>)]
    fn consume(&self) -> Self::Good {
        let mut result = Err(ConsumeFailure::EmptyStock);

        for consumer in &self.consumers {
            result = consumer.consume();

            if let Err(ConsumeFailure::EmptyStock) = result {
            } else {
                break;
            }
        }

        result?
    }
}

impl<G, E> Debug for Collector<G, E> {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "Collector {{ .. }}")
    }
}

/// Distributes goods to multiple producers.

pub struct Distributor<G, T> {
    /// The producers.

    producers: Vec<Box<dyn Producer<Good = G, Fault = T>>>,
}

impl<G, T> Distributor<G, T>
where
    T: 'static,
{
    /// Creates a new, empty [`Distributor`].

    #[must_use]
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds `producer` to the end of the [`Producers`]s held by `self`.

    #[inline]
    pub fn push<P>(&mut self, producer: std::rc::Rc<P>)
    where
        P: Producer + 'static,
        G: core::convert::TryInto<P::Good> + 'static,
        T: From<P::Fault> + Error + 'static,
    {
        self.producers.push(Box::new(map::Converter::new(producer)));
    }
}

impl<G, T> Debug for Distributor<G, T> {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "Distributor {{ .. }}")
    }
}

impl<G, T> Default for Distributor<G, T> {
    #[inline]
    fn default() -> Self {
        Self {
            producers: Vec::new(),
        }
    }
}

impl<G, T> Producer for Distributor<G, T>
where
    T: Error + 'static,
    G: Clone,
{
    type Good = G;
    type Fault = T;

    #[inline]
    #[throws(ProduceFailure<Self::Fault>)]
    fn produce(&self, good: Self::Good) {
        for producer in &self.producers {
            producer.produce(good.clone())?;
        }
    }
}

/// Inspects if goods meet defined requirements.

pub trait Inspector {
    /// The good to be inspected.

    type Good;

    /// Returns if `good` meets requirements.

    fn allows(&self, good: &Self::Good) -> bool;
}

/// Consumes goods that an [`Inspector`] has allowed.

#[derive(Debug)]
pub struct VigilantConsumer<C, I> {
    /// The consumer.

    consumer: C,
    /// The inspector.

    inspector: I,
}

impl<C, I> VigilantConsumer<C, I> {
    /// Creates a new [`VigilantConsumer`].

    #[inline]
    pub const fn new(consumer: C, inspector: I) -> Self {
        Self {
            consumer,
            inspector,
        }
    }
}

impl<C, I> Consumer for VigilantConsumer<C, I>
where
    C: Consumer,
    I: Inspector<Good = <C as Consumer>::Good> + Debug,
{
    type Good = <C as Consumer>::Good;
    type Fault = <C as Consumer>::Fault;

    #[inline]
    #[throws(ConsumeFailure<Self::Fault>)]
    fn consume(&self) -> Self::Good {
        let mut input;

        loop {
            input = self.consumer.consume()?;

            if self.inspector.allows(&input) {
                break;
            }
        }

        input
    }
}

/// Defines a queue with unlimited size that implements [`Consumer`] and [`Producer`].

///

/// An [`UnlimitedQueue`] can be closed, which prevents the [`Producer`] from producing new goods while allowing the [`Consumer`] to consume only the remaining goods on the queue.

#[derive(Debug)]
pub struct UnlimitedQueue<G> {
    /// The queue.

    queue: SegQueue<G>,
    /// A trigger to close the queue.

    closure: sync::Trigger,
}

impl<G> UnlimitedQueue<G> {
    /// Creates a new empty [`UnlimitedQueue`].

    #[must_use]
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }

    /// Closes `self`.

    #[inline]
    pub fn close(&self) {
        self.closure.trigger();
    }
}

impl<G> Consumer for UnlimitedQueue<G>
where
    G: Debug,
{
    type Good = G;
    type Fault = ClosedMarketFault;

    #[inline]
    #[throws(ConsumeFailure<Self::Fault>)]
    fn consume(&self) -> Self::Good {
        if let Some(good) = self.queue.pop() {
            good
        } else if self.closure.is_triggered() {
            throw!(ConsumeFailure::Fault(ClosedMarketFault));
        } else {
            throw!(ConsumeFailure::EmptyStock);
        }
    }
}

impl<G> Default for UnlimitedQueue<G> {
    #[inline]
    fn default() -> Self {
        Self {
            queue: SegQueue::new(),
            closure: sync::Trigger::new(),
        }
    }
}

impl<G> Producer for UnlimitedQueue<G>
where
    G: Debug,
{
    type Good = G;
    type Fault = ClosedMarketFault;

    #[inline]
    #[throws(ProduceFailure<Self::Fault>)]
    fn produce(&self, good: Self::Good) {
        if self.closure.is_triggered() {
            throw!(ProduceFailure::Fault(ClosedMarketFault));
        } else {
            self.queue.push(good);
        }
    }
}

/// An unlimited queue with a producer and consumer that are always functional.

#[derive(Debug, Default)]
pub struct PermanentQueue<G> {
    /// The queue.

    queue: SegQueue<G>,
}

impl<G> PermanentQueue<G> {
    /// Creates a new [`PermanentQueue`].

    #[must_use]
    #[inline]
    pub fn new() -> Self {
        Self {
            queue: SegQueue::new(),
        }
    }
}

impl<G> Consumer for PermanentQueue<G>
where
    G: Debug,
{
    type Good = G;
    type Fault = Never;

    #[inline]
    #[throws(ConsumeFailure<Self::Fault>)]
    fn consume(&self) -> Self::Good {
        if let Some(good) = self.queue.pop() {
            good
        } else {
            throw!(ConsumeFailure::EmptyStock)
        }
    }
}

impl<G> Producer for PermanentQueue<G>
where
    G: Debug,
{
    type Good = G;
    type Fault = Never;

    // TODO: Find a way to indicate this never fails.

    #[inline]
    #[throws(ProduceFailure<Self::Fault>)]
    fn produce(&self, good: Self::Good) {
        self.queue.push(good);
    }
}