-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlib.rs
More file actions
53 lines (47 loc) · 1.59 KB
/
lib.rs
File metadata and controls
53 lines (47 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! # rustecal-types-bytes
//!
//! Provides support for sending and receiving raw binary messages (`Vec<u8>`) with rustecal.
//!
//! ## Example
//! ```rust
//! use std::sync::Arc;
//! use rustecal_types_bytes::BytesMessage;
//! let msg = BytesMessage(Arc::from([1, 2, 3, 4]));
//! ```
use std::sync::Arc;
use rustecal_core::types::DataTypeInfo;
use rustecal_pubsub::typed_publisher::PublisherMessage;
use rustecal_pubsub::typed_subscriber::SubscriberMessage;
/// A wrapper for raw binary messages used with typed eCAL pub/sub.
///
/// This type allows sending and receiving raw binary payloads through the
/// `TypedPublisher` and `TypedSubscriber` APIs.
pub struct BytesMessage {
pub data: Arc<[u8]>,
}
impl SubscriberMessage for BytesMessage {
/// Returns metadata describing the message encoding and type.
///
/// Encoding is `"raw"`, type name is `"bytes"`, and no descriptor is included.
fn datatype() -> DataTypeInfo {
DataTypeInfo {
encoding: "raw".into(),
type_name: "bytes".into(),
descriptor: vec![],
}
}
/// Creates a `BytesMessage` from a raw byte slice.
fn from_bytes(bytes: Arc<[u8]>) -> Option<Self> {
Some(BytesMessage { data: Arc::from(bytes) })
}
}
impl PublisherMessage for BytesMessage {
/// Reuses the `SubscriberMessage::datatype()` implementation.
fn datatype() -> DataTypeInfo {
<BytesMessage as SubscriberMessage>::datatype()
}
/// Returns the internal binary data as an Arc<[u8]> for zero-copy transmission.
fn to_bytes(&self) -> Arc<[u8]> {
self.data.clone()
}
}