Skip to main content

fission_core/ui/
traits.rs

1//! Lowering traits for converting widgets into the intermediate representation.
2
3use crate::lowering::LoweringContext;
4use fission_ir::NodeId;
5use std::fmt::Debug;
6
7/// Converts a widget struct into `fission-ir` nodes.
8///
9/// Every built-in widget implements `Lower`. The method receives a
10/// [`LoweringContext`] and returns the root [`NodeId`] of the emitted IR
11/// subgraph.
12pub trait Lower {
13    /// Lower this widget into the IR, returning the root node id.
14    fn lower(&self, cx: &mut LoweringContext) -> NodeId;
15}
16
17/// Object-safe variant of [`Lower`] for use inside [`CustomNode`](crate::ui::CustomNode).
18///
19/// Implement this trait when you need custom lowering logic that cannot be
20/// expressed with the built-in widget primitives.
21///
22/// # Example
23///
24/// ```rust,ignore
25/// #[derive(Debug)]
26/// struct MyCanvasLowerer;
27///
28/// impl LowerDyn for MyCanvasLowerer {
29///     fn lower_dyn(&self, cx: &mut LoweringContext) -> NodeId {
30///         // emit custom IR nodes...
31///         cx.next_node_id()
32///     }
33///
34///     fn stable_key(&self) -> u64 {
35///         0xCAFE
36///     }
37/// }
38/// ```
39pub trait LowerDyn: Send + Sync + Debug {
40    /// Lower this widget into the IR, returning the root node id.
41    fn lower_dyn(&self, cx: &mut LoweringContext) -> NodeId;
42    /// A stable key used for structural diffing. Override to provide a
43    /// content-based hash.
44    fn stable_key(&self) -> u64 {
45        0
46    }
47}