Skip to content

Commit 975c99c

Browse files
authored
Add functions to get topic name to publisher and subscriber (#194)
* feat: add rcl_subscription_get_topic_name to subscription.rs (#148) * tested addition of rcl_subscription_get_topic_name (#148) * add rcl_publisher_get_topic to publisher.rs (#148) * compiling successfully * added wrappers for rcl_publisher_get_topic_name and rcl_subscription_get_topic_name (#148) * replaced if let with match for error handling (#148) * changes made to formatting * modified code to return String instead of Cow (#148) * changed to_owned to into_owned (#148) * returning String instead of Result (#148) * grammar changes to comments (#148)
1 parent 6a7d2a7 commit 975c99c

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

rclrs/src/node/publisher.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::rcl_bindings::*;
44
use crate::Node;
55

66
use std::borrow::Cow;
7+
use std::ffi::CStr;
78
use std::ffi::CString;
89
use std::marker::PhantomData;
910
use std::sync::Arc;
@@ -97,6 +98,21 @@ where
9798
})
9899
}
99100

101+
/// Returns the topic name of the publisher.
102+
///
103+
/// This returns the topic name after remapping, so it is not necessarily the
104+
/// topic name which was used when creating the publisher.
105+
pub fn get_topic(&self) -> String {
106+
// SAFETY: No preconditions for the functions called.
107+
// The unsafe variables created get converted to safe types before being returned
108+
unsafe {
109+
let raw_topic_pointer = rcl_publisher_get_topic_name(&*self.handle.lock());
110+
CStr::from_ptr(raw_topic_pointer)
111+
.to_string_lossy()
112+
.into_owned()
113+
}
114+
}
115+
100116
/// Publishes a message.
101117
///
102118
/// The [`MessageCow`] trait is implemented by any

rclrs/src/node/subscription.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::Node;
44
use crate::{rcl_bindings::*, RclrsError};
55

66
use std::boxed::Box;
7+
use std::ffi::CStr;
78
use std::ffi::CString;
89
use std::marker::PhantomData;
910
use std::sync::Arc;
@@ -124,6 +125,21 @@ where
124125
})
125126
}
126127

128+
/// Returns the topic name of the subscription.
129+
///
130+
/// This returns the topic name after remapping, so it is not necessarily the
131+
/// topic name which was used when creating the subscription.
132+
pub fn get_topic(&self) -> String {
133+
// SAFETY: No preconditions for the function used
134+
// The unsafe variables get converted to safe types before being returned
135+
unsafe {
136+
let raw_topic_pointer = rcl_subscription_get_topic_name(&*self.handle.lock());
137+
CStr::from_ptr(raw_topic_pointer)
138+
.to_string_lossy()
139+
.into_owned()
140+
}
141+
}
142+
127143
/// Fetches a new message.
128144
///
129145
/// When there is no new message, this will return a

0 commit comments

Comments
 (0)