Skip to content

Commit 6965776

Browse files
committedMar 17, 2025·
Don't require derive_more in the macro
we gen the from impls ourselves now.
1 parent e282b19 commit 6965776

File tree

8 files changed

+46
-10
lines changed

8 files changed

+46
-10
lines changed
 

‎Cargo.lock

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ tracing-subscriber = { version = "0.3.19", features = ["fmt"] }
4242
# used in the derive example. This must not be a main crate dep or else it will be circular!
4343
quic-rpc-derive = { path = "quic-rpc-derive" }
4444
# just convenient for the enum definitions
45-
derive_more = { version = "2", features = ["debug", "display", "from"] }
45+
derive_more = { version = "2", features = ["from"] }
4646
# we need full for example main etc.
4747
tokio = { version = "1", features = ["full"] }
4848
# formatting

‎examples/compute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ enum ComputeRequest {
5858

5959
// Define the protocol and message enums using the macro
6060
#[rpc_requests(ComputeService, ComputeMessage)]
61-
#[derive(derive_more::From, Serialize, Deserialize)]
61+
#[derive(Serialize, Deserialize)]
6262
enum ComputeProtocol {
6363
#[rpc(tx=oneshot::Sender<u128>)]
6464
Sqr(Sqr),

‎examples/derive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct Set {
4141
// Use the macro to generate both the StorageProtocol and StorageMessage enums
4242
// plus implement Channels for each type
4343
#[rpc_requests(StorageService, StorageMessage)]
44-
#[derive(derive_more::From, Serialize, Deserialize)]
44+
#[derive(Serialize, Deserialize)]
4545
enum StorageProtocol {
4646
#[rpc(tx=oneshot::Sender<Option<String>>)]
4747
Get(Get),

‎quic-rpc-derive/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ quote = "1"
1818
proc-macro2 = "1"
1919

2020
[dev-dependencies]
21-
derive_more = { version = "1", features = ["from", "try_into", "display"] }
21+
derive_more = { version = "2", features = ["from"] }
2222
serde = { version = "1", features = ["serde_derive"] }
2323
trybuild = "1.0"
2424
quic-rpc = { path = ".." }

‎quic-rpc-derive/src/lib.rs

+39-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,35 @@ fn generate_channels_impl(
4848
args.check_empty(attr_span)?;
4949
Ok(res)
5050
}
51+
fn generate_from_impls(
52+
message_enum_name: &Ident,
53+
variants: &[(Ident, Type)],
54+
service_name: &Ident,
55+
original_enum_name: &Ident,
56+
additional_items: &mut Vec<proc_macro2::TokenTree>,
57+
) {
58+
// Generate and add From impls for the message enum
59+
for (variant_name, inner_type) in variants {
60+
let message_impl = quote! {
61+
impl From<::quic_rpc::WithChannels<#inner_type, #service_name>> for #message_enum_name {
62+
fn from(value: ::quic_rpc::WithChannels<#inner_type, #service_name>) -> Self {
63+
#message_enum_name::#variant_name(value)
64+
}
65+
}
66+
};
67+
additional_items.extend(message_impl);
68+
69+
// Generate and add From impls for the original enum
70+
let original_impl = quote! {
71+
impl From<#inner_type> for #original_enum_name {
72+
fn from(value: #inner_type) -> Self {
73+
#original_enum_name::#variant_name(value)
74+
}
75+
}
76+
};
77+
additional_items.extend(original_impl);
78+
}
79+
}
5180

5281
#[proc_macro_attribute]
5382
pub fn rpc_requests(attr: TokenStream, item: TokenStream) -> TokenStream {
@@ -127,7 +156,7 @@ pub fn rpc_requests(attr: TokenStream, item: TokenStream) -> TokenStream {
127156
}
128157

129158
let message_variants = variants
130-
.into_iter()
159+
.iter()
131160
.map(|(variant_name, inner_type)| {
132161
quote! {
133162
#variant_name(::quic_rpc::WithChannels<#inner_type, #service_name>)
@@ -136,12 +165,20 @@ pub fn rpc_requests(attr: TokenStream, item: TokenStream) -> TokenStream {
136165
.collect::<Vec<_>>();
137166

138167
let message_enum = quote! {
139-
#[derive(derive_more::From)]
140168
enum #message_enum_name {
141169
#(#message_variants),*
142170
}
143171
};
144172

173+
// Generate the From implementations
174+
generate_from_impls(
175+
&message_enum_name,
176+
&variants,
177+
&service_name,
178+
&input.ident,
179+
&mut additional_items,
180+
);
181+
145182
let output = quote! {
146183
#input
147184

‎quic-rpc-derive/tests/smoke.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn simple() {
3535
struct Response4;
3636

3737
#[rpc_requests(Service, RequestWithChannels)]
38-
#[derive(Debug, Serialize, Deserialize, derive_more::From, derive_more::TryInto)]
38+
#[derive(Debug, Serialize, Deserialize)]
3939
enum Request {
4040
#[rpc(tx=oneshot::Sender<()>)]
4141
Rpc(RpcRequest),

‎src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ impl<M: Send + Sync + 'static, R, S: Service> ServiceSender<M, R, S> {
450450
Self::Remote(endpoint, addr, _) => {
451451
let connection = endpoint
452452
.connect(*addr, "localhost")
453-
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
453+
.map_err(io::Error::other)?
454454
.await?;
455455
let (send, recv) = connection.open_bi().await?;
456456
Ok(ServiceRequest::Remote(rpc::RemoteRequest::new(send, recv)))

0 commit comments

Comments
 (0)
Please sign in to comment.