Skip to content

Commit

Permalink
changing owned to ref on publish for rosbridge
Browse files Browse the repository at this point in the history
  • Loading branch information
Carter committed Aug 8, 2024
1 parent 24cc12f commit 13736c4
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion roslibrust/examples/basic_publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn main() -> Result<(), anyhow::Error> {
loop {
let msg = std_msgs::Header::default();
info!("About to publish");
let result = publisher.publish(msg).await;
let result = publisher.publish(&msg).await;
match result {
Ok(()) => {
info!("Published msg!");
Expand Down
2 changes: 1 addition & 1 deletion roslibrust/examples/ros1_ros2_bridge_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async fn main() -> Result<(), anyhow::Error> {
};

// and re-publish it!
publisher.publish(converted_msg).await?;
publisher.publish(&converted_msg).await?;
info!("Message successfully sent to ros2!");
}
}
Expand Down
2 changes: 1 addition & 1 deletion roslibrust/src/rosbridge/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl ClientHandle {
// Publishes a message
// Fails immediately(ish) if disconnected
// Returns success when message is put on websocket (no confirmation of receipt)
pub(crate) async fn publish<T>(&self, topic: &str, msg: T) -> RosLibRustResult<()>
pub(crate) async fn publish<T>(&self, topic: &str, msg: &T) -> RosLibRustResult<()>
where
T: RosMessageType,
{
Expand Down
4 changes: 2 additions & 2 deletions roslibrust/src/rosbridge/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl FromStr for Ops {
pub(crate) trait RosBridgeComm {
async fn subscribe(&mut self, topic: &str, msg_type: &str) -> RosLibRustResult<()>;
async fn unsubscribe(&mut self, topic: &str) -> RosLibRustResult<()>;
async fn publish<T: RosMessageType>(&mut self, topic: &str, msg: T) -> RosLibRustResult<()>;
async fn publish<T: RosMessageType>(&mut self, topic: &str, msg: &T) -> RosLibRustResult<()>;
async fn advertise<T: RosMessageType>(&mut self, topic: &str) -> RosLibRustResult<()>;
async fn advertise_str(&mut self, topic: &str, msg_type: &str) -> RosLibRustResult<()>;
async fn call_service<Req: RosMessageType>(
Expand Down Expand Up @@ -139,7 +139,7 @@ impl RosBridgeComm for Writer {
Ok(())
}

async fn publish<T: RosMessageType>(&mut self, topic: &str, msg: T) -> RosLibRustResult<()> {
async fn publish<T: RosMessageType>(&mut self, topic: &str, msg: &T) -> RosLibRustResult<()> {
let msg = json!(
{
"op": Ops::Publish.to_string(),
Expand Down
16 changes: 8 additions & 8 deletions roslibrust/src/rosbridge/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ mod integration_tests {
frame_id: "self_publish".to_string(),
};

timeout(TIMEOUT, client.publish(TOPIC, msg_out.clone()))
timeout(TIMEOUT, client.publish(TOPIC, &msg_out))
.await
.expect("Failed to publish in time")
.unwrap();
Expand All @@ -109,7 +109,7 @@ mod integration_tests {

#[cfg(feature = "ros1_test")]
publisher
.publish(Time {
.publish(&Time {
data: roslibrust_codegen::Time { secs: 0, nsecs: 0 },
})
.await?;
Expand Down Expand Up @@ -200,7 +200,7 @@ mod integration_tests {
debug!("Got subscriber");

let msg = Header::default();
publisher.publish(msg).await?;
publisher.publish(&msg).await?;
timeout(TIMEOUT, sub.next()).await?;

debug!("Dropping publisher");
Expand All @@ -215,7 +215,7 @@ mod integration_tests {
let sub = client.subscribe::<Header>(TOPIC).await?;
// manually publishing using private api
let msg = Header::default();
client.publish(TOPIC, msg).await?;
client.publish(TOPIC, &msg).await?;

match timeout(TIMEOUT, sub.next()).await {
Ok(_msg) => {
Expand Down Expand Up @@ -338,7 +338,7 @@ mod integration_tests {
let x = std_msgs::Char {
data: 'x'.try_into().unwrap(),
};
publisher.publish(x.clone()).await?;
publisher.publish(&x).await?;

let y = timeout(TIMEOUT, subscriber.next())
.await
Expand Down Expand Up @@ -440,7 +440,7 @@ mod integration_tests {

// Confirm we can send and receive messages
publisher
.publish(Header::default())
.publish(&Header::default())
.await
.expect("Failed to publish");

Expand All @@ -453,7 +453,7 @@ mod integration_tests {
tokio::time::sleep(WAIT_FOR_ROSBRIDGE).await;

// Try to publish and confirm we get an error
let res = publisher.publish(Header::default()).await;
let res = publisher.publish(&Header::default()).await;
match res {
Ok(_) => {
panic!("Should have failed to publish after rosbridge died");
Expand Down Expand Up @@ -485,7 +485,7 @@ mod integration_tests {

// Try to publish and confirm we reconnect automatically
publisher
.publish(Header::default())
.publish(&Header::default())
.await
.expect("Failed to publish after rosbridge died");

Expand Down
2 changes: 1 addition & 1 deletion roslibrust/src/rosbridge/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<T: RosMessageType> Publisher<T> {
/// Successful sending of the message does not guarantee successful receipt or re-transmission by
/// rosbridge_server, rosbridge_server will fail to re-transmit if the type of the message does not
/// match the topic's definition on roscore.
pub async fn publish(&self, msg: T) -> RosLibRustResult<()> {
pub async fn publish(&self, msg: &T) -> RosLibRustResult<()> {
self.client.publish(&self.topic, msg).await
}
}
3 changes: 1 addition & 2 deletions roslibrust/src/topic_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ pub trait Publish<T: RosMessageType> {

impl<T: RosMessageType> Publish<T> for crate::Publisher<T> {
async fn publish(&self, data: &T) -> RosLibRustResult<()> {
// TODO clone here is bad and we should standardized on ownership of publish
self.publish(data.clone()).await
self.publish(data).await
}
}

Expand Down

0 comments on commit 13736c4

Please sign in to comment.