ratatui_kit/hooks/
use_future.rs1use std::task::Poll;
2
3use futures::future::BoxFuture;
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 = ()> + Send + 'static;
18}
19
20pub struct UseFutureImpl {
21 f: Option<BoxFuture<'static, ()>>,
22}
23
24impl UseFutureImpl {
25 pub fn new<F>(f: F) -> Self
26 where
27 F: Future<Output = ()> + Send + 'static,
28 {
29 UseFutureImpl {
30 f: Some(Box::pin(f)),
31 }
32 }
33}
34
35impl Hook for UseFutureImpl {
36 fn poll_change(
37 mut self: std::pin::Pin<&mut Self>,
38 cx: &mut std::task::Context,
39 ) -> std::task::Poll<()> {
40 if let Some(future) = self.f.as_mut()
41 && future.as_mut().poll(cx).is_ready() {
42 self.f = None; }
44 Poll::Pending
45 }
46}
47
48impl UseFuture for Hooks<'_, '_> {
49 fn use_future<F>(&mut self, f: F)
50 where
51 F: Future<Output = ()> + Send + 'static,
52 {
53 self.use_hook(move || UseFutureImpl::new(f));
54 }
55}