Make node trait consume self

This commit is contained in:
Dennis
2022-08-19 18:58:17 +02:00
committed by Keavon Chambers
parent fa0b896825
commit ce9e02f36d
10 changed files with 286 additions and 188 deletions

View File

@@ -3,34 +3,26 @@ use once_cell::sync::OnceCell;
use std::marker::PhantomData;
/// Caches the output of a given Node and acts as a proxy
pub struct CacheNode<'n, CachedNode: Node<'n, I>, I> {
pub struct CacheNode<CachedNode: Node<I>, I> {
node: CachedNode,
cache: OnceCell<CachedNode::Output>,
_phantom: PhantomData<&'n ()>,
}
impl<'n, CashedNode: Node<'n, I>, I> Node<'n, I> for CacheNode<'n, CashedNode, I>
where
CashedNode::Output: 'n,
{
impl<'n, CashedNode: Node<I> + Copy, I> Node<I> for &'n CacheNode<CashedNode, I> {
type Output = &'n CashedNode::Output;
fn eval(&'n self, input: I) -> Self::Output {
fn eval(self, input: I) -> Self::Output {
self.cache.get_or_init(|| self.node.eval(input))
}
}
impl<'n, CachedNode: Node<'n, I>, I> CacheNode<'n, CachedNode, I> {
impl<'n, CachedNode: Node<I>, I> CacheNode<CachedNode, I> {
pub fn clear(&'n mut self) {
self.cache = OnceCell::new();
}
pub fn new(node: CachedNode) -> CacheNode<'n, CachedNode, I> {
CacheNode {
node,
cache: OnceCell::new(),
_phantom: PhantomData,
}
pub fn new(node: CachedNode) -> CacheNode<CachedNode, I> {
CacheNode { node, cache: OnceCell::new() }
}
}
impl<'n, CachedNode: Node<'n, I>, I> Cache for CacheNode<'n, CachedNode, I> {
impl<CachedNode: Node<I>, I> Cache for CacheNode<CachedNode, I> {
fn clear(&mut self) {
self.cache = OnceCell::new();
}