Skip to content

Commit 2653af2

Browse files
Copilotkarthiknadig
andcommitted
Address code review feedback: add CachedValue::set method and fix pet-poetry caching logic
Co-authored-by: karthiknadig <3840081+karthiknadig@users.noreply.github.com>
1 parent 4ad900b commit 2653af2

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

crates/pet-core/src/cache.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ impl<V: Clone> CachedValue<V> {
119119
self.value.read().unwrap().clone()
120120
}
121121

122+
/// Sets the cached value.
123+
pub fn set(&self, value: V) {
124+
*self.value.write().unwrap() = Some(value);
125+
}
126+
122127
/// Returns the cached value if present, otherwise computes it using the
123128
/// provided function, caches it, and returns it.
124129
pub fn get_or_compute<F>(&self, f: F) -> V
@@ -272,4 +277,20 @@ mod tests {
272277
let result = cached.get_or_compute(|| 100);
273278
assert_eq!(result, 100);
274279
}
280+
281+
#[test]
282+
fn test_cached_value_set() {
283+
let cached: CachedValue<i32> = CachedValue::new();
284+
285+
// Initially empty
286+
assert_eq!(cached.get(), None);
287+
288+
// Set a value
289+
cached.set(42);
290+
assert_eq!(cached.get(), Some(42));
291+
292+
// Set overwrites existing value
293+
cached.set(100);
294+
assert_eq!(cached.get(), Some(100));
295+
}
275296
}

crates/pet-poetry/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,8 @@ impl Poetry {
171171
let envs = list_environments(&self.env_vars, &workspace_dirs, manager).unwrap_or_default();
172172
result.environments.extend(envs.clone());
173173

174-
// Having a value in the search result means that we have already searched for environments
175-
// We need to explicitly compute and cache since get_or_compute doesn't fit here
176-
let _ = self.search_result.get_or_compute(|| result.clone());
174+
// Cache the result (even if empty, to avoid recomputing)
175+
self.search_result.set(result.clone());
177176

178177
if result.managers.is_empty() && result.environments.is_empty() {
179178
None

0 commit comments

Comments
 (0)