grpc / grpc/grpc-rust

Support for customizing trait attributes in tonic-build

Open
#2,341 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
12.5k
Forks
1.3k
Avg merge
4d 7h
Merged PRs (30d)
24

Description

## Feature Request

### Crates

tonic-build

### Motivation

Currently, tonic-build generates trait without any way to attach additional attributes or proc-macros.
This makes it difficult to integrate with frameworks or tools that rely on macros, such as automocking struct by trait

### Proposal

Add a configuration option in tonic-build to inject custom attributes into generated service traits:

**In build options**
```rust
...
.trait_atttribute("myservice.MyService", "#[mockall::automock]")
...
```

**In generated code (Before)**
```rust
/// Generated trait containing gRPC methods that should be implemented for use with ProjectsPrivateServer.
#[async_trait]
pub trait MyServce: std::marker::Send + std::marker::Sync + 'static {
...
}
```

**In generated code (After)**
```rust
/// Generated trait containing gRPC methods that should be implemented for use with ProjectsPrivateServer.
#[async_trait]
#[mockall::automock]
pub trait MyServce: std::marker::Send + std::marker::Sync + 'static {
...
}
```

### Alternatives

I ran into this limitation when trying to use `#[mockall::automock]` for a generated service trait to simplify testing.
In my case:

* I use the trait to describe a client interface for another service.
* Functions in my code accept a generic parameter implementing this trait, so I can pass either:

* the real gRPC client generated alongside the trait, or
* a mocked implementation for tests.

Without this feature, mocking is cumbersome because I must **manually implement the entire trait for mocks** instead of simply deriving it via `mockall`.

While my primary need is mocking, I believe there are many other potential use cases for injecting proc-macro attributes, such as logging, tracing, or applying custom derives.

---

If this feature aligns with the vision of the project, I am willing to implement the functionality and open a Pull Request

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.