trillium_server_common/runtime/droppable_future.rs
1use pin_project_lite::pin_project;
2use std::{
3 future::Future,
4 pin::Pin,
5 task::{Context, Poll},
6};
7
8pin_project! {
9 /// A wrapper type for futures that do not need to be polled but still can be awaited.
10 ///
11 /// This exists to silence the default `#[must_use]` that anonymous async functions return
12 ///
13 /// Futures contained by this type must conform to the semantics of trillium join handles described
14 /// at [RuntimeTrait::spawn].
15 #[derive(Debug, Clone)]
16 pub struct DroppableFuture<T> {
17 #[pin] future: T
18 }
19}
20impl<T: Future> DroppableFuture<T> {
21 /// Removes the `#[must_use]` for a future.
22 ///
23 /// This must only be called with a join-handle type future that does not depend on polling to
24 /// execute.
25 pub fn new(future: T) -> Self {
26 Self { future }
27 }
28
29 /// Returns the inner future.
30 pub fn into_inner(self) -> T {
31 self.future
32 }
33}
34impl<F: Future> Future for DroppableFuture<F> {
35 type Output = F::Output;
36
37 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
38 self.project().future.poll(cx)
39 }
40}