Skip to content

Add functions to get topic name to publisher and subscriber #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 13, 2022
Merged
16 changes: 16 additions & 0 deletions rclrs/src/node/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::rcl_bindings::*;
use crate::Node;

use std::borrow::Cow;
use std::ffi::CStr;
use std::ffi::CString;
use std::marker::PhantomData;
use std::sync::Arc;
Expand Down Expand Up @@ -107,6 +108,21 @@ where
})
}

/// 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.
pub fn get_topic(&self) -> String {
// SAFETY: No preconditions for the functions called.
// The unsafe variables created get converted to safe types before being returned
unsafe {
let raw_topic_pointer = rcl_publisher_get_topic_name(&*self.handle.lock());
CStr::from_ptr(raw_topic_pointer)
.to_string_lossy()
.into_owned()
}
}

/// Publishes a message.
///
/// The [`MessageCow`] trait is implemented by any
Expand Down
16 changes: 16 additions & 0 deletions rclrs/src/node/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{rcl_bindings::*, RclrsError};

use std::borrow::Borrow;
use std::boxed::Box;
use std::ffi::CStr;
use std::ffi::CString;
use std::marker::PhantomData;
use std::sync::Arc;
Expand Down Expand Up @@ -125,6 +126,21 @@ where
})
}

/// Returns the topic name of the subscription.
///
/// This returns the topic name after remapping, so it is not necessarily the
/// topic name which was used when creating the subscription.
pub fn get_topic(&self) -> String {
// SAFETY: No preconditions for the function used
// The unsafe variables get converted to safe types before being returned
unsafe {
let raw_topic_pointer = rcl_subscription_get_topic_name(&*self.handle.lock());
CStr::from_ptr(raw_topic_pointer)
.to_string_lossy()
.into_owned()
}
}

/// Fetches a new message.
///
/// When there is no new message, this will return a
Expand Down