From 907fe96628ffcdbe0cdad51da1a4704c5ddfb08b Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Sat, 29 Sep 2018 07:24:53 -0400 Subject: [PATCH] add a `Memoized` struct --- src/memoized.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/memoized.rs b/src/memoized.rs index 01f4e641..5710f08a 100644 --- a/src/memoized.rs +++ b/src/memoized.rs @@ -33,7 +33,12 @@ enum QueryState { InProgress, /// We have computed the query already, and here is the result. - Memoized(V), + Memoized(Memoized), +} + +#[derive(Debug)] +struct Memoized { + value: V, } impl Default for MemoizedStorage @@ -64,7 +69,7 @@ where if let Some(value) = map_read.get(key) { return match value { QueryState::InProgress => Err(CycleDetected), - QueryState::Memoized(value) => Ok(value.clone()), + QueryState::Memoized(m) => Ok(m.value.clone()), }; } @@ -81,7 +86,12 @@ where { let mut map_write = self.map.write(); - let old_value = map_write.insert(key.clone(), QueryState::Memoized(value.clone())); + let old_value = map_write.insert( + key.clone(), + QueryState::Memoized(Memoized { + value: value.clone(), + }), + ); assert!( match old_value { Some(QueryState::InProgress) => true,