Replace GAT with lifetime on trait in node graph

This commit is contained in:
Dennis
2022-04-03 10:44:02 +02:00
committed by Keavon Chambers
parent 3243b80cf2
commit cb337fd338
10 changed files with 185 additions and 217 deletions

View File

@@ -1,25 +1,23 @@
use graphene_core::{ExecPtr, Node};
use graphene_core::Node;
use once_cell::sync::OnceCell;
use std::{any::Any, borrow::Borrow, ops::Deref};
/// Caches the output of a given Node and acts as a proxy
pub struct CacheNode<'n, 'c, CachedNode: Node + 'c> {
pub struct CacheNode<'n, CachedNode: Node<'n, Input>, Input> {
node: &'n CachedNode,
cache: OnceCell<CachedNode::Output<'c>>,
cache: OnceCell<CachedNode::Output>,
}
impl<'n: 'c, 'c, CashedNode: Node> Node for CacheNode<'n, 'c, CashedNode> {
type Output<'a> = &'a CashedNode::Output<'c> where 'c: 'a;
type Input<'a> = CashedNode::Input<'c> where 'c: 'a;
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, input: I) -> Self::Output<'a> {
impl<'n, CashedNode: Node<'n, Input>, Input> Node<'n, Input> for CacheNode<'n, CashedNode, Input> {
type Output = &'n CashedNode::Output;
fn eval(&'n self, input: &'n Input) -> Self::Output {
self.cache.get_or_init(|| self.node.eval(input))
}
}
impl<'n, 'c, CachedNode: Node> CacheNode<'n, 'c, CachedNode> {
impl<'n, CachedNode: Node<'n, Input>, Input> CacheNode<'n, CachedNode, Input> {
pub fn clear(&'n mut self) {
self.cache = OnceCell::new();
}
pub fn new(node: &'n CachedNode) -> CacheNode<'n, 'c, CachedNode> {
pub fn new(node: &'n CachedNode) -> CacheNode<'n, CachedNode, Input> {
CacheNode {
node,
cache: OnceCell::new(),