Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 25 additions & 45 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ members = [
"crates/cgp",
"crates/cgp-core",
"crates/cgp-extra",
"crates/cgp-async",
"crates/cgp-async-macro",
"crates/cgp-sync",
"crates/cgp-component",
"crates/cgp-macro",
"crates/cgp-macro-lib",
Expand Down Expand Up @@ -42,9 +40,7 @@ keywords = ["cgp"]
cgp = { path = "./crates/cgp" }
cgp-core = { path = "./crates/cgp-core" }
cgp-extra = { path = "./crates/cgp-extra" }
cgp-async = { path = "./crates/cgp-async" }
cgp-async-macro = { path = "./crates/cgp-async-macro" }
cgp-sync = { path = "./crates/cgp-sync" }
cgp-component = { path = "./crates/cgp-component" }
cgp-macro = { path = "./crates/cgp-macro" }
cgp-macro-lib = { path = "./crates/cgp-macro-lib" }
Expand Down
2 changes: 1 addition & 1 deletion crates/cgp-async-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = { workspace = true }
repository = { workspace = true }
authors = { workspace = true }
rust-version = { workspace = true }
version = "0.4.2"
version = "0.5.0-alpha"
keywords = { workspace = true }
description = """
Context-generic programming async macros
Expand Down
2 changes: 1 addition & 1 deletion crates/cgp-async-macro/src/impl_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn impl_async(item: TokenStream) -> TokenStream {
};

let impl_return: ReturnType = parse_quote! {
-> impl ::core::future::Future<Output = #return_type> + MaybeSend
-> impl ::core::future::Future<Output = #return_type>
};

trait_fn.sig.output = impl_return;
Expand Down
30 changes: 3 additions & 27 deletions crates/cgp-async-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,16 @@ extern crate proc_macro;
use proc_macro::TokenStream;

mod impl_async;
mod strip_async;

/**
This macro can be used in place of the [`macro@native_async`] macro
to strip away all use of `async` and `.await` syntax. This helps emulate
async-generic by turnining async functions into sync functions.
*/
#[proc_macro_attribute]
pub fn strip_async(_attr: TokenStream, stream: TokenStream) -> TokenStream {
strip_async::strip_async(stream.into()).into()
}

/**
The `#[async_trait]` macro is used to desugar async functions in traits
to return `impl Future + Send`.
to return `impl Future`.

This macro is required mainly to get around the current limitation of
[async functions in traits](https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits/),
which would produce a lint warning for `async_fn_in_trait` if bare async
functions are defined in a trait.

The default approach adopted by CGP is to always include a `Send` bound
on the desugared `Future`, so that any async function that indirectly calls
the method can still implement `Send` be be used with libraries such as `tokio::spawn`.

It is possible to remove default `Send` by deactivating the `send` feature in the `cgp-async`
crate. However, the removal may be unreliable, as the feature flag could still be accidentally
enabled by one of the dependencies.

Note that although the macro shares the same name as the
[`async-trait`](https://docs.rs/async-trait) crate, it is implemented very differently.
The other crate with the same name returns a `Pin<Box<dyn Future + Send>>`, while
this macro returns a `impl Future + Send`.

## Example

Given the following trait definition:
Expand All @@ -59,11 +35,11 @@ pub fn strip_async(_attr: TokenStream, stream: TokenStream) -> TokenStream {

```rust,ignore
pub trait CanRun {
fn run(&self) -> impl Future<Output = ()> + Send;
fn run(&self) -> impl Future<Output = ()>;
}
```
*/
#[proc_macro_attribute]
pub fn native_async(_attr: TokenStream, stream: TokenStream) -> TokenStream {
pub fn async_trait(_attr: TokenStream, stream: TokenStream) -> TokenStream {
impl_async::impl_async(stream.into()).into()
}
52 changes: 0 additions & 52 deletions crates/cgp-async-macro/src/strip_async.rs

This file was deleted.

29 changes: 0 additions & 29 deletions crates/cgp-async/Cargo.toml

This file was deleted.

Loading
Loading