Skip to content

Commit

Permalink
feat: add lifetime support
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
andrewgazelka committed Jan 18, 2025
1 parent 4c2dbf1 commit ea00b94
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/smart-cache-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ proc-macro2 = "1.0"
sha2 = "0.11.0-pre.4"

[dev-dependencies]
trybuild = "1.0"
trybuild = "1.0.89"
smart-cache = { path = "../smart-cache" }
rkyv = { version = "0.8.9" }
10 changes: 9 additions & 1 deletion crates/smart-cache-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ fn check_for_mutable_refs(
Ok(())
}

fn get_param_type(ty: &Type) -> &Type {
if let Type::Reference(type_ref) = ty {
&type_ref.elem
} else {
ty
}
}

#[proc_macro_attribute]
pub fn cached(_attr: TokenStream, item: TokenStream) -> TokenStream {
let input_fn = parse_macro_input!(item as ItemFn);
Expand Down Expand Up @@ -99,7 +107,7 @@ pub fn cached(_attr: TokenStream, item: TokenStream) -> TokenStream {
let param_types: Vec<_> = fn_inputs
.iter()
.filter_map(|arg| match arg {
FnArg::Typed(pat_type) => Some(&pat_type.ty),
FnArg::Typed(pat_type) => Some(get_param_type(&pat_type.ty)),
_ => None,
})
.collect();
Expand Down
8 changes: 3 additions & 5 deletions crates/smart-cache-macro/tests/success/ref.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use smart_cache_macro::cached;

#[cached]
fn pure_function(x: &i32) -> i32 {
*x + 1
fn takes_ref(s: String) -> String {
s
}

fn main() {
let x = 42;
assert_eq!(pure_function(&x), 43);
assert_eq!(pure_function(&x), 43); // Should hit cache
takes_ref("hello".to_string());
}
11 changes: 11 additions & 0 deletions crates/smart-cache-macro/tests/success/unit_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use smart_cache_macro::cached;

#[cached]
fn unit(s: String) -> String {
s
}

fn main() {
let result = unit("hello".to_string());
assert_eq!(result, "hello");
}
2 changes: 1 addition & 1 deletion crates/smart-cache-macro/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ fn compile_tests() {
t.compile_fail("tests/compile-fail/*.rs");

// skip for now as refs are broken (#1)
// t.pass("tests/success/*.rs");
t.pass("tests/success/*.rs");
}
4 changes: 2 additions & 2 deletions crates/smart-cache/tests/something.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use smart_cache_macro::cached;

#[cached]
fn expensive_computation(x: String, y: i32) -> String {
fn expensive_computation(x: &str, y: i32) -> String {
use std::{thread, time::Duration};

thread::sleep(Duration::from_secs(3));
Expand All @@ -11,6 +11,6 @@ fn expensive_computation(x: String, y: i32) -> String {

#[test]
fn test_cached() {
let x = expensive_computation("hello".to_string(), 2);
let x = expensive_computation("hello", 2);
println!("{x}");
}

0 comments on commit ea00b94

Please sign in to comment.