diff --git a/lib/protoflow-derive/Cargo.toml b/lib/protoflow-derive/Cargo.toml index 585ea31..f4b642d 100644 --- a/lib/protoflow-derive/Cargo.toml +++ b/lib/protoflow-derive/Cargo.toml @@ -22,6 +22,7 @@ default = [] [dependencies] proc-macro2 = { version = "1", default-features = false } +proc-macro-crate = "3.1" quote = { version = "1", default-features = false } syn = { version = "2", default-features = true } diff --git a/lib/protoflow-derive/src/derives/block.rs b/lib/protoflow-derive/src/derives/block.rs index be1a49a..a29d8b2 100644 --- a/lib/protoflow-derive/src/derives/block.rs +++ b/lib/protoflow-derive/src/derives/block.rs @@ -1,10 +1,12 @@ // This is free and unencumbered software released into the public domain. +use crate::util::protoflow_crate; use proc_macro2::TokenStream; use quote::quote; use syn::{self, Data, DataStruct, DeriveInput, Fields, FieldsNamed, FieldsUnnamed}; pub(crate) fn expand_derive_block(input: &DeriveInput) -> Result { + let protoflow = protoflow_crate(); let ident = &input.ident; let generics = &input.generics; let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); @@ -33,13 +35,13 @@ pub(crate) fn expand_derive_block(input: &DeriveInput) -> Result protoflow::prelude::Vec { - protoflow::prelude::vec![] // TODO + impl #impl_generics #protoflow::BlockDescriptor for #ident #ty_generics #where_clause { + fn inputs(&self) -> #protoflow::prelude::Vec<#protoflow::PortDescriptor> { + #protoflow::prelude::vec![] // TODO } - fn outputs(&self) -> protoflow::prelude::Vec { - protoflow::prelude::vec![] // TODO + fn outputs(&self) -> #protoflow::prelude::Vec<#protoflow::PortDescriptor> { + #protoflow::prelude::vec![] // TODO } } }) diff --git a/lib/protoflow-derive/src/derives/function_block.rs b/lib/protoflow-derive/src/derives/function_block.rs index 21a5720..e20de3e 100644 --- a/lib/protoflow-derive/src/derives/function_block.rs +++ b/lib/protoflow-derive/src/derives/function_block.rs @@ -1,10 +1,12 @@ // This is free and unencumbered software released into the public domain. +use crate::util::protoflow_crate; use proc_macro2::TokenStream; use quote::quote; use syn::{self, Data, DataStruct, DeriveInput, Fields, FieldsNamed, FieldsUnnamed}; pub(crate) fn expand_derive_function_block(input: &DeriveInput) -> Result { + let protoflow = protoflow_crate(); let ident = &input.ident; let generics = &input.generics; let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); @@ -33,13 +35,13 @@ pub(crate) fn expand_derive_function_block(input: &DeriveInput) -> Result protoflow::prelude::Vec { - protoflow::prelude::vec![protoflow::PortDescriptor::from(&self.0)] + impl #impl_generics #protoflow::BlockDescriptor for #ident #ty_generics #where_clause { + fn inputs(&self) -> #protoflow::prelude::Vec<#protoflow::PortDescriptor> { + #protoflow::prelude::vec![#protoflow::PortDescriptor::from(&self.0)] } - fn outputs(&self) -> protoflow::prelude::Vec { - protoflow::prelude::vec![protoflow::PortDescriptor::from(&self.1)] + fn outputs(&self) -> #protoflow::prelude::Vec<#protoflow::PortDescriptor> { + #protoflow::prelude::vec![#protoflow::PortDescriptor::from(&self.1)] } } @@ -48,14 +50,14 @@ pub(crate) fn expand_derive_function_block(input: &DeriveInput) -> Result protoflow::BlockResult<()> { + impl #impl_generics #protoflow::Block for #ident #ty_generics #where_clause { + fn execute(&mut self, runtime: &dyn #protoflow::Runtime) -> #protoflow::BlockResult<()> { let input = &self.0; let output = &self.1; - while let Some(message) = protoflow::InputPort::receive(input)? { - if protoflow::Port::is_connected(output) { - let result = protoflow::FunctionBlock::compute(self, message)?; - protoflow::OutputPort::send(output, &result)?; + while let Some(message) = #protoflow::InputPort::receive(input)? { + if #protoflow::Port::is_connected(output) { + let result = #protoflow::FunctionBlock::compute(self, message)?; + #protoflow::OutputPort::send(output, &result)?; } } Ok(()) diff --git a/lib/protoflow-derive/src/derives/system.rs b/lib/protoflow-derive/src/derives/system.rs index 113d2a5..43194b8 100644 --- a/lib/protoflow-derive/src/derives/system.rs +++ b/lib/protoflow-derive/src/derives/system.rs @@ -1,10 +1,12 @@ // This is free and unencumbered software released into the public domain. +use crate::util::protoflow_crate; use proc_macro2::TokenStream; use quote::quote; use syn::{self, Data, DataStruct, DeriveInput, Fields, FieldsNamed, FieldsUnnamed}; pub(crate) fn expand_derive_system(input: &DeriveInput) -> Result { + let protoflow = protoflow_crate(); let ident = &input.ident; let generics = &input.generics; let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); @@ -33,6 +35,6 @@ pub(crate) fn expand_derive_system(input: &DeriveInput) -> Result TokenStream { + let found_crate = crate_name("protoflow").expect("protoflow is present in `Cargo.toml`"); + match found_crate { + FoundCrate::Itself => quote!(crate), + FoundCrate::Name(name) => { + let ident = Ident::new(&name, Span::call_site()); + quote!(#ident) + } + } +} diff --git a/lib/protoflow/src/lib.rs b/lib/protoflow/src/lib.rs index bfb601b..5d6b8e1 100644 --- a/lib/protoflow/src/lib.rs +++ b/lib/protoflow/src/lib.rs @@ -2,6 +2,7 @@ #![no_std] +#[doc(hidden)] pub mod prelude; mod block;