You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add wrap option to macro to optionally generate wrapper types for enum variants (#51)
This adds an optional `wrap` attribute to the macro. When set, it will take the enum fields (can be any kind: unit, named, tuple), turn them into a struct, and modify the enum to have a single field as usually expected by irpc.
This reduces the boiler plate a lot for simple protocols. Also it allows you to skip the tedious newtyping if you e.g. have a lot of different requests that all take a single arg of the same type (e.g. a Uuid or a Hash).
It is purely optional, and works quite well with editors thanks to the span support (jump to definition jumps to the value of the wrap type, hover over that type gives the full fields, and jump to definition on a field of the generated struct takes you directly to the field on the enum variant even).
See the `simple.rs` example in the diff for the full boilerplatefree irpc-iroh galore.
I.e. it turns this
```rust
#[rpc_requests(message = FooMessage)]
#[derive(Debug, Serialize, Deserialize)]
pub enum FooProtocol {
#[rpc(wrap=GetRequest, tx=oneshot::Sender<Option<String>>)]
Get(String),
#[rpc(wrap=SetRequest, tx=oneshot::Sender<Option<String>>)]
Set { key: String, value: String },
}
```
into this
```rust
#[rpc_requests(message = FooMessage)]
#[derive(Debug, Serialize, Deserialize)]
pub enum FooProtocol {
#[rpc(wrap=GetRequest, tx=oneshot::Sender<Option<String>>)]
Get(GetRequest),
#[rpc(wrap=SetRequest, tx=oneshot::Sender<Option<String>>)]
Set(SetRequest)
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GetRequst(String)
#[derive(Debug, Serialize, Deserialize)]
pub struct SetRequest {
key: String,
value : String
}
```
0 commit comments