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
use std::time::Duration;

use crate::blocking_queue::BlockingQueue;
use crate::crossbeam_blocking_queue::CrossbeamBlockingQueue;
use crate::queue_type::QueueType;

/// Wrapper over available queue types.
pub enum BlockingQueueAdapter<E> where E: Send + Sync {
    BlockingQueue {
        blocking_queue: BlockingQueue<E>,
    },
    CrossbeamBlockingQueue {
        crossbeam_blocking_queue: CrossbeamBlockingQueue<E>
    },
}

impl<E> BlockingQueueAdapter<E> where E: Send + Sync {
    pub fn new(queue_type: QueueType, size: usize) -> BlockingQueueAdapter::<E> {
        match queue_type {
            QueueType::BlockingQueue => {
                BlockingQueueAdapter::BlockingQueue {
                    blocking_queue: BlockingQueue::new(size)
                }
            }
            QueueType::CrossbeamBlockingQueue => {
                BlockingQueueAdapter::CrossbeamBlockingQueue {
                    crossbeam_blocking_queue: CrossbeamBlockingQueue::new(size)
                }
            }
        }
    }

    pub fn len(&self) -> usize {
        match self {
            BlockingQueueAdapter::BlockingQueue { blocking_queue } => {
                blocking_queue.len()
            }
            BlockingQueueAdapter::CrossbeamBlockingQueue { crossbeam_blocking_queue } => {
                crossbeam_blocking_queue.len()
            }
        }
    }

    pub fn capacity(&self) -> usize {
        match self {
            BlockingQueueAdapter::BlockingQueue { blocking_queue } => {
                blocking_queue.capacity()
            }
            BlockingQueueAdapter::CrossbeamBlockingQueue { crossbeam_blocking_queue } => {
                crossbeam_blocking_queue.capacity()
            }
        }
    }

    pub fn is_empty(&self) -> bool {
        match self {
            BlockingQueueAdapter::BlockingQueue { blocking_queue } => {
                blocking_queue.is_empty()
            }
            BlockingQueueAdapter::CrossbeamBlockingQueue { crossbeam_blocking_queue } => {
                crossbeam_blocking_queue.is_empty()
            }
        }
    }

    pub fn is_full(&self) -> bool {
        match self {
            BlockingQueueAdapter::BlockingQueue { blocking_queue } => {
                blocking_queue.is_full()
            }
            BlockingQueueAdapter::CrossbeamBlockingQueue { crossbeam_blocking_queue } => {
                crossbeam_blocking_queue.is_full()
            }
        }
    }

    pub fn wait_empty(&self, timeout: Duration) -> bool {
        match self {
            BlockingQueueAdapter::BlockingQueue { blocking_queue } => {
                blocking_queue.wait_empty(timeout)
            }
            BlockingQueueAdapter::CrossbeamBlockingQueue { crossbeam_blocking_queue } => {
                crossbeam_blocking_queue.wait_empty(timeout)
            }
        }
    }

    pub fn enqueue(&self, element: E) {
        match self {
            BlockingQueueAdapter::BlockingQueue { blocking_queue } => {
                blocking_queue.enqueue(element)
            }
            BlockingQueueAdapter::CrossbeamBlockingQueue { crossbeam_blocking_queue } => {
                crossbeam_blocking_queue.enqueue(element)
            }
        }
    }

    pub fn try_enqueue(&self, element: E, timeout: Duration) -> Option<E> {
        match self {
            BlockingQueueAdapter::BlockingQueue { blocking_queue } => {
                blocking_queue.try_enqueue(element, timeout)
            }
            BlockingQueueAdapter::CrossbeamBlockingQueue { crossbeam_blocking_queue } => {
                crossbeam_blocking_queue.try_enqueue(element, timeout)
            }
        }
    }

    pub fn dequeue(&self) -> Option<E> {
        match self {
            BlockingQueueAdapter::BlockingQueue { blocking_queue } => {
                blocking_queue.dequeue()
            }
            BlockingQueueAdapter::CrossbeamBlockingQueue { crossbeam_blocking_queue } => {
                crossbeam_blocking_queue.dequeue()
            }
        }
    }

    pub fn try_dequeue(&self, timeout: Duration) -> Option<E> {
        match self {
            BlockingQueueAdapter::BlockingQueue { blocking_queue } => {
                blocking_queue.try_dequeue(timeout)
            }
            BlockingQueueAdapter::CrossbeamBlockingQueue { crossbeam_blocking_queue } => {
                crossbeam_blocking_queue.try_dequeue(timeout)
            }
        }
    }
}