Skip to content

Commit 136ca65

Browse files
Rename lru_config => cache_cfg; Remove cache_type config attribute
1 parent e5385b4 commit 136ca65

File tree

3 files changed

+7
-46
lines changed

3 files changed

+7
-46
lines changed

src/config.rs

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ use std::collections::HashSet;
44
use proc_macro2;
55
use syn::{self, Token, parenthesized};
66
use syn::parse::{Parse, ParseStream};
7-
use syn::punctuated::Punctuated;
87

98
use crate::error::{DiagnosticError, Result};
109

1110
pub struct Config {
12-
pub cache_type: syn::Path,
1311
pub ignore_args: HashSet<syn::Ident>,
1412
pub use_tls: bool,
1513
}
@@ -18,16 +16,13 @@ struct IgnoreArgsAttrib {
1816
ignore_args: HashSet<syn::Ident>,
1917
}
2018

21-
struct CacheTypeAttrib {
22-
type_path: syn::Path,
23-
}
24-
2519
enum ConfigAttrib {
26-
CacheType(CacheTypeAttrib),
2720
IgnoreArgs(IgnoreArgsAttrib),
2821
UseTls,
2922
}
3023

24+
const CONFIG_ATTRIBUTE_NAME: &'static str = "cache_cfg";
25+
3126
impl Config {
3227
// Parse any additional attributes present after `lru_cache` and return a configuration object
3328
// created from their contents. Additionally, return any attributes that were not handled here.
@@ -38,7 +33,7 @@ impl Config {
3833
for attrib in attribs {
3934
let segs = &attrib.path.segments;
4035
if segs.len() > 0 {
41-
if segs[0].ident.to_string() == "lru_config" {
36+
if segs[0].ident == CONFIG_ATTRIBUTE_NAME {
4237
let tokens = attrib.tts.clone();
4338
let parsed = syn::parse2::<ConfigAttrib>(tokens);
4439
match parsed {
@@ -60,7 +55,6 @@ impl Config {
6055

6156
for parsed_attrib in parsed_attributes {
6257
match parsed_attrib {
63-
ConfigAttrib::CacheType(val) => config.cache_type = val.type_path,
6458
ConfigAttrib::IgnoreArgs(val) => config.ignore_args = val.ignore_args,
6559
ConfigAttrib::UseTls => config.use_tls = true,
6660
}
@@ -73,43 +67,19 @@ impl Config {
7367
impl Default for Config {
7468
fn default() -> Config {
7569
Config {
76-
cache_type: make_path_from_segments(&["lru_cache", "LruCache"], true, proc_macro2::Span::call_site()),
7770
ignore_args: HashSet::new(),
7871
use_tls: false,
7972
}
8073
}
8174
}
8275

83-
fn make_path_from_segments(segments: &[&str], has_leading_colon: bool, span: proc_macro2::Span) -> syn::Path {
84-
let leading_colon = if has_leading_colon {
85-
Some(syn::token::Colon2 { spans: [span; 2] })
86-
} else {
87-
None
88-
};
89-
let mut segments_ast = Punctuated::<syn::PathSegment, Token![::]>::new();
90-
for name in segments {
91-
let ident = syn::Ident::new(name, span);
92-
let seg = syn::PathSegment {
93-
ident,
94-
arguments: syn::PathArguments::None,
95-
};
96-
segments_ast.push(seg);
97-
}
98-
99-
syn::Path {
100-
leading_colon,
101-
segments: segments_ast,
102-
}
103-
}
104-
10576
impl Parse for ConfigAttrib {
10677
fn parse(input: ParseStream) -> syn::parse::Result<Self> {
10778
let content;
10879
let _paren = parenthesized!(content in input);
10980
let name = content.parse::<syn::Ident>()?;
11081

11182
match &name.to_string()[..] {
112-
"cache_type" => Ok(ConfigAttrib::CacheType(content.parse::<CacheTypeAttrib>()?)),
11383
"ignore_args" => Ok(ConfigAttrib::IgnoreArgs(content.parse::<IgnoreArgsAttrib>()?)),
11484
"thread_local" => Ok(ConfigAttrib::UseTls),
11585
_ => Err(syn::parse::Error::new(
@@ -119,15 +89,6 @@ impl Parse for ConfigAttrib {
11989
}
12090
}
12191

122-
impl Parse for CacheTypeAttrib {
123-
fn parse(input: ParseStream) -> syn::parse::Result<Self> {
124-
input.parse::<Token![=]>()?;
125-
Ok(CacheTypeAttrib {
126-
type_path: input.parse::<syn::Path>()?
127-
})
128-
}
129-
}
130-
13192
impl Parse for IgnoreArgsAttrib {
13293
fn parse(input: ParseStream) -> syn::parse::Result<Self> {
13394
input.parse::<Token![=]>()?;

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
//! use lru_cache_macros::lru_cache as cache;
6868
//! use lru_cache::LruCache;
6969
//! #[cache(LruCache : LruCache::new(20))]
70-
//! #[lru_config(ignore_args = call_count)]
70+
//! #[cache_cfg(ignore_args = call_count)]
7171
//! fn fib(x: u64, call_count: &mut u32) -> u64 {
7272
//! *call_count += 1;
7373
//! if x <= 1 {
@@ -96,7 +96,7 @@
9696
//! use lru_cache::LruCache;
9797
//!
9898
//! #[cache(LruCache : LruCache::new(20))]
99-
//! #[lru_config(thread_local)]
99+
//! #[cache_cfg(thread_local)]
100100
//! fn fib(x: u32) -> u64 {
101101
//! println!("{:?}", x);
102102
//! if x <= 1 {

tests/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::time;
88
#[test]
99
fn thread_local_ignore_args() {
1010
#[cache(LruCache : LruCache::new(20))]
11-
#[lru_config(ignore_args = call_count)]
12-
#[lru_config(thread_local)]
11+
#[cache_cfg(ignore_args = call_count)]
12+
#[cache_cfg(thread_local)]
1313
fn fib(x: u32, call_count: &mut u32) -> u64 {
1414
*call_count += 1;
1515
if x <= 1 {

0 commit comments

Comments
 (0)