Skip to content

Commit

Permalink
docs: key type is not Clone and related improvements
Browse files Browse the repository at this point in the history
closes #147

Co-authored-by: José Manuel Peña <josemanuelp2@gmail.com>
Co-authored-by: Pranav Kesavarapu <pranavkesavarapu@gmail.com>
  • Loading branch information
3 people committed May 13, 2022
1 parent f4d3dcf commit 652c638
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 7 additions & 3 deletions README.md
Expand Up @@ -98,9 +98,10 @@ The following apply to the key type and to the function's return type:

- [`Sized`]: for one, the instrumentation stores the key in a `let` binding.
- [`'static`]: key and return values are owned by a store which is owned by a static.
- [`Clone`]: the key and return values are cloned for insertion into the store.
- [`Send`] and [`Sync`]: for parallel access.

And the return type must be [`Clone`] because it is cloned for insertion into the store.

## Store bounds

Bounds on `K` and `R` in the `store_type`'s implementation of [`MemoizationStore`] apply to the key type and the return type respectively.
Expand All @@ -116,8 +117,11 @@ use std::hash::Hash;
#[memoized(key_expr = input.clone())]
fn f<A, B>(input: A) -> B
where
A: 'static + Clone + Send + Sync + Eq + Hash,
B: 'static + Clone + Send + Sync + From<A>,
A: 'static + Send + Sync // bounds from instrumentation
+ Eq + Hash // store-specific bounds
+ Clone, // used in this function's `key_expr`
B: 'static + Send + Sync + Clone // bounds from instrumentation
+ From<A>, // used in this function's body
{
input.into()
}
Expand Down
12 changes: 12 additions & 0 deletions tests/test.rs
Expand Up @@ -33,6 +33,18 @@ fn on_a_generic_fn_in_an_impl_block() {
assert_eq!(concrete_struct.f("foo"), (false, "foo"));
}

#[test]
fn key_type_does_not_need_to_be_clone() {
#[memoized(key_expr = input)]
fn f<A, B>(input: A) -> B
where
A: 'static + Copy + Send + Sync + Eq + Hash,
B: 'static + Clone + Send + Sync + From<A>,
{
input.into()
}
}

#[test]
fn on_a_fn_in_a_trait_impl_block() {
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
Expand Down

0 comments on commit 652c638

Please sign in to comment.