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
#[cfg(feature = "alloc")]
mod queue_alloc;
#[cfg(feature = "alloc")]
pub use queue_alloc::*;
#[cfg(feature = "alloc")]
mod park_queue;
#[cfg(feature = "alloc")]
pub use park_queue::*;
#[cfg(feature = "impl_crossbeam")]
mod queue_crossbeam;
use core::future::Future;
use core::time::Duration;
pub trait TryQueue {
type Item;
fn try_push(&self, value: Self::Item) -> Result<(), Self::Item>;
fn try_pop(&self) -> Option<Self::Item>;
fn clear(&self);
}
pub trait Queue: TryQueue {
fn push(&self, value: Self::Item);
fn pop(&self) -> Self::Item;
}
pub trait AsyncQueue {
type AsyncItem;
type PushFuture: Future<Output = ()>;
type PopFuture: Future<Output = Self::AsyncItem>;
fn push_async(&self, value: Self::AsyncItem) -> Self::PushFuture;
fn pop_async(&self) -> Self::PopFuture;
}
pub trait TimeoutQueue: Queue {
fn push_timeout(&self, value: Self::Item, timeout: Duration) -> Result<(), Self::Item>;
fn pop_timeout(&self, timeout: Duration) -> Option<Self::Item>;
}
pub trait AsyncTimeoutQueue: AsyncQueue {
type PushTimeoutFuture: Future<Output = Result<(), Self::AsyncItem>>;
type PopTimeoutFuture: Future<Output = Option<Self::AsyncItem>>;
fn push_timeout_async(
&self,
value: Self::AsyncItem,
timeout: Duration,
) -> Self::PushTimeoutFuture;
fn pop_timeout_async(&self, timeout: Duration) -> Self::PopTimeoutFuture;
}
pub trait TryPrependQueue: TryQueue {
fn try_push_front(&self, value: Self::Item) -> Result<(), Self::Item>;
}
pub trait PrependQueue: Queue + TryPrependQueue {
fn push_front(&self, value: Self::Item);
}
pub trait AsyncPrependQueue: AsyncQueue {
type PushBackFuture: Future<Output = ()>;
fn push_front_async(&self, value: Self::AsyncItem) -> Self::PushBackFuture;
}
pub trait TryReverseQueue: TryQueue {
fn try_pop_back(&self) -> Option<Self::Item>;
}
pub trait ReverseQueue: TryReverseQueue + Queue {
fn pop_back(&self) -> Self::Item;
}
pub trait AsyncReverseQueue: AsyncQueue {
type PopBackFuture: Future<Output = Self::AsyncItem>;
fn pop_back_async(&self) -> Self::PopBackFuture;
}
pub trait TryPeekQueue: TryQueue {
type Peeked;
fn try_peek(&self) -> Option<Self::Peeked>;
}
pub trait PeekQueue: Queue + TryPeekQueue {
fn peek(&self) -> Self::Peeked;
}
pub trait AsyncPeekQueue: AsyncQueue {
type AsyncPeeked;
type PeekFuture: Future<Output = Self::AsyncPeeked>;
fn peek_async(&self) -> Self::PeekFuture;
}
pub trait TryPeekReverseQueue: TryPeekQueue + TryReverseQueue {
fn try_peek_back(&self) -> Option<Self::Peeked>;
}
pub trait PeekReverseQueue: PeekQueue + ReverseQueue + TryPeekReverseQueue {
fn peek_back(&self) -> Self::Peeked;
}
pub trait AsyncPeekReverseQueue: AsyncPeekQueue + AsyncReverseQueue {
type PeekBackFuture: Future<Output = Self::AsyncPeeked>;
fn peek_back_async(&self) -> Self::PeekBackFuture;
}
pub trait TryDoubleEndedQueue: TryPrependQueue + TryReverseQueue {}
pub trait DoubleEndedQueue: PrependQueue + ReverseQueue + TryDoubleEndedQueue {}
pub trait AsyncDoubleEndedQueue: AsyncPrependQueue + AsyncReverseQueue {}
#[cfg(test)]
pub(super) mod test {
use crate::queue::{Queue, TryQueue};
pub fn try_queue_test<Q>(queue: Q)
where
Q: TryQueue<Item = usize>,
{
assert!(queue.try_pop().is_none());
assert!(queue.try_push(100).is_ok());
assert_eq!(queue.try_pop(), Some(100));
assert!(queue.try_push(200).is_ok());
queue.clear();
assert!(queue.try_pop().is_none());
}
pub fn queue_test<Q>(queue: Q)
where
Q: Queue<Item = usize>,
{
assert!(queue.try_pop().is_none());
queue.push(100);
assert_eq!(queue.pop(), 100);
queue.push(200);
queue.clear();
assert!(queue.try_pop().is_none());
}
}