use std::time::Duration;
use hiroz::Builder;
use hiroz::encoding::Encoding;
use hiroz_msgs::std_msgs::String as RosString;
fn main() -> zenoh::Result<()> {
println!("=== hiroz Dynamic Encoding Demo ===\n");
let ctx = hiroz::context::ZContextBuilder::default().build()?;
let node = ctx.create_node("encoding_demo").build()?;
println!("1. Creating publishers with different encodings...\n");
let cdr_pub = node
.create_pub::<RosString>("/topic/cdr")
.with_encoding(Encoding::cdr())
.build()?;
println!(" ✓ CDR publisher ready: /topic/cdr");
#[cfg(feature = "protobuf")]
let proto_pub = {
let pub_ = node
.create_pub::<RosString>("/topic/protobuf")
.with_encoding(Encoding::protobuf().with_schema("std_msgs/msg/String"))
.build()?;
println!(" ✓ Protobuf publisher ready: /topic/protobuf (schema: std_msgs/msg/String)");
pub_
};
#[cfg(not(feature = "protobuf"))]
println!(" ℹ Protobuf support not enabled (use --features protobuf)");
println!("\n2. Publishing messages...\n");
let msg = RosString {
data: "Hello from CDR!".to_string(),
};
cdr_pub.publish(&msg)?;
println!(" → Published CDR message: '{}'", msg.data);
#[cfg(feature = "protobuf")]
{
let msg = RosString {
data: "Hello from Protobuf!".to_string(),
};
proto_pub.publish(&msg)?;
println!(" → Published Protobuf message: '{}'", msg.data);
}
println!("\n3. Encoding metadata is automatically transmitted with each message");
println!(" Subscribers can detect the encoding via sample.encoding()");
println!(" Format: mime_type[; schema=schema_name]");
println!(" - CDR: application/octet-stream");
println!(" - Protobuf: application/protobuf; schema=std_msgs/msg/String");
println!("\n4. Performance benefits:");
println!(" ✓ Zenoh encoding is cached (computed once during build())");
println!(" ✓ Zero overhead per publish() call");
println!(" ✓ No string formatting on hot path");
println!("\n=== Demo complete ===");
println!("Press Ctrl+C to exit");
std::thread::sleep(Duration::from_secs(60));
Ok(())
}