use crate::ast::SlotShape;
use crate::ast::{NodeMeta, PolydatNode, Port, PortType, Slot, Value};
#[crate::polydat_node(category = Diagnostic)]
fn identity(input: Value) -> Value {
input
}
pub struct PortPassthrough {
meta: NodeMeta,
}
impl PortPassthrough {
pub fn new(name: &str, port_type: crate::ast::PortType) -> Self {
Self {
meta: NodeMeta {
name: format!("__port_{name}"),
outs: vec![Port::new("output", port_type)],
ins: vec![Slot::Wire(Port::new("input", port_type))],
},
}
}
}
impl PolydatNode for PortPassthrough {
fn meta(&self) -> &NodeMeta {
&self.meta
}
fn eval(&self, inputs: &[Value], outputs: &mut [Value]) {
outputs[0] = inputs[0].clone();
}
fn compiled_u64(&self) -> Option<crate::ast::CompiledU64Op> {
if self.meta.outs[0].typ.slot_color() == crate::ast::SlotColor::Ref2 {
return None;
}
Some(Box::new(|inputs: &[u64], outputs: &mut [u64]| {
outputs.copy_from_slice(inputs)
}))
}
}
#[crate::polydat_node(category = Math)]
fn const_u64(value: crate::derive_support::Const<u64>) -> u64 {
*value
}
fn const_str_arc(s: &str) -> std::sync::Arc<str> {
std::sync::Arc::from(s)
}
fn const_str_compiled(
node: &ConstStr,
_wire_types: &[crate::ast::PortType],
) -> crate::ast::CompiledSlotKit {
let (ptr, len) = crate::kernel::static_pair(crate::kernel::StaticInterner::intern(&node.value));
crate::ast::CompiledSlotKit {
scratch: Vec::new(),
op: Box::new(
move |_inputs: &[u64], outputs: &mut [u64], _scratch: &mut [crate::ast::ScratchBuf]| {
outputs[0] = ptr;
outputs[1] = len;
},
),
}
}
#[crate::polydat_node(category = Diagnostic, compiled_slot = const_str_compiled)]
fn const_str(
#[poly_default("")] value: crate::derive_support::Const<&str>,
#[poly_const(const_str_arc, from = value)] cached: &std::sync::Arc<str>,
) -> std::sync::Arc<str> {
cached.clone()
}
pub struct ConstHandle {
meta: NodeMeta,
value: std::sync::Arc<dyn std::any::Any + Send + Sync>,
}
impl ConstHandle {
pub fn new(value: std::sync::Arc<dyn std::any::Any + Send + Sync>) -> Self {
Self {
meta: NodeMeta {
name: "const_handle".into(),
outs: vec![Port::new("output", PortType::Handle)],
ins: vec![],
},
value,
}
}
}
impl PolydatNode for ConstHandle {
fn meta(&self) -> &NodeMeta {
&self.meta
}
fn eval(&self, _inputs: &[Value], outputs: &mut [Value]) {
outputs[0] = Value::Handle(self.value.clone());
}
}
pub struct ConstExt {
meta: NodeMeta,
value: Box<dyn crate::ast::ReflectedValue>,
}
impl ConstExt {
pub fn new(value: Box<dyn crate::ast::ReflectedValue>) -> Self {
Self {
meta: NodeMeta {
name: "const_ext".into(),
outs: vec![Port::new("output", PortType::Ext)],
ins: vec![],
},
value,
}
}
}
impl PolydatNode for ConstExt {
fn meta(&self) -> &NodeMeta {
&self.meta
}
fn eval(&self, _inputs: &[Value], outputs: &mut [Value]) {
outputs[0] = Value::Ext(self.value.clone());
}
}