From cc1c343bf384751d40b602ef6a24067a7e97d5ba Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Sat, 5 Dec 2026 12:15:08 +0100 Subject: [PATCH] Flag adapter nodes and register them by identifier --- .../interpreted-executor/src/node_registry.rs | 64 ++++++++++++------- .../libraries/core-types/src/registry.rs | 11 ++++ node-graph/node-macro/src/codegen.rs | 7 ++ node-graph/node-macro/src/parsing.rs | 30 ++++++++- node-graph/nodes/gcore/src/ops.rs | 24 +++---- node-graph/preprocessor/src/lib.rs | 1 + 6 files changed, 102 insertions(+), 35 deletions(-) diff --git a/node-graph/interpreted-executor/src/node_registry.rs b/node-graph/interpreted-executor/src/node_registry.rs index c33668f962..826d44dc32 100644 --- a/node-graph/interpreted-executor/src/node_registry.rs +++ b/node-graph/interpreted-executor/src/node_registry.rs @@ -155,14 +155,24 @@ fn node_registry() -> HashMap {{ + let row: (ProtoNodeIdentifier, NodeConstructor, NodeIOTypes) = $row; + let own = (graphene_core::ops::$module::IDENTIFIER, row.1, row.2.clone()); + [row, own] + }}; + } // A conversion row registered under the same `input_adapter<$element>` identifier so a convertible-but-not-identical // ranked wire can feed an `Item<$element>` connector, converting each element via `Into` macro_rules! input_adapter_row { (from_element: $from:ty, element: $element:ty) => {{ - let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = vec![ - input_adapter_row!(node: IntoItemNode, from: Item<$from>, to: Item<$element>, element: $element), - input_adapter_row!(node: IntoListNode, from: List<$from>, to: List<$element>, element: $element), - ]; + let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = [ + also_under_own_identifier!(into_item, input_adapter_row!(node: IntoItemNode, from: Item<$from>, to: Item<$element>, element: $element)), + also_under_own_identifier!(into_list, input_adapter_row!(node: IntoListNode, from: List<$from>, to: List<$element>, element: $element)), + ] + .concat(); entries }}; (node: $node:ident, from: $from:ty, to: $to:ty, element: $element:ty) => { @@ -254,7 +264,7 @@ fn node_registry() -> HashMap = Vec::new(); $( entries.extend(input_adapter_node!(element: $element)); - entries.push(item_to_list_node!(element: $element)); + entries.extend(also_under_own_identifier!(item_to_list, item_to_list_node!(element: $element))); )* entries }}; @@ -359,9 +369,10 @@ fn node_registry() -> HashMap {{ - let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = vec![ - $(input_adapter_node!(name: "input_adapter", from: List<$element>, to: ListDyn, element: ListDyn),)* - ]; + let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = [ + $(also_under_own_identifier!(into, input_adapter_node!(name: "input_adapter", from: List<$element>, to: ListDyn, element: ListDyn)),)* + ] + .concat(); entries }}; } @@ -394,8 +405,8 @@ fn node_registry() -> HashMap {{ let mut entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = Vec::new(); $( - entries.push(bundle_node!(element: $element)); - entries.push(unbundle_node!(element: $element)); + entries.extend(also_under_own_identifier!(bundle, bundle_node!(element: $element))); + entries.extend(also_under_own_identifier!(unbundle, unbundle_node!(element: $element))); )* entries }}; @@ -455,10 +466,11 @@ fn node_registry() -> HashMap {{ - let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = vec![ - input_adapter_row!(node: EmbedItemNode, from: Item<$from>, to: Item<$element>, element: $element), - input_adapter_row!(node: EmbedListNode, from: List<$from>, to: List<$element>, element: $element), - ]; + let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = [ + also_under_own_identifier!(embed_item, input_adapter_row!(node: EmbedItemNode, from: Item<$from>, to: Item<$element>, element: $element)), + also_under_own_identifier!(embed_list, input_adapter_row!(node: EmbedListNode, from: List<$from>, to: List<$element>, element: $element)), + ] + .concat(); entries }}; } @@ -475,10 +487,11 @@ fn node_registry() -> HashMap {{ - let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = vec![ - input_adapter_row!(node: ConvertItemNode, from: Item<$from>, to: Item<$element>, element: $element), - input_adapter_row!(node: ConvertListNode, from: List<$from>, to: List<$element>, element: $element), - ]; + let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = [ + also_under_own_identifier!(convert_item, input_adapter_row!(node: ConvertItemNode, from: Item<$from>, to: Item<$element>, element: $element)), + also_under_own_identifier!(convert_list, input_adapter_row!(node: ConvertListNode, from: List<$from>, to: List<$element>, element: $element)), + ] + .concat(); entries }}; } @@ -500,13 +513,19 @@ fn node_registry() -> HashMap) => { - input_adapter_row!(node: ItemToAttributeValueNode, from: Item<$element>, to: Item, element: AttributeValueDyn) + also_under_own_identifier!( + item_to_attribute_value, + input_adapter_row!(node: ItemToAttributeValueNode, from: Item<$element>, to: Item, element: AttributeValueDyn) + ) }; (List<$element:ty>) => { - input_adapter_row!(node: ListToAttributeValueNode, from: List<$element>, to: Item, element: AttributeValueDyn) + also_under_own_identifier!( + list_to_attribute_value, + input_adapter_row!(node: ListToAttributeValueNode, from: List<$element>, to: Item, element: AttributeValueDyn) + ) }; } - let attribute_value_rows: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = vec![ + let attribute_value_rows: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = [ attribute_value_node!(Item), attribute_value_node!(Item), attribute_value_node!(Item), @@ -531,7 +550,8 @@ fn node_registry() -> HashMap>), attribute_value_node!(List), attribute_value_node!(List), - ]; + ] + .concat(); node_types.extend(attribute_value_rows); // Whole-List Transform companion rows: a rank-1 content wire composes the matrix onto every item macro_rules! transform_list_node { diff --git a/node-graph/libraries/core-types/src/registry.rs b/node-graph/libraries/core-types/src/registry.rs index 6413e87862..241055c95d 100644 --- a/node-graph/libraries/core-types/src/registry.rs +++ b/node-graph/libraries/core-types/src/registry.rs @@ -29,6 +29,17 @@ pub struct NodeFlags { pub forwards_input: bool, pub inject_scope: bool, pub asynchronous: bool, + /// Whether type resolution may insert this node on an edge, from `#[node_macro::node(adapter(...))]`. + pub adapter: Option, +} + +/// How an adapter node changes the wire it is inserted on. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum AdapterKind { + /// Converts the wire's element into another element type. + Conversion, + /// Changes the wire's rank, like raising an `Item` onto a `List`. + Rank, } // Translation struct between macro and definition diff --git a/node-graph/node-macro/src/codegen.rs b/node-graph/node-macro/src/codegen.rs index 7cd9bd37a8..ab51890087 100644 --- a/node-graph/node-macro/src/codegen.rs +++ b/node-graph/node-macro/src/codegen.rs @@ -838,8 +838,14 @@ pub(crate) fn generate_node_code(crate_ident: &CrateIdent, parsed: &ParsedNodeFn footprint_invariant, forwards_input, inject_scope, + adapter, .. } = attributes.flags; + let adapter = match adapter { + Some(AdapterKind::Conversion) => quote!(Some(gcore::registry::AdapterKind::Conversion)), + Some(AdapterKind::Rank) => quote!(Some(gcore::registry::AdapterKind::Rank)), + None => quote!(None), + }; let asynchronous = parsed.is_async; let version = attributes.version.unwrap_or(1); // A node returning a `Destructure` struct records its fields as output connectors @@ -1053,6 +1059,7 @@ pub(crate) fn generate_node_code(crate_ident: &CrateIdent, parsed: &ParsedNodeFn forwards_input: #forwards_input, inject_scope: #inject_scope, asynchronous: #asynchronous, + adapter: #adapter, }, version: #version, output_fields: #output_fields, diff --git a/node-graph/node-macro/src/parsing.rs b/node-graph/node-macro/src/parsing.rs index fd8aa03d68..609e858764 100644 --- a/node-graph/node-macro/src/parsing.rs +++ b/node-graph/node-macro/src/parsing.rs @@ -54,6 +54,15 @@ pub(crate) struct NodeFlags { pub(crate) no_partial: bool, /// Set on the `Destructure` derive's generated extractor nodes, which take the struct rather than an `Item` or `List`. Not parsed from source. pub(crate) destructure_extractor: bool, + /// Whether type resolution may insert this node on an edge, and how it changes the wire. + pub(crate) adapter: Option, +} + +/// Mirrors `core_types::registry::AdapterKind`, which this proc-macro crate cannot depend on. +#[derive(Debug, Clone, Copy)] +pub(crate) enum AdapterKind { + Conversion, + Rank, } #[derive(Debug, Default, Clone)] @@ -557,6 +566,25 @@ impl Parse for NodeFnAttributes { // Example usage: // #[node_macro::node(..., forwards_input, ...)] "forwards_input" => set_flag(&mut flags.forwards_input, meta, "forwards_input")?, + // Marks the node as an adapter that type resolution may insert on an edge, either converting the wire's element type or changing its rank. + // + // Example usage: + // #[node_macro::node(..., adapter(conversion), ...)] + // #[node_macro::node(..., adapter(rank), ...)] + "adapter" => { + let meta = meta.require_list()?; + if flags.adapter.is_some() { + return Err(Error::new_spanned(meta, "Multiple 'adapter' attributes are not allowed")); + } + let kind: Ident = meta + .parse_args() + .map_err(|_| Error::new_spanned(meta, "Expected 'conversion' or 'rank' for 'adapter', e.g., adapter(rank)"))?; + flags.adapter = Some(match kind.to_string().as_str() { + "conversion" => AdapterKind::Conversion, + "rank" => AdapterKind::Rank, + _ => return Err(Error::new_spanned(kind, "Expected 'conversion' or 'rank' for 'adapter', e.g., adapter(rank)")), + }); + } // Instructs the preprocessor to make this node available as a scope. // Other nodes can then access it with `#[scope(node::IDENTIFIER)]`. // @@ -604,7 +632,7 @@ impl Parse for NodeFnAttributes { indoc!( r#" Unsupported attribute in `node`. - Supported attributes are 'category', 'name', 'icon', 'path', 'skip_impl', 'inspector', 'cfg', 'memoize', 'footprint_invariant', 'forwards_input', 'inject_scope', 'placeholder', 'no_partial', and 'version'. + Supported attributes are 'category', 'name', 'icon', 'path', 'skip_impl', 'inspector', 'cfg', 'memoize', 'footprint_invariant', 'forwards_input', 'adapter', 'inject_scope', 'placeholder', 'no_partial', and 'version'. Example usage: #[node_macro::node(..., name("Test Node"), ...)] "# diff --git a/node-graph/nodes/gcore/src/ops.rs b/node-graph/nodes/gcore/src/ops.rs index 2362f84637..37ed05c228 100644 --- a/node-graph/nodes/gcore/src/ops.rs +++ b/node-graph/nodes/gcore/src/ops.rs @@ -13,19 +13,19 @@ fn passthrough<'i, T: 'i + Send>(_: impl Ctx, content: T) -> T { } /// Shifts a whole wire value onto a connector's type through the std `Into` trait, serving the whole-`List` erasure onto `ListDyn` under the input adapter identifier. -#[node_macro::node(category("_Adapters"), skip_impl)] +#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))] fn into<'i, T: 'i + Send + Into, O: 'i + Send>(_: impl Ctx, value: T, _out_ty: PhantomData) -> O { value.into() } /// Raises an `Item` wire onto a `List` connector as its one-element list. -#[node_macro::node(category("_Adapters"), skip_impl)] +#[node_macro::node(category("_Adapters"), skip_impl, adapter(rank))] fn item_to_list<'i, T: 'i + Send>(_: impl Ctx, value: Item) -> List { value.into() } /// Boxes a ranked wire's element into a type-erased attribute value, carrying the cell's attributes through the wire. -#[node_macro::node(category("_Adapters"), skip_impl)] +#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))] fn item_to_attribute_value<'i, T: 'i + Clone + Send + Sync + Default + std::fmt::Debug + PartialEq + CacheHash + 'static>( _: impl Ctx, value: Item, @@ -36,7 +36,7 @@ fn item_to_attribute_value<'i, T: 'i + Clone + Send + Sync + Default + std::fmt: } /// Boxes a whole `List` wire as one type-erased attribute value, for attributes whose per-item value is itself a collection. -#[node_macro::node(category("_Adapters"), skip_impl)] +#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))] fn list_to_attribute_value<'i, T: 'i + Clone + Send + Sync + Default + std::fmt::Debug + PartialEq + CacheHash + 'static>( _: impl Ctx, value: T, @@ -46,27 +46,27 @@ fn list_to_attribute_value<'i, T: 'i + Clone + Send + Sync + Default + std::fmt: } /// Wraps a whole `List` onto the wire as one rank-0 `Item>` so an entire collection can feed a connector that carries it as one opaque cell. -#[node_macro::node(category("_Adapters"), skip_impl)] +#[node_macro::node(category("_Adapters"), skip_impl, adapter(rank))] fn bundle<'i, T: 'i + Send>(_: impl Ctx, value: List) -> Item> { Item::new_from_element(Bundle(value)) } /// Unwraps a `Bundle` wire back into the whole `List` it carries, restoring the collection after it passed through a connector as one opaque cell. -#[node_macro::node(category("_Adapters"), skip_impl)] +#[node_macro::node(category("_Adapters"), skip_impl, adapter(rank))] fn unbundle<'i, T: 'i + Send>(_: impl Ctx, value: Item>) -> List { value.into_element().0 } /// Converts an `Item` wire's element to a different element type it can produce through the std `Into` trait, /// letting a convertible wire feed an `Item` connector whose element type it does not match by identity. -#[node_macro::node(category("_Adapters"), skip_impl)] +#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))] fn into_item<'i, T: 'i + Send + Into, E: 'i + Send>(_: impl Ctx, value: Item, _element_ty: PhantomData) -> Item { let (value, attributes) = value.into_parts(); Item::from_parts(value.into(), attributes) } /// The `List` counterpart of `into_item`, converting every element to a different element type it can produce. -#[node_macro::node(category("_Adapters"), skip_impl)] +#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))] fn into_list<'i, T: 'i + Send + Clone + Into, E: 'i + Send>(_: impl Ctx, value: List, _element_ty: PhantomData) -> List { value .into_iter() @@ -79,19 +79,19 @@ fn into_list<'i, T: 'i + Send + Clone + Into, E: 'i + Send>(_: impl Ctx, valu /// Moves a whole `Item` wire inside an element type like `Graphic` that embeds ranked values as its variants. /// The attributes describe the element they arrived with, so they travel inside it and the fresh envelope starts empty. -#[node_macro::node(category("_Adapters"), skip_impl)] +#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))] fn embed_item<'i, T: 'i + Send, E: 'i + Send + From>>(_: impl Ctx, value: Item, _element_ty: PhantomData) -> Item { Item::new_from_element(value.into()) } /// The `List` counterpart of `embed_item`, each item moving whole inside its own embedding element. -#[node_macro::node(category("_Adapters"), skip_impl)] +#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))] fn embed_list<'i, T: 'i + Send + Clone, E: 'i + Send + From>>(_: impl Ctx, value: List, _element_ty: PhantomData) -> List { value.into_iter().map(|item| Item::new_from_element(E::from(item))).collect() } /// The [`Convert`]-based counterpart of `into_item`, casting an `Item` wire's element to a connector's numeric element type. -#[node_macro::node(category("_Adapters"), skip_impl)] +#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))] fn convert_item<'i, T: 'i + Send + Convert, E: 'i + Send>(ctx: impl Ctx + ExtractFootprint, value: Item, _element_ty: PhantomData) -> Item { let footprint = *ctx.try_footprint().unwrap_or(&Footprint::DEFAULT); let (value, attributes) = value.into_parts(); @@ -100,7 +100,7 @@ fn convert_item<'i, T: 'i + Send + Convert, E: 'i + Send>(ctx: impl Ctx + } /// The `List` counterpart of `convert_item`, casting every element to the connector's numeric element type. -#[node_macro::node(category("_Adapters"), skip_impl)] +#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))] fn convert_list<'i, T: 'i + Send + Clone + Convert, E: 'i + Send + Clone>(ctx: impl Ctx + ExtractFootprint, value: List, _element_ty: PhantomData) -> List { let footprint = *ctx.try_footprint().unwrap_or(&Footprint::DEFAULT); diff --git a/node-graph/preprocessor/src/lib.rs b/node-graph/preprocessor/src/lib.rs index 3622d65394..4841d8e5a9 100644 --- a/node-graph/preprocessor/src/lib.rs +++ b/node-graph/preprocessor/src/lib.rs @@ -206,6 +206,7 @@ impl Preprocessor { forwards_input, inject_scope, asynchronous, + adapter: _, }, output_fields, context_features,