Skip to content

Commit 7e445cf

Browse files
committed
documentation
1 parent 6640abd commit 7e445cf

File tree

10 files changed

+324
-91
lines changed

10 files changed

+324
-91
lines changed

rustecal-types-bytes/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
//! `rustecal-types-bytes` provides typed support for raw byte messages over eCAL.
2+
//!
3+
//! This crate defines a wrapper type `BytesMessage` that implements the
4+
//! [`PublisherMessage`] and [`SubscriberMessage`] traits from the `rustecal` crate,
5+
//! enabling ergonomic and type-safe publishing and subscribing of binary blobs.
6+
17
use rustecal::{PublisherMessage, SubscriberMessage};
28
use rustecal::pubsub::types::DataTypeInfo;
39

10+
/// A wrapper for raw binary data transmitted via eCAL.
11+
///
12+
/// This message type is ideal for non-structured byte payloads such as images,
13+
/// serialized custom formats, or arbitrary buffers.
14+
///
15+
/// Implements both [`PublisherMessage`] and [`SubscriberMessage`] to support
16+
/// bidirectional pub/sub use.
417
pub struct BytesMessage(pub Vec<u8>);
518

619
impl SubscriberMessage for BytesMessage {
20+
/// Returns metadata describing the message encoding and type.
21+
///
22+
/// Encoding is `"raw"`, type name is `"bytes"`, and no descriptor is included.
723
fn datatype() -> DataTypeInfo {
824
DataTypeInfo {
925
encoding: "raw".into(),
@@ -12,16 +28,19 @@ impl SubscriberMessage for BytesMessage {
1228
}
1329
}
1430

31+
/// Creates a `BytesMessage` from a raw byte slice.
1532
fn from_bytes(bytes: &[u8]) -> Option<Self> {
1633
Some(BytesMessage(bytes.to_vec()))
1734
}
1835
}
1936

2037
impl PublisherMessage for BytesMessage {
38+
/// Reuses the `SubscriberMessage::datatype()` implementation.
2139
fn datatype() -> DataTypeInfo {
2240
<BytesMessage as SubscriberMessage>::datatype()
2341
}
2442

43+
/// Converts the internal byte vector into a byte slice for sending.
2544
fn to_bytes(&self) -> Vec<u8> {
2645
self.0.clone()
2746
}

rustecal-types-protobuf/src/lib.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,59 @@
1+
//! Typed Protobuf message support for `rustecal`.
2+
//!
3+
//! This crate provides a wrapper around `prost::Message`-based types, allowing
4+
//! seamless integration with `TypedPublisher` and `TypedSubscriber` from the `rustecal` API.
5+
//!
6+
//! ## Usage
7+
//!
8+
//! To publish or subscribe to Protobuf messages, wrap your message type in `ProtobufMessage<T>`
9+
//! and ensure the inner type implements both `prost::Message` and `IsProtobufType`.
10+
//!
11+
//! ```rust
12+
//! use rustecal_types_protobuf::{ProtobufMessage, IsProtobufType};
13+
//! use my_proto::MyMessage;
14+
//!
15+
//! impl IsProtobufType for MyMessage {}
16+
//!
17+
//! let msg = ProtobufMessage(MyMessage::default());
18+
//! ```
19+
120
use prost::Message;
221
use rustecal::pubsub::{PublisherMessage, SubscriberMessage};
322
use rustecal::pubsub::types::DataTypeInfo;
423

5-
/// Marker trait to opt-in for Protobuf support
24+
/// Marker trait to opt-in a Protobuf type for use with eCAL.
25+
///
26+
/// This trait must be implemented for any `prost::Message` you wish to use
27+
/// with `ProtobufMessage<T>`. It provides a type-level opt-in mechanism
28+
/// to ensure users are aware of what's being exposed to eCAL.
629
pub trait IsProtobufType {}
730

8-
/// Wrapper around a `prost::Message` to enable use with TypedPublisher and TypedSubscriber
31+
/// Wrapper around a `prost::Message` type to enable typed publishing and subscription.
32+
///
33+
/// This is the type that should be used with `TypedPublisher` and `TypedSubscriber`
34+
/// for Protobuf messages.
35+
///
36+
/// ```rust
37+
/// use rustecal_types_protobuf::{ProtobufMessage, IsProtobufType};
38+
/// use my_proto::MyMessage;
39+
///
40+
/// impl IsProtobufType for MyMessage {}
41+
///
42+
/// let wrapped = ProtobufMessage(MyMessage::default());
43+
/// ```
944
#[derive(Debug, Clone)]
1045
pub struct ProtobufMessage<T>(pub T);
1146

1247
impl<T> SubscriberMessage for ProtobufMessage<T>
1348
where
1449
T: Message + Default + IsProtobufType,
1550
{
51+
/// Returns metadata used by eCAL to describe the Protobuf type.
52+
///
53+
/// This includes:
54+
/// - `proto` as encoding
55+
/// - the Rust type name
56+
/// - an optional descriptor (currently empty)
1657
fn datatype() -> DataTypeInfo {
1758
DataTypeInfo {
1859
encoding: "proto".to_string(),
@@ -21,6 +62,11 @@ where
2162
}
2263
}
2364

65+
/// Decodes a Protobuf message from bytes.
66+
///
67+
/// # Returns
68+
/// - `Some(ProtobufMessage<T>)` on success
69+
/// - `None` if decoding fails
2470
fn from_bytes(bytes: &[u8]) -> Option<Self> {
2571
T::decode(bytes).ok().map(ProtobufMessage)
2672
}
@@ -30,10 +76,15 @@ impl<T> PublisherMessage for ProtobufMessage<T>
3076
where
3177
T: Message + Default + IsProtobufType,
3278
{
79+
/// Returns the same datatype information as [`SubscriberMessage`] implementation.
3380
fn datatype() -> DataTypeInfo {
3481
<ProtobufMessage<T> as SubscriberMessage>::datatype()
3582
}
3683

84+
/// Encodes the message to a byte buffer.
85+
///
86+
/// # Panics
87+
/// Will panic if `prost::Message::encode` fails (should never panic for valid messages).
3788
fn to_bytes(&self) -> Vec<u8> {
3889
let mut buf = Vec::with_capacity(self.0.encoded_len());
3990
self.0

rustecal-types-string/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
//! # rustecal-types-string
2+
//!
3+
//! This crate provides support for sending and receiving UTF-8 string messages
4+
//! using the `rustecal` typed publisher and subscriber APIs.
5+
//!
6+
//! It defines a wrapper type [`StringMessage`] that implements the necessary traits
7+
//! [`PublisherMessage`] and [`SubscriberMessage`] for type-safe usage with
8+
//! [`TypedPublisher`] and [`TypedSubscriber`] respectively.
9+
110
use rustecal::{PublisherMessage, SubscriberMessage};
211
use rustecal::pubsub::types::DataTypeInfo;
312
use std::str;
413

14+
/// A wrapper for UTF-8 string messages used with typed eCAL pub/sub.
15+
///
16+
/// This type allows sending and receiving strings through the
17+
/// `TypedPublisher<StringMessage>` and `TypedSubscriber<StringMessage>` APIs.
518
pub struct StringMessage(pub String);
619

720
impl SubscriberMessage for StringMessage {
21+
/// Returns metadata describing this message type (`utf-8` encoded string).
822
fn datatype() -> DataTypeInfo {
923
DataTypeInfo {
1024
encoding: "utf-8".to_string(),
@@ -13,16 +27,19 @@ impl SubscriberMessage for StringMessage {
1327
}
1428
}
1529

30+
/// Attempts to decode a UTF-8 string from a byte buffer.
1631
fn from_bytes(bytes: &[u8]) -> Option<Self> {
1732
str::from_utf8(bytes).ok().map(|s| StringMessage(s.to_string()))
1833
}
1934
}
2035

2136
impl PublisherMessage for StringMessage {
37+
/// Returns the same metadata as [`SubscriberMessage::datatype`].
2238
fn datatype() -> DataTypeInfo {
2339
<StringMessage as SubscriberMessage>::datatype()
2440
}
2541

42+
/// Serializes the string into a byte buffer.
2643
fn to_bytes(&self) -> Vec<u8> {
2744
self.0.as_bytes().to_vec()
2845
}

rustecal/src/ecal/components.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,62 @@
1+
//! Component selection for eCAL initialization.
2+
//!
3+
//! This module provides the [`EcalComponents`] bitflag struct used to specify
4+
//! which parts of the eCAL middleware should be activated during initialization.
5+
//!
6+
//! The flags can be combined using bitwise OR operations (e.g., `PUBLISHER | LOGGING`).
7+
//!
8+
//! These flags are passed to [`crate::ecal::core::Ecal::initialize`] to enable
9+
//! or disable subsystems for performance, resource usage, or system design reasons.
10+
111
use bitflags::bitflags;
212

313
bitflags! {
14+
/// Bitflags representing the subsystems of eCAL that can be individually enabled.
15+
///
16+
/// These flags are used with [`Ecal::initialize`](crate::ecal::core::Ecal::initialize)
17+
/// to control which components are active.
418
#[derive(Default)]
519
pub struct EcalComponents: u32 {
20+
/// Disable all components (no subsystems enabled).
621
const NONE = 0x000;
22+
23+
/// Enable the publish interface.
724
const PUBLISHER = 0x001;
25+
26+
/// Enable the subscribe interface.
827
const SUBSCRIBER = 0x002;
28+
29+
/// Enable eCAL service (RPC-style communication).
930
const SERVICE = 0x004;
31+
32+
/// Enable the monitoring component (e.g., for visualizing in eCAL Monitor).
1033
const MONITORING = 0x008;
34+
35+
/// Enable logging (console/file output from eCAL runtime).
1136
const LOGGING = 0x010;
37+
38+
/// Enable time synchronization (e.g., simulated time or global time sync).
1239
const TIMESYNC = 0x020;
1340

41+
/// Common default configuration used for most applications.
42+
///
43+
/// This enables all major communication components:
44+
/// - `PUBLISHER`
45+
/// - `SUBSCRIBER`
46+
/// - `SERVICE`
47+
/// - `LOGGING`
48+
/// - `TIMESYNC`
49+
///
50+
/// It does **not** include `MONITORING`, which can be enabled separately.
1451
const DEFAULT = Self::PUBLISHER.bits()
1552
| Self::SUBSCRIBER.bits()
1653
| Self::SERVICE.bits()
1754
| Self::LOGGING.bits()
1855
| Self::TIMESYNC.bits();
1956

57+
/// Enables all available components.
58+
///
59+
/// This is equivalent to enabling every individual flag.
2060
const ALL = Self::PUBLISHER.bits()
2161
| Self::SUBSCRIBER.bits()
2262
| Self::SERVICE.bits()

rustecal/src/ecal/core.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
1+
//! Core lifecycle control for the eCAL runtime.
2+
//!
3+
//! This module provides safe Rust wrappers around initializing and finalizing
4+
//! the eCAL communication system, as well as querying its state.
5+
//!
6+
//! The main entry point is the [`Ecal`] struct which provides:
7+
//! - [`Ecal::initialize`] to start the middleware
8+
//! - [`Ecal::finalize`] to shut it down
9+
//! - [`Ecal::ok`] to query if eCAL is currently running
10+
//!
11+
//! Typically, you will call [`Ecal::initialize`] once at the beginning of your
12+
//! application and [`Ecal::finalize`] at shutdown.
13+
114
use std::ffi::CString;
215
use crate::ecal::components::EcalComponents;
316
use rustecal_sys;
417

18+
/// Provides access to the core initialization and finalization functions of eCAL.
519
pub struct Ecal;
620

721
impl Ecal {
8-
/// Initialize eCAL with specific components
22+
/// Initializes the eCAL runtime system.
23+
///
24+
/// This function must be called before using any publisher or subscriber functionality.
25+
///
26+
/// # Arguments
27+
///
28+
/// * `unit_name` - Optional name to identify this process in eCAL (e.g. in monitoring).
29+
/// * `components` - Bitmask of which subsystems (e.g. pub/sub, monitoring) to enable.
30+
///
31+
/// # Returns
32+
///
33+
/// Returns `Ok(())` on success, or `Err(code)` with a non-zero error code.
934
pub fn initialize(unit_name: Option<&str>, components: EcalComponents) -> Result<(), i32> {
1035
let cstr = unit_name.map(|s| CString::new(s).unwrap());
1136
let ptr = cstr.as_ref().map_or(std::ptr::null(), |c| c.as_ptr());
@@ -21,14 +46,22 @@ impl Ecal {
2146
}
2247
}
2348

24-
/// Finalize eCAL
49+
/// Finalizes and shuts down the eCAL runtime system.
50+
///
51+
/// After calling this function, all publishers and subscribers are invalidated.
2552
pub fn finalize() {
2653
unsafe {
2754
rustecal_sys::eCAL_Finalize();
2855
}
2956
}
3057

31-
/// Check if eCAL is running and in a valid state
58+
/// Checks if the eCAL system is initialized and running properly.
59+
///
60+
/// This can be used as the main loop condition in long-running processes.
61+
///
62+
/// # Returns
63+
///
64+
/// `true` if the system is operational, `false` otherwise.
3265
pub fn ok() -> bool {
3366
unsafe { rustecal_sys::eCAL_Ok() != 0 }
3467
}

rustecal/src/pubsub/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
1+
//! Typed and untyped eCAL publish-subscribe API.
2+
//!
3+
//! This module provides both low-level and high-level publish-subscribe functionality.
4+
//!
5+
//! ## Modules
6+
//!
7+
//! - [`publisher`] and [`subscriber`] offer low-level wrappers over the C eCAL pub/sub API.
8+
//! - [`typed_publisher`] and [`typed_subscriber`] offer type-safe, generic Rust APIs with
9+
//! automatic serialization and callback handling.
10+
//!
11+
//! ## Re-exports
12+
//!
13+
//! The traits and wrappers from the typed API are re-exported at this level for convenience:
14+
//!
15+
//! ```rust
16+
//! use rustecal::TypedPublisher;
17+
//! use rustecal::TypedSubscriber;
18+
//! ```
19+
120
pub mod types;
221
pub mod publisher;
322
pub mod subscriber;
423

524
pub mod typed_subscriber;
625
pub mod typed_publisher;
726

27+
// Publicly re-export high-level typed pub/sub traits and wrappers
828
pub use typed_subscriber::{TypedSubscriber, SubscriberMessage};
929
pub use typed_publisher::{TypedPublisher, PublisherMessage};

0 commit comments

Comments
 (0)