-
Notifications
You must be signed in to change notification settings - Fork 122
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
Use async-trait
in Service
trait ?
#241
Comments
Using a generic associated type saves a heap allocation. I am not deeply familiar with the server skeletons and how this design might complicate the implementation. |
By reading the blog, the new desugar is: trait HttpService {
async fn fetch(&self, url: Url) -> HtmlBody;
// ^^^^^^^^ desugars to:
// fn fetch(&self, url: Url) -> impl Future<Output = HtmlBody>;
}
Me neither 😆 , but I think that only having 1 function and 1 (or 2) GAT would simplify greatly the usage of the trait, and the implementation. /// A Modbus server service.
pub trait Service {
/// Errors produced by the service.
type Error;
/// The future response value.
type Future: Future<Output = Result<Response, Self::Error>> + Send + Sync + Unpin;
/// Process the `req` and return the [`Response`] asynchronously.
fn call(&self, req: Request<'static>) -> Self::Future;
} Or if you think that the new async or with rust 1.75: /// A Modbus server service.
#[trait_variant::make(Service: Send)]
pub trait LocalService {
/// Errors produced by the service.
type Error;
/// Process the `req` and return the [`Response`] asynchronously.
async fn call(&self, req: Request<'static>) -> Result<Response, Self::Error>;
} with /// A Modbus server service.
#[async_trait]
pub trait Service {
/// Errors produced by the service.
type Error;
/// Process the `req` and return the [`Response`] asynchronously.
async fn call(&self, req: Request<'static>) -> Result<Response, Self::Error>;
} |
Since the trait is supposed to be implemented only once in client code and not used for dynamic dispatch the non-boxed GAT version is more efficient. If you need dynamic dispatch in your code you could probably do this behind a generic |
Maybe related: #244 |
Would #245 fix this issue? |
If you are going to do it like this: fn call(
&self,
req: Self::Request,
) -> Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>; We might just use async_trait because it desugar to a similar type but remove the boilerplate of having to add The original issue is to have an I was thinking that maybe we can have 2 |
For me yes, but the user needs to always add : But I'm not sure if it enables dynamic dispatch 🤔 because |
|
Oh, okay I see. If we want to add |
The main issue is that it removes the I need to try if I can use an |
Hi,
I'm trying to call
async
fn's incall
, but I run into issues.I've a question for the
Service
trait insrc/server/service.rs
.Is there a reason for not using
async fn
withasync-trait
?This could give us the ability to call
.await
in thecall
function.This will change the trait like:
The text was updated successfully, but these errors were encountered: