Skip to content

Commit

Permalink
Give NodeContext a reference to an associated Node type.
Browse files Browse the repository at this point in the history
  • Loading branch information
stuhood committed Jun 23, 2018
1 parent 775e8cb commit 868cb58
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/rust/engine/graph/src/lib.rs
Expand Up @@ -96,7 +96,7 @@ impl<N: Node> Entry<N> {
///
fn state<C>(&mut self, context: &C, entry_id: EntryId) -> EntryStateField<N::Item, N::Error>
where
C: NodeContext<CloneFor = N::Context>,
C: NodeContext<Node = N>,
{
if let Some(ref state) = self.state {
state.field.clone()
Expand Down Expand Up @@ -560,7 +560,7 @@ impl<N: Node> Graph<N> {
///
pub fn get<C>(&self, src_id: EntryId, context: &C, dst_node: N) -> BoxFuture<N::Item, N::Error>
where
C: NodeContext<CloneFor = N::Context>,
C: NodeContext<Node = N>,
{
// Get or create the destination, and then insert the dep and return its state.
let dst_state = {
Expand Down Expand Up @@ -596,7 +596,7 @@ impl<N: Node> Graph<N> {
///
pub fn create<C>(&self, node: N, context: &C) -> BoxFuture<N::Item, N::Error>
where
C: NodeContext<CloneFor = N::Context>,
C: NodeContext<Node = N>,
{
// Initialize the state while under the lock...
let state = {
Expand Down
13 changes: 10 additions & 3 deletions src/rust/engine/graph/src/node.rs
Expand Up @@ -10,14 +10,16 @@ use hashing::Digest;

use petgraph::stable_graph;

use Graph;

// 2^32 Nodes ought to be more than enough for anyone!
pub type EntryId = stable_graph::NodeIndex<u32>;

///
/// Defines executing a cacheable/memoizable step within the given NodeContext.
///
pub trait Node: Clone + Eq + Hash + Send + 'static {
type Context: NodeContext;
type Context: NodeContext<Node=Self>;

type Item: Clone + Debug + Send + 'static;
type Error: NodeError;
Expand Down Expand Up @@ -89,12 +91,17 @@ pub trait NodeContext: Clone + Send + 'static {
///
/// The type generated when this Context is cloned for another Node.
///
type CloneFor: NodeContext;
type Node: Node;

///
/// Creates a clone of this NodeContext to be used for a different Node.
///
/// To clone a Context for use for the same Node, `Clone` is used directly.
///
fn clone_for(&self, entry_id: EntryId) -> Self::CloneFor;
fn clone_for(&self, entry_id: EntryId) -> <Self::Node as Node>::Context;

///
/// Returns a reference to the Graph for this Context.
///
fn graph(&self) -> &Graph<Self::Node>;
}
6 changes: 5 additions & 1 deletion src/rust/engine/src/context.rs
Expand Up @@ -166,7 +166,7 @@ impl Context {
}

impl NodeContext for Context {
type CloneFor = Context;
type Node = NodeKey;

///
/// Clones this Context for a new EntryId. Because the Core of the context is an Arc, this
Expand All @@ -178,4 +178,8 @@ impl NodeContext for Context {
core: self.core.clone(),
}
}

fn graph(&self) -> &Graph<NodeKey> {
&self.core.graph
}
}
8 changes: 6 additions & 2 deletions src/rust/engine/src/scheduler.rs
Expand Up @@ -13,7 +13,7 @@ use boxfuture::{BoxFuture, Boxable};
use context::{Context, Core};
use core::{Failure, Key, TypeConstraint, TypeId, Value};
use fs::{self, GlobMatching, PosixFS};
use graph::{EntryId, Node, NodeContext};
use graph::{EntryId, Graph, Node, NodeContext};
use nodes::{NodeKey, Select, Tracer, TryInto, Visualizer};
use rule_graph;
use selectors;
Expand Down Expand Up @@ -287,9 +287,13 @@ struct RootContext {
}

impl NodeContext for RootContext {
type CloneFor = Context;
type Node = NodeKey;

fn clone_for(&self, entry_id: EntryId) -> Context {
Context::new(entry_id, self.core.clone())
}

fn graph(&self) -> &Graph<NodeKey> {
&self.core.graph
}
}

0 comments on commit 868cb58

Please sign in to comment.