-
Notifications
You must be signed in to change notification settings - Fork 170
Draft: Derive macro for StructuredParameters #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
BCSol
wants to merge
1
commit into
ros2-rust:main
Choose a base branch
from
BCSol:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
[workspace] | ||
members = [ | ||
"rclrs", | ||
"rclrs_proc_macros", | ||
] | ||
resolver = "2" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,9 +19,10 @@ | |
<depend>builtin_interfaces</depend> | ||
<depend>rcl_interfaces</depend> | ||
<depend>rosgraph_msgs</depend> | ||
<depend>rosidl_default_generators</depend> | ||
|
||
<test_depend>test_msgs</test_depend> | ||
<test_depend>example_interfaces</test_depend> | ||
<depend>test_msgs</depend> | ||
<depend>example_interfaces</depend> | ||
Comment on lines
+22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these are tinkering steps to resolve cargo build without colcon. |
||
|
||
<export> | ||
<build_type>ament_cargo</build_type> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name= "rclrs_proc_macros" | ||
version = "0.0.1" | ||
authors = ["Balthasar Schüss <[email protected]>"] | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
description = "A rust library providing proc macros for rclrs" | ||
rust-version = "1.75" | ||
|
||
|
||
[lib] | ||
path = "src/lib.rs" | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = "1.0" | ||
quote = "1.0" | ||
syn = "2.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::DeriveInput; | ||
|
||
pub(crate) fn derive_struct_parameters(input: DeriveInput) -> syn::Result<TokenStream> { | ||
let ident = input.ident; | ||
|
||
let fields = match input.data { | ||
syn::Data::Struct(ref s) => &s.fields, | ||
_ => { | ||
return syn::Result::Err(syn::Error::new_spanned( | ||
ident, | ||
"StrucutredParameter trait can only be derived for structs", | ||
)); | ||
} | ||
}; | ||
|
||
let field_types: Vec<_> = fields.iter().map(|f| &f.ty).collect(); | ||
|
||
let mut args = Vec::new(); | ||
for f in fields { | ||
let ident = f.ident.as_ref().unwrap(); | ||
let ident_str = syn::LitStr::new(&f.ident.as_ref().unwrap().to_string(), ident.span()); | ||
let field_type = match &f.ty { | ||
syn::Type::Path(p) => { | ||
let mut p = p.path.clone(); | ||
for segment in &mut p.segments { | ||
segment.arguments = syn::PathArguments::None; | ||
} | ||
p | ||
} | ||
e => { | ||
return syn::Result::Err(syn::Error::new_spanned( | ||
e, | ||
"attribute can only be path type", | ||
)); | ||
} | ||
}; | ||
let r = quote! { | ||
#ident : #field_type::declare_structured( | ||
node, &{match name { | ||
"" => #ident_str.to_string(), | ||
prefix => [prefix, ".", #ident_str].concat(), | ||
} | ||
})?, | ||
}; | ||
args.push(r); | ||
} | ||
|
||
let result = quote!( | ||
impl #ident { | ||
const _ASSERT_PARAMETER: fn() = || { | ||
fn assert_parameter<T: rclrs::StructuredParameters>() {} | ||
#( | ||
assert_parameter::<#field_types>(); | ||
)* | ||
}; | ||
} | ||
|
||
impl rclrs::StructuredParameters for #ident { | ||
fn declare_structured(node: &rclrs::NodeState, name: &str) -> core::result::Result<Self, rclrs::DeclarationError> { | ||
core::result::Result::Ok(Self{ #(#args)*}) | ||
} | ||
} | ||
); | ||
syn::Result::Ok(result) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
mod r#impl; | ||
use proc_macro::TokenStream; | ||
use syn::{parse_macro_input, DeriveInput}; | ||
|
||
#[proc_macro_derive(StructuredParameters)] | ||
pub fn derive_struct_parameters(input: TokenStream) -> TokenStream { | ||
let input = parse_macro_input!(input as DeriveInput); | ||
r#impl::derive_struct_parameters(input) | ||
.unwrap_or_else(|e| e.to_compile_error()) | ||
.into() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was unable to build with cargo without these changes.
With colcon everything was working fine.
These can probably be reverted, but then development with rust-analyzer doesn't really work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only ran into this issue after adding the crate
rclrs_proc_macros
and enabling proc-macro=True in the cargo config