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
use {clock, Delay, Error};
use futures::future::Either;
use futures::{Async, Future, Poll, Stream};
use std::{
error::Error as StdError,
fmt::{Display, Formatter, Result as FmtResult},
time::Duration,
};
#[derive(Debug)]
#[must_use = "streams do nothing unless polled"]
pub struct Throttle<T> {
delay: Option<Delay>,
duration: Duration,
stream: T,
}
#[derive(Debug)]
pub struct ThrottleError<T>(Either<T, Error>);
impl<T> Throttle<T> {
pub fn new(stream: T, duration: Duration) -> Self {
Self {
delay: None,
duration: duration,
stream: stream,
}
}
pub fn get_ref(&self) -> &T {
&self.stream
}
pub fn get_mut(&mut self) -> &mut T {
&mut self.stream
}
pub fn into_inner(self) -> T {
self.stream
}
}
impl<T: Stream> Stream for Throttle<T> {
type Item = T::Item;
type Error = ThrottleError<T::Error>;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
if let Some(ref mut delay) = self.delay {
try_ready!({ delay.poll().map_err(ThrottleError::from_timer_err) });
}
self.delay = None;
let value = try_ready!({ self.stream.poll().map_err(ThrottleError::from_stream_err) });
if value.is_some() {
self.delay = Some(Delay::new(clock::now() + self.duration));
}
Ok(Async::Ready(value))
}
}
impl<T> ThrottleError<T> {
pub fn from_stream_err(err: T) -> Self {
ThrottleError(Either::A(err))
}
pub fn from_timer_err(err: Error) -> Self {
ThrottleError(Either::B(err))
}
pub fn get_stream_error(&self) -> Option<&T> {
match self.0 {
Either::A(ref x) => Some(x),
_ => None,
}
}
pub fn get_timer_error(&self) -> Option<&Error> {
match self.0 {
Either::B(ref x) => Some(x),
_ => None,
}
}
pub fn into_stream_error(self) -> Option<T> {
match self.0 {
Either::A(x) => Some(x),
_ => None,
}
}
pub fn into_timer_error(self) -> Option<Error> {
match self.0 {
Either::B(x) => Some(x),
_ => None,
}
}
pub fn is_stream_error(&self) -> bool {
!self.is_timer_error()
}
pub fn is_timer_error(&self) -> bool {
match self.0 {
Either::A(_) => false,
Either::B(_) => true,
}
}
}
impl<T: StdError> Display for ThrottleError<T> {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match self.0 {
Either::A(ref err) => write!(f, "stream error: {}", err),
Either::B(ref err) => write!(f, "timer error: {}", err),
}
}
}
impl<T: StdError + 'static> StdError for ThrottleError<T> {
fn description(&self) -> &str {
match self.0 {
Either::A(_) => "stream error",
Either::B(_) => "timer error",
}
}
#[allow(deprecated)]
fn cause(&self) -> Option<&dyn StdError> {
match self.0 {
Either::A(ref err) => Some(err),
Either::B(ref err) => Some(err),
}
}
}