Mercurial > lbo > hg > lazycall
changeset 2:2b71ed370156
rustfmt
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Fri, 20 Nov 2020 23:26:10 +0100 |
parents | babb2f9cb5ad |
children | 86c31f04fd15 |
files | examples/test_functions.rs lazycall_support/src/lib.rs src/lib.rs |
diffstat | 3 files changed, 36 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/test_functions.rs Fri Nov 20 23:20:03 2020 +0100 +++ b/examples/test_functions.rs Fri Nov 20 23:26:10 2020 +0100 @@ -9,26 +9,34 @@ #[lazy] fn test_function_someargs(a: i32, b: usize, c: TestStruct) -> bool { - a%2 == 0 + a % 2 == 0 } #[lazy] fn test_function_onearg(a: i32) -> bool { - a%2 == 0 + a % 2 == 0 } impl TestStruct { #[lazy] fn test_receiving_func(&self, a: i32) -> bool { - (self.0+a)%2 == 0 + (self.0 + a) % 2 == 0 } } +fn a_very_expensive_function() -> i32 { + use std::thread; + use std::time; + thread::sleep(time::Duration::new(2, 0)); + 32 +} fn main() { - - println!("{}", test_function_onearg(32));; - println!("lazy: {}", lazycall!(test_function_onearg(32))); + println!("{}", test_function_onearg(a_very_expensive_function())); + println!( + "lazy: {}", + lazycall!(test_function_onearg(a_very_expensive_function())) + ); let mystruct = TestStruct(32); println!("{}", mystruct.test_receiving_func(33));
--- a/lazycall_support/src/lib.rs Fri Nov 20 23:20:03 2020 +0100 +++ b/lazycall_support/src/lib.rs Fri Nov 20 23:26:10 2020 +0100 @@ -1,4 +1,3 @@ - pub struct LazyVal<'a, Result>(&'a mut dyn FnMut() -> Result); impl<'a, Result> LazyVal<'a, Result> { @@ -17,7 +16,6 @@ } } - #[cfg(test)] mod tests { #[test]
--- a/src/lib.rs Fri Nov 20 23:20:03 2020 +0100 +++ b/src/lib.rs Fri Nov 20 23:26:10 2020 +0100 @@ -1,12 +1,12 @@ +use std::iter::IntoIterator; use std::ops::Deref; -use std::iter::IntoIterator; use proc_macro; use syn::parse_macro_input; -fn tuple_type_from_types<TT: quote::ToTokens, T: Deref<Target=TT>>( - types: &[T]) -> syn::TypeTuple { - +fn tuple_type_from_types<TT: quote::ToTokens, T: Deref<Target = TT>>( + types: &[T], +) -> syn::TypeTuple { if types.len() == 1 { let typ = types[0].deref(); syn::parse_quote! { (#typ,) } @@ -76,7 +76,8 @@ let receiver = args_iter.next().unwrap(); let tuty = tuple_type_from_sig(args_iter); // We are not evaluating for the receiver, thus one fewer argument. - let args_ixs: Vec<syn::Index> = (0..newsig.inputs.len()-1).map(syn::Index::from).collect(); + let args_ixs: Vec<syn::Index> = + (0..newsig.inputs.len() - 1).map(syn::Index::from).collect(); (quote::quote! { #parsedfn @@ -90,7 +91,8 @@ Some(syn::FnArg::Typed(_)) => { // This is a freestanding function let tuty = tuple_type_from_sig(&newsig.inputs); - let args_ixs: Vec<syn::Index> = (0..newsig.inputs.len()).map(syn::Index::from).collect(); + let args_ixs: Vec<syn::Index> = + (0..newsig.inputs.len()).map(syn::Index::from).collect(); (quote::quote! { #parsedfn @@ -118,8 +120,8 @@ #[proc_macro] pub fn lazycall(item: proc_macro::TokenStream) -> proc_macro::TokenStream { + use quote::ToTokens; use syn::spanned::Spanned; - use quote::ToTokens; let funcall = parse_macro_input!(item as syn::Expr); match funcall { @@ -140,7 +142,7 @@ } (quote::quote! { #receiver.#renamed_method(::lazycall_support::LazyVal::new(&mut || #tuple )) }).into() - }, + } syn::Expr::Call(c) => { let func = &c.func; let renamed_func; @@ -149,8 +151,11 @@ let path = &ep.path; let last = path.segments.last().unwrap(); renamed_func = lazied_method_name(&last.ident); - }, - other => { println!("{:?}", other.to_token_stream()); unimplemented!() }, + } + other => { + println!("{:?}", other.to_token_stream()); + unimplemented!() + } } let args = (&c.args).into_iter().collect::<Vec<&syn::Expr>>(); @@ -171,7 +176,12 @@ } let result = (quote::quote! { #renamed_func(::lazycall_support::LazyVal::new(&mut || #tuple )) }).into(); result - }, - _ => syn::Error::new(funcall.span(), "syntax for lazycall!: lazycall!([receiver.]myfunction(a, b, c));").to_compile_error().into(), + } + _ => syn::Error::new( + funcall.span(), + "syntax for lazycall!: lazycall!([receiver.]myfunction(a, b, c));", + ) + .to_compile_error() + .into(), } }