google / tarpc

An RPC framework for Rust with a focus on ease of use.
MIT License
3.09k stars 189 forks source link

Allow use of custom Result #420

Closed rtbo closed 5 months ago

rtbo commented 5 months ago

This PR allows to use custom Result type in tarpc::service

I.e, this code fails to compile with 0.34.0:

#[derive(Debug)]
enum Error {
    Msg(String),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Msg(msg) => f.write_str(msg),
        }
    }
}

impl std::error::Error for Error {}

type Result<T> = std::result::Result<T, Error>;

#[tarpc::service]
trait Test {
    async fn method() -> Result<()>;
}

Error message is

error[E0107]: type alias takes 1 generic argument but 2 generic arguments were supplied
  --> src/main.rs:19:1
   |
19 | #[tarpc::service]
   | ^^^^^^^^^^^^^^^^^
   | |
   | expected 1 generic argument
   | help: remove this generic argument
   |
note: type alias defined here, with 1 generic parameter: `T`
  --> src/main.rs:17:6
   |
17 | type Result<T> = std::result::Result<T, Error>;
   |      ^^^^^^ -
   = note: this error originates in the attribute macro `tarpc::service` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0271]: expected `impl Future<Output = Result<TestResponse, Error>>` to be a future that resolves to `Result<TestResponse, ServerError>`, but it resolves to `Result<TestResponse, Error>`
  --> src/main.rs:19:1
   |
19 | #[tarpc::service]
   | ^^^^^^^^^^^^^^^^^ expected `Result<TestResponse, ServerError>`, found `Result<TestResponse, Error>`
   |
   = note: expected enum `std::result::Result<_, ServerError>`
              found enum `std::result::Result<_, Error>`
note: required by a bound in `Serve::{opaque#0}`
  --> /home/remi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tarpc-0.34.0/src/server.rs:79:5
   |
79 |     async fn serve(self, ctx: context::Context, req: Self::Req) -> Result<Self::Resp, ServerError>;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Serve::{opaque#0}`
google-cla[bot] commented 5 months ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

rtbo commented 5 months ago

duplicate of #417