use graphene_core::{Cache, Node}; use once_cell::sync::OnceCell; /// Caches the output of a given Node and acts as a proxy pub struct CacheNode { cache: OnceCell, } impl<'n, T> Node for &'n CacheNode { type Output = &'n T; fn eval(self, input: T) -> Self::Output { self.cache.get_or_init(|| { trace!("Creating new cache node"); input }) } } impl Node for CacheNode { type Output = T; fn eval(self, input: T) -> Self::Output { input } } impl CacheNode { pub fn new() -> CacheNode { CacheNode { cache: OnceCell::new() } } } impl Cache for CacheNode { fn clear(&mut self) { self.cache = OnceCell::new(); } }