-
Notifications
You must be signed in to change notification settings - Fork 268
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
Client methods traits should desugar async functions to return impl Future
#1796
Comments
The main concern here is the If you use trait DoAsyncThing {
async fn do_async_thing(&mut self) -> bool;
} Which is desugared to trait DoAsyncThing {
fn do_async_thing(&mut self) -> impl Future<Output = bool>;
} Looking only at the trait, it is not possible to verify that async fn spawn_async_thing(mut thing: impl DoAsyncThing + Send + 'static)
{
tokio::task::spawn(async move {
if !thing.do_async_thing().await {
println!("nope!")
}
});
} It fails because
However, I think we can move forward without being concerned about that for now, given a few mitigating circumstances:
async fn spawn_async_thing_2(mut thing: MyAsyncThing)
{
tokio::task::spawn(async move {
if !thing.do_async_thing().await {
println!("nope!")
}
});
}
#![feature(return_type_notation)]
async fn spawn_async_thing_3(mut thing: impl DoAsyncThing<do_async_thing(..): Send> + Send + 'static) {
tokio::task::spawn(async move {
if !thing.do_async_thing().await {
println!("nope!")
}
});
}
On top of all those mitigations, I think that requiring a Having said all that, I also concede that the use cases for non- So that's just my $0.02 on the topic. For now, in the Cosmos crate, I've gone ahead and used |
Won't that require us to use the 2024 edition instead of 2021 like we do now? We'd have to discuss if we want to jump that far ahead. We generally don't use the latest-and-greatest. Heck, even Go barely made the case for moving to 1.18 to use generics but has no plans as of yet to use newer (despite some interesting use cases for range-over-funcs). |
And before we get too far into the weeds to make everything object-safe - which I'm not opposed to - we may want to reconsider how to support mocking. I'm open to other suggestions and, frankly, would prefer not to have the clients implement a trait full of client methods. It's fragile anyway and can lead to breaking changes for devs who implement the trait for mocking. I considered making a |
No, it doesn't require edition 2024, but it does require rustc 1.81 and our current MSRV is 1.76. Updating to 1.81 would also enable the |
💯 I'd love to be able to get rid of the trait altogether. I'm not really worried about object-safety though, if we do need to keep using traits. The main question here is if we are using traits, do we need to constraint |
I don't think we're ready to make that jump just yet. It just came out, and even Go has an N-2 policy for language versions (~6 months) which move even slower than Rust's (~every month). At least, I don't think |
Absolutely agreed. It's good to know that we don't need to move editions though, so when we feel reasonable bumping MSRV to 1.81 we can use it fairly safely. |
To avoid a valid lint, we should desugar
FooClientMethods
methods asasync fn foo() -> Result<Response<T>>
intofn foo() -> impl Future<Result<Response<T>>>
instead.See #1773 (comment) for context.
The text was updated successfully, but these errors were encountered: