Skip to content

Commit

Permalink
fix(build): Allow Services to be named Result (#1203)
Browse files Browse the repository at this point in the history
closes #1156
  • Loading branch information
eskawl authored Dec 21, 2022
1 parent a72cf04 commit a562a3c
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ members = [
"tests/root-crate-path",
"tests/compression",
"tonic-web/tests/integration",
"tests/service_named_result",
]
14 changes: 14 additions & 0 deletions tests/service_named_result/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "service_named_result"
version = "0.1.0"
edition = "2021"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
prost = "0.11"
tonic = {path = "../../tonic"}

[build-dependencies]
tonic-build = {path = "../../tonic-build"}
19 changes: 19 additions & 0 deletions tests/service_named_result/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2020 Lucio Franco

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions tests/service_named_result/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
tonic_build::compile_protos("proto/result.proto").unwrap();
}
13 changes: 13 additions & 0 deletions tests/service_named_result/proto/result.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

import "google/protobuf/empty.proto";

package result;

service Result {
rpc Listen(google.protobuf.Empty) returns (Reply) {}
}

message Reply {
int32 step = 1;
}
3 changes: 3 additions & 0 deletions tests/service_named_result/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod pb {
tonic::include_proto!("result");
}
8 changes: 4 additions & 4 deletions tonic-build/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ fn generate_unary<T: Method>(
pub async fn #ident(
&mut self,
request: impl tonic::IntoRequest<#request>,
) -> Result<tonic::Response<#response>, tonic::Status> {
) -> std::result::Result<tonic::Response<#response>, tonic::Status> {
self.inner.ready().await.map_err(|e| {
tonic::Status::new(tonic::Code::Unknown, format!("Service was not ready: {}", e.into()))
})?;
Expand All @@ -251,7 +251,7 @@ fn generate_server_streaming<T: Method>(
pub async fn #ident(
&mut self,
request: impl tonic::IntoRequest<#request>,
) -> Result<tonic::Response<tonic::codec::Streaming<#response>>, tonic::Status> {
) -> std::result::Result<tonic::Response<tonic::codec::Streaming<#response>>, tonic::Status> {
self.inner.ready().await.map_err(|e| {
tonic::Status::new(tonic::Code::Unknown, format!("Service was not ready: {}", e.into()))
})?;
Expand All @@ -277,7 +277,7 @@ fn generate_client_streaming<T: Method>(
pub async fn #ident(
&mut self,
request: impl tonic::IntoStreamingRequest<Message = #request>
) -> Result<tonic::Response<#response>, tonic::Status> {
) -> std::result::Result<tonic::Response<#response>, tonic::Status> {
self.inner.ready().await.map_err(|e| {
tonic::Status::new(tonic::Code::Unknown, format!("Service was not ready: {}", e.into()))
})?;
Expand All @@ -303,7 +303,7 @@ fn generate_streaming<T: Method>(
pub async fn #ident(
&mut self,
request: impl tonic::IntoStreamingRequest<Message = #request>
) -> Result<tonic::Response<tonic::codec::Streaming<#response>>, tonic::Status> {
) -> std::result::Result<tonic::Response<tonic::codec::Streaming<#response>>, tonic::Status> {
self.inner.ready().await.map_err(|e| {
tonic::Status::new(tonic::Code::Unknown, format!("Service was not ready: {}", e.into()))
})?;
Expand Down
14 changes: 7 additions & 7 deletions tonic-build/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub(crate) fn generate_internal<T: Service>(
type Error = std::convert::Infallible;
type Future = BoxFuture<Self::Response, Self::Error>;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<std::result::Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

Expand Down Expand Up @@ -251,14 +251,14 @@ fn generate_trait_methods<T: Service>(
quote! {
#method_doc
async fn #name(&self, request: tonic::Request<#req_message>)
-> Result<tonic::Response<#res_message>, tonic::Status>;
-> std::result::Result<tonic::Response<#res_message>, tonic::Status>;
}
}
(true, false) => {
quote! {
#method_doc
async fn #name(&self, request: tonic::Request<tonic::Streaming<#req_message>>)
-> Result<tonic::Response<#res_message>, tonic::Status>;
-> std::result::Result<tonic::Response<#res_message>, tonic::Status>;
}
}
(false, true) => {
Expand All @@ -270,11 +270,11 @@ fn generate_trait_methods<T: Service>(

quote! {
#stream_doc
type #stream: futures_core::Stream<Item = Result<#res_message, tonic::Status>> + Send + 'static;
type #stream: futures_core::Stream<Item = std::result::Result<#res_message, tonic::Status>> + Send + 'static;

#method_doc
async fn #name(&self, request: tonic::Request<#req_message>)
-> Result<tonic::Response<Self::#stream>, tonic::Status>;
-> std::result::Result<tonic::Response<Self::#stream>, tonic::Status>;
}
}
(true, true) => {
Expand All @@ -286,11 +286,11 @@ fn generate_trait_methods<T: Service>(

quote! {
#stream_doc
type #stream: futures_core::Stream<Item = Result<#res_message, tonic::Status>> + Send + 'static;
type #stream: futures_core::Stream<Item = std::result::Result<#res_message, tonic::Status>> + Send + 'static;

#method_doc
async fn #name(&self, request: tonic::Request<tonic::Streaming<#req_message>>)
-> Result<tonic::Response<Self::#stream>, tonic::Status>;
-> std::result::Result<tonic::Response<Self::#stream>, tonic::Status>;
}
}
};
Expand Down
18 changes: 12 additions & 6 deletions tonic-health/src/generated/grpc.health.v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ pub mod health_client {
pub async fn check(
&mut self,
request: impl tonic::IntoRequest<super::HealthCheckRequest>,
) -> Result<tonic::Response<super::HealthCheckResponse>, tonic::Status> {
) -> std::result::Result<
tonic::Response<super::HealthCheckResponse>,
tonic::Status,
> {
self.inner
.ready()
.await
Expand Down Expand Up @@ -153,7 +156,7 @@ pub mod health_client {
pub async fn watch(
&mut self,
request: impl tonic::IntoRequest<super::HealthCheckRequest>,
) -> Result<
) -> std::result::Result<
tonic::Response<tonic::codec::Streaming<super::HealthCheckResponse>>,
tonic::Status,
> {
Expand Down Expand Up @@ -186,10 +189,13 @@ pub mod health_server {
async fn check(
&self,
request: tonic::Request<super::HealthCheckRequest>,
) -> Result<tonic::Response<super::HealthCheckResponse>, tonic::Status>;
) -> std::result::Result<
tonic::Response<super::HealthCheckResponse>,
tonic::Status,
>;
/// Server streaming response type for the Watch method.
type WatchStream: futures_core::Stream<
Item = Result<super::HealthCheckResponse, tonic::Status>,
Item = std::result::Result<super::HealthCheckResponse, tonic::Status>,
>
+ Send
+ 'static;
Expand All @@ -211,7 +217,7 @@ pub mod health_server {
async fn watch(
&self,
request: tonic::Request<super::HealthCheckRequest>,
) -> Result<tonic::Response<Self::WatchStream>, tonic::Status>;
) -> std::result::Result<tonic::Response<Self::WatchStream>, tonic::Status>;
}
#[derive(Debug)]
pub struct HealthServer<T: Health> {
Expand Down Expand Up @@ -266,7 +272,7 @@ pub mod health_server {
fn poll_ready(
&mut self,
_cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
) -> Poll<std::result::Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: http::Request<B>) -> Self::Future {
Expand Down
14 changes: 10 additions & 4 deletions tonic-reflection/src/generated/grpc.reflection.v1alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ pub mod server_reflection_client {
request: impl tonic::IntoStreamingRequest<
Message = super::ServerReflectionRequest,
>,
) -> Result<
) -> std::result::Result<
tonic::Response<tonic::codec::Streaming<super::ServerReflectionResponse>>,
tonic::Status,
> {
Expand Down Expand Up @@ -248,7 +248,10 @@ pub mod server_reflection_server {
pub trait ServerReflection: Send + Sync + 'static {
/// Server streaming response type for the ServerReflectionInfo method.
type ServerReflectionInfoStream: futures_core::Stream<
Item = Result<super::ServerReflectionResponse, tonic::Status>,
Item = std::result::Result<
super::ServerReflectionResponse,
tonic::Status,
>,
>
+ Send
+ 'static;
Expand All @@ -257,7 +260,10 @@ pub mod server_reflection_server {
async fn server_reflection_info(
&self,
request: tonic::Request<tonic::Streaming<super::ServerReflectionRequest>>,
) -> Result<tonic::Response<Self::ServerReflectionInfoStream>, tonic::Status>;
) -> std::result::Result<
tonic::Response<Self::ServerReflectionInfoStream>,
tonic::Status,
>;
}
#[derive(Debug)]
pub struct ServerReflectionServer<T: ServerReflection> {
Expand Down Expand Up @@ -312,7 +318,7 @@ pub mod server_reflection_server {
fn poll_ready(
&mut self,
_cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
) -> Poll<std::result::Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: http::Request<B>) -> Self::Future {
Expand Down

0 comments on commit a562a3c

Please sign in to comment.