wip
1//! Redefinitions of task::Future to be incompatible with them
2
3use std::{
4 ops::{self, DerefMut},
5 pin::Pin,
6 task::Poll,
7};
8
9/// A future represents an asynchronous computation obtained by use of `async`.
10///
11/// This future assumes a nonstandard Context, which is incompatible with
12/// executors or reactors made for `core::future::Future`. In the interest of
13/// safety, it has a dedicated type.
14///
15/// A future is a value that might not have finished computing yet. This kind of
16/// "asynchronous value" makes it possible for a thread to continue doing useful
17/// work while it waits for the value to become available.
18///
19/// # The `poll` method
20///
21/// The core method of future, `poll`, *attempts* to resolve the future into a
22/// final value. This method does not block if the value is not ready. Instead,
23/// the current task is scheduled to be woken up when it's possible to make
24/// further progress by `poll`ing again. The `context` passed to the `poll`
25/// method can provide a [`Waker`], which is a handle for waking up the current
26/// task.
27///
28/// When using a future, you generally won't call `poll` directly, but instead
29/// `.await` the value.
30///
31/// [`Waker`]: crate::task::Waker
32#[must_use = "futures do nothing unless you `.await` or poll them"]
33#[diagnostic::on_unimplemented(
34 label = "`{Self}` is not a `bcsc::Future`",
35 message = "`{Self}` is not a `bcsc::Future`",
36 note = "If you are trying to await a `core::future::Future` from within a `bcsc::Future`, note that the systems are incompatible."
37)]
38pub trait Future<Waker> {
39 /// The type of value produced on completion.
40 type Output;
41
42 /// Attempts to resolve the future to a final value, registering
43 /// the current task for wakeup if the value is not yet available.
44 ///
45 /// # Return value
46 ///
47 /// This function returns:
48 ///
49 /// - [`Poll::Pending`] if the future is not ready yet
50 /// - [`Poll::Ready(val)`] with the result `val` of this future if it
51 /// finished successfully.
52 ///
53 /// Once a future has finished, clients should not `poll` it again.
54 ///
55 /// When a future is not ready yet, `poll` returns `Poll::Pending` and
56 /// stores a clone of the [`Waker`] copied from the current [`Context`].
57 /// This [`Waker`] is then woken once the future can make progress.
58 /// For example, a future waiting for a socket to become
59 /// readable would call `.clone()` on the [`Waker`] and store it.
60 /// When a signal arrives elsewhere indicating that the socket is readable,
61 /// [`Waker::wake`] is called and the socket future's task is awoken.
62 /// Once a task has been woken up, it should attempt to `poll` the future
63 /// again, which may or may not produce a final value.
64 ///
65 /// Note that on multiple calls to `poll`, only the [`Waker`] from the
66 /// [`Context`] passed to the most recent call should be scheduled to
67 /// receive a wakeup.
68 ///
69 /// # Runtime characteristics
70 ///
71 /// Futures alone are *inert*; they must be *actively* `poll`ed to make
72 /// progress, meaning that each time the current task is woken up, it should
73 /// actively re-`poll` pending futures that it still has an interest in.
74 ///
75 /// The `poll` function is not called repeatedly in a tight loop -- instead,
76 /// it should only be called when the future indicates that it is ready to
77 /// make progress (by calling `wake()`). If you're familiar with the
78 /// `poll(2)` or `select(2)` syscalls on Unix it's worth noting that futures
79 /// typically do *not* suffer the same problems of "all wakeups must poll
80 /// all events"; they are more like `epoll(4)`.
81 ///
82 /// An implementation of `poll` should strive to return quickly, and should
83 /// not block. Returning quickly prevents unnecessarily clogging up
84 /// threads or event loops. If it is known ahead of time that a call to
85 /// `poll` may end up taking a while, the work should be offloaded to a
86 /// thread pool (or something similar) to ensure that `poll` can return
87 /// quickly.
88 ///
89 /// # Panics
90 ///
91 /// Once a future has completed (returned `Ready` from `poll`), calling its
92 /// `poll` method again may panic, block forever, or cause other kinds of
93 /// problems; the `Future` trait places no requirements on the effects of
94 /// such a call. However, as the `poll` method is not marked `unsafe`,
95 /// Rust's usual rules apply: calls must never cause undefined behavior
96 /// (memory corruption, incorrect use of `unsafe` functions, or the like),
97 /// regardless of the future's state.
98 ///
99 /// [`Poll::Ready(val)`]: Poll::Ready
100 /// [`Waker`]: crate::task::Waker
101 /// [`Waker::wake`]: crate::task::Waker::wake
102 fn poll(self: Pin<&mut Self>, waker: Pin<&Waker>) -> Poll<Self::Output>;
103}
104
105impl<Waker, F: ?Sized + Future<Waker> + Unpin> Future<Waker> for &mut F {
106 type Output = F::Output;
107
108 fn poll(
109 mut self: Pin<&mut Self>,
110 waker: Pin<&Waker>,
111 ) -> Poll<Self::Output> {
112 F::poll(Pin::new(&mut **self), waker)
113 }
114}
115
116impl<Waker, P> Future<Waker> for Pin<P>
117where
118 P: ops::DerefMut<Target: Future<Waker>>,
119{
120 type Output = <<P as ops::Deref>::Target as Future<Waker>>::Output;
121
122 fn poll(self: Pin<&mut Self>, waker: Pin<&Waker>) -> Poll<Self::Output> {
123 <P::Target as Future<Waker>>::poll(self.as_deref_mut(), waker)
124 }
125}
126
127/// A future which tracks whether or not the underlying future
128/// should no longer be polled.
129///
130/// `is_terminated` will return `true` if a future should no longer be polled.
131/// Usually, this state occurs after `poll` (or `try_poll`) returned
132/// `Poll::Ready`. However, `is_terminated` may also return `true` if a future
133/// has become inactive and can no longer make progress and should be ignored
134/// or dropped rather than being `poll`ed again.
135pub trait FusedFuture<Waker>: Future<Waker> {
136 /// Returns `true` if the underlying future should no longer be polled.
137 fn is_terminated(&self) -> bool;
138}
139
140impl<Waker, F: FusedFuture<Waker> + ?Sized + Unpin> FusedFuture<Waker>
141 for &mut F
142{
143 fn is_terminated(&self) -> bool {
144 <F as FusedFuture<Waker>>::is_terminated(&**self)
145 }
146}
147
148impl<Waker, P> FusedFuture<Waker> for Pin<P>
149where
150 P: DerefMut + Unpin,
151 P::Target: FusedFuture<Waker>,
152{
153 fn is_terminated(&self) -> bool {
154 <P::Target as FusedFuture<Waker>>::is_terminated(&**self)
155 }
156}
157
158/// temporary trait until Fn::call is stabilized
159pub trait Wake {
160 fn wake(&self);
161}