-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Remove #[async_trait]
#5
Conversation
Manually specifying the |
src/de/mod.rs
Outdated
@@ -102,119 +99,210 @@ pub trait Decoder: Send { | |||
/// Decoder what type is in the input. Know that relying on | |||
/// `Decoder::decode_any` means your data type will be able to | |||
/// decode self-describing formats only. | |||
async fn decode_any<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>; | |||
fn decode_any<V: Visitor>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do the implementations use fn ... -> impl Future<RT>
rather than async fn ... -> RT
? My understanding is that async
is still allowed in trait implementations and this would reduce the amount of code that has to be updated while maintaining the clarity of the return signature: https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html#can-i-mix-async-fn-and-impl-trait (same below)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the latter (async fn ... -> RT
) is only equivalent to fn ... -> impl Future<Output=RT>
, and that Future
is not Send
, so you can't use that function across threads, i linked some discussion that felt relevant in the description, e.g. this forum post and this SO question
an alternative would be to follow the example you linked, and do something like
#[trait_variant::make]
trait MyTrait {
async fn MyAsync() -> i32;
}
adding that attribute would be equivalent to removing the async and making the return type impl Future<Output=i32> + Send
(and I think we could configure it to make both a Send
and not-Send
version).
does that track?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes thank you for clarifying. Per the discussion in #5 it sounds like the #[trait_variant]
is the way to go.
Thanks @DrewMcArthur ! Would you mind merging this into the @js2xxx I agree the |
…ithub.com:DrewMcArthur/destream; branch '0.9' of github.com:haydnv/destream into 4-remove-async-trait
It's not necessary though, since the current version already restricts the returned futures to be RTN allows the downstream users of decoders to choose whether they should be In short, RTN makes the core traits more general and extends their applications to some extent. |
@js2xxx thanks for bringing this up, I think after staring at it for a bit I'm beginning to understand - I made these changes because a) of the warning i got, and b) downstream when was implementing these traits, i got an error that the It seems like the way forward is to replace the the last paragraph of MVP part 2 here mentions that RTN ends up being really verbose for traits with many methods, like the |
Thank you for clarifying, my preference is for now to retain the Does that sound right? |
Yes, it sounds good to me. :) |
i've made that change to use I just added a workflow that should show the errors i'm getting directly on the PR once it's approved alternatively you can check the same PR in my repo and see the error i'm getting. any hints? |
looks like i just needed to add some edit: yeah this is a workaround for rust-lang/impl-trait-utils#17. implementations should work fine though |
@DrewMcArthur I understand that this is necessary in the default method implementations due to a quirk of |
@haydnv I believe so, but I'd like to get a PR for |
just opened haydnv/destream_json#11 ! |
closes #4
This removes the dependency on the
async-trait
crate, and refactors trait definitions to returnimpl Future<Output=T> + Send
, complying with the compiler warningasync_fn_in_trait
mentioned here and here.Seems like tests pass, but opening this as a draft PR so it can undergo some scrutiny before it's merged.
A nice-to-add piece would be something for the documentation - if you look at the docs.rs page for the
FromStream
trait, you can see the definition is the expanded code, which makes it a little tricky to know how to define the compatible function. This will clean that up a bit, but we'd still have the discrepancy where the trait function is defined aswhile the implementation looks like
so it would be cool if the docs mentioned this & made it clear what the "right" way to implement the traits is! cheers!