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
#[cfg(feature = "async")]
extern crate futures;

extern crate arc_swap;

use arc_swap::ArcSwapOption;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

/// Provides an interface for subscribers
///
/// Every BusReader that can keep up with the push frequency should recv every pushed object.
/// BusReaders unable to keep up will miss object once the writer's index wi is larger then
/// reader's index ri + size
pub struct BusReader<T> {
    buffer: Arc<Vec<ArcSwapOption<T>>>,
    wi: Arc<AtomicUsize>,
    ri: usize,
    size: usize,
}

impl<T> BusReader<T> {
    /// Receives some atomic refrence to an object if queue is not empty, or None if it is
    pub fn recv(&mut self) -> Option<Arc<T>> {
        if self.ri == self.wi.load(Ordering::Relaxed) {
            return None;
        }
        loop {
            match self.buffer[self.ri % self.size].load() {
                Some(some) => if self.wi.load(Ordering::Relaxed) > self.ri + self.size {
                    self.ri = self.wi.load(Ordering::Relaxed) - self.size;
                } else {
                    self.ri += 1;
                    return Some(some);
                },
                None => unreachable!(),
            }
        }
    }
}

/// Provides an interface for the publisher
pub struct Bus<T> {
    // atp to an array of atps of option<arc<t>>
    buffer: Arc<Vec<ArcSwapOption<T>>>,
    wi: Arc<AtomicUsize>,
    size: usize,
}

impl<T> Bus<T> {
    /// Instantiates the Bus struct, creating the initial buffer filled with None.
    /// # Arguments
    /// * `size` - a usize size of the internal circular buffer
    pub fn new(size: usize) -> Self {
        let mut temp: Vec<ArcSwapOption<T>> = Vec::new();
        temp.resize(size, ArcSwapOption::new(None));

        Self {
            buffer: Arc::new(temp),
            wi: Arc::new(AtomicUsize::new(0)),
            size: size,
        }
    }
    /// Instantiates the BusReader struct connected the Bus circular buffer
    pub fn add_sub(&mut self) -> BusReader<T> {
        BusReader {
            buffer: self.buffer.clone(),
            wi: self.wi.clone(),
            ri: 0,
            size: self.size,
        }
    }
    /// Publishes values to the circular buffer at wi % size
    /// # Arguments
    /// * `object` - owned object to be published
    pub fn push(&mut self, object: T) {
        self.buffer[self.wi.load(Ordering::Relaxed) % self.size].store(Some(Arc::new(object)));
        self.wi.fetch_add(1, Ordering::Relaxed);
    }
}

#[cfg(feature = "async")]
pub mod async;