Skip to content

Commit

Permalink
cleanup: return no results from shared_fn when the result is SharedFn…
Browse files Browse the repository at this point in the history
…Result<()>
  • Loading branch information
zshipko committed Jun 26, 2024
1 parent 068165d commit 2f13fd8
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro2::{Ident, Span};
use quote::quote;
use syn::{parse_macro_input, FnArg, ItemFn, ItemForeignMod};
use syn::{parse_macro_input, FnArg, GenericArgument, ItemFn, ItemForeignMod, PathArguments};

/// `plugin_fn` is used to define an Extism callable function to export
///
Expand Down Expand Up @@ -179,16 +179,36 @@ pub fn shared_fn(
let (no_result, raw_output) = match output {
syn::ReturnType::Default => (true, quote! {}),
syn::ReturnType::Type(_, t) => {
let mut is_unit = false;
if let syn::Type::Path(p) = t.as_ref() {
if let Some(t) = p.path.segments.last() {
if t.ident != "SharedFnResult" {
panic!("extism_pdk::shared_fn expects a function that returns extism_pdk::SharedFnResult");
}
match &t.arguments {
PathArguments::AngleBracketed(args) => {
if args.args.len() == 1 {
match &args.args[0] {
GenericArgument::Type(syn::Type::Tuple(t)) => {
if t.elems.is_empty() {
is_unit = true;
}
}
_ => (),
}
}
}
_ => (),
}
} else {
panic!("extism_pdk::shared_fn expects a function that returns extism_pdk::SharedFnResult");
}
};
(false, quote! {-> u64 })
if is_unit {
(true, quote! {})
} else {
(false, quote! {-> u64 })
}
}
};

Expand Down

0 comments on commit 2f13fd8

Please sign in to comment.