use super::*;
#[tokio::test]
async fn test_from_future_sendable() {
let future: BoxFuture<'_, u32> = FromFuture::from_future(async { 42 });
assert_eq!(future.await, 42);
}
#[tokio::test]
async fn test_from_future_local() {
let future: LocalBoxFuture<'_, u32> = FromFuture::from_future(async { 42 });
assert_eq!(future.await, 42);
}
#[tokio::test]
async fn test_sendable_from_future() {
let future = Sendable::from_future(async { 42 });
assert_eq!(future.await, 42);
}
#[tokio::test]
async fn test_local_from_future() {
let future = Local::from_future(async { 42 });
assert_eq!(future.await, 42);
}
trait Calculator<K: FutureForm> {
fn add(&self, a: u32, b: u32) -> K::Future<'_, u32>;
}
struct SimpleCalculator;
impl Calculator<Sendable> for SimpleCalculator {
fn add(&self, a: u32, b: u32) -> BoxFuture<'_, u32> {
Sendable::from_future(async move { a + b })
}
}
impl Calculator<Local> for SimpleCalculator {
fn add(&self, a: u32, b: u32) -> LocalBoxFuture<'_, u32> {
Local::from_future(async move { a + b })
}
}
#[tokio::test]
async fn test_from_future_in_trait_sendable() {
let calc = SimpleCalculator;
let result = <SimpleCalculator as Calculator<Sendable>>::add(&calc, 20, 22).await;
assert_eq!(result, 42);
}
#[tokio::test]
async fn test_from_future_in_trait_local() {
let calc = SimpleCalculator;
let result = <SimpleCalculator as Calculator<Local>>::add(&calc, 20, 22).await;
assert_eq!(result, 42);
}
#[tokio::test]
async fn test_sendable_from_future_with_send_data() {
let data = String::from("hello");
let future = Sendable::from_future(async move { if data.is_empty() { 0u32 } else { 42u32 } });
assert_eq!(future.await, 42);
}
#[tokio::test]
async fn test_local_from_future_with_non_send_data() {
use std::rc::Rc;
let data = Rc::new(42u32);
let future = Local::from_future(async move { *data });
assert_eq!(future.await, 42);
}