hooks_core/ext/
hook_ext.rs

1use std::{pin::Pin, task::Poll};
2
3use crate::{Hook, HookPollNextUpdate, HookValue};
4
5use super::NextUpdate;
6
7/// Extend [`HookPollNextUpdate`] with convenient methods.
8pub trait HookPollNextUpdateExt: HookPollNextUpdate {
9    /// A shortcut to call [`HookPollNextUpdate::poll_next_update`] on Unpin hooks.
10    #[inline]
11    fn poll_next_update(&mut self, cx: &mut std::task::Context<'_>) -> Poll<bool>
12    where
13        Self: Unpin,
14    {
15        HookPollNextUpdate::poll_next_update(Pin::new(self), cx)
16    }
17
18    /// Get a future which polls [`HookPollNextUpdate::poll_next_update`].
19    #[inline]
20    fn next_update(&mut self) -> NextUpdate<'_, Self>
21    where
22        Self: Unpin,
23    {
24        NextUpdate::new(self)
25    }
26}
27
28impl<H: HookPollNextUpdate + ?Sized> HookPollNextUpdateExt for H {}
29
30/// Extend [`Hook`](trait@Hook) with convenient methods.
31pub trait HookExt: Hook {
32    /// A shortcut to call [`Hook::use_hook`] on Unpin hooks.
33    #[inline(always)]
34    fn use_hook(&mut self) -> <Self as HookValue<'_>>::Value
35    where
36        Self: Unpin,
37    {
38        <Self as Hook>::use_hook(Pin::new(self))
39    }
40
41    #[inline]
42    fn next_value(&mut self) -> super::NextValue<'_, Self>
43    where
44        Self: Unpin,
45    {
46        crate::NextValue::new(Pin::new(self))
47    }
48
49    #[inline]
50    fn into_values(self) -> super::Values<Self>
51    where
52        Self: Sized,
53    {
54        super::Values::new(self)
55    }
56
57    #[inline]
58    fn values(&mut self) -> super::Values<&mut Self>
59    where
60        Self: Unpin,
61    {
62        super::Values::new(self)
63    }
64}
65
66impl<H: Hook + ?Sized> HookExt for H {}