Mercurial > lbo > hg > memoize
changeset 85:fbf408e00f17
Fix #17: Allow memoizing functions with no arguments
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Fri, 02 Dec 2022 21:03:43 +0100 |
parents | 868eb5c58450 |
children | 2ccee100cd33 |
files | examples/empty.rs inner/src/lib.rs |
diffstat | 2 files changed, 22 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/empty.rs Fri Dec 02 21:03:43 2022 +0100 @@ -0,0 +1,18 @@ +//! Reproduces (verifies) issue #17: Panics when used on fn without args. + +use memoize::memoize; + +#[memoize] +fn hello() -> bool { + println!("hello!"); + true +} + +fn main() { + // `hello` is only called once here. + assert!(hello()); + assert!(hello()); + memoized_flush_hello(); + // and again here. + assert!(hello()); +}
--- a/inner/src/lib.rs Fri Oct 21 19:05:50 2022 +0200 +++ b/inner/src/lib.rs Fri Dec 02 21:03:43 2022 +0100 @@ -221,12 +221,11 @@ Ok((t, n)) => { input_types = t; input_names = n; - } + }, Err(e) => return e.to_compile_error().into(), } let input_tuple_type = quote::quote! { (#(#input_types),*) }; - match &sig.output { syn::ReturnType::Default => return_type = quote::quote! { () }, syn::ReturnType::Type(_, ty) => return_type = ty.to_token_stream(), @@ -339,6 +338,9 @@ fn check_signature( sig: &syn::Signature, ) -> Result<(Vec<Box<syn::Type>>, Vec<syn::Ident>), syn::Error> { + if sig.inputs.is_empty() { + return Ok((vec![], vec![])); + } if let syn::FnArg::Receiver(_) = sig.inputs[0] { return Err(syn::Error::new( sig.span(),