-
Notifications
You must be signed in to change notification settings - Fork 46
Rust::com Interface declarative macro for com-api #159
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
bharatGoswami8
wants to merge
1
commit into
eclipse-score:main
Choose a base branch
from
bharatGoswami8:declarative_macro_for_interface
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
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
170 changes: 170 additions & 0 deletions
170
score/mw/com/impl/rust/com-api/com-api-concept-macros/interface_macros.rs
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,170 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2025 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Apache License Version 2.0 which is available at | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| ********************************************************************************/ | ||
|
|
||
| /// Main interface macro that generates Consumer, Producer, and OfferedProducer types | ||
| /// along with all necessary trait implementations. | ||
| /// | ||
| /// Automatically generates unique type names from the identifier of macro invocation. | ||
| /// For an interface with identifier `{id}`, it generates: | ||
| /// - `{id}Interface` - Struct representing the interface with INTERFACE_ID constant | ||
| /// - `{id}Consumer<R>` - Consumer implementation with event subscribers | ||
| /// - `{id}Producer<R>` - Producer implementation | ||
| /// - `{id}OfferedProducer<R>` - Offered producer implementation with event publishers | ||
| /// - Implements the `Interface`, `Consumer`, `Producer`, and `OfferedProducer` traits | ||
| /// for the respective types. | ||
| /// - `id` is used to be UID for the interface. | ||
| /// | ||
| /// Parameters: | ||
| /// - Keywords: `interface` followed by the interface identifier and a block of event definitions. | ||
| /// - `$id`: Simple identifier used for type name generation (e.g., Vehicle, Engine) | ||
| /// - `$event_name`: Event field name | ||
| /// - `$event_type`: Event data type | ||
| /// Example usage: | ||
| /// ``` | ||
| /// interface!( | ||
| /// interface Vehicle { | ||
| /// left_tire: Event<Tire>, | ||
| /// exhaust: Event<Exhaust>, | ||
| /// } | ||
| /// ); | ||
| /// ``` | ||
| /// The generated code will include: | ||
| /// - `VehicleInterface` struct with `INTERFACE_ID = "Vehicle"` | ||
| /// - `VehicleConsumer<R>` struct that implements `Consumer` trait for subscribing to "left_tire" and "exhaust" events. | ||
| /// - `VehicleProducer<R>` struct that implements `Producer` trait for producing "left_tire" and "exhaust" events. | ||
| /// - `VehicleOfferedProducer<R>` struct that implements `OfferedProducer` trait for offering "left_tire" and "exhaust" events. | ||
| pub use paste::paste; | ||
|
|
||
| #[macro_export] | ||
| macro_rules! interface { | ||
| (interface $id:ident { $($event_name:ident : Event<$event_type:ty>),+$(,)? }) => { | ||
| $crate::paste! { | ||
| $crate::interface_common!($id); | ||
| $crate::interface_consumer!($id, $($event_name, Event<$event_type>),+); | ||
| $crate::interface_producer!($id, $($event_name, Event<$event_type>),+); | ||
| } | ||
| }; | ||
|
|
||
| (interface $id:ident { $($event_name:ident : Method<$event_type:ty>),+$(,)? }) => { | ||
| compile_error!("Method definitions are not supported in this macro version. Please use Event<T> syntax for defining events."); | ||
| }; | ||
|
|
||
| (interface $id:ident { $($event_name:ident : Field<$event_type:ty>),+$(,)? }) => { | ||
| compile_error!("Field definitions are not supported in this macro version. Please use Event<T> syntax for defining events."); | ||
| }; | ||
| } | ||
|
|
||
| /// Macro create a unique interface struct and implement the Interface trait for it. | ||
| /// Generates the INTERFACE_ID constant and associated Consumer/Producer types. | ||
| #[macro_export] | ||
| macro_rules! interface_common { | ||
| ($id:ident) => { | ||
| $crate::paste! { | ||
| pub struct [<$id Interface>] {} | ||
| impl com_api::Interface for [<$id Interface>] { | ||
| const INTERFACE_ID: &'static str = stringify!($id); | ||
|
Contributor
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. misssing fully qualified path ? |
||
| type Consumer<R: com_api::Runtime + ?Sized> = [<$id Consumer>]<R>; | ||
| type Producer<R: com_api::Runtime + ?Sized> = [<$id Producer>]<R>; | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /// Macro to implement the Consumer trait for a given interface ID and its events. | ||
| /// Generates the Consumer struct with subscribers for each event. | ||
| #[macro_export] | ||
| macro_rules! interface_consumer { | ||
| ($id:ident, $($event_name:ident, Event<$event_type:ty>),+$(,)?) => { | ||
| $crate::paste! { | ||
| pub struct [<$id Consumer>]<R: com_api::Runtime + ?Sized> { | ||
| $( | ||
| pub $event_name: R::Subscriber<$event_type>, | ||
| )+ | ||
| } | ||
|
|
||
| impl<R: com_api::Runtime + ?Sized> com_api::Consumer<R> for [<$id Consumer>]<R> { | ||
| fn new(instance_info: R::ConsumerInfo) -> Self { | ||
| [<$id Consumer>] { | ||
| $( | ||
| $event_name: R::Subscriber::new( | ||
| stringify!($event_name), | ||
| instance_info.clone() | ||
| ).expect(&format!("Failed to create subscriber for {}", stringify!($event_name))), | ||
| )+ | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /// Macro to implement the Producer and OfferedProducer traits for a given interface ID and its events. | ||
| /// Generates Producer and OfferedProducer structs with publishers for each event. | ||
| #[macro_export] | ||
| macro_rules! interface_producer { | ||
| ($id:ident, $($event_name:ident, Event<$event_type:ty>),+$(,)?) => { | ||
| $crate::paste! { | ||
| pub struct [<$id Producer>]<R: com_api::Runtime + ?Sized> { | ||
| _runtime: core::marker::PhantomData<R>, | ||
| instance_info: R::ProviderInfo, | ||
| } | ||
|
|
||
| pub struct [<$id OfferedProducer>]<R: com_api::Runtime + ?Sized> { | ||
| $( | ||
| pub $event_name: R::Publisher<$event_type>, | ||
| )+ | ||
| instance_info: R::ProviderInfo, | ||
| } | ||
|
|
||
| impl<R: com_api::Runtime + ?Sized> com_api::Producer<R> for [<$id Producer>]<R> { | ||
| type Interface = [<$id Interface>]; | ||
| type OfferedProducer = [<$id OfferedProducer>]<R>; | ||
| fn offer(self) -> com_api::Result<Self::OfferedProducer> { | ||
| let offered = [<$id OfferedProducer>] { | ||
| $( | ||
| $event_name: R::Publisher::new( | ||
| stringify!($event_name), | ||
| self.instance_info.clone() | ||
| ).expect(&format!("Failed to create publisher for {}", stringify!($event_name))), | ||
| )+ | ||
| instance_info: self.instance_info.clone(), | ||
| }; | ||
| // Offer the service instance to make it discoverable | ||
| self.instance_info.offer_service()?; | ||
| Ok(offered) | ||
| } | ||
|
|
||
| fn new(instance_info: R::ProviderInfo) -> com_api::Result<Self> { | ||
| Ok([<$id Producer>] { | ||
| _runtime: core::marker::PhantomData, | ||
| instance_info, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl<R: com_api::Runtime + ?Sized> com_api::OfferedProducer<R> for [<$id OfferedProducer>]<R> { | ||
| type Interface = [<$id Interface>]; | ||
| type Producer = [<$id Producer>]<R>; | ||
| fn unoffer(self) -> com_api::Result<Self::Producer> { | ||
| let producer = [<$id Producer>] { | ||
| _runtime: core::marker::PhantomData, | ||
| instance_info: self.instance_info.clone(), | ||
| }; | ||
| // Stop offering the service instance to withdraw it from system availability | ||
| self.instance_info.stop_offer_service()?; | ||
| Ok(producer) | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -22,3 +22,5 @@ pub use com_api_concept::{ | |
| ProviderInfo, Publisher, Reloc, Result, Runtime, RuntimeBuilder, SampleContainer, | ||
| SampleMaybeUninit, SampleMut, ServiceDiscovery, Subscriber, Subscription, | ||
| }; | ||
|
|
||
| pub use com_api_concept_interface_macros::interface; | ||
|
Contributor
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. missing empty line at the end |
||
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.
why seperate lib ?
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.
We can not export declarative_macro form proc_macro crate.
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.
cant this be part of main crate ?