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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use std::{
    ops::{Deref, DerefMut},
    pin::Pin,
    task::Poll,
};

mod sealed {
    pub trait HookLifetimeBounds<'hook, This: ?Sized> {}
    impl<'hook, T: super::HookBounds<Bounds = B> + ?Sized, B: ?Sized> HookLifetimeBounds<'hook, T>
        for &'hook B
    {
    }
}

pub trait HookBounds {
    type Bounds: ?Sized;
}

/// This is a helper trait to define
/// *lifetime generic associated types (lifetime-GAT)*
/// for [`Hook`].
///
/// This trait follows the [*better GAT*] pattern so that
/// *lifetime-GAT* can be used in earlier rust versions.
///
/// This pattern also enables implicit bounds.
/// Comparing to the common *better GAT* pattern
/// which uses `&'a Self` as the implicit bound,
/// this trait uses [`HookBounds`] trait to allow
/// custom implicit bounds.
/// With just `&'a Self`, returning `impl for<'hook> HookLifetime<'hook, Value = ...>`
/// would emit *"return type cannot contain a projection or Self..."* error.
/// This might be a [compiler bug].
///
/// [*better GAT*]: https://sabrinajewson.org/blog/the-better-alternative-to-lifetime-gats#the-better-gats
/// [compiler bug]: https://github.com/rust-lang/rust/issues/61949#issuecomment-789664939
pub trait HookLifetime<
    'hook,
    Args,
    ImplicitBounds: sealed::HookLifetimeBounds<'hook, Self> = &'hook <Self as HookBounds>::Bounds,
>: HookBounds
{
    type Value;
}

pub trait HookPollNextUpdate {
    /// The meaning of the return value is:
    ///
    /// - `Poll::Pending` means this hook's inner state is not updated
    ///   after the last `use_hook`.
    ///   The executor **DO NOT NEED** to call `use_hook` again
    ///   because the returned value is expected to remain the same
    ///   as the value from the last call.
    ///   The executor **CAN** still call `use_hook`
    ///   to re-get the returned value.
    ///
    /// - `Poll::Ready(true)` means this hook's inner state has been updated
    ///   since the last `use_hook`.
    ///   The executor **SHOULD** call `use_hook` again to get the new value.
    ///   The executor **CAN** ignore this update, by polling next update
    ///   without calling `use_hook`.
    ///
    /// - `Poll::Ready(false)` means this hook's inner state will never be updated.
    ///   The executor **CAN** no longer call `use_hook` or even drop this hook.
    ///   The executor **CAN** also call `use_hook` to get the value and
    ///   the hook **MIGHT** become dynamic again during `use_hook` or when
    ///   some operations is done to the returned value.
    fn poll_next_update(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<bool>;
}

/// ## How to impl `Hook`
///
/// ### with [`hook`] macro
///
/// Usually, we just need a function which returns a hook,
/// without needing a type which implements `Hook`.
/// With `hook` macro, we can do this easily.
///
/// ```
/// # use hooks::{use_effect, hook};
///
/// /// Print debug on `value` change.
/// #[hook]
/// fn use_debug<'a, T: std::fmt::Debug + Eq + 'a>(value: &'a T) {
///     use_effect(|v: &_| {
///         println!("{v:?}");
///     }, value);
/// }
/// ```
///
/// ### implement `Hook` manually.
///
/// To implement `Hook` for a type, implement
/// [`HookBounds`], [HookLifetime<'hook>] and [HookPollNextUpdate]
/// first.
///
/// ```
/// # use hooks_core::{HookBounds, HookLifetime, HookPollNextUpdate};
///
/// struct MyHook<T>(Option<T>);
///
/// impl<T> HookBounds for MyHook<T> {
///     type Bounds = Self;
/// }
///
/// impl<'hook, T> HookLifetime<'hook, (T,), &'hook Self> for MyHook<T> {
/// //                                       ^^^^^^^^^^^
/// //                                       This must be exactly
/// //                                       `&'hook <Self as HookBounds>::Bounds`
///
///     type Value = &'hook T;
/// //               ^^^^^^^^  We can write `&'hook T` without
/// //                         implicitly specifying `T: 'hook`
/// //                         because `&'hook Self` implies this.
/// }
///
/// impl<T> HookPollNextUpdate for MyHook<T> {
///     fn poll_next_update(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<bool> {
///         todo!()
///     }
/// }
/// ```
///
/// ## Comparison with `LendingAsyncIterator`
///
/// A `Hook` is like a `LendingAsyncIterator`.
/// They both produce items asynchronously,
/// but they have different meanings on pending and terminating:
///
/// For pending:
///
/// - If a `LendingAsyncIterator` is pending
///   (`poll_next` returns `Poll::Pending`),
///   it is producing the next item.
///
/// - If a `Hook` is pending,
///   (`poll_next_update` returns `Poll::Pending`),
///   it is waiting for its inner state to update.
///   When a `Hook` is pending, the executor can still *use* it
///   by calling [`use_hook`](Hook::use_hook) and
///   the returned value would remain the *same* as the last returned value.
///   *Using* a hook is like *inspecting* it.
///   Some hooks may do heavy work in `use_hook`.
///   For example, `use_state_clone` clones the data in `use_hook`.
///   It is advised to call `use_hook` only after
///   `poll_next_update` returns `Poll::Ready(true)`.
///
/// For terminating:
///
/// - If a `LendingAsyncIterator` is terminated
///   (`poll_next` returns `Poll::Ready(None)`),
///   the executor MUST NOT call `poll_next` again.
///
/// - There is no termination for a `Hook` until dropped.
///   When `poll_next_update` returns `Poll::Ready(false)`,
///   this means the hook is no longer dynamic
///   (its inner state will no longer update).
///   Thus, there is no need to call `use_hook` again because
///   the returned value is expected to remain the *same*.
///   But the executor can still call `use_hook` to re-get the value
///   and this might make the hook dynamic again.
///
///   This behavior makes it possible to combine multiple hooks.
///   When some hooks are no longer dynamic
///   but other hooks depend on their returned values,
///   the executor can still get the values
///   from the no-longer-dynamic hooks,
///   and pass the values to the dynamic hooks.
pub trait Hook<Args>: HookPollNextUpdate + for<'hook> HookLifetime<'hook, Args> {
    fn use_hook<'hook>(
        self: Pin<&'hook mut Self>,
        args: Args,
    ) -> <Self as HookLifetime<'hook, Args>>::Value
    where
        Self: 'hook;
}

impl<H: HookBounds + ?Sized> HookBounds for &mut H {
    type Bounds = H::Bounds;
}

impl<'hook, Args, H: HookLifetime<'hook, Args> + ?Sized> HookLifetime<'hook, Args> for &mut H {
    type Value = <H as HookLifetime<'hook, Args>>::Value;
}

impl<H: HookPollNextUpdate + Unpin + ?Sized> HookPollNextUpdate for &mut H {
    fn poll_next_update(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<bool> {
        H::poll_next_update(Pin::new(self.get_mut()), cx)
    }
}

impl<H: Hook<Args> + Unpin + ?Sized, Args> Hook<Args> for &mut H {
    fn use_hook<'hook>(
        self: Pin<&'hook mut Self>,
        args: Args,
    ) -> <Self as HookLifetime<'hook, Args>>::Value
    where
        Self: 'hook,
    {
        H::use_hook(Pin::new(self.get_mut()), args)
    }
}

/// `NonLendingHook` is a subset of [`Hook`].
/// `Value` of `NonLendingHook` is not generic,
/// thus not borrowing from the hook.
/// In other words, `NonLendingHook` doesn't lend to its `Value`.
///
/// [`Hook`] can be run by executor and become a `LendingAsyncIterator`,
/// `NonLendingHook` can be run by executor and become an `AsyncIterator`
/// (also known as [`Stream`](futures_lite::Stream)).
pub trait NonLendingHook<Args>:
    Hook<Args> + for<'hook> HookLifetime<'hook, Args, Value = Self::NonGenericValue>
{
    type NonGenericValue;
}

impl<H: ?Sized, Args, V> NonLendingHook<Args> for H
where
    H: Hook<Args> + for<'hook> HookLifetime<'hook, Args, Value = V>,
{
    type NonGenericValue = V;
}

impl<P> HookBounds for Pin<P>
where
    P: DerefMut,
    <P as Deref>::Target: HookBounds,
{
    type Bounds = <P::Target as HookBounds>::Bounds;
}

impl<'hook, P, Args> HookLifetime<'hook, Args> for Pin<P>
where
    P: DerefMut,
    <P as Deref>::Target: HookLifetime<'hook, Args>,
{
    type Value = <<P as Deref>::Target as HookLifetime<'hook, Args>>::Value;
}

impl<P> HookPollNextUpdate for Pin<P>
where
    P: DerefMut,
    <P as Deref>::Target: HookPollNextUpdate,
{
    #[inline]
    fn poll_next_update(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<bool> {
        <P::Target as HookPollNextUpdate>::poll_next_update(
            crate::utils::pin_as_deref_mut(self),
            cx,
        )
    }
}

impl<P, Args> Hook<Args> for Pin<P>
where
    P: DerefMut,
    <P as Deref>::Target: Hook<Args>,
{
    #[inline]
    fn use_hook<'hook>(
        self: Pin<&'hook mut Self>,
        args: Args,
    ) -> <Self as HookLifetime<'hook, Args>>::Value
    where
        Self: 'hook,
    {
        <P::Target as Hook<Args>>::use_hook(crate::utils::pin_as_deref_mut(self), args)
    }
}