Skip to content

Remove 'static bound from Async trait alias #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025
Merged

Conversation

soareschen
Copy link
Collaborator

@soareschen soareschen commented May 1, 2025

Summary

This PR removes the 'static bound from the Async trait, which was originally an alias to Send + Sync + 'static.

The Async trait is important for CGP code to more easily define abstract types that can be used more easily within async functions. Currently, it is necessary for async functions in traits to include at least the Send bound in the returned Future, in order for it to be usable in async executors such as tokio::spawn. This implies that almost all abstract types need to have at least the Send + Sync trait bounds to be included.

Although CGP makes it possible to add these bounds at the implementation site, it would quickly become overly tedious if we need to do this for almost every abstract type. As a result, CGP encourages abstract types to be define with Send + Sync bounds for them to be usable inside async code. Originally, the 'static bound was also included, as it was also common for async code to also require 'static bound when spawning async tasks.

The Async trait is introduced as a helper trait, so that users can use it instead of writing Send + Sync everywhere. Originally, the 'static bound was also included, as it is common for async code to also require 'static when spawning async tasks. However, in practice, the use is infrequent enough that we don't need to include that inside the default trait bound for all abstract types.

Furthermore, the inclusion of 'static inside Async by default makes it more challenging to instantiate abstract types with concrete types that contain non-static lifetimes. As the use of CGP grows, we want to avoid the default from making it almost impossible to use non-static concrete types with CGP. As a result, it is best to remove this before CGP gains wider adoption.

On the other hand, the default inclusion of Send + Sync is almost a necessary evil with the current state of async Rust. However, this may soon change when Return Type Notation (RTN) gets stabilized in Rust in the near future in rust-lang/rust#138424. Once that is stabilized, the Async trait itself can entirely be deprecated or removed.

However, until RTN is stabilized, we still need Async trait with only the Send + Sync bounds. It is also unclear when RTN will actually gets stabilized, and whether we will be able to completely remove Async by then. Hence, at the very least, it is better to remove 'static from Async in case if Async lingers longer than we expect.

@soareschen soareschen changed the title Remove 'static bound from Async trait alias Remove 'static bound from Async trait alias May 1, 2025
@soareschen soareschen merged commit cbdcb55 into main May 1, 2025
5 checks passed
@soareschen soareschen deleted the remove-async-static branch May 1, 2025 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant