Skip to content

Commit 106990f

Browse files
authored
Support async-generic feature flags in cgp-async (#37)
* Add cargo feature flags for cgp-async * Propagate default features for cgp-async * Add full feature to cgp and cgp-core * Fix cgp-error-eyre when default-features = false * Add changelog
1 parent fdeaa90 commit 106990f

File tree

25 files changed

+137
-51
lines changed

25 files changed

+137
-51
lines changed

.github/workflows/tests.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ jobs:
4646
name: clippy-all-features
4747
token: ${{ secrets.GITHUB_TOKEN }}
4848
args: --all-features --all-targets -- -D warnings
49+
- uses: actions-rs/clippy-check@v1
50+
with:
51+
name: clippy-no-default-features
52+
token: ${{ secrets.GITHUB_TOKEN }}
53+
args: --no-default-features --all-targets -- -D warnings
4954

5055
test:
5156
runs-on: ubuntu-latest

CHANGELOG.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,26 @@
22

33
## Pre-Release
44

5-
- Introduce new cgp-field constructs [#36](https://github.com/contextgeneric/cgp/pull/36)
5+
- Support async-generic feature flags in cgp-async - [#37](https://github.com/contextgeneric/cgp/pull/37)
6+
- Introduce the following feature flags to `cgp-async`:
7+
- `async`
8+
- `send`
9+
- `sync`
10+
- `static`
11+
- `full` - default feature with all enabled
12+
- Introduce the following traits in `cgp-async`:
13+
- `MaybeSend` - alias to `Send` when the `send` feature is enabled, otherwise nothing.
14+
- `MaybeSync` - alias to `Sync` when the `sync` feature is enabled, otherwise nothing.
15+
- `MaybeStatic` - alias to `'static` when the `static` feature is enabled, otherwise nothing.
16+
- Update the `Async` trait from `Sized + Send + Sync + 'static` to `MaybeSend + MaybeSync + MaybeStatic`.
17+
- The `Sized` constraint is removed from `Async` to allow use inside `dyn` traits.
18+
- Update the `#[async_trait]` macro to desugar async functions to return `impl Future<Output: MaybeSend>`.
19+
- Use of `#[async_trait]` now requires import of `cgp::prelude::*` to allow `MaybeSend` to be auto imported.
20+
- `cgp-async` now re-exports `cgp_sync::strip_async` if the `async` feature is not enabled.
21+
- i.e. async functions are desugared into sync functions if the `async` feature is disabled.
22+
- Crates such as `cgp` and `cgp-core` offers the `full` feature, which can be disabled to disable the indirect default features in `cgp-async`.
23+
24+
- Introduce new cgp-field constructs - [#36](https://github.com/contextgeneric/cgp/pull/36)
625
- Introduce the product type constructs `Cons` and `Nil`.
726
- Introduce the sum type constructs `Either` and `Void`.
827
- Introduce the `Field` type for tagged field value.

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cgp-async-macro/src/impl_async.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn impl_async(item: TokenStream) -> TokenStream {
1515
};
1616

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

2121
trait_fn.sig.output = impl_return;

crates/cgp-async/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,19 @@ description = """
1111
Async-generic primitives to support both sync/async in context-generic programming
1212
"""
1313

14+
[features]
15+
default = [ "full" ]
16+
full = [
17+
"async",
18+
"send",
19+
"sync",
20+
"static",
21+
]
22+
async = []
23+
send = [ "async" ]
24+
sync = [ "async" ]
25+
static = [ "async" ]
26+
1427
[dependencies]
1528
cgp-async-macro = { version = "0.1.0" }
29+
cgp-sync = { version = "0.1.0" }

crates/cgp-async/src/lib.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,9 @@
1-
pub use cgp_async_macro::native_async as async_trait;
2-
3-
/**
4-
This is defined as a convenient constraint alias to
5-
`Sized + Send + Sync + 'static`.
1+
pub mod traits;
62

7-
This constraint is commonly required to be present in almost all associated
8-
types. The `Sized` constraint is commonly required for associated types to be
9-
used as generic parameters. The `Send + Sync + 'static` constraints are
10-
important for the use of async functions inside traits.
3+
pub use traits::{Async, MaybeSend, MaybeStatic, MaybeSync};
114

12-
Because Rust do not currently natively support the use of async functions
13-
in traits, we use the [`async_trait`] crate to desugar async functions
14-
inside traits into functions returning
15-
`Pin<Box<dyn Future + Send>>`. Due to the additional `Send` and lifetime
16-
trait bounds inside the returned boxed future, almost all values that are
17-
used inside the async functions are required to have types that implement
18-
`Send` and `Sync`.
19-
20-
It is also common to require the associated types to have the `'static`
21-
lifetime for them to be used inside async functions, because Rust would
22-
otherwise infer a more restrictive lifetime that does not outlive the
23-
async functions. The `'static` lifetime constraint here really means
24-
that the types implementing `Async` must not contain any lifetime
25-
parameter.
26-
*/
27-
pub trait Async: Send + Sync + 'static {}
5+
#[cfg(feature = "async")]
6+
pub use cgp_async_macro::native_async as async_trait;
287

29-
impl<A> Async for A where A: Send + Sync + 'static {}
8+
#[cfg(not(feature = "async"))]
9+
pub use cgp_sync::async_trait;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::traits::r#static::MaybeStatic;
2+
use crate::traits::send::MaybeSend;
3+
use crate::traits::sync::MaybeSync;
4+
5+
/**
6+
This is defined as a convenient constraint alias to
7+
`Sized + Send + Sync + 'static`.
8+
9+
This constraint is commonly required to be present in almost all associated
10+
types. The `Sized` constraint is commonly required for associated types to be
11+
used as generic parameters. The `Send + Sync + 'static` constraints are
12+
important for the use of async functions inside traits.
13+
14+
Because Rust do not currently natively support the use of async functions
15+
in traits, we use the [`async_trait`] crate to desugar async functions
16+
inside traits into functions returning
17+
`Pin<Box<dyn Future + Send>>`. Due to the additional `Send` and lifetime
18+
trait bounds inside the returned boxed future, almost all values that are
19+
used inside the async functions are required to have types that implement
20+
`Send` and `Sync`.
21+
22+
It is also common to require the associated types to have the `'static`
23+
lifetime for them to be used inside async functions, because Rust would
24+
otherwise infer a more restrictive lifetime that does not outlive the
25+
async functions. The `'static` lifetime constraint here really means
26+
that the types implementing `Async` must not contain any lifetime
27+
parameter.
28+
*/
29+
pub trait Async: MaybeSend + MaybeSync + MaybeStatic {}
30+
31+
impl<A> Async for A where A: MaybeSend + MaybeSync + MaybeStatic {}

crates/cgp-async/src/traits/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub mod r#async;
2+
pub mod send;
3+
pub mod r#static;
4+
pub mod sync;
5+
6+
pub use r#async::Async;
7+
pub use r#static::MaybeStatic;
8+
pub use send::MaybeSend;
9+
pub use sync::MaybeSync;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[cfg(feature = "send")]
2+
pub use core::marker::Send as MaybeSend;
3+
4+
#[cfg(not(feature = "send"))]
5+
pub trait MaybeSend {}
6+
7+
#[cfg(not(feature = "send"))]
8+
impl<T> MaybeSend for T {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[cfg(feature = "static")]
2+
pub trait MaybeStatic: 'static {}
3+
4+
#[cfg(feature = "static")]
5+
impl<T: 'static> MaybeStatic for T {}
6+
7+
#[cfg(not(feature = "static"))]
8+
pub trait MaybeStatic {}
9+
10+
#[cfg(not(feature = "static"))]
11+
impl<T> MaybeStatic for T {}

0 commit comments

Comments
 (0)