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
/// Trait that concurrent, non-blocking queues implement.
pub trait ConcurrentQueue<T> {
/// Tries to put a value onto the queue.
///
/// If the queue is not full, the method returns `None`, signifying success.
/// If the queue is full, it returns `Some(v)`, where `v` is the original,
/// specified value.
///
/// # Examples
///
/// ```
/// use cosmo::collection::{ConcurrentQueue, SpscConcurrentQueue};
/// let queue = SpscConcurrentQueue::<u64>::with_capacity(16);
/// match queue.offer(10) {
/// Some(v) => println!("Queue is full"),
/// None => println!("Value added to the queue")
/// }
/// ```
fn offer(&self, val: T) -> Option<T>;
/// Tries to remove a value from the queue.
///
/// If the queue is not empty, the method returns `Some(v)`, effectively
/// removing `v` from the queue. If the queue is empty, it returns `None`.
///
/// # Examples
///
/// ```
/// use cosmo::collection::{ConcurrentQueue, SpscConcurrentQueue};
/// let queue = SpscConcurrentQueue::<u64>::with_capacity(16);
/// match queue.poll() {
/// Some(v) => println!("Removed item {}", v),
/// None => println!("Queue is empty")
/// }
/// ```
fn poll(&self) -> Option<T>;
/// Tries to peek a value from the queue.
///
/// If the queue is not empty, the method returns `Some(v)`, where `v` is
/// the value at the head of the queue. If the queue is empty, it returns
/// `None`.
///
/// # Examples
///
/// ```
/// use cosmo::collection::{ConcurrentQueue, SpscConcurrentQueue};
/// let queue = SpscConcurrentQueue::<u64>::with_capacity(16);
/// match queue.peek() {
/// Some(v) => println!("Peeked value {}", v),
/// None => println!("Queue is empty")
/// }
/// ```
fn peek(&self) -> Option<T>;
/// Returns the capacity of the queue.
///
/// # Examples
///
/// ```
/// use cosmo::collection::{ConcurrentQueue, SpscConcurrentQueue};
/// let queue = SpscConcurrentQueue::<u64>::with_capacity(16);
/// assert_eq!(16, queue.capacity());
/// queue.offer(10);
/// assert_eq!(16, queue.capacity());
/// ```
fn capacity(&self) -> usize;
/// Returns how many items are in the queue.
///
/// # Examples
///
/// ```
/// use cosmo::collection::{ConcurrentQueue, SpscConcurrentQueue};
/// let queue = SpscConcurrentQueue::<u64>::with_capacity(16);
/// assert_eq!(0, queue.size());
/// queue.offer(10);
/// assert_eq!(1, queue.size());
/// ```
fn size(&self) -> usize;
/// Tells whether the queue is empty or not.
///
/// # Examples
///
/// ```
/// use cosmo::collection::{ConcurrentQueue, SpscConcurrentQueue};
/// let queue = SpscConcurrentQueue::<u64>::with_capacity(16);
/// if queue.is_empty() {
/// queue.offer(10);
/// }
/// assert_eq!(Some(10), queue.poll());
/// ```
fn is_empty(&self) -> bool {
self.size() == 0
}
/// Tells whether the queue is full or not.
///
/// # Examples
///
/// ```
/// use cosmo::collection::{ConcurrentQueue, SpscConcurrentQueue};
/// let queue = SpscConcurrentQueue::<u64>::with_capacity(16);
/// if !queue.is_full() {
/// queue.offer(10);
/// }
/// assert_eq!(Some(10), queue.poll());
/// ```
fn is_full(&self) -> bool {
self.capacity() == self.size()
}
}