From dcf839b31469b691620d85a81ec42ca42dc57644 Mon Sep 17 00:00:00 2001 From: Shadaj Laddad Date: Wed, 6 Nov 2024 11:20:10 -0800 Subject: [PATCH] refactor(hydroflow_plus): deduplicate some error messages and drop unused `Interval` IR node --- hydroflow_plus/src/builder/mod.rs | 2 + hydroflow_plus/src/ir.rs | 7 ---- hydroflow_plus/src/optional.rs | 50 +++++++++++++++++-------- hydroflow_plus/src/singleton.rs | 34 +++++++++++------ hydroflow_plus/src/stream.rs | 62 +++++++++++++++++++++---------- 5 files changed, 102 insertions(+), 53 deletions(-) diff --git a/hydroflow_plus/src/builder/mod.rs b/hydroflow_plus/src/builder/mod.rs index e4c65a069a95..33101b554e67 100644 --- a/hydroflow_plus/src/builder/mod.rs +++ b/hydroflow_plus/src/builder/mod.rs @@ -27,6 +27,8 @@ pub struct FlowStateInner { pub type FlowState = Rc>; +pub const FLOW_USED_MESSAGE: &str = "Attempted to add a leaf to a flow that has already been finalized. No leaves can be added after the flow has been compiled."; + pub struct FlowBuilder<'a> { flow_state: FlowState, nodes: RefCell>, diff --git a/hydroflow_plus/src/ir.rs b/hydroflow_plus/src/ir.rs index 62d1604b2b1c..557eb8076e65 100644 --- a/hydroflow_plus/src/ir.rs +++ b/hydroflow_plus/src/ir.rs @@ -69,7 +69,6 @@ pub enum HfPlusSource { Stream(DebugExpr), ExternalNetwork(), Iter(DebugExpr), - Interval(DebugExpr), Spin(), } @@ -616,12 +615,6 @@ impl<'a> HfPlusNode { } } - HfPlusSource::Interval(expr) => { - parse_quote! { - #source_ident = source_interval(#expr); - } - } - HfPlusSource::Spin() => { parse_quote! { #source_ident = spin(); diff --git a/hydroflow_plus/src/optional.rs b/hydroflow_plus/src/optional.rs index 5c951990b120..ddf05115633c 100644 --- a/hydroflow_plus/src/optional.rs +++ b/hydroflow_plus/src/optional.rs @@ -6,7 +6,7 @@ use std::rc::Rc; use stageleft::{q, IntoQuotedMut, Quoted}; use syn::parse_quote; -use crate::builder::FlowState; +use crate::builder::{FlowState, FLOW_USED_MESSAGE}; use crate::cycle::{CycleCollection, CycleComplete, DeferTick, ForwardRef, TickCycle}; use crate::ir::{HfPlusLeaf, HfPlusNode, HfPlusSource, TeeNode}; use crate::location::{check_matching_location, LocationId, NoTick}; @@ -64,11 +64,17 @@ impl<'a, T, N: Location<'a>> CycleCollection<'a, TickCycle> for Optional> CycleComplete<'a, TickCycle> for Optional> { fn complete(self, ident: syn::Ident) { - self.flow_state().clone().borrow_mut().leaves.as_mut().expect("Attempted to add a leaf to a flow that has already been finalized. No leaves can be added after the flow has been compiled.").push(HfPlusLeaf::CycleSink { - ident, - location_kind: self.location_kind(), - input: Box::new(self.ir_node.into_inner()), - }); + self.flow_state() + .clone() + .borrow_mut() + .leaves + .as_mut() + .expect(FLOW_USED_MESSAGE) + .push(HfPlusLeaf::CycleSink { + ident, + location_kind: self.location_kind(), + input: Box::new(self.ir_node.into_inner()), + }); } } @@ -89,11 +95,17 @@ impl<'a, T, N: Location<'a>> CycleCollection<'a, ForwardRef> for Optional> CycleComplete<'a, ForwardRef> for Optional> { fn complete(self, ident: syn::Ident) { - self.flow_state().clone().borrow_mut().leaves.as_mut().expect("Attempted to add a leaf to a flow that has already been finalized. No leaves can be added after the flow has been compiled.").push(HfPlusLeaf::CycleSink { - ident, - location_kind: self.location_kind(), - input: Box::new(self.ir_node.into_inner()), - }); + self.flow_state() + .clone() + .borrow_mut() + .leaves + .as_mut() + .expect(FLOW_USED_MESSAGE) + .push(HfPlusLeaf::CycleSink { + ident, + location_kind: self.location_kind(), + input: Box::new(self.ir_node.into_inner()), + }); } } @@ -114,11 +126,17 @@ impl<'a, T, W, N: Location<'a> + NoTick> CycleCollection<'a, ForwardRef> for Opt impl<'a, T, W, N: Location<'a> + NoTick> CycleComplete<'a, ForwardRef> for Optional { fn complete(self, ident: syn::Ident) { - self.flow_state().clone().borrow_mut().leaves.as_mut().expect("Attempted to add a leaf to a flow that has already been finalized. No leaves can be added after the flow has been compiled.").push(HfPlusLeaf::CycleSink { - ident, - location_kind: self.location_kind(), - input: Box::new(HfPlusNode::Unpersist(Box::new(self.ir_node.into_inner()))), - }); + self.flow_state() + .clone() + .borrow_mut() + .leaves + .as_mut() + .expect(FLOW_USED_MESSAGE) + .push(HfPlusLeaf::CycleSink { + ident, + location_kind: self.location_kind(), + input: Box::new(HfPlusNode::Unpersist(Box::new(self.ir_node.into_inner()))), + }); } } diff --git a/hydroflow_plus/src/singleton.rs b/hydroflow_plus/src/singleton.rs index 7c5df816fa89..608fa8dae742 100644 --- a/hydroflow_plus/src/singleton.rs +++ b/hydroflow_plus/src/singleton.rs @@ -5,7 +5,7 @@ use std::rc::Rc; use stageleft::{q, IntoQuotedMut, Quoted}; -use crate::builder::FlowState; +use crate::builder::{FlowState, FLOW_USED_MESSAGE}; use crate::cycle::{ CycleCollection, CycleCollectionWithInitial, CycleComplete, DeferTick, ForwardRef, TickCycle, }; @@ -73,11 +73,17 @@ impl<'a, T, N: Location<'a>> CycleCollectionWithInitial<'a, TickCycle> impl<'a, T, N: Location<'a>> CycleComplete<'a, TickCycle> for Singleton> { fn complete(self, ident: syn::Ident) { - self.flow_state().clone().borrow_mut().leaves.as_mut().expect("Attempted to add a leaf to a flow that has already been finalized. No leaves can be added after the flow has been compiled.").push(HfPlusLeaf::CycleSink { - ident, - location_kind: self.location_kind(), - input: Box::new(self.ir_node.into_inner()), - }); + self.flow_state() + .clone() + .borrow_mut() + .leaves + .as_mut() + .expect(FLOW_USED_MESSAGE) + .push(HfPlusLeaf::CycleSink { + ident, + location_kind: self.location_kind(), + input: Box::new(self.ir_node.into_inner()), + }); } } @@ -98,11 +104,17 @@ impl<'a, T, N: Location<'a>> CycleCollection<'a, ForwardRef> for Singleton> CycleComplete<'a, ForwardRef> for Singleton> { fn complete(self, ident: syn::Ident) { - self.flow_state().clone().borrow_mut().leaves.as_mut().expect("Attempted to add a leaf to a flow that has already been finalized. No leaves can be added after the flow has been compiled.").push(HfPlusLeaf::CycleSink { - ident, - location_kind: self.location_kind(), - input: Box::new(self.ir_node.into_inner()), - }); + self.flow_state() + .clone() + .borrow_mut() + .leaves + .as_mut() + .expect(FLOW_USED_MESSAGE) + .push(HfPlusLeaf::CycleSink { + ident, + location_kind: self.location_kind(), + input: Box::new(self.ir_node.into_inner()), + }); } } diff --git a/hydroflow_plus/src/stream.rs b/hydroflow_plus/src/stream.rs index c2c791558a5e..777f8648bd49 100644 --- a/hydroflow_plus/src/stream.rs +++ b/hydroflow_plus/src/stream.rs @@ -12,7 +12,7 @@ use serde::Serialize; use stageleft::{q, IntoQuotedMut, Quoted}; use syn::parse_quote; -use crate::builder::FlowState; +use crate::builder::{FlowState, FLOW_USED_MESSAGE}; use crate::cycle::{CycleCollection, CycleComplete, DeferTick, ForwardRef, TickCycle}; use crate::ir::{DebugInstantiate, HfPlusLeaf, HfPlusNode, TeeNode}; use crate::location::cluster::ClusterSelfId; @@ -78,11 +78,17 @@ impl<'a, T, N: Location<'a>> CycleCollection<'a, TickCycle> for Stream> CycleComplete<'a, TickCycle> for Stream> { fn complete(self, ident: syn::Ident) { - self.flow_state().clone().borrow_mut().leaves.as_mut().expect("Attempted to add a leaf to a flow that has already been finalized. No leaves can be added after the flow has been compiled.").push(HfPlusLeaf::CycleSink { - ident, - location_kind: self.location_kind(), - input: Box::new(self.ir_node.into_inner()), - }); + self.flow_state() + .clone() + .borrow_mut() + .leaves + .as_mut() + .expect(FLOW_USED_MESSAGE) + .push(HfPlusLeaf::CycleSink { + ident, + location_kind: self.location_kind(), + input: Box::new(self.ir_node.into_inner()), + }); } } @@ -103,11 +109,17 @@ impl<'a, T, W, N: Location<'a> + NoTick> CycleCollection<'a, ForwardRef> for Str impl<'a, T, W, N: Location<'a> + NoTick> CycleComplete<'a, ForwardRef> for Stream { fn complete(self, ident: syn::Ident) { - self.flow_state().clone().borrow_mut().leaves.as_mut().expect("Attempted to add a leaf to a flow that has already been finalized. No leaves can be added after the flow has been compiled.").push(HfPlusLeaf::CycleSink { - ident, - location_kind: self.location_kind(), - input: Box::new(HfPlusNode::Unpersist(Box::new(self.ir_node.into_inner()))), - }); + self.flow_state() + .clone() + .borrow_mut() + .leaves + .as_mut() + .expect(FLOW_USED_MESSAGE) + .push(HfPlusLeaf::CycleSink { + ident, + location_kind: self.location_kind(), + input: Box::new(HfPlusNode::Unpersist(Box::new(self.ir_node.into_inner()))), + }); } } @@ -482,17 +494,29 @@ impl<'a, T, W, N: Location<'a> + NoTick> Stream { } pub fn for_each(self, f: impl IntoQuotedMut<'a, F>) { - self.flow_state().clone().borrow_mut().leaves.as_mut().expect("Attempted to add a leaf to a flow that has already been finalized. No leaves can be added after the flow has been compiled.").push(HfPlusLeaf::ForEach { - input: Box::new(HfPlusNode::Unpersist(Box::new(self.ir_node.into_inner()))), - f: f.splice_fn1().into(), - }); + self.flow_state() + .clone() + .borrow_mut() + .leaves + .as_mut() + .expect(FLOW_USED_MESSAGE) + .push(HfPlusLeaf::ForEach { + input: Box::new(HfPlusNode::Unpersist(Box::new(self.ir_node.into_inner()))), + f: f.splice_fn1().into(), + }); } pub fn dest_sink + 'a>(self, sink: impl Quoted<'a, S>) { - self.flow_state().clone().borrow_mut().leaves.as_mut().expect("Attempted to add a leaf to a flow that has already been finalized. No leaves can be added after the flow has been compiled.").push(HfPlusLeaf::DestSink { - sink: sink.splice_typed().into(), - input: Box::new(self.ir_node.into_inner()), - }); + self.flow_state() + .clone() + .borrow_mut() + .leaves + .as_mut() + .expect(FLOW_USED_MESSAGE) + .push(HfPlusLeaf::DestSink { + sink: sink.splice_typed().into(), + input: Box::new(self.ir_node.into_inner()), + }); } }