Skip to main content

rlx_flow/
plugin.rs

1// RLX — versatile ML compiler + runtime.
2// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, version 3.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16//! Type-erased arch blocks — keep model-specific emission out of the core enum.
17
18use crate::blocks::CustomStage;
19use crate::escape::Emit;
20use crate::stage::FlowStage;
21use crate::value::FlowValue;
22
23/// Named plugin stage (alias over tier-2 custom emission).
24pub struct PluginStage(CustomStage);
25
26impl PluginStage {
27    pub fn new<F>(f: F) -> Self
28    where
29        F: Fn(&mut Emit<'_>, Option<FlowValue>) -> anyhow::Result<Option<FlowValue>>
30            + Send
31            + Sync
32            + 'static,
33    {
34        Self(CustomStage::new(f))
35    }
36
37    pub fn named<F>(name: impl Into<String>, f: F) -> Self
38    where
39        F: Fn(&mut Emit<'_>, Option<FlowValue>) -> anyhow::Result<Option<FlowValue>>
40            + Send
41            + Sync
42            + 'static,
43    {
44        Self(CustomStage::named(name, f))
45    }
46
47    pub(crate) fn into_stage(self) -> FlowStage {
48        FlowStage::Custom(self.0)
49    }
50}
51
52pub fn plugin<F>(f: F) -> FlowStage
53where
54    F: Fn(&mut Emit<'_>, Option<FlowValue>) -> anyhow::Result<Option<FlowValue>>
55        + Send
56        + Sync
57        + 'static,
58{
59    PluginStage::new(f).into_stage()
60}
61
62pub fn plugin_named<F>(name: impl Into<String>, f: F) -> FlowStage
63where
64    F: Fn(&mut Emit<'_>, Option<FlowValue>) -> anyhow::Result<Option<FlowValue>>
65        + Send
66        + Sync
67        + 'static,
68{
69    PluginStage::named(name, f).into_stage()
70}