节点
节点是 Roplat 中最小的计算单元。
你可以把节点理解为“一个带生命周期的异步处理器”。
Node Trait(当前实现)
#[async_trait::async_trait]
pub trait Node: Send + Sync {
type Input;
type Output;
type Error: Into<RoplatError> + Debug + Send + Sync;
async fn process(&mut self, input: Self::Input) -> Self::Output;
async fn on_init(&mut self) -> Result<(), Self::Error> { Ok(()) }
async fn on_shutdown(&mut self) -> Result<(), Self::Error> { Ok(()) }
}
说明:
process直接返回Output。- 初始化与关闭通过
on_init/on_shutdown返回Result。
#[roplat::node] 的两种模式
- 原生模式
用于 Rust 原生节点,会触发字段分层与辅助代码生成。
- 多语言傀儡模式
#[roplat::node(
lang = "cpp",
input(sensor_input, SensorData),
output(motor_output, MotorCommand),
)]
pub struct CppController {
pub gain: f64,
pub offset: f64,
}
用于 Rust 侧声明多语言节点镜像,并参与 two-pass 代码生成。
多语言模式的编译期检查
当使用 #[roplat::node(lang = ...)] 时:
- input/output 类型必须实现
TypeBinding<目标语言>。 - 结构体字段类型也必须实现对应
TypeBinding。
类型未绑定时会在编译期报错,不会拖到运行期。
实践建议
- 节点名尽量与目标语言类名一致,减少映射成本。
process内避免阻塞式长耗时调用,必要时用异步任务拆分。- 生命周期逻辑尽量放
on_init/on_shutdown,保持process专注数据处理。