Struct Subscriber

Source
pub struct Subscriber<T> { /* private fields */ }
Expand description

Subscriber.

Implementations§

Source§

impl<T: TypeSupport> Subscriber<T>

Source

pub fn get_topic_name(&self) -> &str

Source

pub fn try_recv(&self) -> RecvResult<TakenMsg<T>, ()>

Non-blocking receive.

Because rcl::rcl_take is non-blocking, try_recv() returns RecvResult::RetryLater if data is not available.

§Example
use safe_drive::{
    logger::Logger, msg::common_interfaces::std_msgs, pr_error, pr_info,
    topic::subscriber::Subscriber, RecvResult,
};

fn pubsub(subscriber: Subscriber<std_msgs::msg::UInt32>, logger: Logger) {
    // Receive the message.
    match subscriber.try_recv() {
        RecvResult::Ok(msg) => pr_info!(logger, "msg = {}", msg.data),
        RecvResult::RetryLater(_) => pr_info!(logger, "retry later"),
        RecvResult::Err(e) => pr_error!(logger, "error = {}", e),
    }
}
§Errors
  • RCLError::InvalidArgument if any arguments are invalid, or
  • RCLError::SubscriptionInvalid if the subscription is invalid, or
  • RCLError::BadAlloc if allocating memory failed, or
  • RCLError::Error if an unspecified error occurs.
Source

pub async fn recv(&mut self) -> Result<TakenMsg<T>, DynError>

Receive a message asynchronously.

This waits and blocks forever until a message arrives. In order to call recv() with timeout, use mechanisms provided by asynchronous libraries, such as async_std::future::timeout.

§Example
#[allow(unused_imports)]
use async_std::{future, prelude::*};
use safe_drive::{
    logger::Logger, msg::common_interfaces::std_msgs, pr_info, pr_warn,
    topic::subscriber::Subscriber,
};
use std::time::Duration;

async fn run_subscriber(mut s: Subscriber<std_msgs::msg::String>) {
    let dur = Duration::from_millis(100);
    let logger = Logger::new("subscriber_rs_recv");
    for _ in 0..3 {
        // receive a message specifying timeout of 100ms
        match future::timeout(dur, s.recv()).await {
            Ok(Ok(msg)) => {
                // received a message
                pr_info!(logger, "Received (async): msg = {}", msg.data);
            }
            Ok(Err(e)) => panic!("{}", e), // fatal error
            Err(_) => {
                // timeout
                pr_warn!(logger, "Subscribe (async): timeout");
                break;
            }
        }
    }
}
§Errors
  • RCLError::InvalidArgument if any arguments are invalid, or
  • RCLError::SubscriptionInvalid if the subscription is invalid, or
  • RCLError::BadAlloc if allocating memory failed, or
  • RCLError::Error if an unspecified error occurs.

Auto Trait Implementations§

§

impl<T> Freeze for Subscriber<T>

§

impl<T> !RefUnwindSafe for Subscriber<T>

§

impl<T> Send for Subscriber<T>
where T: Send,

§

impl<T> !Sync for Subscriber<T>

§

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

§

impl<T> UnwindSafe for Subscriber<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.