|
| 1 | +# rustecal-types-bytes |
| 2 | + |
| 3 | +`rustecal-types-bytes` provides a simple wrapper for arbitrary binary data (`Arc<[u8]>`) to use with the typed eCAL Pub/Sub API. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **BytesMessage**: wrap and transport raw binary payloads |
| 8 | +- Implements `PublisherMessage` and `SubscriberMessage` for seamless integration |
| 9 | +- Zero-copy where possible via `Arc<[u8]>` |
| 10 | +- No extra dependencies beyond `rustecal-core` and `rustecal-pubsub` |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +Add to your **workspace** `Cargo.toml`: |
| 15 | + |
| 16 | +```toml |
| 17 | +[dependencies] |
| 18 | +rustecal-types-bytes = "0.1" |
| 19 | +``` |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +### Publisher Example |
| 24 | + |
| 25 | +```rust |
| 26 | +use std::sync::Arc; |
| 27 | +use rustecal::{Ecal, EcalComponents, TypedPublisher}; |
| 28 | +use rustecal_types_bytes::BytesMessage; |
| 29 | + |
| 30 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 31 | + Ecal::initialize(Some("blob publisher"), EcalComponents::DEFAULT)?; |
| 32 | + |
| 33 | + let publisher = TypedPublisher::<BytesMessage>::new("blob")?; |
| 34 | + |
| 35 | + let mut counter = 0u8; |
| 36 | + while Ecal::ok() { |
| 37 | + let buf = vec![counter; 1024]; |
| 38 | + counter = counter.wrapping_add(1); |
| 39 | + |
| 40 | + let message = BytesMessage { data: Arc::from(buf) }; |
| 41 | + publisher.send(&message); |
| 42 | + |
| 43 | + std::thread::sleep(std::time::Duration::from_millis(500)); |
| 44 | + } |
| 45 | + |
| 46 | + Ecal::finalize(); |
| 47 | + Ok(()) |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### Subscriber Example |
| 52 | + |
| 53 | +```rust |
| 54 | +use rustecal::{Ecal, EcalComponents, TypedSubscriber}; |
| 55 | +use rustecal_types_bytes::BytesMessage; |
| 56 | + |
| 57 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 58 | + Ecal::initialize(Some("blob subscriber"), EcalComponents::DEFAULT)?; |
| 59 | + |
| 60 | + let mut subscriber = TypedSubscriber::<BytesMessage>::new("blob")?; |
| 61 | + subscriber.set_callback(|message| { |
| 62 | + println!("Received blob of {} bytes", message.payload.data.len()); |
| 63 | + }); |
| 64 | + |
| 65 | + while Ecal::ok() { |
| 66 | + std::thread::sleep(std::time::Duration::from_millis(500)); |
| 67 | + } |
| 68 | + |
| 69 | + Ecal::finalize(); |
| 70 | + Ok(()) |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## Traits Reference |
| 75 | + |
| 76 | +- **`PublisherMessage`** |
| 77 | + - `datatype() -> DataTypeInfo` |
| 78 | + - `to_bytes(&self) -> Arc<[u8]>` |
| 79 | + |
| 80 | +- **`SubscriberMessage`** |
| 81 | + - `datatype() -> DataTypeInfo` |
| 82 | + - `from_bytes(bytes: Arc<[u8]>, _info: &DataTypeInfo) -> Option<Self>` |
| 83 | + |
| 84 | +## See Also |
| 85 | + |
| 86 | +- `rustecal-types-string` for UTF-8 string messages |
| 87 | +- `rustecal-types-protobuf` for Protobuf-based messages |
| 88 | +- `rustecal-types-serde` for JSON/CBOR/MessagePack via Serde |
| 89 | +- Examples in the `rustecal-samples/pubsub` directory |
0 commit comments