Skip to content

Commit

Permalink
Merge pull request #236 from mobusoperandi/no_key_type_param
Browse files Browse the repository at this point in the history
feat: remove key_type parameter
  • Loading branch information
mightyiam authored Oct 24, 2022
2 parents f62dda4 + 56dbf31 commit 3df7acf
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 28 deletions.
14 changes: 0 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 4 additions & 7 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ fn expand(args: TokenStream, input: TokenStream) -> syn::Result<ImplItemMethod>
}
#[derive(AttributeDerive)]
struct AttrArgs {
key_type: Option<Type>,
key_expr: Expr,
store_type: Option<Type>,
store_init: Option<Expr>,
Expand All @@ -46,15 +45,13 @@ fn obtain_return_type(return_type: ReturnType) -> syn::Result<Type> {
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),
Expand Down Expand Up @@ -112,22 +109,22 @@ 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");
let type_id: ::core::any::TypeId = {
fn obtain_type_id_with_inference_hint<K: 'static, R: 'static>(_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, R, S: ::michie::MemoizationStore<K, R>>(_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();
Expand Down
3 changes: 2 additions & 1 deletion tests/compile_fail/key_type_mismatch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use michie::memoized;
use std::collections::HashMap;

#[memoized(key_type = (), key_expr = a)]
#[memoized(key_expr = a, store_type = HashMap<usize, bool>)]
fn f(a: bool) -> bool {
a
}
Expand Down
54 changes: 49 additions & 5 deletions tests/compile_fail/key_type_mismatch.stderr
Original file line number Diff line number Diff line change
@@ -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<usize, bool>)]
| ----------------------^-------------------------------------
| | |
| | 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<usize, bool>)]
| -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= 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<usize, bool>)]
| ----------------------^-------------------------------------
| | |
| | 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<R>;
| ^^^
= 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<usize, bool>)]
| ----------------------^-------------------------------------
| | |
| | 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)
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 3df7acf

Please sign in to comment.