diff --git a/README.md b/README.md index 4f603ef..19ad2f8 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ michie (sounds like Mickey) — an attribute macro that adds [memoization] to a 1. [Features](#features) 1. [Non-features](#non-features) 1. [key_expr](#key_expr) -1. [key_type](#key_type) 1. [store_type](#store_type) 1. [store_init](#store_init) 1. [Store inference and the default store](#store-inference-and-the-default-store) @@ -65,19 +64,6 @@ fn f(input: usize) -> usize { } ``` -# key_type - -While the type of the key supports inference, it may also be specified using the `key_type` argument: - -```rust -use michie::memoized; -#[memoized(key_type = u64, key_expr = input.into())] -fn f(input: u32) -> u32 { - // expensive calculation - # unimplemented!() -} -``` - # store_type A store type may be provided via the `store_type` argument. diff --git a/macro/src/lib.rs b/macro/src/lib.rs index 0dbcc75..5ca5929 100644 --- a/macro/src/lib.rs +++ b/macro/src/lib.rs @@ -29,7 +29,6 @@ fn expand(args: TokenStream, input: TokenStream) -> syn::Result } #[derive(AttributeDerive)] struct AttrArgs { - key_type: Option, key_expr: Expr, store_type: Option, store_init: Option, @@ -46,15 +45,13 @@ fn obtain_return_type(return_type: ReturnType) -> syn::Result { fn expand_fn_block(original_fn_block: Block, return_type: Type, attr_args: AttrArgs) -> Block { let AttrArgs { key_expr, - key_type, store_type, store_init, } = attr_args; let key = Ident::new("key", Span::mixed_site().located_at(key_expr.span())); let key_ref: Expr = parse_quote_spanned!(Span::mixed_site().located_at(key_expr.span())=> &#key); - let key_type = key_type.unwrap_or_else(|| parse_quote! { _ }); - let default_store_type = parse_quote!(::std::collections::HashMap::<#key_type, #return_type>); + let default_store_type = parse_quote!(::std::collections::HashMap::<_, #return_type>); let default_store_init = parse_quote!(::core::default::Default::default()); let (store_type, store_init) = match (store_type, store_init) { (None, None) => (default_store_type, default_store_init), @@ -112,7 +109,7 @@ fn expand_fn_block(original_fn_block: Block, return_type: Type, attr_args: AttrA // 2. Was certainly initialized in the same `Once::call_once`. STORES.assume_init_ref() }; - let #key: #key_type = #key_expr; + let #key = #key_expr; let mut type_map_mutex_guard: ::std::sync::MutexGuard<#type_map_type> = type_map_mutex .lock() .expect("handling of poisoning is not supported"); @@ -120,14 +117,14 @@ fn expand_fn_block(original_fn_block: Block, return_type: Type, attr_args: AttrA fn obtain_type_id_with_inference_hint(_k: &K) -> ::core::any::TypeId { ::core::any::TypeId::of::<(K, R)>() } - obtain_type_id_with_inference_hint::<#key_type, #return_type>(#key_ref) + obtain_type_id_with_inference_hint::<_, #return_type>(#key_ref) }; let store: &::std::boxed::Box<#store_trait_object> = type_map_mutex_guard .entry(type_id) .or_insert_with(|| { let store: #store_type = #store_init; fn inference_hint>(_k: &K, _s: &S) {} - inference_hint::<#key_type, #return_type, #store_type>(#key_ref, &store); + inference_hint::<_, #return_type, #store_type>(#key_ref, &store); ::std::boxed::Box::new(store) }); let store: &#store_trait_object = store.as_ref(); diff --git a/tests/compile_fail/key_type_mismatch.rs b/tests/compile_fail/key_type_mismatch.rs index 71fe7a7..9ffa9ee 100644 --- a/tests/compile_fail/key_type_mismatch.rs +++ b/tests/compile_fail/key_type_mismatch.rs @@ -1,6 +1,7 @@ use michie::memoized; +use std::collections::HashMap; -#[memoized(key_type = (), key_expr = a)] +#[memoized(key_expr = a, store_type = HashMap)] fn f(a: bool) -> bool { a } diff --git a/tests/compile_fail/key_type_mismatch.stderr b/tests/compile_fail/key_type_mismatch.stderr index 533008a..1f77a26 100644 --- a/tests/compile_fail/key_type_mismatch.stderr +++ b/tests/compile_fail/key_type_mismatch.stderr @@ -1,7 +1,51 @@ error[E0308]: mismatched types - --> tests/compile_fail/key_type_mismatch.rs:3:38 + --> tests/compile_fail/key_type_mismatch.rs:4:23 | -3 | #[memoized(key_type = (), key_expr = a)] - | -- ^ expected `()`, found `bool` - | | - | expected due to this +4 | #[memoized(key_expr = a, store_type = HashMap)] + | ----------------------^------------------------------------- + | | | + | | expected `usize`, found `bool` + | arguments to this function are incorrect + | + = note: expected reference `&usize` + found reference `&bool` +note: function defined here + --> tests/compile_fail/key_type_mismatch.rs:4:1 + | +4 | #[memoized(key_expr = a, store_type = HashMap)] + | -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: this error originates in the attribute macro `memoized` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0308]: mismatched types + --> tests/compile_fail/key_type_mismatch.rs:4:23 + | +4 | #[memoized(key_expr = a, store_type = HashMap)] + | ----------------------^------------------------------------- + | | | + | | expected `usize`, found `bool` + | arguments to this function are incorrect + | + = note: expected reference `&usize` + found reference `&bool` +note: associated function defined here + --> src/lib.rs + | + | fn get(&self, input: &I) -> Option; + | ^^^ + = note: this error originates in the attribute macro `memoized` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0308]: mismatched types + --> tests/compile_fail/key_type_mismatch.rs:4:23 + | +4 | #[memoized(key_expr = a, store_type = HashMap)] + | ----------------------^------------------------------------- + | | | + | | expected `usize`, found `bool` + | arguments to this function are incorrect + | +note: associated function defined here + --> src/lib.rs + | + | fn insert(&mut self, input: I, return_value: R) -> R; + | ^^^^^^ + = note: this error originates in the attribute macro `memoized` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/test.rs b/tests/test.rs index 686042a..f91023e 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -205,7 +205,7 @@ fn trait_functions_are_called_explicitly() { None } } - #[memoized(key_type = (), key_expr = (), store_type = Store)] + #[memoized(key_expr = (), store_type = Store)] fn f() -> () {} f(); }