rusted_ring/
lib.rs

1pub mod pool;
2pub mod ring;
3
4pub use pool::*;
5pub use ring::*;
6pub const XS_CAPACITY: usize = 2000; // 64 * 2000 = 128KB
7pub const S_CAPACITY: usize = 1000; // 256 * 1000 = 256KB
8pub const M_CAPACITY: usize = 300; // 1024 * 300 = 307KB
9pub const L_CAPACITY: usize = 60; // 4096 * 60 = 245KB
10pub const XL_CAPACITY: usize = 15; // 16384 * 15 = 245KB
11
12pub const XS_TSHIRT_SIZE: usize = 64;
13pub const S_TSHIRT_SIZE: usize = 256;
14pub const M_TSHIRT_SIZE: usize = 1024;
15pub const L_TSHIRT_SIZE: usize = 4096;
16pub const XL_TSHIRT_SIZE: usize = 16384;
17
18/// A Ring Source is the emitter bound to ring buffer
19/// Essentially has a writer that is used by underlying `user` - lets say an actor
20/// This can be leveraged to write to source which then can be read by others.
21/// RingBuffer is a shared entity, expected to be static,
22/// However it is expected that we follow Single Writer and many readers principle,
23/// Source emits events (See `PooledEvent<TSHIRT_SIZE>` for more details).
24#[derive(Debug)]
25pub struct RingSource<const TSHIRT_SIZE: usize, const RING_CAPACITY: usize> {
26    pub writer: Writer<TSHIRT_SIZE, RING_CAPACITY>,
27}
28impl<const TSHIRT_SIZE: usize, const RING_CAPACITY: usize> RingSource<TSHIRT_SIZE, RING_CAPACITY> {
29    pub fn new(out_buffer: &'static RingBuffer<TSHIRT_SIZE, RING_CAPACITY>) -> Self {
30        RingSource {
31            writer: Writer::new(out_buffer),
32        }
33    }
34}
35
36/// A Ring Sink is the just that: a Sink that is  bound to ring buffer
37/// Why a reader? Sink carries an in_buffer which `outsider` writes to.
38/// Reader then used by receiving actor to process messages.
39#[derive(Debug, Clone)]
40pub struct RingSink<const TSHIRT_SIZE: usize, const RING_CAPACITY: usize> {
41    pub reader: Reader<TSHIRT_SIZE, RING_CAPACITY>,
42}
43
44impl<const TSHIRT_SIZE: usize, const RING_CAPACITY: usize> RingSink<TSHIRT_SIZE, RING_CAPACITY> {
45    pub fn new(in_buffer: &'static RingBuffer<TSHIRT_SIZE, RING_CAPACITY>) -> Self {
46        RingSink {
47            reader: Reader::new(in_buffer),
48        }
49    }
50}
51
52/// A conduit meant as a channel replacement in a stack friendly way
53/// This enables actors to communicate following - Single Write and Many reads principle.
54/// Source and Sink both which are underlying entities of a RingPipe
55/// are essentially mechanisms for external parties communicating with anyone
56/// using a RingPipe to leverage reader and writers.
57/// NOTE: the IN_BUFFER is ringbuffer that writer writes to and is read by entity
58/// using the RingPipe using `RingSink`'s `Reader`.
59/// Like wise Writer is used in a manner similar.
60#[derive(Debug)]
61pub struct RingPipe<const TSHIRT_SIZE: usize, const RING_CAPACITY: usize> {
62    pub source: RingSource<TSHIRT_SIZE, RING_CAPACITY>,
63    pub sink: RingSink<TSHIRT_SIZE, RING_CAPACITY>,
64}
65
66impl<const TSHIRT_SIZE: usize, const RING_CAPACITY: usize> RingPipe<TSHIRT_SIZE, RING_CAPACITY> {
67    pub fn new(
68        in_buffer: &'static RingBuffer<TSHIRT_SIZE, RING_CAPACITY>,
69        out_buffer: &'static RingBuffer<TSHIRT_SIZE, RING_CAPACITY>,
70    ) -> Self {
71        RingPipe {
72            source: RingSource::new(out_buffer),
73            sink: RingSink::new(in_buffer),
74        }
75    }
76}