mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-17 23:38:06 +08:00
Make node trait consume self
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user