Skip to content

Commit

Permalink
Support crate renaming in proc macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
artob committed Jul 28, 2024
1 parent 378a7ac commit 23d0424
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 17 deletions.
1 change: 1 addition & 0 deletions lib/protoflow-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
12 changes: 7 additions & 5 deletions lib/protoflow-derive/src/derives/block.rs
Original file line number Diff line number Diff line change
@@ -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<TokenStream, syn::Error> {
let protoflow = protoflow_crate();
let ident = &input.ident;
let generics = &input.generics;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
Expand Down Expand Up @@ -33,13 +35,13 @@ pub(crate) fn expand_derive_block(input: &DeriveInput) -> Result<TokenStream, sy
unused_qualifications,
clippy::redundant_locals,
)]
impl #impl_generics protoflow::BlockDescriptor for #ident #ty_generics #where_clause {
fn inputs(&self) -> protoflow::prelude::Vec<protoflow::PortDescriptor> {
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::PortDescriptor> {
protoflow::prelude::vec![] // TODO
fn outputs(&self) -> #protoflow::prelude::Vec<#protoflow::PortDescriptor> {
#protoflow::prelude::vec![] // TODO
}
}
})
Expand Down
24 changes: 13 additions & 11 deletions lib/protoflow-derive/src/derives/function_block.rs
Original file line number Diff line number Diff line change
@@ -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<TokenStream, syn::Error> {
let protoflow = protoflow_crate();
let ident = &input.ident;
let generics = &input.generics;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
Expand Down Expand Up @@ -33,13 +35,13 @@ pub(crate) fn expand_derive_function_block(input: &DeriveInput) -> Result<TokenS
unused_qualifications,
clippy::redundant_locals,
)]
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)]
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::PortDescriptor> {
protoflow::prelude::vec![protoflow::PortDescriptor::from(&self.1)]
fn outputs(&self) -> #protoflow::prelude::Vec<#protoflow::PortDescriptor> {
#protoflow::prelude::vec![#protoflow::PortDescriptor::from(&self.1)]
}
}

Expand All @@ -48,14 +50,14 @@ pub(crate) fn expand_derive_function_block(input: &DeriveInput) -> Result<TokenS
unused_qualifications,
clippy::redundant_locals,
)]
impl #impl_generics protoflow::Block for #ident #ty_generics #where_clause {
fn execute(&mut self, runtime: &dyn protoflow::Runtime) -> 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(())
Expand Down
4 changes: 3 additions & 1 deletion lib/protoflow-derive/src/derives/system.rs
Original file line number Diff line number Diff line change
@@ -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<TokenStream, syn::Error> {
let protoflow = protoflow_crate();
let ident = &input.ident;
let generics = &input.generics;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
Expand Down Expand Up @@ -33,6 +35,6 @@ pub(crate) fn expand_derive_system(input: &DeriveInput) -> Result<TokenStream, s
unused_qualifications,
clippy::redundant_locals,
)]
impl #impl_generics protoflow::System for #ident #ty_generics #where_clause {}
impl #impl_generics #protoflow::System for #ident #ty_generics #where_clause {}
})
}
1 change: 1 addition & 0 deletions lib/protoflow-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
extern crate proc_macro;

mod derives;
pub(crate) mod util;

use proc_macro::TokenStream;
use syn::{parse_macro_input, DeriveInput};
Expand Down
17 changes: 17 additions & 0 deletions lib/protoflow-derive/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This is free and unencumbered software released into the public domain.

use proc_macro2::{Span, TokenStream};
use proc_macro_crate::{crate_name, FoundCrate};
use quote::quote;
use syn::Ident;

pub(crate) fn protoflow_crate() -> 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)
}
}
}
1 change: 1 addition & 0 deletions lib/protoflow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#![no_std]

#[doc(hidden)]
pub mod prelude;

mod block;
Expand Down

0 comments on commit 23d0424

Please sign in to comment.