Skip to content

Commit ffaf2c7

Browse files
committed
Extract testing functionality into separate module
1 parent 9c9a1a3 commit ffaf2c7

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ mod input;
9595
mod memoize;
9696
mod track;
9797

98+
#[cfg(feature = "testing")]
99+
pub mod testing;
100+
98101
pub use crate::hash::Prehashed;
99102
pub use crate::memoize::evict;
100103
pub use crate::track::{Track, Tracked, TrackedMut, Validate};
@@ -112,7 +115,4 @@ pub mod internal {
112115
pub use crate::input::{Input, Multi, assert_hashable_or_trackable};
113116
pub use crate::memoize::{Cache, CacheData, memoize, register_evictor};
114117
pub use crate::track::{Surfaces, to_parts_mut_mut, to_parts_mut_ref, to_parts_ref};
115-
116-
#[cfg(feature = "testing")]
117-
pub use crate::memoize::last_was_hit;
118118
}

src/memoize.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ use crate::input::Input;
1212
/// The global list of eviction functions.
1313
static EVICTORS: RwLock<Vec<fn(usize)>> = RwLock::new(Vec::new());
1414

15-
#[cfg(feature = "testing")]
16-
thread_local! {
17-
/// Whether the last call was a hit.
18-
static LAST_WAS_HIT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
19-
}
20-
2115
/// Execute a function or use a cached result for it.
2216
pub fn memoize<'a, In, Out, F>(
2317
cache: &Cache<In::Constraint, Out>,
@@ -37,7 +31,7 @@ where
3731

3832
// Ensure that the last call was a miss during testing.
3933
#[cfg(feature = "testing")]
40-
LAST_WAS_HIT.with(|cell| cell.set(false));
34+
crate::testing::register_miss();
4135

4236
return output;
4337
}
@@ -59,7 +53,7 @@ where
5953
input.retrack(constraint).1.join(constrained);
6054

6155
#[cfg(feature = "testing")]
62-
LAST_WAS_HIT.with(|cell| cell.set(true));
56+
crate::testing::register_hit();
6357

6458
return value.clone();
6559
}
@@ -80,7 +74,7 @@ where
8074
borrow.insert::<In>(key, constraint.take(), output.clone());
8175

8276
#[cfg(feature = "testing")]
83-
LAST_WAS_HIT.with(|cell| cell.set(false));
77+
crate::testing::register_miss();
8478

8579
output
8680
}
@@ -104,12 +98,6 @@ pub fn register_evictor(evict: fn(usize)) {
10498
EVICTORS.write().push(evict);
10599
}
106100

107-
/// Whether the last call was a hit.
108-
#[cfg(feature = "testing")]
109-
pub fn last_was_hit() -> bool {
110-
LAST_WAS_HIT.with(|cell| cell.get())
111-
}
112-
113101
/// A cache for a single memoized function.
114102
pub struct Cache<C, Out>(LazyLock<RwLock<CacheData<C, Out>>>);
115103

src/testing.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::cell::Cell;
2+
3+
thread_local! {
4+
/// Whether the last call was a cache hit.
5+
static LAST_WAS_HIT: Cell<bool> = const { Cell::new(false) };
6+
}
7+
8+
/// Whether the last call was a hit.
9+
pub fn last_was_hit() -> bool {
10+
LAST_WAS_HIT.with(|cell| cell.get())
11+
}
12+
13+
/// Marks the last call as a cache hit.
14+
pub(crate) fn register_hit() {
15+
LAST_WAS_HIT.with(|cell| cell.set(true))
16+
}
17+
18+
/// Marks the last call as a cache miss.
19+
pub(crate) fn register_miss() {
20+
LAST_WAS_HIT.with(|cell| cell.set(false))
21+
}

tests/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use serial_test::serial;
1010
macro_rules! test {
1111
(miss: $call:expr, $result:expr) => {{
1212
assert_eq!($call, $result);
13-
assert!(!comemo::internal::last_was_hit());
13+
assert!(!comemo::testing::last_was_hit());
1414
}};
1515
(hit: $call:expr, $result:expr) => {{
1616
assert_eq!($call, $result);
17-
assert!(comemo::internal::last_was_hit());
17+
assert!(comemo::testing::last_was_hit());
1818
}};
1919
}
2020

0 commit comments

Comments
 (0)