Struct depends::DerivedNode

source ·
pub struct DerivedNode<D, F, T> { /* private fields */ }
Expand description

Derived Node

A node which manages a value which depends on other nodes. This node will keep track of the hash value of any dependencies it has and recompute the internal value if any of its dependencies appear to have changed.

Dependencies

A derived node must specify its dependencies. This can be a single node, wrapped in a Dependency, or a struct with multiple node types which has derived Dependencies.

Operation

Along with the dependencies, a derived node must specify a function type which outlines how to transform the target (wrapped value), given the state of the dependencies.

For more, see the Operation macro.

Resolving nodes

Nodes can be Resolved to compute and return a reference to the internal value. To do so, the node must be passed a Visitor which will be used to traverse the graph and compute the value.

Be sure to use the same visitor between calls to resolve, as the visitor is responsible for determining node hashes, and this will not be consistent between different visitor instances.

// Create some input nodes.
let input_1 = InputNode::new("Hello,".to_string());
let input_2 = InputNode::new("???".to_string());

// Create a derived node.
let node = DerivedNode::new(
    TwoStrings::init(Rc::clone(&input_1), Rc::clone(&input_2)),
    Concat,
    String::new()
);

let mut visitor = HashSetVisitor::new();

// Resolve the node.
assert_eq!(
    node.resolve_root(&mut visitor).unwrap().value(),
    "Hello, ???"
);

// Update the input nodes.
input_2.update("world!".to_string()).unwrap();

// The node will detect the input has changed and recompute its value.
assert_eq!(
    node.resolve_root(&mut visitor).unwrap().value(),
    "Hello, world!"
);

// Any nodes which `resolve` to the dependency types can be combined.
let input_3 = InputNode::new("See ya.".to_string());

let another_node = DerivedNode::new(
    TwoStrings::init(Rc::clone(&node), Rc::clone(&input_3)),
    Concat,
    String::new()
);

assert_eq!(
    another_node.resolve_root(&mut visitor).unwrap().value(),
    "Hello, world! See ya."
);

Implementations§

source§

impl<D, F, T> DerivedNode<D, F, T>
where for<'a> D: Resolve + IsDirtyInferenceWorkaround<'a> + 'a, for<'a> F: UpdateDerived<Input<'a> = <D as IsDirtyInferenceWorkaround<'a>>::OutputWorkaround, Target<'a> = RefMut<'a, NodeState<T>>> + 'a, T: HashValue + Clean + Named,

source

pub fn new(dependencies: D, update: F, value: T) -> Rc<Self>

Construct this node.

source

pub fn new_with_id(dependencies: D, _: F, value: T, id: usize) -> Rc<Self>

Create this node with a specified Id. Useful for tests.

Trait Implementations§

source§

impl<D, F, T: Named> Identifiable for DerivedNode<D, F, T>

source§

fn id(&self) -> usize

source§

impl<D, F, T: Named> Named for DerivedNode<D, F, T>

source§

fn name() -> &'static str

source§

impl<D, F, T> Resolve for DerivedNode<D, F, T>
where for<'a> D: Resolve + IsDirtyInferenceWorkaround<'a> + 'a, for<'a> F: UpdateDerived<Input<'a> = <D as IsDirtyInferenceWorkaround<'a>>::OutputWorkaround, Target<'a> = RefMut<'a, NodeState<T>>> + 'a, T: HashValue + Clean + Named,

§

type Output<'a> = Ref<'a, NodeState<T>> where Self: 'a

source§

fn resolve( &self, visitor: &mut impl Visitor ) -> Result<Self::Output<'_>, ResolveError>

You’re probably looking for resolve_root. This is recursively called on each node when a graph is being resolved. Read more
source§

fn resolve_root( &self, visitor: &mut impl Visitor ) -> ResolveResult<Self::Output<'_>>

Pass a Visitor through this node, resolve the latest version of all dependencies, reset the visitor and return this node’s output.

Auto Trait Implementations§

§

impl<D, F, T> !RefUnwindSafe for DerivedNode<D, F, T>

§

impl<D, F, T> Send for DerivedNode<D, F, T>
where D: Send, F: Send, T: Send,

§

impl<D, F, T> !Sync for DerivedNode<D, F, T>

§

impl<D, F, T> Unpin for DerivedNode<D, F, T>
where D: Unpin, F: Unpin, T: Unpin,

§

impl<D, F, T> UnwindSafe for DerivedNode<D, F, T>
where D: UnwindSafe, F: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.