pub fn coroutine_state<Fut: Future>(fut: &Fut) -> CoroutineStateExpand description
Get the current coroutine state of a Future.
use coroutine_state::{coroutine_state, CoroutineState};
async fn f() {}
let fut = f();
assert_eq!(coroutine_state(&fut), CoroutineState::Unresumed);ยงPanics
You should only pass in a reference to a Future returned by an async
function. If you pass in a reference to another type of Future,
this function may either panic or return an arbitrary value.
However the implemenation uses no unsafe code so this can never cause
undefined behavior.
You should be careful not to pass a & Pin<&mut Fut>
instead of a reference to the inner Fut:
use std::pin::pin;
use coroutine_state::{coroutine_state, CoroutineState};
async fn f() {}
let fut = f();
let fut = pin!(fut);
// This might panic or return an arbitrary state:
// println!("{:?}", coroutine_state(&fut));
// Instead use:
println!("{:?}", coroutine_state(&*fut));Example of where an arbitrary value is returned:
use std::future::Future;
use std::pin::{pin, Pin};
use std::task::{Context, Poll};
use coroutine_state::{coroutine_state, CoroutineState};
#[repr(u32)]
enum Foo {
Foo,
}
impl Future for Foo {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(())
}
}
let fut = Foo::Foo;
println!("{:?}", coroutine_state(&fut));