Skip to content
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

service name conflicts with rpc method name #89

Closed
zarvd opened this issue Oct 23, 2019 · 1 comment · Fixed by #92
Closed

service name conflicts with rpc method name #89

zarvd opened this issue Oct 23, 2019 · 1 comment · Fixed by #92

Comments

@zarvd
Copy link

zarvd commented Oct 23, 2019

Bug Report

Version

└── tonic v0.1.0-alpha.3
└── tonic-build v0.1.0-alpha.3

Platform

Linux cccjlx 5.3.7-arch1-1-ARCH #1 SMP PREEMPT Fri Oct 18 00:17:03 UTC 2019 x86_64 GNU/Linux

Crates

tonic-build

Description

Proto buffers definition:

syntax = "proto3";

package foo;

service Foo {
  rpc Foo(stream FooRequest) returns (stream FooResponse) {}
}

message FooRequest {}

message FooResponse {}

The following error occurs when execute cargo build:

error[E0404]: expected trait, found struct `Foo`
   --> /xxx/target/debug/build/xxx-16b05b8476663e11/out/foo.rs:126:35
    |
126 |                     struct Foo<T: Foo>(pub Arc<T>);
    |                                   ^^^ not a trait
help: possible better candidate is found in another module, you can import it into scope
    |
67  |     use crate::proto::foo::server::Foo;
    |

error[E0404]: expected trait, found struct `Foo`
   --> /xxx/target/debug/build/xxx-16b05b8476663e11/out/foo.rs:127:29
    |
127 |                     impl<T: Foo> tonic::server::StreamingService<super::FooRequest> for Foo<T> {
    |                             ^^^ not a trait
help: possible better candidate is found in another module, you can import it into scope
    |
67  |     use crate::proto::foo::server::Foo;

And I notice there is a new struct named Foo in the generated rpc method, which conflicts with the previous defined trait Foo

The complete generated code:

#[derive(Clone, PartialEq, ::prost::Message)]
pub struct FooRequest {}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct FooResponse {}
#[doc = r" Generated client implementations."]
pub mod client {
    #![allow(unused_variables, dead_code, missing_docs)]
    use tonic::codegen::*;
    pub struct FooClient<T> {
        inner: tonic::client::Grpc<T>,
    }
    impl FooClient<tonic::transport::Channel> {
        #[doc = r" Attempt to create a new client by connecting to a given endpoint."]
        pub fn connect<D>(dst: D) -> Result<Self, tonic::transport::Error>
        where
            D: std::convert::TryInto<tonic::transport::Endpoint>,
            D::Error: Into<StdError>,
        {
            tonic::transport::Endpoint::new(dst).map(|c| Self::new(c.channel()))
        }
    }
    impl<T> FooClient<T>
    where
        T: tonic::client::GrpcService<tonic::body::BoxBody>,
        T::ResponseBody: Body + HttpBody + Send + 'static,
        T::Error: Into<StdError>,
        <T::ResponseBody as HttpBody>::Error: Into<StdError> + Send,
        <T::ResponseBody as HttpBody>::Data: Into<bytes::Bytes> + Send,
    {
        pub fn new(inner: T) -> Self {
            let inner = tonic::client::Grpc::new(inner);
            Self { inner }
        }
        #[doc = r" Check if the service is ready."]
        pub async fn ready(&mut self) -> Result<(), tonic::Status> {
            self.inner.ready().await.map_err(|e| {
                tonic::Status::new(
                    tonic::Code::Unknown,
                    format!("Service was not ready: {}", e.into()),
                )
            })
        }
        pub async fn foo<S>(
            &mut self,
            request: tonic::Request<S>,
        ) -> Result<tonic::Response<tonic::codec::Streaming<super::FooResponse>>, tonic::Status>
        where
            S: Stream<Item = Result<super::FooRequest, tonic::Status>> + Send + 'static,
        {
            self.ready().await?;
            let codec = tonic::codec::ProstCodec::new();
            let path = http::uri::PathAndQuery::from_static("/foo.Foo/Foo");
            self.inner.streaming(request, path, codec).await
        }
    }
    impl<T: Clone> Clone for FooClient<T> {
        fn clone(&self) -> Self {
            Self {
                inner: self.inner.clone(),
            }
        }
    }
}
#[doc = r" Generated server implementations."]
pub mod server {
    #![allow(unused_variables, dead_code, missing_docs)]
    use tonic::codegen::*;
    #[doc = "Generated trait containing gRPC methods that should be implemented for use with FooServer."]
    #[async_trait]
    pub trait Foo: Send + Sync + 'static {
        #[doc = "Server streaming response type for the Foo method."]
        type FooStream: Stream<Item = Result<super::FooResponse, tonic::Status>> + Send + 'static;
        async fn foo(
            &self,
            request: tonic::Request<tonic::Streaming<super::FooRequest>>,
        ) -> Result<tonic::Response<Self::FooStream>, tonic::Status> {
            Err(tonic::Status::unimplemented("Not yet implemented"))
        }
    }
    #[derive(Clone, Debug)]
    pub struct FooServer<T: Foo> {
        inner: Arc<T>,
    }
    #[derive(Clone, Debug)]
    #[doc(hidden)]
    pub struct FooServerSvc<T: Foo> {
        inner: Arc<T>,
    }
    impl<T: Foo> FooServer<T> {
        #[doc = "Create a new FooServer from a type that implements Foo."]
        pub fn new(inner: T) -> Self {
            let inner = Arc::new(inner);
            Self::from_shared(inner)
        }
        pub fn from_shared(inner: Arc<T>) -> Self {
            Self { inner }
        }
    }
    impl<T: Foo> FooServerSvc<T> {
        pub fn new(inner: Arc<T>) -> Self {
            Self { inner }
        }
    }
    impl<T: Foo, R> Service<R> for FooServer<T> {
        type Response = FooServerSvc<T>;
        type Error = Never;
        type Future = Ready<Result<Self::Response, Self::Error>>;
        fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
            Poll::Ready(Ok(()))
        }
        fn call(&mut self, _: R) -> Self::Future {
            ok(FooServerSvc::new(self.inner.clone()))
        }
    }
    impl<T: Foo> Service<http::Request<HyperBody>> for FooServerSvc<T> {
        type Response = http::Response<tonic::body::BoxBody>;
        type Error = Never;
        type Future = BoxFuture<Self::Response, Self::Error>;
        fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
            Poll::Ready(Ok(()))
        }
        fn call(&mut self, req: http::Request<HyperBody>) -> Self::Future {
            let inner = self.inner.clone();
            match req.uri().path() {
                "/foo.Foo/Foo" => {
                    struct Foo<T: Foo>(pub Arc<T>);
                    impl<T: Foo> tonic::server::StreamingService<super::FooRequest> for Foo<T> {
                        type Response = super::FooResponse;
                        type ResponseStream = T::FooStream;
                        type Future =
                            BoxFuture<tonic::Response<Self::ResponseStream>, tonic::Status>;
                        fn call(
                            &mut self,
                            request: tonic::Request<tonic::Streaming<super::FooRequest>>,
                        ) -> Self::Future {
                            let inner = self.0.clone();
                            let fut = async move { inner.foo(request).await };
                            Box::pin(fut)
                        }
                    }
                    let inner = self.inner.clone();
                    let fut = async move {
                        let method = Foo(inner);
                        let codec = tonic::codec::ProstCodec::new();
                        let mut grpc = tonic::server::Grpc::new(codec);
                        let res = grpc.streaming(method, req).await;
                        Ok(res)
                    };
                    Box::pin(fut)
                }
                _ => Box::pin(async move {
                    Ok(http::Response::builder()
                        .status(200)
                        .header("grpc-status", "12")
                        .body(tonic::body::BoxBody::empty())
                        .unwrap())
                }),
            }
        }
    }
}
@LucioFranco
Copy link
Member

@ccc13 Hi! I think #92 fixes this issue, thanks for reporting it!

rabbitinspace pushed a commit to satelit-project/tonic that referenced this issue Jan 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants