Skip to content

Commit 23d0424

Browse files
committed
Support crate renaming in proc macros.
See: rust-lang/rust#54363
1 parent 378a7ac commit 23d0424

File tree

7 files changed

+43
-17
lines changed

7 files changed

+43
-17
lines changed

lib/protoflow-derive/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ default = []
2222

2323
[dependencies]
2424
proc-macro2 = { version = "1", default-features = false }
25+
proc-macro-crate = "3.1"
2526
quote = { version = "1", default-features = false }
2627
syn = { version = "2", default-features = true }
2728

lib/protoflow-derive/src/derives/block.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// This is free and unencumbered software released into the public domain.
22

3+
use crate::util::protoflow_crate;
34
use proc_macro2::TokenStream;
45
use quote::quote;
56
use syn::{self, Data, DataStruct, DeriveInput, Fields, FieldsNamed, FieldsUnnamed};
67

78
pub(crate) fn expand_derive_block(input: &DeriveInput) -> Result<TokenStream, syn::Error> {
9+
let protoflow = protoflow_crate();
810
let ident = &input.ident;
911
let generics = &input.generics;
1012
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
@@ -33,13 +35,13 @@ pub(crate) fn expand_derive_block(input: &DeriveInput) -> Result<TokenStream, sy
3335
unused_qualifications,
3436
clippy::redundant_locals,
3537
)]
36-
impl #impl_generics protoflow::BlockDescriptor for #ident #ty_generics #where_clause {
37-
fn inputs(&self) -> protoflow::prelude::Vec<protoflow::PortDescriptor> {
38-
protoflow::prelude::vec![] // TODO
38+
impl #impl_generics #protoflow::BlockDescriptor for #ident #ty_generics #where_clause {
39+
fn inputs(&self) -> #protoflow::prelude::Vec<#protoflow::PortDescriptor> {
40+
#protoflow::prelude::vec![] // TODO
3941
}
4042

41-
fn outputs(&self) -> protoflow::prelude::Vec<protoflow::PortDescriptor> {
42-
protoflow::prelude::vec![] // TODO
43+
fn outputs(&self) -> #protoflow::prelude::Vec<#protoflow::PortDescriptor> {
44+
#protoflow::prelude::vec![] // TODO
4345
}
4446
}
4547
})

lib/protoflow-derive/src/derives/function_block.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// This is free and unencumbered software released into the public domain.
22

3+
use crate::util::protoflow_crate;
34
use proc_macro2::TokenStream;
45
use quote::quote;
56
use syn::{self, Data, DataStruct, DeriveInput, Fields, FieldsNamed, FieldsUnnamed};
67

78
pub(crate) fn expand_derive_function_block(input: &DeriveInput) -> Result<TokenStream, syn::Error> {
9+
let protoflow = protoflow_crate();
810
let ident = &input.ident;
911
let generics = &input.generics;
1012
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<TokenS
3335
unused_qualifications,
3436
clippy::redundant_locals,
3537
)]
36-
impl #impl_generics protoflow::BlockDescriptor for #ident #ty_generics #where_clause {
37-
fn inputs(&self) -> protoflow::prelude::Vec<protoflow::PortDescriptor> {
38-
protoflow::prelude::vec![protoflow::PortDescriptor::from(&self.0)]
38+
impl #impl_generics #protoflow::BlockDescriptor for #ident #ty_generics #where_clause {
39+
fn inputs(&self) -> #protoflow::prelude::Vec<#protoflow::PortDescriptor> {
40+
#protoflow::prelude::vec![#protoflow::PortDescriptor::from(&self.0)]
3941
}
4042

41-
fn outputs(&self) -> protoflow::prelude::Vec<protoflow::PortDescriptor> {
42-
protoflow::prelude::vec![protoflow::PortDescriptor::from(&self.1)]
43+
fn outputs(&self) -> #protoflow::prelude::Vec<#protoflow::PortDescriptor> {
44+
#protoflow::prelude::vec![#protoflow::PortDescriptor::from(&self.1)]
4345
}
4446
}
4547

@@ -48,14 +50,14 @@ pub(crate) fn expand_derive_function_block(input: &DeriveInput) -> Result<TokenS
4850
unused_qualifications,
4951
clippy::redundant_locals,
5052
)]
51-
impl #impl_generics protoflow::Block for #ident #ty_generics #where_clause {
52-
fn execute(&mut self, runtime: &dyn protoflow::Runtime) -> protoflow::BlockResult<()> {
53+
impl #impl_generics #protoflow::Block for #ident #ty_generics #where_clause {
54+
fn execute(&mut self, runtime: &dyn #protoflow::Runtime) -> #protoflow::BlockResult<()> {
5355
let input = &self.0;
5456
let output = &self.1;
55-
while let Some(message) = protoflow::InputPort::receive(input)? {
56-
if protoflow::Port::is_connected(output) {
57-
let result = protoflow::FunctionBlock::compute(self, message)?;
58-
protoflow::OutputPort::send(output, &result)?;
57+
while let Some(message) = #protoflow::InputPort::receive(input)? {
58+
if #protoflow::Port::is_connected(output) {
59+
let result = #protoflow::FunctionBlock::compute(self, message)?;
60+
#protoflow::OutputPort::send(output, &result)?;
5961
}
6062
}
6163
Ok(())

lib/protoflow-derive/src/derives/system.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// This is free and unencumbered software released into the public domain.
22

3+
use crate::util::protoflow_crate;
34
use proc_macro2::TokenStream;
45
use quote::quote;
56
use syn::{self, Data, DataStruct, DeriveInput, Fields, FieldsNamed, FieldsUnnamed};
67

78
pub(crate) fn expand_derive_system(input: &DeriveInput) -> Result<TokenStream, syn::Error> {
9+
let protoflow = protoflow_crate();
810
let ident = &input.ident;
911
let generics = &input.generics;
1012
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, s
3335
unused_qualifications,
3436
clippy::redundant_locals,
3537
)]
36-
impl #impl_generics protoflow::System for #ident #ty_generics #where_clause {}
38+
impl #impl_generics #protoflow::System for #ident #ty_generics #where_clause {}
3739
})
3840
}

lib/protoflow-derive/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
extern crate proc_macro;
1010

1111
mod derives;
12+
pub(crate) mod util;
1213

1314
use proc_macro::TokenStream;
1415
use syn::{parse_macro_input, DeriveInput};

lib/protoflow-derive/src/util.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This is free and unencumbered software released into the public domain.
2+
3+
use proc_macro2::{Span, TokenStream};
4+
use proc_macro_crate::{crate_name, FoundCrate};
5+
use quote::quote;
6+
use syn::Ident;
7+
8+
pub(crate) fn protoflow_crate() -> TokenStream {
9+
let found_crate = crate_name("protoflow").expect("protoflow is present in `Cargo.toml`");
10+
match found_crate {
11+
FoundCrate::Itself => quote!(crate),
12+
FoundCrate::Name(name) => {
13+
let ident = Ident::new(&name, Span::call_site());
14+
quote!(#ident)
15+
}
16+
}
17+
}

lib/protoflow/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#![no_std]
44

5+
#[doc(hidden)]
56
pub mod prelude;
67

78
mod block;

0 commit comments

Comments
 (0)