dtolnay / async-trait

Type erasure for async trait methods
Apache License 2.0
1.81k stars 84 forks source link

Add `+Sync` argument to mark the future as `Sync` #216

Closed rcelha closed 1 year ago

rcelha commented 1 year ago

Hey,

I ran into the issue where some of my futures must be Send + Sync, browsing the issue tracker I've found https://github.com/dtolnay/async-trait/issues/77 too.

Is this something you would be willing to accept contribution to? If that is the case, I'd love to discuss the best approach.

This PR is a draft/proposal on how we can solve this issue. I don´t particularly like my implementation tho, but once we agree on an API I can polish the implementation.


The API I am using here is as follows:

#[async_trait(+Sync)]
pub trait Trait {
    async fn f(&self);
}

struct A;

#[async_trait]
impl Trait for A {
    async fn f(&self) {}
}

Thanks

Qqwy commented 1 year ago

This PR/Fork saved me big time just now!

I wanted to iterate over a tree structure whose nodes are fetched asynchroniously from persistent storage. Needless to say, this results in quite the combination of nesting of Box, Future and trait objects of an iterator/stream-like trait.

Essentially, whenever you'd want to write something close to std::async_iterator::AsyncIterator or futures_core::stream::Stream you end up in a situation in which you are building a trait that:

rcelha commented 1 year ago

I am glad it helped someone else as well @Qqwy !