future_form 0.3.1

Abstractions over Send and !Send futures
Documentation
use super::*;

trait SendableOnly<K: FutureForm> {
    fn send_only(&self) -> K::Future<'_, u32>;
}

struct SendOnlyType {
    val: u32,
}

#[future_form(Sendable)]
impl<K: FutureForm> SendableOnly<K> for SendOnlyType {
    fn send_only(&self) -> K::Future<'_, u32> {
        let val = self.val;
        K::from_future(async move { val * 2 })
    }
}

#[tokio::test]
async fn test_sendable_only() {
    let t = SendOnlyType { val: 21 };
    let result = <SendOnlyType as SendableOnly<Sendable>>::send_only(&t).await;
    assert_eq!(result, 42);
}

trait LocalOnly<K: FutureForm> {
    fn local_only(&self) -> K::Future<'_, u32>;
}

struct LocalOnlyType {
    val: u32,
}

#[future_form(Local)]
impl<K: FutureForm> LocalOnly<K> for LocalOnlyType {
    fn local_only(&self) -> K::Future<'_, u32> {
        let val = self.val;
        K::from_future(async move { val * 2 })
    }
}

#[tokio::test]
async fn test_local_only() {
    let t = LocalOnlyType { val: 21 };
    let result = <LocalOnlyType as LocalOnly<Local>>::local_only(&t).await;
    assert_eq!(result, 42);
}

trait ExplicitBoth<K: FutureForm> {
    fn explicit(&self) -> K::Future<'_, u32>;
}

struct ExplicitBothType {
    val: u32,
}

#[future_form(Sendable, Local)]
impl<K: FutureForm> ExplicitBoth<K> for ExplicitBothType {
    fn explicit(&self) -> K::Future<'_, u32> {
        let val = self.val;
        K::from_future(async move { val + 1 })
    }
}

#[tokio::test]
async fn test_explicit_both_sendable() {
    let t = ExplicitBothType { val: 41 };
    let result = <ExplicitBothType as ExplicitBoth<Sendable>>::explicit(&t).await;
    assert_eq!(result, 42);
}

#[tokio::test]
async fn test_explicit_both_local() {
    let t = ExplicitBothType { val: 41 };
    let result = <ExplicitBothType as ExplicitBoth<Local>>::explicit(&t).await;
    assert_eq!(result, 42);
}