Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,33 @@ repos:
entry: python bin/hooks/filter_commit_message.py --check
language: python
stages: [pre-commit]

- id: cargo-fmt
name: cargo fmt
entry: |
bash -c '
set -euo pipefail
git ls-files "*Cargo.toml" | while read -r m; do
cargo locate-project --manifest-path "$m" --workspace --message-format plain
done | sort -u | while read -r root; do
cargo fmt --manifest-path "$root" --all --check
done
'
language: system
types: [rust]
pass_filenames: false
Comment on lines +112 to +125
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dreamsorcerer example of why I care about single path for CI and local dev envs for linting/code modification, think we should do what we discussed with modifying vs validating pre-commit hooks?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sounds good.


- id: cargo-clippy
name: cargo clippy
entry: |
bash -c '
set -euo pipefail
git ls-files "*Cargo.toml" | while read -r m; do
cargo locate-project --manifest-path "$m" --workspace --message-format plain
done | sort -u | while read -r root; do
cargo clippy --manifest-path "$root" --workspace --all-targets -- -D warnings
done
'
language: system
types: [rust]
pass_filenames: false
46 changes: 42 additions & 4 deletions examples/native-modules/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/native-modules/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ name = "native_pong"
path = "src/native_pong.rs"

[dependencies]
dimos-native-module = { path = "../../../native/rust" }
dimos-module = { path = "../../../native/rust/dimos-module" }
lcm-msgs = { git = "https://github.com/dimensionalOS/dimos-lcm.git", branch = "rust-codegen" }
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
serde = { version = "1", features = ["derive"] }
71 changes: 43 additions & 28 deletions examples/native-modules/rust/src/native_ping.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
// NativeModule ping example.
//
// Sends a Twist message at 5 Hz and logs each echo received on `confirm`.

use dimos_native_module::{LcmTransport, NativeModule};
use dimos_module::{run, Input, LcmTransport, Module, Output};
use lcm_msgs::geometry_msgs::{Twist, Vector3};
use tokio::time::{interval, Duration};

#[tokio::main]
async fn main() {
let transport = LcmTransport::new()
.await
.expect("Failed to create transport");
let (mut module, _config) = NativeModule::from_stdin::<()>(transport)
.await
.expect("Failed to read config from stdin");

let mut confirm = module.input("confirm", Twist::decode);
let data = module.output("data", Twist::encode);
let _handle = module.spawn();
#[derive(Module)]
#[module(setup = start_publisher)]
struct Ping {
#[input(decode = Twist::decode)]
confirm: Input<Twist>,

let mut ticker = interval(Duration::from_millis(200));
let mut seq = 0u64;
#[output(encode = Twist::encode)]
data: Output<Twist>,
}

loop {
tokio::select! {
_ = ticker.tick() => {
impl Ping {
async fn start_publisher(&mut self) {
let data = self.data.clone();
tokio::spawn(async move {
let mut ticker = interval(Duration::from_millis(200));
let mut seq = 0u64;
loop {
ticker.tick().await;
let msg = Twist {
linear: Vector3 { x: seq as f64, y: 0.0, z: 0.0 },
angular: Vector3 { x: 0.0, y: 0.0, z: 0.0 },
linear: Vector3 {
x: seq as f64,
y: 0.0,
z: 0.0,
},
angular: Vector3 {
x: 0.0,
y: 0.0,
z: 0.0,
},
};
data.publish(&msg).await.ok();
seq += 1;
}
Some(echo) = confirm.recv() => {
eprintln!("ping: echo received (seq={}, sample_config={})", echo.linear.x as u64, echo.angular.z as i64);
}
}
});
}

async fn handle_confirm(&mut self, echo: Twist) {
println!(
"ping: echo received (seq={}, sample_config={})",
echo.linear.x as u64, echo.angular.z as i64,
);
}
}

#[tokio::main]
async fn main() {
let transport = LcmTransport::new()
.await
.expect("Failed to create transport");
run::<Ping, _>(transport).await.expect("ping run failed");
}
62 changes: 28 additions & 34 deletions examples/native-modules/rust/src/native_pong.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
// NativeModule pong example.
//
// Receives Twist messages on `data` and echoes each one back on `confirm`,
// embedding the sample_config value in the reply's angular.z field.

use dimos_native_module::{LcmTransport, NativeModule};
use dimos_module::{run, Input, LcmTransport, Module, Output};
use lcm_msgs::geometry_msgs::{Twist, Vector3};
use serde::Deserialize;

Expand All @@ -13,37 +8,36 @@ struct PongConfig {
sample_config: i64,
}

#[derive(Module)]
struct Pong {
#[input(decode = Twist::decode)]
data: Input<Twist>,

#[output(encode = Twist::encode)]
confirm: Output<Twist>,

#[config]
config: PongConfig,
}

impl Pong {
async fn handle_data(&mut self, msg: Twist) {
let reply = Twist {
linear: msg.linear,
angular: Vector3 {
x: 0.0,
y: 0.0,
z: self.config.sample_config as f64,
},
};
self.confirm.publish(&reply).await.ok();
}
}

#[tokio::main]
async fn main() {
let transport = LcmTransport::new()
.await
.expect("Failed to create transport");
let (mut module, config) = NativeModule::from_stdin::<PongConfig>(transport)
.await
.expect("Failed to read config from stdin");

eprintln!("pong: sample_config={}", config.sample_config);

let mut data = module.input("data", Twist::decode);
let confirm = module.output("confirm", Twist::encode);
let _handle = module.spawn();

eprintln!("pong ready");

loop {
match data.recv().await {
Some(msg) => {
let reply = Twist {
linear: msg.linear,
angular: Vector3 {
x: 0.0,
y: 0.0,
z: config.sample_config as f64,
},
};
confirm.publish(&reply).await.ok();
}
None => break,
}
}
run::<Pong, _>(transport).await.expect("pong run failed");
}
3 changes: 2 additions & 1 deletion native/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target/
/target/
**/*.rs.bk
Loading
Loading