From 9ea5f061ee0f116caf8fc4ea99b62a9c7691be2a Mon Sep 17 00:00:00 2001 From: Mingwei Samuel Date: Fri, 6 Dec 2024 12:45:11 -0800 Subject: [PATCH] refactor(hydroflow_plus): use `match_box` macro to compile on stable (#1597) https://github.com/hydro-project/matchbox --- Cargo.lock | 12 ++ .../surface_examples__example_2_simple_1.snap | 1 - .../surface_examples__example_2_simple_2.snap | 1 - .../surface_examples__example_3_stream.snap | 1 - ...surface_examples__example_4_neighbors.snap | 1 - ...face_examples__example_5_reachability.snap | 1 - ...ce_examples__example_6_unreachability.snap | 1 - ...amples__example_surface_flows_1_basic.snap | 1 - ...ples__example_surface_flows_2_varname.snap | 1 - ...amples__example_surface_flows_3_ports.snap | 1 - ...ples__example_surface_flows_4_context.snap | 1 - ..._examples__example_syntax_hello_world.snap | 1 - ...urface_examples__example_syntax_input.snap | 1 - hydroflow_plus/Cargo.toml | 28 +-- hydroflow_plus/src/lib.rs | 2 - hydroflow_plus/src/rewrites/persist_pullup.rs | 196 +++++++++--------- 16 files changed, 125 insertions(+), 125 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d26a114e4e8..ca8aab2608b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1590,6 +1590,7 @@ dependencies = [ "hydroflow", "hydroflow_lang", "insta", + "match_box", "nameof", "prettyplease", "proc-macro-crate", @@ -2014,6 +2015,17 @@ version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +[[package]] +name = "match_box" +version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c33c1d4fa92364abfc42bcc58c201cfbb63ae80f5e471aac5c051db48dab6843" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.75", +] + [[package]] name = "matchers" version = "0.1.0" diff --git a/hydroflow/tests/snapshots/surface_examples__example_2_simple_1.snap b/hydroflow/tests/snapshots/surface_examples__example_2_simple_1.snap index cf167828f0c..90de61d5dbe 100644 --- a/hydroflow/tests/snapshots/surface_examples__example_2_simple_1.snap +++ b/hydroflow/tests/snapshots/surface_examples__example_2_simple_1.snap @@ -14,4 +14,3 @@ Howdy 64 Howdy 65 Howdy 81 Howdy 82 - diff --git a/hydroflow/tests/snapshots/surface_examples__example_2_simple_2.snap b/hydroflow/tests/snapshots/surface_examples__example_2_simple_2.snap index 73757ce90bc..c71290ae933 100644 --- a/hydroflow/tests/snapshots/surface_examples__example_2_simple_2.snap +++ b/hydroflow/tests/snapshots/surface_examples__example_2_simple_2.snap @@ -14,4 +14,3 @@ G'day 64 G'day 65 G'day 81 G'day 82 - diff --git a/hydroflow/tests/snapshots/surface_examples__example_3_stream.snap b/hydroflow/tests/snapshots/surface_examples__example_3_stream.snap index 0df8637c397..bd523917a67 100644 --- a/hydroflow/tests/snapshots/surface_examples__example_3_stream.snap +++ b/hydroflow/tests/snapshots/surface_examples__example_3_stream.snap @@ -16,4 +16,3 @@ Ahoy, 64 Ahoy, 65 Ahoy, 81 Ahoy, 82 - diff --git a/hydroflow/tests/snapshots/surface_examples__example_4_neighbors.snap b/hydroflow/tests/snapshots/surface_examples__example_4_neighbors.snap index ebd58d9f81e..7b931190d85 100644 --- a/hydroflow/tests/snapshots/surface_examples__example_4_neighbors.snap +++ b/hydroflow/tests/snapshots/surface_examples__example_4_neighbors.snap @@ -44,4 +44,3 @@ end Reached: 0 Reached: 1 Reached: 3 - diff --git a/hydroflow/tests/snapshots/surface_examples__example_5_reachability.snap b/hydroflow/tests/snapshots/surface_examples__example_5_reachability.snap index 1575fcd2077..157ecc56655 100644 --- a/hydroflow/tests/snapshots/surface_examples__example_5_reachability.snap +++ b/hydroflow/tests/snapshots/surface_examples__example_5_reachability.snap @@ -60,4 +60,3 @@ Reached: 1 Reached: 3 Reached: 2 Reached: 4 - diff --git a/hydroflow/tests/snapshots/surface_examples__example_6_unreachability.snap b/hydroflow/tests/snapshots/surface_examples__example_6_unreachability.snap index 73281bb1a1f..2737c73983a 100644 --- a/hydroflow/tests/snapshots/surface_examples__example_6_unreachability.snap +++ b/hydroflow/tests/snapshots/surface_examples__example_6_unreachability.snap @@ -98,4 +98,3 @@ Received vertex: 11 Received vertex: 12 unreached_vertices vertex: 11 unreached_vertices vertex: 12 - diff --git a/hydroflow/tests/snapshots/surface_examples__example_surface_flows_1_basic.snap b/hydroflow/tests/snapshots/surface_examples__example_surface_flows_1_basic.snap index 4ab296cb6a4..e3a2e0a2da9 100644 --- a/hydroflow/tests/snapshots/surface_examples__example_surface_flows_1_basic.snap +++ b/hydroflow/tests/snapshots/surface_examples__example_surface_flows_1_basic.snap @@ -4,4 +4,3 @@ expression: output --- HELLO WORLD - diff --git a/hydroflow/tests/snapshots/surface_examples__example_surface_flows_2_varname.snap b/hydroflow/tests/snapshots/surface_examples__example_surface_flows_2_varname.snap index 4ab296cb6a4..e3a2e0a2da9 100644 --- a/hydroflow/tests/snapshots/surface_examples__example_surface_flows_2_varname.snap +++ b/hydroflow/tests/snapshots/surface_examples__example_surface_flows_2_varname.snap @@ -4,4 +4,3 @@ expression: output --- HELLO WORLD - diff --git a/hydroflow/tests/snapshots/surface_examples__example_surface_flows_3_ports.snap b/hydroflow/tests/snapshots/surface_examples__example_surface_flows_3_ports.snap index f9fb5c6ec85..83ed8d6a0da 100644 --- a/hydroflow/tests/snapshots/surface_examples__example_surface_flows_3_ports.snap +++ b/hydroflow/tests/snapshots/surface_examples__example_surface_flows_3_ports.snap @@ -47,4 +47,3 @@ hello world HELLO WORLD - diff --git a/hydroflow/tests/snapshots/surface_examples__example_surface_flows_4_context.snap b/hydroflow/tests/snapshots/surface_examples__example_surface_flows_4_context.snap index 3e819e2a484..2008630a0c8 100644 --- a/hydroflow/tests/snapshots/surface_examples__example_surface_flows_4_context.snap +++ b/hydroflow/tests/snapshots/surface_examples__example_surface_flows_4_context.snap @@ -3,4 +3,3 @@ source: hydroflow/tests/surface_examples.rs expression: output --- Current tick: [0], stratum: 0 - diff --git a/hydroflow/tests/snapshots/surface_examples__example_syntax_hello_world.snap b/hydroflow/tests/snapshots/surface_examples__example_syntax_hello_world.snap index 4ab296cb6a4..e3a2e0a2da9 100644 --- a/hydroflow/tests/snapshots/surface_examples__example_syntax_hello_world.snap +++ b/hydroflow/tests/snapshots/surface_examples__example_syntax_hello_world.snap @@ -4,4 +4,3 @@ expression: output --- HELLO WORLD - diff --git a/hydroflow/tests/snapshots/surface_examples__example_syntax_input.snap b/hydroflow/tests/snapshots/surface_examples__example_syntax_input.snap index 4ab296cb6a4..e3a2e0a2da9 100644 --- a/hydroflow/tests/snapshots/surface_examples__example_syntax_input.snap +++ b/hydroflow/tests/snapshots/surface_examples__example_syntax_input.snap @@ -4,4 +4,3 @@ expression: output --- HELLO WORLD - diff --git a/hydroflow_plus/Cargo.toml b/hydroflow_plus/Cargo.toml index 0023e55415f..23ad3d33433 100644 --- a/hydroflow_plus/Cargo.toml +++ b/hydroflow_plus/Cargo.toml @@ -21,32 +21,32 @@ deploy_runtime = [ "hydroflow/deploy_integration" ] deploy = [ "deploy_runtime", "dep:hydro_deploy", "dep:trybuild-internals-api", "dep:toml", "dep:prettyplease" ] [dependencies] -quote = "1.0.35" -syn = { version = "2.0.46", features = [ "parsing", "extra-traits", "visit-mut" ] } -proc-macro2 = "1.0.74" -proc-macro-crate = "1.0.0" +bincode = "1.3.1" +hydro_deploy = { path = "../hydro_deploy/core", version = "^0.10.0", optional = true } hydroflow = { path = "../hydroflow", version = "^0.10.0", default-features = false } hydroflow_lang = { path = "../hydroflow_lang", version = "^0.10.0" } -serde = { version = "1.0.197", features = [ "derive" ] } -bincode = "1.3.1" -tokio = { version = "1.29.0", features = [ "full" ] } -stageleft = { path = "../stageleft", version = "^0.5.0" } - +match_box = "0.0.2" nameof = "1.0.0" +prettyplease = { version = "0.2.0", features = [ "verbatim" ], optional = true } +proc-macro-crate = "1.0.0" +proc-macro2 = "1.0.74" +quote = "1.0.35" +sealed = "0.6.0" +serde = { version = "1.0.197", features = [ "derive" ] } sha2 = "0.10.0" +stageleft = { path = "../stageleft", version = "^0.5.0" } stageleft_tool = { path = "../stageleft_tool", version = "^0.4.0" } -hydro_deploy = { path = "../hydro_deploy/core", version = "^0.10.0", optional = true } -prettyplease = { version = "0.2.0", features = [ "verbatim" ], optional = true } +syn = { version = "2.0.46", features = [ "parsing", "extra-traits", "visit-mut" ] } +tokio = { version = "1.29.0", features = [ "full" ] } toml = { version = "0.8.0", optional = true } trybuild-internals-api = { version = "1.0.99", optional = true } -sealed = "0.6.0" [build-dependencies] stageleft_tool = { path = "../stageleft_tool", version = "^0.4.0" } [dev-dependencies] -insta = "1.39" -hydro_deploy = { path = "../hydro_deploy/core", version = "^0.10.0" } async-ssh2-lite = { version = "0.5.0", features = ["vendored-openssl"] } ctor = "0.2.8" +hydro_deploy = { path = "../hydro_deploy/core", version = "^0.10.0" } +insta = "1.39" trybuild = "1" diff --git a/hydroflow_plus/src/lib.rs b/hydroflow_plus/src/lib.rs index d9b25a8f553..27ac6e51b4a 100644 --- a/hydroflow_plus/src/lib.rs +++ b/hydroflow_plus/src/lib.rs @@ -1,5 +1,3 @@ -#![feature(box_patterns)] - stageleft::stageleft_no_entry_crate!(); pub use hydroflow; diff --git a/hydroflow_plus/src/rewrites/persist_pullup.rs b/hydroflow_plus/src/rewrites/persist_pullup.rs index 7176dfde899..2924bf54484 100644 --- a/hydroflow_plus/src/rewrites/persist_pullup.rs +++ b/hydroflow_plus/src/rewrites/persist_pullup.rs @@ -7,114 +7,116 @@ fn persist_pullup_node( node: &mut HfPlusNode, persist_pulled_tees: &mut HashSet<*const RefCell>, ) { - *node = match std::mem::replace(node, HfPlusNode::Placeholder) { - HfPlusNode::Unpersist(box HfPlusNode::Persist(box behind_persist)) => behind_persist, - - HfPlusNode::Delta(box HfPlusNode::Persist(box behind_persist)) => behind_persist, - - HfPlusNode::Tee { inner } => { - if persist_pulled_tees.contains(&(inner.0.as_ref() as *const RefCell)) { - HfPlusNode::Persist(Box::new(HfPlusNode::Tee { - inner: TeeNode(inner.0.clone()), - })) - } else if matches!(*inner.0.borrow(), HfPlusNode::Persist(_)) { - persist_pulled_tees.insert(inner.0.as_ref() as *const RefCell); - if let HfPlusNode::Persist(box behind_persist) = - inner.0.replace(HfPlusNode::Placeholder) - { - *inner.0.borrow_mut() = behind_persist; + *node = match_box::match_box! { + match std::mem::replace(node, HfPlusNode::Placeholder) { + HfPlusNode::Unpersist(mb!(* HfPlusNode::Persist(mb!(* behind_persist)))) => behind_persist, + + HfPlusNode::Delta(mb!(* HfPlusNode::Persist(mb!(* behind_persist)))) => behind_persist, + + HfPlusNode::Tee { inner } => { + if persist_pulled_tees.contains(&(inner.0.as_ref() as *const RefCell)) { + HfPlusNode::Persist(Box::new(HfPlusNode::Tee { + inner: TeeNode(inner.0.clone()), + })) + } else if matches!(*inner.0.borrow(), HfPlusNode::Persist(_)) { + persist_pulled_tees.insert(inner.0.as_ref() as *const RefCell); + if let HfPlusNode::Persist(behind_persist) = + inner.0.replace(HfPlusNode::Placeholder) + { + *inner.0.borrow_mut() = *behind_persist; + } else { + unreachable!() + } + + HfPlusNode::Persist(Box::new(HfPlusNode::Tee { + inner: TeeNode(inner.0.clone()), + })) } else { - unreachable!() + HfPlusNode::Tee { inner } } + } - HfPlusNode::Persist(Box::new(HfPlusNode::Tee { - inner: TeeNode(inner.0.clone()), - })) - } else { - HfPlusNode::Tee { inner } + HfPlusNode::Map { + f, + input: mb!(* HfPlusNode::Persist(behind_persist)), + } => HfPlusNode::Persist(Box::new(HfPlusNode::Map { + f, + input: behind_persist, + })), + + HfPlusNode::FilterMap { + f, + input: mb!(* HfPlusNode::Persist(behind_persist)), + } => HfPlusNode::Persist(Box::new(HfPlusNode::FilterMap { + f, + input: behind_persist, + })), + + HfPlusNode::FlatMap { + f, + input: mb!(* HfPlusNode::Persist(behind_persist)), + } => HfPlusNode::Persist(Box::new(HfPlusNode::FlatMap { + f, + input: behind_persist, + })), + + HfPlusNode::Filter { + f, + input: mb!(* HfPlusNode::Persist(behind_persist)), + } => HfPlusNode::Persist(Box::new(HfPlusNode::Filter { + f, + input: behind_persist, + })), + + HfPlusNode::Network { + from_location, + from_key, + to_location, + to_key, + serialize_pipeline, + instantiate_fn, + deserialize_pipeline, + input: mb!(* HfPlusNode::Persist(behind_persist)), + .. + } => HfPlusNode::Persist(Box::new(HfPlusNode::Network { + from_location, + from_key, + to_location, + to_key, + serialize_pipeline, + instantiate_fn, + deserialize_pipeline, + input: behind_persist, + })), + + HfPlusNode::Chain(mb!(* HfPlusNode::Persist(left)), mb!(* HfPlusNode::Persist(right))) => { + HfPlusNode::Persist(Box::new(HfPlusNode::Chain(left, right))) } - } - HfPlusNode::Map { - f, - input: box HfPlusNode::Persist(behind_persist), - } => HfPlusNode::Persist(Box::new(HfPlusNode::Map { - f, - input: behind_persist, - })), - - HfPlusNode::FilterMap { - f, - input: box HfPlusNode::Persist(behind_persist), - } => HfPlusNode::Persist(Box::new(HfPlusNode::FilterMap { - f, - input: behind_persist, - })), - - HfPlusNode::FlatMap { - f, - input: box HfPlusNode::Persist(behind_persist), - } => HfPlusNode::Persist(Box::new(HfPlusNode::FlatMap { - f, - input: behind_persist, - })), - - HfPlusNode::Filter { - f, - input: box HfPlusNode::Persist(behind_persist), - } => HfPlusNode::Persist(Box::new(HfPlusNode::Filter { - f, - input: behind_persist, - })), - - HfPlusNode::Network { - from_location, - from_key, - to_location, - to_key, - serialize_pipeline, - instantiate_fn, - deserialize_pipeline, - input: box HfPlusNode::Persist(behind_persist), - .. - } => HfPlusNode::Persist(Box::new(HfPlusNode::Network { - from_location, - from_key, - to_location, - to_key, - serialize_pipeline, - instantiate_fn, - deserialize_pipeline, - input: behind_persist, - })), - - HfPlusNode::Chain(box HfPlusNode::Persist(left), box HfPlusNode::Persist(right)) => { - HfPlusNode::Persist(Box::new(HfPlusNode::Chain(left, right))) - } + HfPlusNode::CrossProduct(mb!(* HfPlusNode::Persist(left)), mb!(* HfPlusNode::Persist(right))) => { + HfPlusNode::Persist(Box::new(HfPlusNode::Delta(Box::new( + HfPlusNode::CrossProduct( + Box::new(HfPlusNode::Persist(left)), + Box::new(HfPlusNode::Persist(right)), + ), + )))) + } - HfPlusNode::CrossProduct(box HfPlusNode::Persist(left), box HfPlusNode::Persist(right)) => { - HfPlusNode::Persist(Box::new(HfPlusNode::Delta(Box::new( - HfPlusNode::CrossProduct( + HfPlusNode::Join(mb!(* HfPlusNode::Persist(left)), mb!(* HfPlusNode::Persist(right))) => { + HfPlusNode::Persist(Box::new(HfPlusNode::Delta(Box::new(HfPlusNode::Join( Box::new(HfPlusNode::Persist(left)), Box::new(HfPlusNode::Persist(right)), - ), - )))) - } + ))))) + } - HfPlusNode::Join(box HfPlusNode::Persist(left), box HfPlusNode::Persist(right)) => { - HfPlusNode::Persist(Box::new(HfPlusNode::Delta(Box::new(HfPlusNode::Join( - Box::new(HfPlusNode::Persist(left)), - Box::new(HfPlusNode::Persist(right)), - ))))) - } + HfPlusNode::Unique(mb!(* HfPlusNode::Persist(inner))) => { + HfPlusNode::Persist(Box::new(HfPlusNode::Delta(Box::new(HfPlusNode::Unique( + Box::new(HfPlusNode::Persist(inner)), + ))))) + } - HfPlusNode::Unique(box HfPlusNode::Persist(inner)) => { - HfPlusNode::Persist(Box::new(HfPlusNode::Delta(Box::new(HfPlusNode::Unique( - Box::new(HfPlusNode::Persist(inner)), - ))))) + node => node, } - - node => node, }; }