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
//! Resting place for [MutinyStream]

use crate::types::ChannelConsumer;
use std::{
    pin::Pin,
    sync::Arc,
    fmt::Debug,
    task::{Context, Poll},
};
use std::marker::PhantomData;
use futures::stream::Stream;


/// Special `Stream` implementation to avoid using dynamic dispatching, so to allow
/// the compiler to fully optimize the whole event consumption chain.\
/// The following paths are covered:
///   - from the container's `consume()` (providing `InItemType` items),
///   - passing through this Stream implementation,
///   - then through the user provided `pipeline_builder()`
///   - and, finally, to the `StreamExecutor`.
/// ... allowing all of them to behave as a single function, that gets optimized together.
pub struct MutinyStream<'a, ItemType:            Debug + Send + Sync + 'a,
                            ChannelConsumerType: ChannelConsumer<'a, DerivedItemType> + ?Sized,
                            DerivedItemType:     'a + Debug> {
    stream_id:     u32,
    events_source: Arc<ChannelConsumerType>,
    _phantom:      PhantomData<(&'a ItemType, &'a DerivedItemType)>,

}

impl<'a, ItemType:            Debug + Send + Sync,
         ChannelConsumerType: ChannelConsumer<'a, DerivedItemType>,
         DerivedItemType:     Debug>
MutinyStream<'a, ItemType, ChannelConsumerType, DerivedItemType> {

    pub fn new(stream_id: u32, events_source: &Arc<ChannelConsumerType>) -> Self {
        Self {
            stream_id,
            events_source: events_source.clone(),
            _phantom:      PhantomData,
        }
    }

}

impl<'a, ItemType:            Debug + Send + Sync + 'a,
         ChannelConsumerType: ChannelConsumer<'a, DerivedItemType>,
         DerivedItemType:     Debug>
Stream for
MutinyStream<'a, ItemType, ChannelConsumerType, DerivedItemType> {

    type Item = DerivedItemType;

    #[inline(always)]
    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let event = self.events_source.consume(self.stream_id);
        match event {
            Some(_) => Poll::Ready(event),
            None => {
                if self.events_source.keep_stream_running(self.stream_id) {
                    self.events_source.register_stream_waker(self.stream_id, cx.waker());
                    Poll::Pending
                } else {
                    Poll::Ready(None)
                }
            },
        }
    }
}

impl<'a, ItemType:            Debug + Send + Sync,
         ChannelConsumerType: ChannelConsumer<'a, DerivedItemType> + ?Sized,
         DerivedItemType:     Debug>
Drop
for MutinyStream<'a, ItemType, ChannelConsumerType, DerivedItemType> {
    fn drop(&mut self) {
        self.events_source.drop_resources(self.stream_id);
    }
}