beet_core 0.0.8

Core utilities and types for other beet crates
use std::future::Future;

/// A trait representing an async test future that returns a Result<(), String>
pub trait AsyncTest: 'static + Future<Output = Result<(), String>> {}
impl<F> AsyncTest for F where F: 'static + Future<Output = Result<(), String>> {}




pub trait IntoFut<M> {
	fn into_fut(self) -> impl AsyncTest;
}
pub struct ReturnsResult;
pub struct ReturnsUnit;
pub struct ReturnsNever;

impl<T> IntoFut<ReturnsResult> for T
where
	T: AsyncTest,
{
	fn into_fut(self) -> impl AsyncTest { self }
}
impl<T> IntoFut<ReturnsUnit> for T
where
	T: 'static + Future<Output = ()>,
{
	fn into_fut(self) -> impl AsyncTest {
		async move {
			self.await;
			Ok(())
		}
	}
}
impl<T> IntoFut<ReturnsNever> for T
where
	T: 'static + Future<Output = !>,
{
	fn into_fut(self) -> impl AsyncTest {
		async move {
			self.await;
		}
	}
}

/// Just block on async tests, useful as fallback
/// for when runner feature is disabled.
#[track_caller]
pub fn block_on_async_test<M>(fut: impl IntoFut<M>) {
	futures_lite::future::block_on(fut.into_fut()).unwrap();
}