Skip to content

Commit 86d7f95

Browse files
bors[bot]setzer22
andauthored
Merge #734
734: Add a `name` optional argument to `#[export]` r=toasteater a=setzer22 This allows exporting methods to godot under a different name. This commit implements the feature described in #732 Co-authored-by: Setzer22 <jsanchezfsms@gmail.com>
2 parents 99a0dee + b424d03 commit 86d7f95

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

gdnative-derive/src/methods.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub(crate) struct ExportMethod {
6565
pub(crate) struct ExportArgs {
6666
pub(crate) optional_args: Option<usize>,
6767
pub(crate) rpc_mode: RpcMode,
68+
pub(crate) name_override: Option<String>,
6869
}
6970

7071
pub(crate) fn derive_methods(item_impl: ItemImpl) -> TokenStream2 {
@@ -82,7 +83,7 @@ pub(crate) fn derive_methods(item_impl: ItemImpl) -> TokenStream2 {
8283
let sig_span = sig.ident.span();
8384

8485
let name = sig.ident;
85-
let name_string = name.to_string();
86+
let name_string = args.name_override.unwrap_or_else(|| name.to_string());
8687
let ret_span = sig.output.span();
8788
let ret_ty = match sig.output {
8889
syn::ReturnType::Default => quote_spanned!(ret_span => ()),
@@ -181,6 +182,7 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
181182
ImplItem::Method(mut method) => {
182183
let mut export_args = None;
183184
let mut rpc = None;
185+
let mut name_override = None;
184186

185187
let mut errors = vec![];
186188

@@ -253,8 +255,9 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
253255
};
254256
let path = last.ident.to_string();
255257

256-
// Match rpc mode
258+
// Match optional export arguments
257259
match path.as_str() {
260+
// rpc mode
258261
"rpc" => {
259262
let value = if let syn::Lit::Str(lit_str) = lit {
260263
lit_str.value()
@@ -281,17 +284,36 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
281284
));
282285
return false;
283286
}
287+
}
288+
// name override
289+
"name" => {
290+
let value = if let syn::Lit::Str(lit_str) = lit {
291+
lit_str.value()
292+
} else {
293+
errors.push(syn::Error::new(
294+
last.span(),
295+
"unexpected type for name value, expected Str",
296+
));
297+
return false;
298+
};
284299

300+
if name_override.replace(value).is_some() {
301+
errors.push(syn::Error::new(
302+
last.span(),
303+
"name was set more than once",
304+
));
305+
return false;
306+
}
307+
}
308+
_ => {
309+
let msg =
310+
format!("unknown option for export: `{}`", path);
311+
errors.push(syn::Error::new(last.span(), msg));
285312
return false;
286313
}
287-
_ => (),
288314
}
289-
290-
let msg = format!("unknown option for export: `{}`", path);
291-
errors.push(syn::Error::new(last.span(), msg));
292315
}
293316
}
294-
295317
return false;
296318
}
297319
}
@@ -340,6 +362,7 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
340362

341363
export_args.optional_args = optional_args;
342364
export_args.rpc_mode = rpc.unwrap_or(RpcMode::Disabled);
365+
export_args.name_override = name_override;
343366

344367
methods_to_export.push(ExportMethod {
345368
sig: method.sig.clone(),

0 commit comments

Comments
 (0)