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

use futures_lite::FutureExt;

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

pub(crate) struct SmolBackend;

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

    #[allow(clippy::manual_async_fn)]
    fn sleep(dur: Duration) -> impl Future<Output = ()> + Send {
        async move {
            let _ = smol::Timer::after(dur).await;
        }
    }
}
