···22//! with an executor/reactor intended for bcsc::Future is strictly unsound.
3344use std::{
55+ hint::unreachable_unchecked,
56 mem::ManuallyDrop,
67 pin::Pin,
78 ptr::NonNull,
···1112use futures_core::Wake;
1213use lifetime_guard::{atomic_guard::AtomicValueGuard, guard::ValueGuard};
13141414-static EVIL_VTABLE: RawWakerVTable = RawWakerVTable::new(
1515- |_| panic!("wtf"),
1616- |_| panic!("wtf"),
1717- |_| panic!("wtf"),
1818- |_| panic!("wtf"),
1919-);
2020-2115pub type WakePtr = Option<NonNull<dyn Wake>>;
1616+pub type LocalWaker = ValueGuard<WakePtr>;
1717+pub type AtomicWaker = AtomicValueGuard<WakePtr>;
1818+1919+static EVIL_VTABLE: RawWakerVTable = unsafe {
2020+ RawWakerVTable::new(
2121+ |_| unreachable_unchecked(),
2222+ |_| unreachable_unchecked(),
2323+ |_| unreachable_unchecked(),
2424+ |_| unreachable_unchecked(),
2525+ )
2626+};
22272328/// Coerces a pinned `ValueGuard` reference to a `Waker` for use in
2429/// `core::future::Future`
2530///
2631/// Any usage or storage of the resulting `Waker` is undefined behavior.
2727-pub unsafe fn guard_to_waker(
2828- guard: Pin<&ValueGuard<WakePtr>>,
2929-) -> ManuallyDrop<Waker> {
3232+pub unsafe fn guard_to_waker(guard: Pin<&LocalWaker>) -> ManuallyDrop<Waker> {
3033 ManuallyDrop::new(unsafe {
3134 Waker::from_raw(RawWaker::new(
3235 guard.get_ref() as *const ValueGuard<WakePtr> as *const (),
···3437 ))
3538 })
3639}
4040+3741pub unsafe fn atomic_guard_to_waker(
3838- guard: Pin<&AtomicValueGuard<WakePtr>>,
3939-) -> Waker {
4040- unsafe {
4242+ guard: Pin<&AtomicWaker>,
4343+) -> ManuallyDrop<Waker> {
4444+ ManuallyDrop::new(unsafe {
4145 Waker::from_raw(RawWaker::new(
4246 guard.get_ref() as *const AtomicValueGuard<WakePtr> as *const (),
4347 &EVIL_VTABLE,
4448 ))
4545- }
4949+ })
4650}
47514852/// Coerces a `Waker` into a pinned `AtomicValueGuard` reference.
4953///
5054/// This should only be used to undo the work of `guard_to_waker`.
5151-pub unsafe fn waker_to_guard<'a>(
5252- waker: &Waker,
5353-) -> Pin<&'a ValueGuard<WakePtr>> {
5555+pub unsafe fn waker_to_guard<'a>(waker: &Waker) -> Pin<&LocalWaker> {
5456 unsafe {
5557 Pin::new_unchecked(&*(waker.data() as *const ValueGuard<WakePtr>))
5658 }
5759}
5858-pub unsafe fn waker_to_atomic_guard<'a>(
5959- waker: &Waker,
6060-) -> Pin<&'a AtomicValueGuard<WakePtr>> {
6060+6161+pub unsafe fn waker_to_atomic_guard<'a>(waker: &Waker) -> Pin<&AtomicWaker> {
6162 unsafe {
6263 Pin::new_unchecked(&*(waker.data() as *const AtomicValueGuard<WakePtr>))
6364 }
6465}
65666666-// TODO should probably return impl futures_core::Future, same for next fn
6767pub unsafe fn std_future_to_bespoke<F: core::future::Future>(
6868 future: F,
6969-) -> NormalFutureWrapper<F> {
6969+) -> impl futures_core::Future<LocalWaker, Output = F::Output> {
7070 NormalFutureWrapper(future)
7171}
72727373-pub unsafe fn bespoke_future_to_std<F: futures_core::Future>(
7373+pub unsafe fn bespoke_future_to_std<F: futures_core::Future<LocalWaker>>(
7474 future: F,
7575-) -> BespokeFutureWrapper<F> {
7575+) -> impl core::future::Future<Output = F::Output> {
7676 BespokeFutureWrapper(future)
7777}
7878···8080#[repr(transparent)]
8181pub struct NormalFutureWrapper<F: core::future::Future>(F);
82828383-impl<F: core::future::Future> futures_core::Future for NormalFutureWrapper<F> {
8383+impl<F: core::future::Future> futures_core::Future<LocalWaker>
8484+ for NormalFutureWrapper<F>
8585+{
8486 type Output = F::Output;
85878686- fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
8787- unsafe { self.map_unchecked_mut(|this| &mut this.0).poll(cx) }
8888+ fn poll(
8989+ self: Pin<&mut Self>,
9090+ waker: Pin<&LocalWaker>,
9191+ ) -> Poll<Self::Output> {
9292+ unsafe {
9393+ self.map_unchecked_mut(|this| &mut this.0)
9494+ .poll(&mut Context::from_waker(&guard_to_waker(waker)))
9595+ }
8896 }
8997}
90989199/// wraps custom `bcsc::Future` in impl of `core::future::Future`
92100#[repr(transparent)]
9393-pub struct BespokeFutureWrapper<F: futures_core::Future>(F);
101101+pub struct BespokeFutureWrapper<F>(F)
102102+where
103103+ F: futures_core::Future<LocalWaker>;
941049595-impl<F: futures_core::Future> core::future::Future for BespokeFutureWrapper<F> {
105105+impl<F> core::future::Future for BespokeFutureWrapper<F>
106106+where
107107+ F: futures_core::Future<LocalWaker>,
108108+{
96109 type Output = F::Output;
9711098111 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
9999- unsafe { self.map_unchecked_mut(|this| &mut this.0).poll(cx) }
112112+ unsafe {
113113+ self.map_unchecked_mut(|this| &mut this.0)
114114+ .poll(waker_to_guard(cx.waker()))
115115+ }
100116 }
101117}
118118+119119+// impl<F> core::future::Future for F
120120+// where
121121+// F: for<'a> futures_core::Future<Context<'a>>,
122122+// {
123123+// type Output = F::Output;
124124+125125+// fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
126126+// self.poll(cx.waker())
127127+// }
128128+// }
102129103130#[cfg(test)]
104131mod test {
+43-12
futures-core/src/lib.rs
···11//! Redefinitions of task::Future to be incompatible with them
2233use std::{
44- ops,
44+ ops::{self, DerefMut},
55 pin::Pin,
66- task::{Context, Poll},
66+ task::Poll,
77};
8899/// A future represents an asynchronous computation obtained by use of `async`.
···3535 message = "`{Self}` is not a `bcsc::Future`",
3636 note = "If you are trying to await a `core::future::Future` from within a `bcsc::Future`, note that the systems are incompatible."
3737)]
3838-pub trait Future {
3838+pub trait Future<Waker> {
3939 /// The type of value produced on completion.
4040 type Output;
4141···9999 /// [`Poll::Ready(val)`]: Poll::Ready
100100 /// [`Waker`]: crate::task::Waker
101101 /// [`Waker::wake`]: crate::task::Waker::wake
102102- fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
102102+ fn poll(self: Pin<&mut Self>, waker: Pin<&Waker>) -> Poll<Self::Output>;
103103}
104104105105-impl<F: ?Sized + Future + Unpin> Future for &mut F {
105105+impl<Waker, F: ?Sized + Future<Waker> + Unpin> Future<Waker> for &mut F {
106106 type Output = F::Output;
107107108108 fn poll(
109109 mut self: Pin<&mut Self>,
110110- cx: &mut Context<'_>,
110110+ waker: Pin<&Waker>,
111111 ) -> Poll<Self::Output> {
112112- F::poll(Pin::new(&mut **self), cx)
112112+ F::poll(Pin::new(&mut **self), waker)
113113 }
114114}
115115116116-impl<P> Future for Pin<P>
116116+impl<Waker, P> Future<Waker> for Pin<P>
117117where
118118- P: ops::DerefMut<Target: Future>,
118118+ P: ops::DerefMut<Target: Future<Waker>>,
119119+{
120120+ type Output = <<P as ops::Deref>::Target as Future<Waker>>::Output;
121121+122122+ fn poll(self: Pin<&mut Self>, waker: Pin<&Waker>) -> Poll<Self::Output> {
123123+ <P::Target as Future<Waker>>::poll(self.as_deref_mut(), waker)
124124+ }
125125+}
126126+127127+/// A future which tracks whether or not the underlying future
128128+/// should no longer be polled.
129129+///
130130+/// `is_terminated` will return `true` if a future should no longer be polled.
131131+/// Usually, this state occurs after `poll` (or `try_poll`) returned
132132+/// `Poll::Ready`. However, `is_terminated` may also return `true` if a future
133133+/// has become inactive and can no longer make progress and should be ignored
134134+/// or dropped rather than being `poll`ed again.
135135+pub trait FusedFuture<Waker>: Future<Waker> {
136136+ /// Returns `true` if the underlying future should no longer be polled.
137137+ fn is_terminated(&self) -> bool;
138138+}
139139+140140+impl<Waker, F: FusedFuture<Waker> + ?Sized + Unpin> FusedFuture<Waker>
141141+ for &mut F
119142{
120120- type Output = <<P as ops::Deref>::Target as Future>::Output;
143143+ fn is_terminated(&self) -> bool {
144144+ <F as FusedFuture<Waker>>::is_terminated(&**self)
145145+ }
146146+}
121147122122- fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
123123- <P::Target as Future>::poll(self.as_deref_mut(), cx)
148148+impl<Waker, P> FusedFuture<Waker> for Pin<P>
149149+where
150150+ P: DerefMut + Unpin,
151151+ P::Target: FusedFuture<Waker>,
152152+{
153153+ fn is_terminated(&self) -> bool {
154154+ <P::Target as FusedFuture<Waker>>::is_terminated(&**self)
124155 }
125156}
126157