Skip to main content

dfir_lang/graph/ops/
mod.rs

1//! DFIR's operators
2
3use std::collections::HashMap;
4use std::fmt::{Debug, Display};
5use std::ops::{Bound, RangeBounds};
6use std::sync::OnceLock;
7
8use documented::DocumentedVariants;
9use proc_macro2::{Ident, Literal, Span, TokenStream};
10use quote::quote_spanned;
11use serde::{Deserialize, Serialize};
12use slotmap::Key;
13use syn::punctuated::Punctuated;
14use syn::{Expr, Token, parse_quote_spanned};
15
16use super::{
17    AsCodeOptions, GraphLoopId, GraphNode, GraphNodeId, GraphSubgraphId, OpInstGenerics,
18    OperatorInstance, PortIndexValue,
19};
20use crate::diagnostic::{Diagnostic, Diagnostics, Level};
21use crate::parse::{Operator, PortIndex};
22
23/// The delay (soft barrier) type, for each input to an operator if needed.
24#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug, Serialize, Deserialize)]
25pub enum DelayType {
26    /// Input must be collected over the previous tick.
27    Tick,
28    /// Input must be collected over the previous tick but also not cause a new tick to occur.
29    TickLazy,
30    /// Input must be collected over the previous loop iteration. Causes the loop to re-fire.
31    Loop,
32    /// Input must be collected over the previous loop iteration. Does NOT cause the loop to re-fire.
33    LoopLazy,
34}
35
36/// Specification of the named (or unnamed) ports for an operator's inputs or outputs.
37pub enum PortListSpec {
38    /// Any number of unnamed (or optionally named) ports.
39    Variadic,
40    /// A specific number of named ports.
41    Fixed(Punctuated<PortIndex, Token![,]>),
42}
43
44/// An instance of this struct represents a single dfir operator.
45pub struct OperatorConstraints {
46    /// Operator's name.
47    pub name: &'static str,
48    /// Operator categories, for docs.
49    pub categories: &'static [OperatorCategory],
50
51    // TODO: generic argument ranges.
52    /// Input argument range required to not show an error.
53    pub hard_range_inn: &'static dyn RangeTrait<usize>,
54    /// Input argument range required to not show a warning.
55    pub soft_range_inn: &'static dyn RangeTrait<usize>,
56    /// Output argument range required to not show an error.
57    pub hard_range_out: &'static dyn RangeTrait<usize>,
58    /// Output argument range required to not show an warning.
59    pub soft_range_out: &'static dyn RangeTrait<usize>,
60    /// Number of arguments i.e. `operator(a, b, c)` has `num_args = 3`.
61    pub num_args: usize,
62    /// How many persistence lifetime arguments can be provided.
63    pub persistence_args: &'static dyn RangeTrait<usize>,
64    // /// How many (non-persistence) lifetime arguments can be provided.
65    // pub lifetime_args: &'static dyn RangeTrait<usize>,
66    /// How many generic type arguments can be provided.
67    pub type_args: &'static dyn RangeTrait<usize>,
68    /// If this operator receives external inputs and therefore must be in
69    /// stratum 0.
70    pub is_external_input: bool,
71    /// Flo semantics type.
72    pub flo_type: Option<FloType>,
73
74    /// What named or numbered input ports to expect?
75    pub ports_inn: Option<fn() -> PortListSpec>,
76    /// What named or numbered output ports to expect?
77    pub ports_out: Option<fn() -> PortListSpec>,
78
79    /// Determines if this input must be preceeded by a stratum barrier.
80    pub input_delaytype_fn: fn(&PortIndexValue) -> Option<DelayType>,
81    /// The operator's codegen. Returns code that is emited is several different locations. See [`OperatorWriteOutput`].
82    pub write_fn: WriteFn,
83}
84
85/// Type alias for [`OperatorConstraints::write_fn`]'s type.
86pub type WriteFn = fn(&WriteContextArgs<'_>, &mut Diagnostics) -> Result<OperatorWriteOutput, ()>;
87
88impl Debug for OperatorConstraints {
89    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90        f.debug_struct("OperatorConstraints")
91            .field("name", &self.name)
92            .field("hard_range_inn", &self.hard_range_inn)
93            .field("soft_range_inn", &self.soft_range_inn)
94            .field("hard_range_out", &self.hard_range_out)
95            .field("soft_range_out", &self.soft_range_out)
96            .field("num_args", &self.num_args)
97            .field("persistence_args", &self.persistence_args)
98            .field("type_args", &self.type_args)
99            .field("is_external_input", &self.is_external_input)
100            .field("ports_inn", &self.ports_inn)
101            .field("ports_out", &self.ports_out)
102            // .field("input_delaytype_fn", &self.input_delaytype_fn)
103            // .field("flow_prop_fn", &self.flow_prop_fn)
104            // .field("write_fn", &self.write_fn)
105            .finish()
106    }
107}
108
109/// The code generated and returned by a [`OperatorConstraints::write_fn`].
110///
111/// This type intentionally does not implement [`Default`]. Constructing it with
112/// `..` or `..Default::default()` silently drops newly added fields (this once
113/// dropped `write_tick_end` from `lattice_reduce`). List every field explicitly
114/// when constructing or destructuring.
115pub struct OperatorWriteOutput {
116    /// Code which runs once outside any subgraphs to set up external state
117    /// (channels, network connections, etc.) to be used by the subgraph.
118    pub write_prologue: TokenStream,
119    /// Iterator (or pusherator) code inside the subgraphs. The code for each
120    /// operator is emitted in order.
121    ///
122    /// Emitted code should assign to [`WriteContextArgs::ident`] and use
123    /// [`WriteContextArgs::inputs`] (pull `Stream`s) or
124    /// [`WriteContextArgs::outputs`] (push `Sink`s).
125    pub write_iterator: TokenStream,
126    /// Code which runs after `Stream`s/`Sink`s have been run. Mainly for flushing IO.
127    pub write_iterator_after: TokenStream,
128    /// Code which runs at the end of each tick, after all subgraphs have run.
129    /// Used for resetting state with `'tick` persistence.
130    pub write_tick_end: TokenStream,
131}
132
133/// Convenience range: zero or more (any number).
134pub const RANGE_ANY: &'static dyn RangeTrait<usize> = &(0..);
135/// Convenience range: exactly zero.
136pub const RANGE_0: &'static dyn RangeTrait<usize> = &(0..=0);
137/// Convenience range: exactly one.
138pub const RANGE_1: &'static dyn RangeTrait<usize> = &(1..=1);
139
140/// Helper to write the `write_iterator` portion of [`OperatorConstraints::write_fn`] output for
141/// unary identity operators.
142///
143/// When no explicit type argument is given and
144/// [`AsCodeOptions::exclude_type_guards`](super::AsCodeOptions::exclude_type_guards) is set, the
145/// identity `check_input`/`check_output` type guard is omitted and the input/output is simply
146/// rebound.
147pub fn identity_write_iterator_fn(
148    &WriteContextArgs {
149        root,
150        op_span,
151        ident,
152        inputs,
153        outputs,
154        is_pull,
155        options,
156        op_inst:
157            OperatorInstance {
158                generics: OpInstGenerics { type_args, .. },
159                ..
160            },
161        ..
162    }: &WriteContextArgs<'_>,
163) -> TokenStream {
164    if type_args.is_empty() && options.exclude_type_guards {
165        // No user-provided type to pin down, so the guard would be a pure (verbose) no-op.
166        let src = if is_pull { &inputs[0] } else { &outputs[0] };
167        return quote_spanned! {op_span=>
168            let #ident = #src;
169        };
170    }
171
172    let generic_type = type_args
173        .first()
174        .map(quote::ToTokens::to_token_stream)
175        .unwrap_or_else(|| quote_spanned!(op_span=> _));
176
177    if is_pull {
178        let input = &inputs[0];
179        quote_spanned! {op_span=>
180            let #ident = {
181                fn check_input<Pull, Item>(pull: Pull) -> impl #root::dfir_pipes::pull::Pull<Item = Item, Meta = Pull::Meta, CanPend = Pull::CanPend, CanEnd = Pull::CanEnd>
182                where
183                    Pull: #root::dfir_pipes::pull::Pull<Item = Item>,
184                {
185                    pull
186                }
187                check_input::<_, #generic_type>(#input)
188            };
189        }
190    } else {
191        let output = &outputs[0];
192        quote_spanned! {op_span=>
193            let #ident = {
194                fn check_output<Psh, Item>(push: Psh) -> impl #root::dfir_pipes::push::Push<Item, (), CanPend = Psh::CanPend>
195                where
196                    Psh: #root::dfir_pipes::push::Push<Item, ()>,
197                {
198                    push
199                }
200                check_output::<_, #generic_type>(#output)
201            };
202        }
203    }
204}
205
206/// [`OperatorConstraints::write_fn`] for unary identity operators.
207pub const IDENTITY_WRITE_FN: WriteFn = |write_context_args, _| {
208    let write_iterator = identity_write_iterator_fn(write_context_args);
209    Ok(OperatorWriteOutput {
210        write_prologue: Default::default(),
211        write_iterator,
212        write_iterator_after: Default::default(),
213        write_tick_end: Default::default(),
214    })
215};
216
217/// Helper to write the `write_iterator` portion of [`OperatorConstraints::write_fn`] output for
218/// the null operator - an operator that ignores all inputs and produces no output.
219pub fn null_write_iterator_fn(
220    &WriteContextArgs {
221        root,
222        op_span,
223        ident,
224        inputs,
225        outputs,
226        is_pull,
227        op_inst:
228            OperatorInstance {
229                generics: OpInstGenerics { type_args, .. },
230                ..
231            },
232        ..
233    }: &WriteContextArgs<'_>,
234) -> TokenStream {
235    let default_type = parse_quote_spanned! {op_span=> _};
236    let iter_type = type_args.first().unwrap_or(&default_type);
237
238    if is_pull {
239        quote_spanned! {op_span=>
240            let #ident = #root::dfir_pipes::pull::poll_fn({
241                #(
242                    let mut #inputs = ::std::boxed::Box::pin(#inputs);
243                )*
244                move |_cx| {
245                    // Make sure to poll all `#inputs` to completion.
246                    // NOTE(mingwei): `null()` can only have 0 or 1 inputs, though.
247                    // TODO(mingwei): Do we actually need to poll to completion or can we short-circuit?
248                    #(
249                        let #inputs = #root::dfir_pipes::pull::Pull::pull(
250                            ::std::pin::Pin::as_mut(&mut #inputs),
251                            <_ as #root::dfir_pipes::Context>::from_task(_cx),
252                        );
253                    )*
254                    #(
255                        if let #root::dfir_pipes::pull::PullStep::Pending(_) = #inputs {
256                            return #root::dfir_pipes::pull::PullStep::Pending(#root::dfir_pipes::Yes);
257                        }
258                    )*
259                    #root::dfir_pipes::pull::PullStep::<_, _, #root::dfir_pipes::Yes, _>::Ended(#root::dfir_pipes::Yes)
260                }
261            });
262        }
263    } else {
264        quote_spanned! {op_span=>
265            #[allow(clippy::let_unit_value, clippy::ignored_unit_patterns)]
266            let _ = (#(#outputs),*);
267            let #ident = #root::dfir_pipes::push::for_each::<_, #iter_type>(::std::mem::drop::<#iter_type>);
268        }
269    }
270}
271
272/// [`OperatorConstraints::write_fn`] for the null operator - an operator that ignores all inputs
273/// and produces no output.
274pub const NULL_WRITE_FN: WriteFn = |write_context_args, _| {
275    let write_iterator = null_write_iterator_fn(write_context_args);
276    Ok(OperatorWriteOutput {
277        write_prologue: Default::default(),
278        write_iterator,
279        write_iterator_after: Default::default(),
280        write_tick_end: Default::default(),
281    })
282};
283
284macro_rules! declare_ops {
285    ( $( $mod:ident :: $op:ident, )* ) => {
286        $( pub(crate) mod $mod; )*
287        /// All DFIR operators.
288        pub const OPERATORS: &[OperatorConstraints] = &[
289            $( $mod :: $op, )*
290        ];
291    };
292}
293declare_ops![
294    all_iterations::ALL_ITERATIONS,
295    anti_join::ANTI_JOIN,
296    assert::ASSERT,
297    assert_eq::ASSERT_EQ,
298    batch::BATCH,
299    batch_eager::BATCH_EAGER,
300    batch_lazy::BATCH_LAZY,
301    chain::CHAIN,
302    chain_first_n::CHAIN_FIRST_N,
303    _counter::_COUNTER,
304    cross_join::CROSS_JOIN,
305    cross_join_multiset::CROSS_JOIN_MULTISET,
306    cross_singleton::CROSS_SINGLETON,
307    demux_enum::DEMUX_ENUM,
308    dest_file::DEST_FILE,
309    dest_sink::DEST_SINK,
310    dest_sink_serde::DEST_SINK_SERDE,
311    difference::DIFFERENCE,
312    enumerate::ENUMERATE,
313    filter::FILTER,
314    filter_map::FILTER_MAP,
315    flat_map::FLAT_MAP,
316    flat_map_stream_blocking::FLAT_MAP_STREAM_BLOCKING,
317    flatten::FLATTEN,
318    flatten_stream_blocking::FLATTEN_STREAM_BLOCKING,
319    fold::FOLD,
320    fold_no_replay::FOLD_NO_REPLAY,
321    for_each::FOR_EACH,
322    identity::IDENTITY,
323    initialize::INITIALIZE,
324    inspect::INSPECT,
325    iter_ref::ITER_REF,
326    join::JOIN,
327    join_fused::JOIN_FUSED,
328    join_fused_lhs::JOIN_FUSED_LHS,
329    join_fused_rhs::JOIN_FUSED_RHS,
330    join_multiset::JOIN_MULTISET,
331    join_multiset_half::JOIN_MULTISET_HALF,
332    fold_keyed::FOLD_KEYED,
333    reduce_keyed::REDUCE_KEYED,
334    lattice_bimorphism::LATTICE_BIMORPHISM,
335    _lattice_fold_batch::_LATTICE_FOLD_BATCH,
336    lattice_fold::LATTICE_FOLD,
337    _lattice_join_fused_join::_LATTICE_JOIN_FUSED_JOIN,
338    lattice_reduce::LATTICE_REDUCE,
339    map::MAP,
340    union::UNION,
341    multiset_delta::MULTISET_DELTA,
342    defer_signal::DEFER_SIGNAL,
343    defer_tick::DEFER_TICK,
344    defer_tick_lazy::DEFER_TICK_LAZY,
345    null::NULL,
346    partition::PARTITION,
347    persist::PERSIST,
348    resolve_futures::RESOLVE_FUTURES,
349    resolve_futures_blocking::RESOLVE_FUTURES_BLOCKING,
350    resolve_futures_blocking_ordered::RESOLVE_FUTURES_BLOCKING_ORDERED,
351    resolve_futures_ordered::RESOLVE_FUTURES_ORDERED,
352    reduce::REDUCE,
353    reduce_no_replay::REDUCE_NO_REPLAY,
354    scan::SCAN,
355    scan_async_blocking::SCAN_ASYNC_BLOCKING,
356    snapshot::SNAPSHOT,
357    snapshot_keyed::SNAPSHOT_KEYED,
358    snapshot_prefix::SNAPSHOT_PREFIX,
359    spin::SPIN,
360    sort::SORT,
361    sort_by_key::SORT_BY_KEY,
362    source_file::SOURCE_FILE,
363    source_interval::SOURCE_INTERVAL,
364    source_iter::SOURCE_ITER,
365    source_json::SOURCE_JSON,
366    source_stdin::SOURCE_STDIN,
367    source_stream::SOURCE_STREAM,
368    source_stream_serde::SOURCE_STREAM_SERDE,
369    state::STATE,
370    state_by::STATE_BY,
371    tee::TEE,
372    unique::UNIQUE,
373    unzip::UNZIP,
374    zip::ZIP,
375    zip_longest::ZIP_LONGEST,
376];
377
378/// Get the operator lookup table, generating it if needed.
379pub fn operator_lookup() -> &'static HashMap<&'static str, &'static OperatorConstraints> {
380    pub static OPERATOR_LOOKUP: OnceLock<HashMap<&'static str, &'static OperatorConstraints>> =
381        OnceLock::new();
382    OPERATOR_LOOKUP.get_or_init(|| OPERATORS.iter().map(|op| (op.name, op)).collect())
383}
384/// Find an operator by [`GraphNode`].
385pub fn find_node_op_constraints(node: &GraphNode) -> Option<&'static OperatorConstraints> {
386    if let GraphNode::Operator(operator) = node {
387        find_op_op_constraints(operator)
388    } else {
389        None
390    }
391}
392/// Find an operator by an AST [`Operator`].
393pub fn find_op_op_constraints(operator: &Operator) -> Option<&'static OperatorConstraints> {
394    let name = &*operator.name_string();
395    operator_lookup().get(name).copied()
396}
397
398/// Context arguments provided to [`OperatorConstraints::write_fn`].
399#[derive(Clone)]
400pub struct WriteContextArgs<'a> {
401    /// `dfir` crate name for `use #root::something`.
402    pub root: &'a TokenStream,
403    /// `context` ident, the name of the provided
404    /// [`dfir_rs::scheduled::Context`](https://hydro.run/rustdoc/dfir_rs/scheduled/context/struct.Context.html).
405    pub context: &'a Ident,
406    /// `df` ident, the name of the
407    /// [`dfir_rs::scheduled::graph::Dfir`](https://hydro.run/rustdoc/dfir_rs/scheduled/graph/struct.Dfir.html)
408    /// instance.
409    pub df_ident: &'a Ident,
410    /// Subgraph ID in which this operator is contained.
411    pub subgraph_id: GraphSubgraphId,
412    /// Node ID identifying this operator in the flat or partitioned graph meta-datastructure.
413    pub node_id: GraphNodeId,
414    /// Loop ID in which this operator is contained, or `None` if not in a loop.
415    pub loop_id: Option<GraphLoopId>,
416    /// The source span of this operator.
417    pub op_span: Span,
418    /// Tag for this operator appended to the generated identifier.
419    pub op_tag: Option<String>,
420    /// Identifier for a function to call when doing work outside the stream.
421    pub work_fn: &'a Ident,
422    /// Identifier for a function to wrap futures when doing work outside the stream.
423    pub work_fn_async: &'a Ident,
424
425    /// Ident the `Stream` or `Sink` should be assigned to.
426    pub ident: &'a Ident,
427    /// If a pull `Stream` (true) or push `Sink` (false) should be used.
428    pub is_pull: bool,
429    /// Input `Stream` operator idents (or ref idents; used for pull).
430    pub inputs: &'a [Ident],
431    /// Output `Sink` operator idents (or ref idents; used for push).
432    pub outputs: &'a [Ident],
433
434    /// Operator name.
435    pub op_name: &'static str,
436    /// Operator instance arguments object.
437    pub op_inst: &'a OperatorInstance,
438    /// Arguments provided by the user into the operator as arguments.
439    /// I.e. the `a, b, c` in `-> my_op(a, b, c) -> `.
440    ///
441    /// These arguments include singleton postprocessing codegen, with
442    /// [`std::cell::RefCell::borrow_mut`] code pre-generated.
443    pub arguments: &'a Punctuated<Expr, Token![,]>,
444
445    /// The codegen options passed to
446    /// [`DfirGraph::as_code_with_options`](super::DfirGraph::as_code_with_options).
447    ///
448    /// Operators may consult these, e.g. to omit their own type-guard helper functions when
449    /// [`AsCodeOptions::exclude_type_guards`] is set.
450    pub options: &'a AsCodeOptions,
451}
452impl WriteContextArgs<'_> {
453    /// Generate a (almost certainly) unique identifier with the given suffix.
454    ///
455    /// Includes the subgraph and node IDs in the generated identifier.
456    ///
457    /// This will always return the same identifier for a given `suffix`.
458    pub fn make_ident(&self, suffix: impl AsRef<str>) -> Ident {
459        Ident::new(
460            &format!(
461                "sg_{:?}_node_{:?}_{}",
462                self.subgraph_id.data(),
463                self.node_id.data(),
464                suffix.as_ref(),
465            ),
466            self.op_span,
467        )
468    }
469
470    /// Returns the given number of persistence arguments, defaulting to `'tick` when unspecified.
471    pub fn persistence_args<const N: usize>(
472        &self,
473        diagnostics: &mut Diagnostics,
474    ) -> [Persistence; N] {
475        let len = self.op_inst.generics.persistence_args.len();
476        if 0 != len && 1 != len && N != len {
477            diagnostics.push(Diagnostic::spanned(
478                self.op_span,
479                Level::Error,
480                format!(
481                    "The operator `{}` only accepts 0, 1, or {} persistence arguments",
482                    self.op_name, N
483                ),
484            ));
485        }
486
487        let mut out = [Persistence::Tick; N];
488        self.op_inst
489            .generics
490            .persistence_args
491            .iter()
492            .copied()
493            .cycle() // Re-use the first element for both persistences.
494            .take(N)
495            .enumerate()
496            .for_each(|(i, p)| {
497                out[i] = p;
498            });
499        out
500    }
501}
502
503/// An object-safe version of [`RangeBounds`].
504pub trait RangeTrait<T>: Send + Sync + Debug
505where
506    T: ?Sized,
507{
508    /// Start (lower) bound.
509    fn start_bound(&self) -> Bound<&T>;
510    /// End (upper) bound.
511    fn end_bound(&self) -> Bound<&T>;
512    /// Returns if `item` is contained in this range.
513    fn contains(&self, item: &T) -> bool
514    where
515        T: PartialOrd<T>;
516
517    /// Turn this range into a human-readable string.
518    fn human_string(&self) -> String
519    where
520        T: Display + PartialEq,
521    {
522        match (self.start_bound(), self.end_bound()) {
523            (Bound::Unbounded, Bound::Unbounded) => "any number of".to_owned(),
524
525            (Bound::Included(n), Bound::Included(x)) if n == x => {
526                format!("exactly {}", n)
527            }
528            (Bound::Included(n), Bound::Included(x)) => {
529                format!("at least {} and at most {}", n, x)
530            }
531            (Bound::Included(n), Bound::Excluded(x)) => {
532                format!("at least {} and less than {}", n, x)
533            }
534            (Bound::Included(n), Bound::Unbounded) => format!("at least {}", n),
535            (Bound::Excluded(n), Bound::Included(x)) => {
536                format!("more than {} and at most {}", n, x)
537            }
538            (Bound::Excluded(n), Bound::Excluded(x)) => {
539                format!("more than {} and less than {}", n, x)
540            }
541            (Bound::Excluded(n), Bound::Unbounded) => format!("more than {}", n),
542            (Bound::Unbounded, Bound::Included(x)) => format!("at most {}", x),
543            (Bound::Unbounded, Bound::Excluded(x)) => format!("less than {}", x),
544        }
545    }
546}
547
548impl<R, T> RangeTrait<T> for R
549where
550    R: RangeBounds<T> + Send + Sync + Debug,
551{
552    fn start_bound(&self) -> Bound<&T> {
553        self.start_bound()
554    }
555
556    fn end_bound(&self) -> Bound<&T> {
557        self.end_bound()
558    }
559
560    fn contains(&self, item: &T) -> bool
561    where
562        T: PartialOrd<T>,
563    {
564        self.contains(item)
565    }
566}
567
568/// Persistence lifetimes: `'none`, `'loop`, `'tick`, or `'static`.
569#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug, Serialize, Deserialize)]
570pub enum Persistence {
571    /// Persistence for one "tick" (or loop iteration), i.e. no persistance at all
572    Tick,
573    /// Persistence across the entire program execution.
574    Static,
575}
576impl Persistence {
577    /// Returns a lowercase string for the persistence type.
578    pub fn to_str_lowercase(self) -> &'static str {
579        match self {
580            Persistence::Tick => "tick",
581            Persistence::Static => "static",
582        }
583    }
584}
585
586/// Helper which creates a error message string literal for when the Tokio runtime is not found.
587fn make_missing_runtime_msg(op_name: &str) -> Literal {
588    Literal::string(&format!(
589        "`{}()` must be used within a Tokio runtime. For example, use `#[dfir_rs::main]` on your main method.",
590        op_name
591    ))
592}
593
594/// Operator categories, for docs.
595#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, DocumentedVariants)]
596pub enum OperatorCategory {
597    /// Maps: Simple one-in-one-out operators.
598    Map,
599    /// Filters: One-in zero-or-one-out operators.
600    Filter,
601    /// Flattens: One-in multiple-out operators.
602    Flatten,
603    /// Folds: Operators which accumulate elements together.
604    Fold,
605    /// Keyed Folds: Operators which accumulate elements together by key.
606    KeyedFold,
607    /// Lattice Folds: Folds based on lattice-merge.
608    LatticeFold,
609    /// Persistent Operators: Persistent (stateful) operators.
610    Persistence,
611    /// Multi-Input Operators: Operators with multiple inputs.
612    MultiIn,
613    /// Multi-Output Operators: Operators with multiple outputs.
614    MultiOut,
615    /// Sources: Operators which produce output elements (and consume no inputs).
616    Source,
617    /// Sinks: Operators which consume input elements (and produce no outputs).
618    Sink,
619    /// Control Flow Operators: Operators which affect control flow/scheduling.
620    Control,
621    /// Compiler Fusion Operators: Operators which are necessary to implement certain optimizations and rewrite rules.
622    CompilerFusionOperator,
623    /// Windowing Operators: Operators for windowing `loop` inputs.
624    Windowing,
625    /// Un-Windowing Operators: Operators for collecting `loop` outputs.
626    Unwindowing,
627}
628impl OperatorCategory {
629    /// Human-readible heading name, for docs.
630    pub fn name(self) -> &'static str {
631        self.get_variant_docs().split_once(':').unwrap().0
632    }
633    /// Human description, for docs.
634    pub fn description(self) -> &'static str {
635        self.get_variant_docs().split_once(':').unwrap().1
636    }
637}
638
639/// Operator type for Flo semantics.
640#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug)]
641pub enum FloType {
642    /// A source operator, which must be at the top level.
643    Source,
644    /// A windowing operator, for moving data into a loop context.
645    Windowing,
646    /// A lazy windowing operator — moves data into a loop context but does not trigger the loop.
647    /// Data is dropped if the loop does not fire that tick.
648    WindowingLazy,
649    /// A retaining windowing operator — moves data into a loop context but does not trigger the
650    /// loop. Unlike [`FloType::WindowingLazy`], state is retained across ticks and emitted, as
651    /// clones, every time the loop fires — it is not consumed. The shape of the retained state
652    /// depends on the operator: `snapshot()` keeps a single last-write-wins value,
653    /// `snapshot_keyed()` the latest value per key, and `snapshot_prefix()` every element received
654    /// so far. Only valid at the entry of a root-level loop (nested loops iterate within a single
655    /// tick, so cross-tick retention does not apply).
656    WindowingRetain,
657    /// An eager windowing operator — moves data into a loop context and always triggers the loop,
658    /// even when the windowed input is empty. Only valid at the entry of a root-level loop (it is
659    /// disallowed in nested loops, where it would prevent the fixpoint iteration from terminating).
660    WindowingEager,
661    /// An un-windowing operator, for moving data out of a loop context.
662    Unwindowing,
663}