Skip to content

Commit 0706479

Browse files
Update documentation and names
1 parent a298de1 commit 0706479

File tree

9 files changed

+86
-99
lines changed

9 files changed

+86
-99
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "lru-cache-macros"
2+
name = "cache-macro"
33
version = "0.3.0"
44
authors = ["Tyler Reisinger <reisinger.tyler@gmail.com>"]
55
edition = "2018"

README.md

Lines changed: 65 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
lru-cache-macros
1+
cache-macro
22
================
33
[![Build Status](https://travis-ci.org/tylerreisinger/lru-cache-macro.svg?branch=master)](https://travis-ci.org/tylerreisinger/lru-cache-macro)
44
[![lru-cache-macros on docs.rs][docsrs-image]][docsrs]
@@ -9,14 +9,15 @@ lru-cache-macros
99
[crates-image]: https://img.shields.io/crates/v/lru-cache-macros.svg
1010
[crates]: https://crates.io/crates/lru-cache-macros/
1111

12-
An attribute procedural macro to automatically cache the result of a function given a set of inputs.
12+
A procedural macro to automatically cache the result of a function given a set of inputs.
1313

1414
# Example:
1515

1616
```rust
17-
use lru_cache_macros::lru_cache;
17+
use cache_macro::cache;
18+
use lru_cache::LruCache;
1819

19-
#[lru_cache(20)]
20+
#[cache(LruCache : LruCache::new(20))]
2021
fn fib(x: u32) -> u64 {
2122
println!("{:?}", x);
2223
if x <= 1 {
@@ -34,89 +35,83 @@ results because of the recursion hit the cache.
3435

3536
# Usage:
3637

37-
Simply place `#[lru_cache([size])]` above your function. The function must obey a few properties
38+
Simply place `#[cache(CacheType : constructor)]` above your function. The function must obey a few properties
3839
to use lru_cache:
3940

4041
* All arguments and return values must implement `Clone`.
4142
* The function may not take `self` in any form.
4243

43-
The macro will use the LruCache at `::lru_cache::LruCache` by default. This can be changed by
44-
setting the `cache_type` config variable as shown in the configuration section.
45-
4644
The `LruCache` type used must accept two generic parameters `<Args, Return>` and must support methods
47-
`get_mut(&K)` and `insert(K, V)`. The `lru-cache` crate meets these requirements.
45+
`get_mut(&K) -> Option<&mut V>` and `insert(K, V)`. The `lru-cache` (for LRU caching)
46+
and `expiring_map` (for time-to-live caching) crates currently meet these requirements.
4847

4948
Currently, this crate only works on nightly rust. However, once the 2018 edition stabilizes as well as the
5049
procedural macro diagnostic interface, it should be able to run on stable.
5150

5251
# Configuration:
5352

54-
The lru_cache macro can be configured by adding additional attributes under `#[lru_cache(size)]`.
55-
56-
All configuration attributes take the form `#[lru_config(...)]`. The available attributes are:
57-
58-
* `#[lru_config(cache_type = ...)]`
59-
60-
This allows the cache type used internally to be changed. The default is equivalent to
61-
62-
```#[lru_config(cache_type = ::lru_cache::LruCache)]```
63-
64-
* `#[lru_config(ignore_args = ...)]`
65-
66-
This allows certain arguments to be ignored for the purposes of caching. That means they are not part of the
67-
hash table key and thus should never influence the output of the function. It can be useful for diagnostic settings,
68-
returning the number of times executed, or other introspection purposes.
69-
70-
`ignore_args` takes a comma-separated list of variable identifiers to ignore.
71-
72-
### Example:
73-
```rust
74-
use lru_cache_macros::lru_cache;
75-
#[lru_cache(20)]
76-
#[lru_config(ignore_args = call_count)]
77-
fn fib(x: u64, call_count: &mut u32) -> u64 {
78-
*call_count += 1;
79-
if x <= 1 {
80-
1
81-
} else {
82-
fib(x - 1, call_count) + fib(x - 2, call_count)
83-
}
53+
The lru_cache macro can be configured by adding additional attributes under `#[cache(...)]`.
54+
55+
All configuration attributes take the form `#[cache_cfg(...)]`. The available attributes are:
56+
57+
* `#[cache_cfg(ignore_args = ...)]`
58+
59+
This allows certain arguments to be ignored for the purposes of caching. That means they are not part of the
60+
hash table key and thus should never influence the output of the function. It can be useful for diagnostic settings,
61+
returning the number of times executed, or other introspection purposes.
62+
63+
`ignore_args` takes a comma-separated list of variable identifiers to ignore.
64+
65+
### Example:
66+
```rust
67+
use cache_macro::cache;
68+
use lru_cache::LruCache;
69+
#[cache(LruCache : LruCache::new(20))]
70+
#[cache_cfg(ignore_args = call_count)]
71+
fn fib(x: u64, call_count: &mut u32) -> u64 {
72+
*call_count += 1;
73+
if x <= 1 {
74+
1
75+
} else {
76+
fib(x - 1, call_count) + fib(x - 2, call_count)
8477
}
78+
}
79+
80+
let mut call_count = 0;
81+
assert_eq!(fib(39, &mut call_count), 102_334_155);
82+
assert_eq!(call_count, 40);
83+
```
84+
85+
The `call_count` argument can vary, caching is only done based on `x`.
86+
87+
* `#[cache_cfg(thread_local)]`
8588

86-
let mut call_count = 0;
87-
assert_eq!(fib(39, &mut call_count), 102_334_155);
88-
assert_eq!(call_count, 40);
89-
```
90-
91-
The `call_count` argument can vary, caching is only done based on `x`.
92-
93-
* `#[lru_config(thread_local)]`
94-
95-
Store the cache in thread-local storage instead of global static storage. This avoids the overhead of Mutex locking,
96-
but each thread will be given its own cache, and all caching will not affect any other thread.
97-
98-
Expanding on the first example:
99-
100-
```rust
101-
use lru_cache_macros::lru_cache;
102-
103-
#[lru_cache(20)]
104-
#[lru_config(thread_local)]
105-
fn fib(x: u32) -> u64 {
106-
println!("{:?}", x);
107-
if x <= 1 {
108-
1
109-
} else {
110-
fib(x - 1) + fib(x - 2)
111-
}
89+
Store the cache in thread-local storage instead of global static storage. This avoids the overhead of Mutex locking,
90+
but each thread will be given its own cache, and all caching will not affect any other thread.
91+
92+
Expanding on the first example:
93+
94+
```rust
95+
use cache_macro::cache;
96+
use lru_cache::LruCache;
97+
98+
#[cache(LruCache : LruCache::new(20))]
99+
#[cache_cfg(thread_local)]
100+
fn fib(x: u32) -> u64 {
101+
println!("{:?}", x);
102+
if x <= 1 {
103+
1
104+
} else {
105+
fib(x - 1) + fib(x - 2)
112106
}
107+
}
113108

114-
assert_eq!(fib(19), 6765);
115-
```
109+
assert_eq!(fib(19), 6765);
110+
```
116111

117112
# Details
118-
The created cache is stored as a static variable protected by a mutex unless the `#[lru_config(thread_local)]` configuration
119-
is added.
113+
The created cache is stored as a static variable protected by a mutex unless the `#[cache_cfg(thread_local)]`
114+
configuration is added.
120115

121116
With the default settings, the fibonacci example will generate the following code:
122117

@@ -132,7 +127,7 @@ fn fib(x: u32) -> u64 {
132127
static ref cache: Mutex<::lru_cache::LruCache<(u32,), u64>> =
133128
Mutex::new(::lru_cache::LruCache::new(20usize));
134129
}
135-
130+
136131
let cloned_args = (x.clone(),);
137132
let mut cache_unlocked = cache.lock().unwrap();
138133
let stored_result = cache_unlocked.get_mut(&cloned_args);

src/lib.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
//! lru-cache-macros
1+
//! cache-macro
22
//! ================
33
//!
4-
//! An attribute procedural macro to automatically cache the result of a function given a set of inputs.
4+
//! A procedural macro to automatically cache the result of a function given a set of inputs.
55
//!
66
//! # Example:
77
//!
88
//! ```rust
9-
//! use lru_cache_macros::cache;
9+
//! use cache_macro::cache;
1010
//! use lru_cache::LruCache;
1111
//!
1212
//! #[cache(LruCache : LruCache::new(20))]
@@ -27,34 +27,26 @@
2727
//!
2828
//! # Usage:
2929
//!
30-
//! Simply place `#[lru_cache([size])]` above your function. The function must obey a few properties
30+
//! Simply place `#[cache(CacheType : constructor)]` above your function. The function must obey a few properties
3131
//! to use lru_cache:
3232
//!
3333
//! * All arguments and return values must implement `Clone`.
3434
//! * The function may not take `self` in any form.
3535
//!
36-
//! The macro will use the LruCache at `::lru_cache::LruCache` by default. This can be changed by
37-
//! setting the `cache_type` config variable as shown in the configuration section.
38-
//!
3936
//! The `LruCache` type used must accept two generic parameters `<Args, Return>` and must support methods
40-
//! `get_mut(&K)` and `insert(K, V)`. The `lru-cache` crate meets these requirements.
37+
//! `get_mut(&K) -> Option<&mut V>` and `insert(K, V)`. The `lru-cache` (for LRU caching)
38+
//! and `expiring_map` (for time-to-live caching) crates currently meet these requirements.
4139
//!
4240
//! Currently, this crate only works on nightly rust. However, once the 2018 edition stabilizes as well as the
4341
//! procedural macro diagnostic interface, it should be able to run on stable.
4442
//!
4543
//! # Configuration:
4644
//!
47-
//! The lru_cache macro can be configured by adding additional attributes under `#[lru_cache(size)]`.
48-
//!
49-
//! All configuration attributes take the form `#[lru_config(...)]`. The available attributes are:
50-
//!
51-
//! * `#[lru_config(cache_type = ...)]`
52-
//!
53-
//! This allows the cache type used internally to be changed. The default is equivalent to
45+
//! The lru_cache macro can be configured by adding additional attributes under `#[cache(...)]`.
5446
//!
55-
//! ```#[lru_config(cache_type = ::lru_cache::LruCache)]```
47+
//! All configuration attributes take the form `#[cache_cfg(...)]`. The available attributes are:
5648
//!
57-
//! * `#[lru_config(ignore_args = ...)]`
49+
//! * `#[cache_cfg(ignore_args = ...)]`
5850
//!
5951
//! This allows certain arguments to be ignored for the purposes of caching. That means they are not part of the
6052
//! hash table key and thus should never influence the output of the function. It can be useful for diagnostic settings,
@@ -64,7 +56,7 @@
6456
//!
6557
//! ### Example:
6658
//! ```rust
67-
//! use lru_cache_macros::cache;
59+
//! use cache_macro::cache;
6860
//! use lru_cache::LruCache;
6961
//! #[cache(LruCache : LruCache::new(20))]
7062
//! #[cache_cfg(ignore_args = call_count)]
@@ -84,15 +76,15 @@
8476
//!
8577
//! The `call_count` argument can vary, caching is only done based on `x`.
8678
//!
87-
//! * `#[lru_config(thread_local)]`
79+
//! * `#[cache_cfg(thread_local)]`
8880
//!
8981
//! Store the cache in thread-local storage instead of global static storage. This avoids the overhead of Mutex locking,
9082
//! but each thread will be given its own cache, and all caching will not affect any other thread.
9183
//!
9284
//! Expanding on the first example:
9385
//!
9486
//! ```rust
95-
//! use lru_cache_macros::cache;
87+
//! use cache_macro::cache;
9688
//! use lru_cache::LruCache;
9789
//!
9890
//! #[cache(LruCache : LruCache::new(20))]
@@ -110,8 +102,8 @@
110102
//! ```
111103
//!
112104
//! # Details
113-
//! The created cache is stored as a static variable protected by a mutex unless the `#[lru_config(thread_local)]` configuration
114-
//! is added.
105+
//! The created cache is stored as a static variable protected by a mutex unless the `#[cache_cfg(thread_local)]`
106+
//! configuration is added.
115107
//!
116108
//! With the default settings, the fibonacci example will generate the following code:
117109
//!

tests/args_as_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lru_cache_macros::cache;
1+
use cache_macro::cache;
22
use lru_cache::LruCache;
33

44
#[test]

tests/cache_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lru_cache_macros::cache;
1+
use cache_macro::cache;
22
use std::time::Duration;
33
use expiring_map::ExpiringMap;
44
use std::hash::Hash;

tests/clone_only_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lru_cache_macros::cache;
1+
use cache_macro::cache;
22
use lru_cache::LruCache;
33

44
use std::ops;

tests/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lru_cache_macros::cache;
1+
use cache_macro::cache;
22
use lru_cache::LruCache;
33

44
use std::thread;

tests/multiple_args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lru_cache_macros::cache;
1+
use cache_macro::cache;
22
use lru_cache::LruCache;
33

44
#[test]

tests/multiple_caches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lru_cache_macros::cache;
1+
use cache_macro::cache;
22
use lru_cache::LruCache;
33

44
#[test]

0 commit comments

Comments
 (0)