diff --git a/zenoh-flow/src/types/message.rs b/zenoh-flow/src/types/message.rs index 29101225..709caa89 100644 --- a/zenoh-flow/src/types/message.rs +++ b/zenoh-flow/src/types/message.rs @@ -99,6 +99,25 @@ impl Payload { } } } + + /// Return an [Arc] containing the bytes representation of the [Payload]. + /// + /// # Performance + /// + /// This method will only serialize (and thus allocate) the [Payload] if it is typed. Otherwise + /// the [Arc] is cloned. + // + // NOTE: This method is used by, at least, our Python API. + pub fn try_as_gytes(&self) -> Result>> { + match self { + Payload::Bytes(bytes) => Ok(bytes.clone()), + Payload::Typed((typed_data, serializer)) => { + let mut buffer = Vec::default(); + (serializer)(&mut buffer, Arc::clone(typed_data))?; + Ok(Arc::new(buffer)) + } + } + } } /// Creates a new `Data` from a `Vec`. @@ -151,6 +170,13 @@ impl DataMessage { timestamp, } } + + /// Return the [Timestamp] associated with this [DataMessage]. + // + // NOTE: This method is used by, at least, our Python API. + pub fn get_timestamp(&self) -> &Timestamp { + &self.timestamp + } } /// Metadata stored in Zenoh's time series storages.