hyperium / tonic

A native gRPC client & server implementation with async/await support.
https://docs.rs/tonic
MIT License
9.35k stars 957 forks source link

Expose formatted service name as a `const fn` #1684

Open mkatychev opened 2 months ago

mkatychev commented 2 months ago

Motivation

It is oftentimes cumbersome to use tonic::server::NamedService::NAME when T has multiple generics:

impl<T: ServerTrait> tonic::server::NamedService for ServiceServer<T> {
    const NAME: &'static str = "service_api.Service";
}

This is particularly egregious when using the HealthReporter methods since HealthReporter::set_serving requires a fully qualified ServiceServer<T>:

let (mut health_reporter, health_service) = tonic_health::server::health_reporter();
health_reporter.set_serving::<MyServer<MyInner<MyLongQualifiedType, MyOtherQualifiedType, T, T2>>>().await;

Solution

expose a pub const fn service_name() -> &'static str that returns the formatted string.

AmeliasCode commented 1 month ago

+1 to this! It would make it a lot less verbose to get the service name.

mkatychev commented 3 weeks ago

I cannot seem to reproduce this diff locally with protobuf 25: https://github.com/hyperium/tonic/actions/runs/9472891207/job/26134316596#step:7:16

LucioFranco commented 3 weeks ago

@mkatychev you just need to run cargo run --package codegen

mkatychev commented 2 weeks ago

@mkatychev you just need to run cargo run --package codegen

Hi @LucioFranco I did that with the given version of protoc (25.1) on macOS and I am not able to produce the comment diff in the run log

djc commented 2 weeks ago

@mkatychev you just need to run cargo run --package codegen

Hi @LucioFranco I did that with the given version of protoc (25.1) on macOS and I am not able to produce the comment diff in the run log

Run cargo update locally before you run the codegen?