pub struct Subscriber<T> { /* private fields */ }
Expand description
Subscriber.
Implementations§
Source§impl<T: TypeSupport> Subscriber<T>
impl<T: TypeSupport> Subscriber<T>
pub fn get_topic_name(&self) -> &str
Sourcepub fn try_recv(&self) -> RecvResult<TakenMsg<T>, ()>
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, orRCLError::SubscriptionInvalid
if the subscription is invalid, orRCLError::BadAlloc if allocating
memory failed, orRCLError::Error
if an unspecified error occurs.
Sourcepub async fn recv(&mut self) -> Result<TakenMsg<T>, DynError>
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, orRCLError::SubscriptionInvalid
if the subscription is invalid, orRCLError::BadAlloc
if allocating memory failed, orRCLError::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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more