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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#![doc = "../README.md"]

#[doc(hidden)]
#[path = "exports.rs"]
pub mod __private;

use futures_core::{Future, Stream};

use std::{
    ops::{Deref, DerefMut},
    pin::Pin,
    task::{Context, Poll},
};

use bitflags::bitflags;

pub trait AsyncComponent: Unpin {
    fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<ComponentPollFlags>;
}

impl<T: ?Sized + AsyncComponent> AsyncComponent for Box<T> {
    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<ComponentPollFlags> {
        T::poll_next(Pin::new(&mut *self), cx)
    }
}

impl<T: ?Sized + AsyncComponent> AsyncComponent for &mut T {
    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<ComponentPollFlags> {
        Pin::new(&mut **self).poll_next(cx)
    }
}

pub trait AsyncComponentExt {
    fn next(&mut self) -> Next<Self>;

    fn into_stream(self) -> AsyncComponentStream<Self>;
}

impl<T: AsyncComponent> AsyncComponentExt for T {
    fn next(&mut self) -> Next<Self> {
        Next(self)
    }

    fn into_stream(self) -> AsyncComponentStream<Self> {
        AsyncComponentStream(self)
    }
}

#[derive(Debug)]
pub struct Next<'a, T: ?Sized>(&'a mut T);

impl<T: AsyncComponent> Future for Next<'_, T> {
    type Output = ComponentPollFlags;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        Pin::new(&mut *self.0).poll_next(cx)
    }
}

#[derive(Debug, Clone)]
pub struct AsyncComponentStream<T: ?Sized>(T);

impl<T: AsyncComponent> Stream for AsyncComponentStream<T> {
    type Item = ComponentPollFlags;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        Pin::new(&mut self.0).poll_next(cx).map(Some)
    }
}

pub type PhantomState = StateCell<()>;

#[derive(Debug)]
pub struct StateCell<T> {
    status: StateStatus,
    inner: T,
}

impl<T> StateCell<T> {
    pub const fn new(inner: T) -> Self {
        Self {
            status: StateStatus::Changed,
            inner,
        }
    }

    pub fn invalidate(this: &mut Self) {
        if let StateStatus::None = this.status {
            this.status = StateStatus::Changed;
        }
    }

    pub fn refresh(this: &mut Self) -> bool {
        match this.status {
            StateStatus::Changed => {
                this.status = StateStatus::None;
                true
            }

            _ => false,
        }
    }
}

impl<T> Deref for StateCell<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<T> DerefMut for StateCell<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        StateCell::invalidate(self);

        &mut self.inner
    }
}

impl<T> From<T> for StateCell<T> {
    fn from(value: T) -> Self {
        Self::new(value)
    }
}

impl<T: Default> Default for StateCell<T> {
    fn default() -> Self {
        Self::new(Default::default())
    }
}

#[derive(Debug, Clone)]
enum StateStatus {
    None,
    Changed,
}

impl Default for StateStatus {
    fn default() -> Self {
        Self::None
    }
}

bitflags! {
    pub struct ComponentPollFlags: u32 {
        const STATE = 0b00000001;
        const STREAM = 0b00000010;
    }
}