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
use {
crate::{ClosedMarketFailure, ConsumeError, Consumer, ProduceError, Producer},
core::fmt::{Debug, Display},
fehler::throws,
std::sync::mpsc,
};
#[derive(Debug)]
pub struct StdConsumer<G> {
rx: mpsc::Receiver<G>,
}
impl<G> Consumer for StdConsumer<G>
where
G: Debug,
{
type Good = G;
type Failure = ClosedMarketFailure;
#[inline]
#[throws(ConsumeError<Self::Failure>)]
fn consume(&self) -> Self::Good {
self.rx.try_recv()?
}
}
impl<G> From<mpsc::Receiver<G>> for StdConsumer<G> {
#[inline]
fn from(value: mpsc::Receiver<G>) -> Self {
Self { rx: value }
}
}
#[derive(Debug)]
pub struct CrossbeamConsumer<G>
where
G: Debug,
{
rx: crossbeam_channel::Receiver<G>,
}
impl<G> Consumer for CrossbeamConsumer<G>
where
G: Debug,
{
type Good = G;
type Failure = ClosedMarketFailure;
#[inline]
#[throws(ConsumeError<Self::Failure>)]
fn consume(&self) -> Self::Good {
self.rx.try_recv()?
}
}
impl<G> From<crossbeam_channel::Receiver<G>> for CrossbeamConsumer<G>
where
G: Debug,
{
#[inline]
fn from(value: crossbeam_channel::Receiver<G>) -> Self {
Self { rx: value }
}
}
#[derive(Debug)]
pub struct CrossbeamProducer<G> {
tx: crossbeam_channel::Sender<G>,
}
impl<G> Producer for CrossbeamProducer<G>
where
G: Debug + Display,
{
type Good = G;
type Failure = ClosedMarketFailure;
#[inline]
#[throws(ProduceError<Self::Failure>)]
fn produce(&self, good: Self::Good) {
self.tx.try_send(good)?
}
}
impl<G> From<crossbeam_channel::Sender<G>> for CrossbeamProducer<G> {
#[inline]
fn from(value: crossbeam_channel::Sender<G>) -> Self {
Self { tx: value }
}
}