From 45a72fbf76f6c435f97e071b0bfc0010970fe75a Mon Sep 17 00:00:00 2001 From: Maxim Raznatovski Date: Tue, 7 Jan 2025 17:29:18 +0100 Subject: [PATCH] refactor: use ComputeGraph::compute_with instead of deprecated variants --- crates/computegraph/src/lib.rs | 11 ++++- crates/computegraph/tests/context.rs | 71 ++++++++++++++++++++++++---- crates/viewport/src/pipeline.rs | 30 +++++++++--- 3 files changed, 94 insertions(+), 18 deletions(-) diff --git a/crates/computegraph/src/lib.rs b/crates/computegraph/src/lib.rs index 44a7d45..fb5ed49 100644 --- a/crates/computegraph/src/lib.rs +++ b/crates/computegraph/src/lib.rs @@ -57,7 +57,8 @@ //! context.set_override(multiply_node.input_b(), 2); //! //! // Compute the result -//! let result = graph.compute_with_context(multiply_node.output(), &context).unwrap(); +//! let options = ComputationOptions {context: Some(&context) }; +//! let result = graph.compute_with(multiply_node.output(), &options, None).unwrap(); //! assert_eq!(result, (3 + 4) * 2); //! ``` //! @@ -1736,7 +1737,13 @@ impl ComputeGraph { output: OutputPort, context: &ComputationContext, ) -> Result { - let res = self.compute_untyped_with_context(output.port.clone(), context)?; + let res = self.compute_untyped_with( + output.port.clone(), + &ComputationOptions { + context: Some(context), + }, + None, + )?; let res = res .into_any() .downcast::() diff --git a/crates/computegraph/tests/context.rs b/crates/computegraph/tests/context.rs index 8a20e78..472fa98 100644 --- a/crates/computegraph/tests/context.rs +++ b/crates/computegraph/tests/context.rs @@ -18,13 +18,25 @@ fn test_context_override() -> Result<()> { ctx.set_override(addition.input_a(), 5); assert_eq!( - graph.compute_with_context(addition.output(), &ctx)?, + graph.compute_with( + addition.output(), + &ComputationOptions { + context: Some(&ctx), + }, + None + )?, 8, "ctx should use the latest given value" ); assert_eq!( *graph - .compute_untyped_with_context(addition.output().into(), &ctx)? + .compute_untyped_with( + addition.output().into(), + &ComputationOptions { + context: Some(&ctx), + }, + None + )? .as_ref() .as_any() .downcast_ref::() @@ -55,7 +67,13 @@ fn test_context_override_skip_dependencies() -> Result<()> { assert_eq!( graph - .compute_with_context(addition.output(), &ctx) + .compute_with( + addition.output(), + &ComputationOptions { + context: Some(&ctx), + }, + None + ) .expect("This should skip 'invalid_dep' entirely"), 15 ); @@ -85,10 +103,25 @@ fn test_context_fallback() -> Result<()> { ctx.set_fallback(5_usize); ctx.set_fallback(10_usize); - assert_eq!(graph.compute_with_context(addition.output(), &ctx)?, 20); + assert_eq!( + graph.compute_with( + addition.output(), + &ComputationOptions { + context: Some(&ctx), + }, + None + )?, + 20 + ); assert_eq!( *graph - .compute_untyped_with_context(addition.output().into(), &ctx)? + .compute_untyped_with( + addition.output().into(), + &ComputationOptions { + context: Some(&ctx), + }, + None + )? .as_ref() .as_any() .downcast_ref::() @@ -112,10 +145,25 @@ fn test_context_fallback_generator() -> Result<()> { ctx.set_fallback(5_usize); ctx.set_fallback_generator(|_name| 10_usize); - assert_eq!(graph.compute_with_context(addition.output(), &ctx)?, 20); + assert_eq!( + graph.compute_with( + addition.output(), + &ComputationOptions { + context: Some(&ctx), + }, + None + )?, + 20 + ); assert_eq!( *graph - .compute_untyped_with_context(addition.output().into(), &ctx)? + .compute_untyped_with( + addition.output().into(), + &ComputationOptions { + context: Some(&ctx), + }, + None + )? .as_ref() .as_any() .downcast_ref::() @@ -127,7 +175,6 @@ fn test_context_fallback_generator() -> Result<()> { Ok(()) } - #[test] fn test_context_priority() -> Result<()> { let mut graph = ComputeGraph::new(); @@ -144,7 +191,13 @@ fn test_context_priority() -> Result<()> { ctx.set_fallback(10_usize); assert_eq!( - graph.compute_with_context(addition.output(), &ctx)?, + graph.compute_with( + addition.output(), + &ComputationOptions { + context: Some(&ctx), + }, + None + )?, 1, "priority should be override > connected > fallback" ); diff --git a/crates/viewport/src/pipeline.rs b/crates/viewport/src/pipeline.rs index 69db9fd..d4778b7 100644 --- a/crates/viewport/src/pipeline.rs +++ b/crates/viewport/src/pipeline.rs @@ -1,7 +1,7 @@ use crate::ViewportEvent; use computegraph::{ - ComputationContext, ComputeGraph, DynamicNode, InputPort, InputPortUntyped, NodeFactory, - NodeHandle, OutputPort, OutputPortUntyped, + ComputationContext, ComputationOptions, ComputeGraph, DynamicNode, InputPort, InputPortUntyped, + NodeFactory, NodeHandle, OutputPort, OutputPortUntyped, }; use project::ProjectView; use std::any::TypeId; @@ -472,9 +472,13 @@ impl ViewportPipeline { let last_node = self.nodes.last().ok_or(ExecuteError::EmptyPipeline)?; let mut ctx = ComputationContext::default(); ctx.set_fallback(project_view); - let scene = self - .graph - .compute_with_context(last_node.scene_output.clone(), &ctx)?; + let scene = self.graph.compute_with( + last_node.scene_output.clone(), + &ComputationOptions { + context: Some(&ctx), + }, + None, + )?; Ok(scene) } @@ -500,7 +504,13 @@ impl ViewportPipeline { let result = scene .graph - .compute_untyped_with_context(scene.update_state_out, &ctx) + .compute_untyped_with( + scene.update_state_out, + &ComputationOptions { + context: Some(&ctx), + }, + None, + ) .map_err(ExecuteError::ComputeError)?; state.state = Some(result); Ok(()) @@ -525,7 +535,13 @@ impl ViewportPipeline { let result = scene .graph - .compute_with_context(scene.render_primitive_out, &ctx) + .compute_with( + scene.render_primitive_out, + &ComputationOptions { + context: Some(&ctx), + }, + None, + ) .map_err(ExecuteError::ComputeError); let a = ctx.remove_override_untyped(&scene.render_state_in); debug_assert!(a.is_some());