-
Notifications
You must be signed in to change notification settings - Fork 13
#94 — Minimal example: hello-mailbox
#98
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 11 commits into
aimdb-dev:main
from
ggmaldo:fix/Add-minimal-example-hello-mailbox
May 10, 2026
+141
−4
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c2f4c7e
feat: add hello-mailbox example
ggmaldo ed9ee42
Suggestions by the mantainer
ggmaldo 077d929
Update Cargo.toml
ggmaldo 2041e3f
Update examples/hello-mailbox/Cargo.toml
ggmaldo e132dbf
Update examples/hello-mailbox/Cargo.toml
ggmaldo 61cc628
Update examples/hello-mailbox/src/main.rs
ggmaldo b2f23a5
Update examples/hello-mailbox/src/main.rs
ggmaldo 2621392
Update examples/hello-mailbox/src/main.rs
ggmaldo 835bcd2
Update examples/hello-mailbox/README.md
ggmaldo a5f3289
Update examples/hello-mailbox/README.md
ggmaldo 9068c14
chore: add hello-mailbox to Makefile fmt and examples targets
ggmaldo 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| [package] | ||
| name = "hello-mailbox" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| description = "Hello Mailbox for issue #94" | ||
|
ggmaldo marked this conversation as resolved.
Outdated
|
||
| publish = false | ||
|
|
||
| [dependencies] | ||
| aimdb-core = { path = "../../aimdb-core", features = ["std"] } | ||
| aimdb-tokio-adapter = { path = "../../aimdb-tokio-adapter", features = ["tokio-runtime"] } | ||
| aimdb-sync = { path = "../../aimdb-sync" } | ||
| tokio = { workspace = true, features = ["full"] } | ||
| serde = { workspace = true, features = ["derive"] } | ||
| rand = { version = "0.8", features = ["std_rng"] } | ||
|
ggmaldo marked this conversation as resolved.
Outdated
|
||
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,36 @@ | ||
| # hello-mailbox: Mailbox buffer demo | ||
| The Mailbox buffer demo proves that only the last message is retained/read, in simple terms | ||
| It's like when you have a mailbox and only you can see the last letter. | ||
|
ggmaldo marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## 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. | ||
| it runs 2 rounds: | ||
| - the first round sends 3 Colors and the MailBox only gets the last message. | ||
| - the second round sends 2 Colors and the MailBox only gets the last message. | ||
|
|
||
| ## How to run | ||
| To run the demo, compile and run the `hello-mailbox` example, you need to go to the `aimdb/examples/hello-mailbox` directory and run: | ||
|
ggmaldo marked this conversation as resolved.
Outdated
|
||
| ``` | ||
| cargo run -p hello-mailbox | ||
| ``` | ||
| **Expected output** | ||
| ``` | ||
| === hello-mailbox: Mailbox buffer demo === | ||
|
|
||
| Round 1 | ||
| 1. Firing three rapid commands BEFORE consumer exists: Red → Green → Blue | ||
| 2. Consumer created AFTER the burst — reads once: | ||
| ✓ Got: Blue ← only the latest survived | ||
| (Red and Green were overwritten before anyone could read them) | ||
|
|
||
| Round 2 | ||
| 1. Firing two rapid commands BEFORE consumer exists: Red → Green | ||
| 2. Consumer created AFTER the burst — reads once: | ||
| ✓ Got: Green ← only the latest survived (Green) | ||
| 3. Shutting down... | ||
| ✓ Done. | ||
| ``` | ||
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,87 @@ | ||
| use aimdb_core::{buffer::BufferCfg, AimDbBuilder}; | ||
| use aimdb_sync::AimDbBuilderSyncExt; | ||
| use aimdb_tokio_adapter::{TokioAdapter, TokioRecordRegistrarExt}; | ||
| use serde::{Deserialize, Serialize}; | ||
|
ggmaldo marked this conversation as resolved.
Outdated
|
||
| use std::sync::Arc; | ||
| use std::thread; | ||
| use std::time::Duration; | ||
|
|
||
| // Enum of colors for the LED | ||
| #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
|
ggmaldo marked this conversation as resolved.
Outdated
|
||
| enum Color { | ||
| Red, | ||
| Green, | ||
| Blue, | ||
| } | ||
|
|
||
| // Struct representing the LED state | ||
| #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
|
ggmaldo marked this conversation as resolved.
Outdated
|
||
| struct Led { | ||
| color: Color, | ||
| } | ||
|
|
||
| // Main function | ||
| fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| println!("=== hello-mailbox: 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); | ||
| }); | ||
|
|
||
| let handle = builder.attach()?; | ||
| let producer = handle.producer::<Led>("actuator.led")?; | ||
|
|
||
| { | ||
| // Produce quickly BEFORE creating the consumer | ||
| println!(" Round 1 "); | ||
| println!("1. Firing three rapid commands BEFORE consumer exists: Red → Green → Blue"); | ||
| producer.set(Led { color: Color::Red })?; | ||
| producer.set(Led { | ||
| color: Color::Green, | ||
| })?; | ||
| producer.set(Led { color: Color::Blue })?; | ||
| thread::sleep(Duration::from_millis(100)); | ||
|
|
||
| // Now we create the consumer — it will only see the last value in the Mailbox | ||
| println!("2. Consumer created AFTER the burst — reads once:"); | ||
| let consumer = handle.consumer::<Led>("actuator.led")?; | ||
| thread::sleep(Duration::from_millis(100)); | ||
|
|
||
| match consumer.try_get() { | ||
| Ok(msg) => println!(" ✓ Got: {:?} ← only the latest survived", msg.color), | ||
| Err(_) => println!(" (mailbox was already empty)"), | ||
| } | ||
|
|
||
| println!(" (Red and Green were overwritten before anyone could read them)\n"); | ||
| } | ||
|
|
||
| println!(" Round 2 "); | ||
| println!("1. Firing two rapid commands BEFORE consumer exists: Red → Green"); | ||
| // Create the color burst again | ||
| producer.set(Led { color: Color::Red })?; | ||
| producer.set(Led { | ||
| color: Color::Green, | ||
| })?; | ||
| thread::sleep(Duration::from_millis(100)); | ||
|
|
||
| println!("2. Consumer created AFTER the burst — reads once:"); | ||
| let consumer2 = handle.consumer::<Led>("actuator.led")?; | ||
| thread::sleep(Duration::from_millis(100)); | ||
|
|
||
| match consumer2.try_get() { | ||
| Ok(msg) => println!( | ||
| " ✓ Got: {:?} ← only the latest survived (Green)", | ||
| msg.color | ||
| ), | ||
| Err(_) => println!(" (mailbox was already empty)"), | ||
| } | ||
|
|
||
| println!("3. Shutting down..."); | ||
| handle.detach()?; | ||
| println!(" ✓ Done."); | ||
| Ok(()) | ||
| } | ||
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.