use std::future::Future;
use std::panic::AssertUnwindSafe;
use std::time::Duration;

use futures_lite::FutureExt;

use super::Backend;
use crate::JoinHandle;

/// Zero-sized tokio backend.
pub(crate) struct TokioBackend;

impl Backend for TokioBackend {
    fn spawn<T: Send + 'static>(fut: impl Future<Output = T> + Send + 'static) -> JoinHandle<T> {
        let handle = tokio::task::spawn(AssertUnwindSafe(fut).catch_unwind());
        JoinHandle { inner: handle }
    }

    fn sleep(dur: Duration) -> impl Future<Output = ()> + Send {
        tokio::time::sleep(dur)
    }
}
