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

Expose formatted service name as a const fn #1684

Merged
merged 11 commits into from
Aug 8, 2024

Conversation

mkatychev
Copy link
Contributor

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
Copy link
Contributor

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


quote! {
#name_doc
pub const fn service_name() -> &'static str {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would still make sense for this to a const SERVICE_NAME: &str?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair point, should the trait const below be const NAME: &'static str = SERVICE_NAME; then?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice, want to make that change?

@mkatychev mkatychev requested a review from djc June 11, 2024 22:44
@mkatychev
Copy link
Contributor Author

mkatychev commented Jun 12, 2024

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
Copy link
Member

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

@mkatychev
Copy link
Contributor Author

mkatychev commented Jun 20, 2024

@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
Copy link
Collaborator

djc commented Jun 21, 2024

@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?

@djc djc added this pull request to the merge queue Aug 8, 2024
@djc
Copy link
Collaborator

djc commented Aug 8, 2024

Thanks!

Merged via the queue into hyperium:master with commit d42daaf Aug 8, 2024
16 checks passed
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 this pull request may close these issues.

4 participants