Skip to content

Commit

Permalink
fix(zenoh-flow-nodes): provide accessors on LinkMessage
Browse files Browse the repository at this point in the history
Writing the Python bindings highlighted a tiny nuisance of the `Deref`
trait: in order to access a Payload, an explicit turbofished call to
Deref was required, which was not particularly ergonomic.

This commit introduces accessors for both the `payload` and `timestamp`
fields of a `LinkMessage`.

The method `get_timestamp` was deprecated in favour of `timestamp`. This
change is purely aesthetic and for coherence purposes.

* zenoh-flow-nodes/src/messages.rs:
  - removed the implementation of the Deref trait,
  - added an accessor `payload()`,
  - added an accessor `timestamp()`,
  - deprecated the accessor `get_timestamp()` in favour of
    `timestamp()`.
* zenoh-flow-runtime/src/runners/builtin/zenoh/sink.rs: as the Deref
  trait is no longer implemented on `LinkMessage`, a call to `payload()`
  is required.

Signed-off-by: Julien Loudet <[email protected]>
  • Loading branch information
J-Loudet committed Jun 4, 2024
1 parent 2aba38f commit be9f4df
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
32 changes: 20 additions & 12 deletions zenoh-flow-nodes/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub struct LinkMessage {

impl Ord for LinkMessage {
fn cmp(&self, other: &Self) -> Ordering {
self.get_timestamp().cmp(other.get_timestamp())
self.timestamp().cmp(other.timestamp())
}
}

Expand All @@ -159,20 +159,12 @@ impl PartialOrd for LinkMessage {

impl PartialEq for LinkMessage {
fn eq(&self, other: &Self) -> bool {
self.get_timestamp() == other.get_timestamp()
self.timestamp() == other.timestamp()
}
}

impl Eq for LinkMessage {}

impl Deref for LinkMessage {
type Target = Payload;

fn deref(&self) -> &Self::Target {
&self.payload
}
}

impl LinkMessage {
pub fn new(payload: Payload, timestamp: Timestamp) -> Self {
Self { payload, timestamp }
Expand All @@ -188,13 +180,29 @@ impl LinkMessage {
}
}

/// Return the [Timestamp] associated with this message.
/// Return the [Payload] associated with this [LinkMessage].
//
// NOTE: This method is used by, at least, our Python API.
// NOTE: Used by our Python API. 🐍
pub fn payload(&self) -> &Payload {
&self.payload
}

/// Return the [Timestamp] associated with this message.
#[deprecated(
since = "0.6.0-alpha.3",
note = "This method will be deprecated in the next major version of Zenoh-Flow in favour of `timestamp()`."
)]
pub fn get_timestamp(&self) -> &Timestamp {
&self.timestamp
}

/// Return the [Timestamp] associated with this message.
//
// NOTE: Used by our Python API. 🐍
pub fn timestamp(&self) -> &Timestamp {
&self.timestamp
}

/// Serialises the [LinkMessage] using [bincode] into the given `buffer`.
///
/// The `inner_buffer` is used to serialise (if need be) the [Payload] contained inside the
Expand Down
2 changes: 1 addition & 1 deletion zenoh-flow-runtime/src/runners/builtin/zenoh/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ Caused by:

#[cfg(not(feature = "shared-memory"))]
{
data.try_as_bytes_into(&mut payload_buffer)?;
data.payload().try_as_bytes_into(&mut payload_buffer)?;
publisher
.put(payload_buffer)
.res()
Expand Down

0 comments on commit be9f4df

Please sign in to comment.