ratatui_kit/hooks/
use_future.rs1use std::task::Poll;
2
3use futures::{FutureExt, future::LocalBoxFuture};
4
5use super::{Hook, Hooks};
6
7mod private {
8 pub trait Sealed {}
9
10 impl Sealed for crate::hooks::Hooks<'_, '_> {}
11}
12
13pub trait UseFuture: private::Sealed {
14 fn use_future<F>(&mut self, f: F)
16 where
17 F: Future<Output = ()> + 'static;
18}
19
20pub struct UseFutureImpl {
21 f: Option<LocalBoxFuture<'static, ()>>,
22}
23
24impl UseFutureImpl {
25 pub fn new<F>(f: F) -> Self
26 where
27 F: Future<Output = ()> + 'static,
28 {
29 UseFutureImpl {
30 f: Some(f.boxed_local()),
31 }
32 }
33}
34
35impl Hook for UseFutureImpl {
36 fn poll_change(&mut self, cx: &mut std::task::Context) -> std::task::Poll<()> {
37 if let Some(future) = self.f.as_mut()
38 && future.as_mut().poll(cx).is_ready()
39 {
40 self.f = None; return Poll::Ready(());
42 }
43 Poll::Pending
44 }
45}
46
47impl UseFuture for Hooks<'_, '_> {
48 fn use_future<F>(&mut self, f: F)
49 where
50 F: Future<Output = ()> + 'static,
51 {
52 self.use_hook(move || UseFutureImpl::new(f));
53 }
54}