-
Notifications
You must be signed in to change notification settings - Fork 89
Rust::com Field and Method Rust Design and Example of APIs usage #818
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
Open
bharatGoswami8
wants to merge
27
commits into
eclipse-score:main
Choose a base branch
from
bharatGoswami8:Rust_Field_and_Method_APIs
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.
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
c278b25
Rust::com Method Interface APIs and Macro update
bharatGoswami8 a0b0359
Rust::com Type State Pattern derive macro added
bharatGoswami8 fdbea91
Rust::com Lola Runtime update for Method APIs
bharatGoswami8 afad28e
Rust::com Update mock runtime for method APIs
bharatGoswami8 0af8fe3
Rust::com score_com crate updated for public method interface
bharatGoswami8 7137981
Rust::com Example app update with Method APIs usage
bharatGoswami8 f9abcf4
Rust::com Crate documentation Update
bharatGoswami8 01e0831
Rust::com Update the return value of Methods
bharatGoswami8 36d892c
Rust::com Design documentation for Method APIs
bharatGoswami8 394cc7a
Rust::com MethodReturnSample and MethodInArgPtr trait added
bharatGoswami8 058e74f
Rust::com Create the Field APIs
bharatGoswami8 f971a9a
Rust::com Runtime placeholder Implementation for Field
bharatGoswami8 14f8257
Rust::com Create type state macro for field Init
bharatGoswami8 3d76ef9
Rust::com Update the example app with Field APIs usage
bharatGoswami8 8e26653
Rust::com Interface and type state macro optimization
bharatGoswami8 9a388ac
Merge branch 'method_branch' into Rust_Field_and_Method_APIs
bharatGoswami8 8c61c73
Merge Field APIs and Method APIs into single branch
bharatGoswami8 52b559d
Rust::com Update Field and Method documentation
bharatGoswami8 0bde0fc
Rust::com Update Field Methods using Method interface
bharatGoswami8 28ce074
Rust::com Add the Field Tags to enable different feature
bharatGoswami8 b8a687e
Rust::com Interface macro moduler
bharatGoswami8 bb7545a
Rust::com Update field design for registration APIs
bharatGoswami8 e89ee9f
Rust::com Update the sample example app
bharatGoswami8 f02fa96
Rust::com Doc test and additinal example added for Method and Field
bharatGoswami8 34e66c2
Rust::com Add get handler in type state pattern and tags for Field
bharatGoswami8 7907870
Rust::com Field Design diagram and document
bharatGoswami8 0920265
Rust::com Update the Interface macro modules
bharatGoswami8 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
101 changes: 101 additions & 0 deletions
101
score/mw/com/example/com-api-example/src/field_consumer.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,101 @@ | ||
| /******************************************************************************** | ||
| * 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 | ||
| ********************************************************************************/ | ||
|
|
||
| // This file demonstrate the usage of consumer field APIs, which are generated for the VehicleFieldInterface. | ||
|
|
||
| // Notes: we are creating consumer instance specific for field here but this is just for demonstration purpose, | ||
| // for same consumer instance method / event/ field can be subscribed as per subscribe interface. | ||
|
|
||
| // All the functions and types in this file are just for demonstration purpose, | ||
| // as this are not part of any callable because of that unused warning is suppressed for this file. | ||
|
|
||
| use score_com::{ | ||
| Builder, FindServiceSpecifier, InstanceSpecifier, Interface, Runtime, SampleContainer, | ||
| ServiceDiscovery, Subscriber, Subscription, | ||
| }; | ||
|
|
||
| use com_api_gen::{Tire, VehicleFieldInterface}; | ||
|
|
||
| #[allow(dead_code)] | ||
| type VehicleFieldConsumer<R> = <VehicleFieldInterface as Interface>::Consumer<R>; | ||
|
|
||
| // create the consumer. | ||
| #[allow(dead_code)] | ||
| fn create_consumer_field<R: Runtime>( | ||
| runtime: &R, | ||
| service_id: InstanceSpecifier, | ||
| ) -> VehicleFieldConsumer<R> { | ||
| let consumer_discovery = | ||
| runtime.find_service::<VehicleFieldInterface>(FindServiceSpecifier::Specific(service_id)); | ||
| let available_service_instances = consumer_discovery | ||
| .get_available_instances() | ||
| .expect("Failed to get available service instances"); | ||
|
|
||
| // Select service instance at specific handle_index | ||
| let handle_index = 0; // or any index you need from vector of instances | ||
| let consumer_builder = available_service_instances | ||
| .into_iter() | ||
| .nth(handle_index) | ||
| .expect("Failed to get consumer builder at specified handle index"); | ||
|
|
||
| consumer_builder | ||
| .build() | ||
| .expect("Failed to build consumer instance") | ||
| } | ||
|
|
||
| // Function to demonstrate the usage of the consumer to get and set fields, | ||
| // and subscribe to field update notifications. | ||
| // | ||
| // Field get/set are now fully async via the MethodCaller-based wrappers generated by the | ||
| // interface! macro. They live on the consumer struct as separate fields (`{name}_get`, | ||
| // `{name}_set`) and are independent of subscribe() - they remain usable before and after. | ||
| // | ||
| // Because subscribe() takes `left_tire` by value, extract the callers before subscribing | ||
| // if both are needed in the same async context. | ||
| #[allow(dead_code)] | ||
| async fn consumer_processing_field<R: Runtime>(consumer: VehicleFieldConsumer<R>) { | ||
| // Async get via the generated get_left_tire() wrapper. | ||
| // Uses MethodCaller<(), Tire> under the hood - reuses Method infrastructure. | ||
| match consumer.get_left_tire().await { | ||
| Ok(result) => println!("Current tire pressure (async get): {:?}", *result), | ||
| Err(e) => eprintln!("Failed to get tire pressure: {:?}", e), | ||
| } | ||
|
|
||
| // Async set via the generated set_left_tire(val) wrapper. | ||
| // Uses MethodCaller<(Tire,), Tire> under the hood - reuses Method infrastructure. | ||
| match consumer.set_left_tire(Tire { pressure: 35.0 }).await { | ||
| Ok(result) => println!("Confirmed tire pressure after set: {:?}", *result), | ||
| Err(e) => eprintln!("Failed to set tire pressure: {:?}", e), | ||
| } | ||
|
|
||
| // Subscribe to the field to receive update notifications (field value changes). | ||
| // Note: subscribe() takes `left_tire` by value. The get/set callers are separate | ||
| // struct fields so they are not consumed here, but the consumer is partially moved. | ||
| let subscription = consumer | ||
| .left_tire | ||
| .subscribe(3) | ||
| .expect("Failed to subscribe to field"); | ||
|
|
||
| // Poll for updates (non-blocking) | ||
| let mut sample_buf = SampleContainer::new(3); | ||
| match subscription.try_receive(&mut sample_buf, 1) { | ||
| Ok(n) if n > 0 => { | ||
| while let Some(sample) = sample_buf.pop_front() { | ||
| println!("Updated tire pressure: {:?}", *sample); | ||
| } | ||
| } | ||
| _ => println!("No new tire pressure updates available"), | ||
| } | ||
|
|
||
| // subscription is automatically unsubscribed when dropped | ||
| } |
96 changes: 96 additions & 0 deletions
96
score/mw/com/example/com-api-example/src/field_producer.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,96 @@ | ||
| /******************************************************************************** | ||
| * 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 | ||
| ********************************************************************************/ | ||
|
|
||
| // This file demonstrate the usage of producer field APIs, which are generated for the VehicleFieldInterface. | ||
|
|
||
| // Notes: we are creating producer instance specific for field here but this is just for demonstration purpose, | ||
| // for same producer instance method / event/ field can be offered as per offer interface. | ||
|
|
||
| // All the functions and types in this file are just for demonstration purpose, | ||
| // as this are not part of any callable because of that unused warning is suppressed for this file. | ||
|
|
||
| use score_com::{Builder, FieldPublisher, InstanceSpecifier, Interface, Producer, Runtime}; | ||
|
|
||
| use com_api_gen::{Exhaust, Tire, VehicleFieldInterface}; | ||
|
|
||
| // VehicleFieldProducer is the producer type for the VehicleField interface (before offering) | ||
| #[allow(dead_code)] | ||
| type VehicleFieldProducer<R> = <VehicleFieldInterface as Interface>::Producer<R>; | ||
| // VehicleFieldOfferedProducer is the offered producer type for the VehicleField interface (fields support update/set-handler) | ||
| #[allow(dead_code)] | ||
| type VehicleFieldOfferedProducer<R> = | ||
| <<VehicleFieldInterface as Interface>::Producer<R> as Producer<R>>::OfferedProducer; | ||
|
|
||
| // Below function demonstrates the field APIs usage. | ||
| // This builds fine, but it cannot run as the field APIs in Lola runtime are not implemented yet. | ||
|
|
||
| // Producer creation and initialization of fields with initial values and set handlers for the fields. | ||
| // It will return the offered producer instance which can be used to update the fields. | ||
| #[allow(dead_code)] | ||
| fn create_producer_field<R: Runtime + 'static>( | ||
| runtime: &R, | ||
| service_id: InstanceSpecifier, | ||
| initial_tire_value: Tire, | ||
| initial_exhaust_value: Exhaust, | ||
| ) -> VehicleFieldOfferedProducer<R> | ||
| where | ||
| <R as Runtime>::FieldPublisher<Tire>: Send + Sync, | ||
| <R as Runtime>::FieldPublisher<Exhaust>: Send, | ||
| { | ||
| let producer_builder = runtime.producer_builder::<VehicleFieldInterface>(service_id); | ||
| let producer = producer_builder | ||
| .build() | ||
| .expect("Failed to build producer instance"); | ||
|
|
||
| // Use validator pattern with compile-time type-state validation | ||
| // Must register handlers and initialize all fields before offer() is available | ||
| let offered = producer | ||
| .init() | ||
| // Register set-handler callbacks - required before offer() for WithSetter fields | ||
| .register_set_handler_left_tire(move |val: Tire| { | ||
| println!("Received tire pressure update: {:?}", val); | ||
| // Additional logic: inspect or act on the accepted value (logging, telemetry, etc.). | ||
| // TODO: in working example add that logic to demonstrate the set handler usage. | ||
| }) | ||
| .register_set_handler_exhaust(|val: Exhaust| { | ||
| let _ = val; | ||
| println!("Received exhaust update"); | ||
| }) | ||
| // Register get-handler callbacks - required before offer() for WithGetter fields | ||
| .register_get_handler_left_tire(|| Tire { pressure: 32.0 }) | ||
| .register_get_handler_exhaust(|| Exhaust {}) | ||
| .update_left_tire(initial_tire_value) | ||
| .expect("Failed to update left_tire field") | ||
| .update_exhaust(initial_exhaust_value) | ||
| .expect("Failed to update exhaust field") | ||
| .offer() | ||
| .expect("Failed to offer producer instance"); | ||
|
|
||
| offered | ||
| } | ||
|
|
||
| // Function to demonstrate the usage of the offered producer to update fields | ||
| #[allow(dead_code)] | ||
| fn offered_producer_process<R: Runtime>(offered_producer: VehicleFieldOfferedProducer<R>) { | ||
| // Use the offered producer to update fields | ||
| let new_tire_value = Tire { pressure: 32.0 }; | ||
| let new_exhaust_value = Exhaust {}; | ||
| offered_producer | ||
| .left_tire | ||
| .update(new_tire_value) | ||
| .expect("Failed to update left_tire field"); | ||
| offered_producer | ||
| .exhaust | ||
| .update(new_exhaust_value) | ||
| .expect("Failed to update exhaust field"); | ||
| } | ||
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
Oops, something went wrong.
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.
update example with return value.