Skip to content

Commit 976e425

Browse files
committed
fix accidental dynamic dispatch
1 parent 558517e commit 976e425

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "proxy-enum"
3-
version = "0.1.2"
3+
version = "0.2.0"
44
authors = ["Moritz Bischof <[email protected]>"]
55
edition = "2018"
66
description = "Emulate dynamic dispatch and sealed classes using a proxy enum, which defers all method calls to its variants."

src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ fn find_attr<'a>(attrs: &'a [Attribute], ident: &str) -> Option<&'a Attribute> {
141141
attr_idx(&attrs, ident).map(|idx| &attrs[idx])
142142
}
143143

144-
fn gen_static_method_call(receiver_ty: &Type, signature: &Signature) -> TokenStream2 {
144+
fn gen_static_method_call(receiver: TokenStream2, signature: &Signature) -> TokenStream2 {
145145
let method_ident = &signature.ident;
146146

147147
let args = signature
@@ -156,7 +156,7 @@ fn gen_static_method_call(receiver_ty: &Type, signature: &Signature) -> TokenStr
156156
_ => panic!("parameter binding must be an identifier"),
157157
});
158158

159-
quote! { <#receiver_ty>::#method_ident(__self #(, #args)*) }
159+
quote! { #receiver::#method_ident(__self #(, #args)*) }
160160
}
161161

162162
struct WrapperVariant {
@@ -217,8 +217,7 @@ fn implement_trait(
217217
) {
218218
assert!(pseudo_impl.items.is_empty());
219219

220-
let receiver = &pseudo_impl.trait_.as_ref().unwrap().1;
221-
let trait_ty = parse2::<Type>(quote! { #receiver }).unwrap();
220+
let trait_ident = &trait_decl.ident;
222221

223222
let proxy_methods = trait_decl.items.iter().map(|i| match i {
224223
TraitItem::Method(i) => {
@@ -233,7 +232,7 @@ fn implement_trait(
233232
}
234233
}
235234

236-
let match_block = gen_match_block(variants, |_| gen_static_method_call(&trait_ty, sig));
235+
let match_block = gen_match_block(variants, |_| gen_static_method_call(quote! { #trait_ident }, sig));
237236
let tokens = quote! { #sig { #match_block } };
238237
parse2::<ImplItem>(tokens).unwrap()
239238
}
@@ -261,7 +260,8 @@ fn implement_raw(variants: &[WrapperVariant], pseudo_impl: &mut ItemImpl) {
261260
}
262261

263262
let match_block = gen_match_block(variants, |variant| {
264-
gen_static_method_call(&variant.wrapped, &method.sig)
263+
let ty = &variant.wrapped;
264+
gen_static_method_call(quote! { #ty }, &method.sig)
265265
});
266266
let body = quote! { { #match_block } };
267267
method.block = syn::parse2(body).unwrap();

0 commit comments

Comments
 (0)