Skip to content

Commit 10f31ea

Browse files
committed
allow memo on methods
1 parent e17029d commit 10f31ea

File tree

7 files changed

+285
-28
lines changed

7 files changed

+285
-28
lines changed

Cargo.lock

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ clap = { version = "4.5.18", features = ["derive"] }
1414
colored = "2.0.4"
1515
colorize = "0.1.0"
1616
crossbeam = "0.8"
17+
darling = "0.21.1"
1718
dashmap = "6.0.1"
1819
lazy_static = "1.4"
1920
log = { version = "0.4.17", features = ["kv_unstable", "kv_unstable_std"] }
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::sync::atomic::{AtomicUsize, Ordering};
2+
3+
use pico::{Database, SourceId, Storage};
4+
use pico_macros::{memo, Db, Source};
5+
6+
static FIRST_LETTER_COUNTER: AtomicUsize = AtomicUsize::new(0);
7+
8+
#[derive(Db, Default)]
9+
struct TestDatabase {
10+
pub storage: Storage<Self>,
11+
}
12+
13+
impl TestDatabase {
14+
#[memo]
15+
fn first_letter(&self, input_id: SourceId<Input>) -> char {
16+
FIRST_LETTER_COUNTER.fetch_add(1, Ordering::SeqCst);
17+
let input = self.get(input_id);
18+
input.value.chars().next().unwrap()
19+
}
20+
}
21+
22+
#[test]
23+
fn memo_on_db_method() {
24+
let mut db = TestDatabase::default();
25+
26+
let input_id = db.set(Input {
27+
key: "key",
28+
value: "asdf".to_string(),
29+
});
30+
31+
assert_eq!(*db.first_letter(input_id), 'a');
32+
assert_eq!(FIRST_LETTER_COUNTER.load(Ordering::SeqCst), 1);
33+
34+
db.set(Input {
35+
key: "key",
36+
value: "qwer".to_string(),
37+
});
38+
39+
assert_eq!(*db.first_letter(input_id), 'q');
40+
assert_eq!(FIRST_LETTER_COUNTER.load(Ordering::SeqCst), 2);
41+
}
42+
43+
#[derive(Debug, Clone, PartialEq, Eq, Source)]
44+
struct Input {
45+
#[key]
46+
pub key: &'static str,
47+
pub value: String,
48+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use std::sync::atomic::{AtomicUsize, Ordering};
2+
3+
use pico::{Database, SourceId, Storage};
4+
use pico_macros::{memo, Db, Source};
5+
6+
static FIRST_LETTER_COUNTER: AtomicUsize = AtomicUsize::new(0);
7+
8+
#[derive(Db, Default)]
9+
struct TestDatabase {
10+
pub storage: Storage<Self>,
11+
}
12+
13+
struct TestStruct;
14+
15+
impl TestStruct {
16+
#[memo(db = test_db)]
17+
fn first_letter(&self, test_db: &TestDatabase, input_id: SourceId<Input>) -> char {
18+
FIRST_LETTER_COUNTER.fetch_add(1, Ordering::SeqCst);
19+
let input = test_db.get(input_id);
20+
input.value.chars().next().unwrap()
21+
}
22+
}
23+
24+
#[test]
25+
fn memo_on_struct_method() {
26+
let mut db = TestDatabase::default();
27+
28+
let test_struct = TestStruct {};
29+
30+
let input_id = db.set(Input {
31+
key: "key",
32+
value: "asdf".to_string(),
33+
});
34+
35+
assert_eq!(*test_struct.first_letter(&db, input_id), 'a');
36+
assert_eq!(FIRST_LETTER_COUNTER.load(Ordering::SeqCst), 1);
37+
38+
db.set(Input {
39+
key: "key",
40+
value: "qwer".to_string(),
41+
});
42+
43+
assert_eq!(*test_struct.first_letter(&db, input_id), 'q');
44+
assert_eq!(FIRST_LETTER_COUNTER.load(Ordering::SeqCst), 2);
45+
}
46+
47+
#[derive(Debug, Clone, PartialEq, Eq, Source)]
48+
struct Input {
49+
#[key]
50+
pub key: &'static str,
51+
pub value: String,
52+
}

crates/pico/tests/params/memo_ref_never_cloned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ fn get_output(_db: &TestDatabase) -> Output {
3939
}
4040

4141
#[memo]
42-
fn consume_output(db: &TestDatabase, _output: MemoRef<Output>) {}
42+
fn consume_output(_db: &TestDatabase, _output: MemoRef<Output>) {}

crates/pico_macros/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition.workspace = true
55
license.workspace = true
66

77
[dependencies]
8+
darling = { workspace = true }
89
proc-macro2 = { workspace = true }
910
quote = { workspace = true }
1011
syn = { workspace = true }

0 commit comments

Comments
 (0)