-
Notifications
You must be signed in to change notification settings - Fork 11
Add minimal example: hello-mailbox-async #151
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
Merged
lxsaah
merged 3 commits into
aimdb-dev:main
from
CernyMil:Add-minimal-example--hello-mailbox-async--#99
Jun 29, 2026
+118
−41
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
|
CernyMil marked this conversation as resolved.
|
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
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,12 @@ | ||
| [package] | ||
| name = "hello-mailbox-async" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| description = "AimDB minimal async example demonstrating Mailbox buffer (latest-wins) semantics" | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
| aimdb-core = { path = "../../aimdb-core", features = ["std"] } | ||
| aimdb-tokio-adapter = { path = "../../aimdb-tokio-adapter", features = ["tokio-runtime"] } | ||
| tokio = { workspace = true, features = ["time"] } |
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,25 @@ | ||
| # hello-mailbox-async: async Mailbox buffer demo | ||
| The Mailbox buffer keeps only the last message written. It's like a real mailbox where only the most recent letter is visible. | ||
|
|
||
| ## When to use | ||
| This Mailbox buffer demo is useful in a variety of scenarios where you want to retain only the last message, for example: | ||
| - When you have a robotic arm that needs to execute a sequence of commands, and you only want to execute the last command, or even if the robot gets too much commands in a short period of time. | ||
|
|
||
| ## How it works | ||
| The Mailbox buffer demo works by simulating a mailbox buffer that retains only the last message using async API. | ||
| It sends 3 Colors and the MailBox only gets the last message. | ||
|
|
||
| ## How to run | ||
| From the workspace root, run: | ||
| ``` | ||
| cargo run -p hello-mailbox-async | ||
| ``` | ||
| **Expected output** | ||
| ``` | ||
| === hello-mailbox-async: Mailbox buffer demo === | ||
| Firing three rapid commands BEFORE consumer exists: Red → Green → Blue | ||
| ✓ Got: Blue ← only the latest survived | ||
| Shutting down... | ||
| ✓ Done. | ||
| ``` | ||
|
CernyMil marked this conversation as resolved.
|
||
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,55 @@ | ||
| use aimdb_core::{buffer::BufferCfg, AimDbBuilder}; | ||
| use aimdb_tokio_adapter::{TokioAdapter, TokioRecordRegistrarExt}; | ||
| use std::sync::Arc; | ||
|
|
||
| // Enum of colors for the LED | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| enum Color { | ||
| Red, | ||
| Green, | ||
| Blue, | ||
| } | ||
|
|
||
| // Struct representing the LED state | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| struct Led { | ||
| color: Color, | ||
| } | ||
|
|
||
| // Main function | ||
| #[tokio::main] | ||
| async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| println!("=== hello-mailbox-async: Mailbox buffer demo ===\n"); | ||
|
|
||
| // configuration | ||
| let adapter = Arc::new(TokioAdapter); | ||
| let mut builder = AimDbBuilder::new().runtime(adapter); | ||
|
|
||
| builder.configure::<Led>("actuator.led", |reg| { | ||
| reg.buffer(BufferCfg::Mailbox) | ||
| .source(|ctx, producer| async move { | ||
| // Produce quickly BEFORE creating the consumer | ||
| println!("Firing three rapid commands BEFORE consumer exists: Red → Green → Blue"); | ||
| producer.produce(Led { color: Color::Red }); | ||
| producer.produce(Led { | ||
| color: Color::Green, | ||
| }); | ||
| producer.produce(Led { color: Color::Blue }); | ||
| ctx.time().sleep_millis(100).await; | ||
| }) | ||
| .tap(|ctx, consumer| async move { | ||
| // Now we create the consumer — it will only see the last value in the Mailbox | ||
| let mut reader = consumer.subscribe(); | ||
| ctx.time().sleep_millis(100).await; | ||
|
|
||
| match reader.recv().await { | ||
| Ok(msg) => println!(" ✓ Got: {:?} ← only the latest survived", msg.color), | ||
| Err(_) => println!(" (mailbox was already empty)"), | ||
| } | ||
| }); | ||
| }); | ||
| builder.run().await?; | ||
| println!("Shutting down..."); | ||
| println!(" ✓ Done."); | ||
| Ok(()) | ||
| } |
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 |
|---|---|---|
|
|
@@ -32,4 +32,5 @@ cargo run -p hello-mailbox | |
| ✓ Got: Green ← only the latest survived (Green) | ||
| 3. Shutting down... | ||
| ✓ Done. | ||
| ``` | ||
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.
Uh oh!
There was an error while loading. Please reload this page.