use crate::node::{CompiledU64Op, GkNode, NodeMeta, Port, PortType, Slot, Value};
pub struct Identity {
meta: NodeMeta,
}
impl Default for Identity {
fn default() -> Self {
Self::new()
}
}
impl Identity {
pub fn new() -> Self {
Self {
meta: NodeMeta {
name: "identity".into(),
outs: vec![Port::u64("output")],
ins: vec![Slot::Wire(Port::u64("input"))],
},
}
}
}
impl GkNode for Identity {
fn meta(&self) -> &NodeMeta {
&self.meta
}
fn eval(&self, inputs: &[Value], outputs: &mut [Value]) {
outputs[0] = inputs[0].clone();
}
fn compiled_u64(&self) -> Option<CompiledU64Op> {
Some(Box::new(|inputs, outputs| {
outputs[0] = inputs[0];
}))
}
}
pub struct PortPassthrough {
meta: NodeMeta,
}
impl PortPassthrough {
pub fn new(name: &str, port_type: crate::node::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 GkNode for PortPassthrough {
fn meta(&self) -> &NodeMeta {
&self.meta
}
fn eval(&self, inputs: &[Value], outputs: &mut [Value]) {
outputs[0] = inputs[0].clone();
}
}
pub struct ConstU64 {
meta: NodeMeta,
value: u64,
}
impl ConstU64 {
pub fn new(value: u64) -> Self {
Self {
meta: NodeMeta {
name: "const".into(),
outs: vec![Port::u64("output")],
ins: vec![Slot::const_u64("value", value)],
},
value,
}
}
}
impl GkNode for ConstU64 {
fn meta(&self) -> &NodeMeta {
&self.meta
}
fn eval(&self, _inputs: &[Value], outputs: &mut [Value]) {
outputs[0] = Value::U64(self.value);
}
fn compiled_u64(&self) -> Option<CompiledU64Op> {
let value = self.value;
Some(Box::new(move |_inputs, outputs| {
outputs[0] = value;
}))
}
}
pub struct ConstStr {
meta: NodeMeta,
value: std::sync::Arc<str>,
}
impl ConstStr {
pub fn new(value: impl Into<std::sync::Arc<str>>) -> Self {
let value: std::sync::Arc<str> = value.into();
Self {
meta: NodeMeta {
name: "const_str".into(),
outs: vec![Port::str("output")],
ins: vec![Slot::const_str("value", value.to_string())],
},
value,
}
}
}
impl GkNode for ConstStr {
fn meta(&self) -> &NodeMeta {
&self.meta
}
fn eval(&self, _inputs: &[Value], outputs: &mut [Value]) {
outputs[0] = Value::Str(self.value.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 GkNode 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::node::ReflectedValue>,
}
impl ConstExt {
pub fn new(value: Box<dyn crate::node::ReflectedValue>) -> Self {
Self {
meta: NodeMeta {
name: "const_ext".into(),
outs: vec![Port::new("output", PortType::Ext)],
ins: vec![],
},
value,
}
}
}
impl GkNode for ConstExt {
fn meta(&self) -> &NodeMeta { &self.meta }
fn eval(&self, _inputs: &[Value], outputs: &mut [Value]) {
outputs[0] = Value::Ext(self.value.clone());
}
}