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
use crate::queue::{Element, QueueBehavior, QueueError, QueueSize};
use std::fmt::Debug;
use std::sync::mpsc::{channel, Receiver, SendError, Sender, TryRecvError};
use std::sync::{Arc, Mutex};

use anyhow::Result;

#[derive(Debug, Clone)]
pub struct QueueMPSC<E> {
  rx: Arc<Mutex<Receiver<E>>>,
  tx: Sender<E>,
  count: Arc<Mutex<QueueSize>>,
  capacity: Arc<Mutex<QueueSize>>,
}

impl<E: Element + 'static> QueueBehavior<E> for QueueMPSC<E> {
  fn len(&self) -> QueueSize {
    let count_guard = self.count.lock().unwrap();
    count_guard.clone()
  }

  fn capacity(&self) -> QueueSize {
    let capacity_guard = self.capacity.lock().unwrap();
    capacity_guard.clone()
  }

  fn offer(&mut self, e: E) -> anyhow::Result<()> {
    match self.tx.send(e) {
      Ok(_) => {
        let mut count_guard = self.count.lock().unwrap();
        count_guard.increment();
        Ok(())
      }
      Err(SendError(e)) => Err(anyhow::Error::new(QueueError::OfferError(e))),
    }
  }

  fn poll(&mut self) -> Result<Option<E>> {
    let receiver_guard = self.rx.lock().unwrap();
    match receiver_guard.try_recv() {
      Ok(e) => {
        let mut count_guard = self.count.lock().unwrap();
        count_guard.decrement();
        Ok(Some(e))
      }
      Err(TryRecvError::Empty) => Ok(None),
      Err(TryRecvError::Disconnected) => Err(anyhow::Error::new(QueueError::<E>::PoolError)),
    }
  }
}

impl<E: Element + 'static> QueueMPSC<E> {
  pub fn new() -> Self {
    let (tx, rx) = channel();
    Self {
      rx: Arc::new(Mutex::new(rx)),
      tx,
      count: Arc::new(Mutex::new(QueueSize::Limited(0))),
      capacity: Arc::new(Mutex::new(QueueSize::Limitless)),
    }
  }

  pub fn with_num_elements(num_elements: usize) -> Self {
    let (tx, rx) = channel();
    Self {
      rx: Arc::new(Mutex::new(rx)),
      tx,
      count: Arc::new(Mutex::new(QueueSize::Limited(0))),
      capacity: Arc::new(Mutex::new(QueueSize::Limited(num_elements))),
    }
  }
}