rclrs

Struct Publisher

Source
pub struct Publisher<T>
where T: Message,
{ /* private fields */ }
Expand description

Struct for sending messages of type T.

Multiple publishers can be created for the same topic, in different nodes or the same node.

The underlying RMW will decide on the concrete delivery mechanism (network stack, shared memory, or intraprocess).

Sending messages does not require the node’s executor to spin.

Implementations§

Source§

impl<T> Publisher<T>
where T: Message,

Source

pub fn topic_name(&self) -> String

Returns the topic name of the publisher.

This returns the topic name after remapping, so it is not necessarily the topic name which was used when creating the publisher.

Source

pub fn get_subscription_count(&self) -> Result<usize, RclrsError>

Returns the number of subscriptions of the publisher.

Source

pub fn publish<'a, M: MessageCow<'a, T>>( &self, message: M, ) -> Result<(), RclrsError>

Publishes a message.

The MessageCow trait is implemented by any [Message] as well as any reference to a Message.

The reason for allowing owned messages is that publishing owned messages can be more efficient in the case of idiomatic messages1.

Hence, when a message will not be needed anymore after publishing, pass it by value. When a message will be needed again after publishing, pass it by reference, instead of cloning and passing by value.

Calling publish() is a potentially blocking call, see this issue for details.


  1. See the [Message] trait for an explanation of “idiomatic”. 

Source§

impl<T> Publisher<T>
where T: RmwMessage,

Source

pub fn borrow_loaned_message(&self) -> Result<LoanedMessage<'_, T>, RclrsError>

Obtains a writable handle to a message owned by the middleware.

This lets the middleware control how and where to allocate memory for the message. The purpose of this is typically to achieve zero-copy communication between publishers and subscriptions on the same machine: the message is placed directly in a shared memory region, and a reference to the same memory is returned by Subscription::take_loaned_message(). No copying or serialization/deserialization takes place, which is much more efficient, especially as the message size grows.

§Conditions for zero-copy communication
  1. A middleware with support for shared memory is used, e.g. CycloneDDS with iceoryx
  2. Shared memory transport is enabled in the middleware configuration
  3. Publishers and subscriptions are on the same machine
  4. The message is a “plain old data” type containing no variable-size members, whether bounded or unbounded
  5. The publisher’s QOS settings are compatible with zero-copy, e.g. the default QOS
  6. Publisher::borrow_loaned_message() is used and the subscription uses a callback taking a ReadOnlyLoanedMessage

This function is only implemented for [RmwMessage]s since the “idiomatic” message type does not have a typesupport library.

Source

pub fn can_loan_messages(&self) -> bool

Returns true if message loans are possible, false otherwise.

Trait Implementations§

Source§

impl<T> Send for Publisher<T>
where T: Message,

Source§

impl<T> Sync for Publisher<T>
where T: Message,

Auto Trait Implementations§

§

impl<T> !Freeze for Publisher<T>

§

impl<T> RefUnwindSafe for Publisher<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for Publisher<T>
where T: Unpin,

§

impl<T> UnwindSafe for Publisher<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.