diff --git a/docs/writing-your-first-rclrs-node.md b/docs/writing-your-first-rclrs-node.md index 0a64e7bc..b6b1dadd 100644 --- a/docs/writing-your-first-rclrs-node.md +++ b/docs/writing-your-first-rclrs-node.md @@ -76,7 +76,7 @@ Next, add a main function to launch it: fn main() -> Result<(), rclrs::RclrsError> { let context = rclrs::Context::new(std::env::args())?; let republisher = RepublisherNode::new(&context)?; - rclrs::spin(&republisher.node) + rclrs::spin(republisher.node) } ``` @@ -190,7 +190,7 @@ fn main() -> Result<(), rclrs::RclrsError> { republisher.republish()?; } }); - rclrs::spin(&republisher.node) + rclrs::spin(republisher.node) } ``` @@ -212,7 +212,7 @@ fn main() -> Result<(), rclrs::RclrsError> { republisher_other_thread.republish()?; } }); - rclrs::spin(&republisher.node) + rclrs::spin(republisher.node) } ``` diff --git a/examples/message_demo/src/message_demo.rs b/examples/message_demo/src/message_demo.rs index e6a5a79a..76d46f67 100644 --- a/examples/message_demo/src/message_demo.rs +++ b/examples/message_demo/src/message_demo.rs @@ -1,5 +1,6 @@ use std::convert::TryInto; use std::env; +use std::sync::Arc; use anyhow::{Error, Result}; use rosidl_runtime_rs::{seq, BoundedSequence, Message, Sequence}; @@ -159,10 +160,10 @@ fn demonstrate_pubsub() -> Result<(), Error> { )?; println!("Sending idiomatic message."); idiomatic_publisher.publish(rclrs_example_msgs::msg::VariousTypes::default())?; - rclrs::spin_once(&node, None)?; + rclrs::spin_once(Arc::clone(&node), None)?; println!("Sending RMW-native message."); direct_publisher.publish(rclrs_example_msgs::msg::rmw::VariousTypes::default())?; - rclrs::spin_once(&node, None)?; + rclrs::spin_once(Arc::clone(&node), None)?; Ok(()) } diff --git a/examples/minimal_client_service/src/minimal_client.rs b/examples/minimal_client_service/src/minimal_client.rs index bfe20741..c91cedc9 100644 --- a/examples/minimal_client_service/src/minimal_client.rs +++ b/examples/minimal_client_service/src/minimal_client.rs @@ -28,5 +28,5 @@ fn main() -> Result<(), Error> { std::thread::sleep(std::time::Duration::from_millis(500)); println!("Waiting for response"); - rclrs::spin(&node).map_err(|err| err.into()) + rclrs::spin(node).map_err(|err| err.into()) } diff --git a/examples/minimal_client_service/src/minimal_client_async.rs b/examples/minimal_client_service/src/minimal_client_async.rs index d9937357..26f41e07 100644 --- a/examples/minimal_client_service/src/minimal_client_async.rs +++ b/examples/minimal_client_service/src/minimal_client_async.rs @@ -20,7 +20,7 @@ async fn main() -> Result<(), Error> { println!("Waiting for response"); - let rclrs_spin = tokio::task::spawn_blocking(move || rclrs::spin(&node)); + let rclrs_spin = tokio::task::spawn_blocking(move || rclrs::spin(node)); let response = future.await?; println!( diff --git a/examples/minimal_client_service/src/minimal_service.rs b/examples/minimal_client_service/src/minimal_service.rs index 933b367d..b4149c81 100644 --- a/examples/minimal_client_service/src/minimal_service.rs +++ b/examples/minimal_client_service/src/minimal_service.rs @@ -21,5 +21,5 @@ fn main() -> Result<(), Error> { .create_service::("add_two_ints", handle_service)?; println!("Starting server"); - rclrs::spin(&node).map_err(|err| err.into()) + rclrs::spin(node).map_err(|err| err.into()) } diff --git a/examples/minimal_pub_sub/src/minimal_subscriber.rs b/examples/minimal_pub_sub/src/minimal_subscriber.rs index cb15f063..ebc5fc19 100644 --- a/examples/minimal_pub_sub/src/minimal_subscriber.rs +++ b/examples/minimal_pub_sub/src/minimal_subscriber.rs @@ -19,5 +19,5 @@ fn main() -> Result<(), Error> { }, )?; - rclrs::spin(&node).map_err(|err| err.into()) + rclrs::spin(node).map_err(|err| err.into()) } diff --git a/examples/minimal_pub_sub/src/zero_copy_subscriber.rs b/examples/minimal_pub_sub/src/zero_copy_subscriber.rs index 635808a2..9551dba0 100644 --- a/examples/minimal_pub_sub/src/zero_copy_subscriber.rs +++ b/examples/minimal_pub_sub/src/zero_copy_subscriber.rs @@ -19,5 +19,5 @@ fn main() -> Result<(), Error> { }, )?; - rclrs::spin(&node).map_err(|err| err.into()) + rclrs::spin(node).map_err(|err| err.into()) } diff --git a/rclrs/src/lib.rs b/rclrs/src/lib.rs index 343eac17..fb1e3bfe 100644 --- a/rclrs/src/lib.rs +++ b/rclrs/src/lib.rs @@ -50,8 +50,8 @@ pub use wait::*; /// This can usually be ignored. /// /// [1]: crate::RclReturnCode -pub fn spin_once(node: &Node, timeout: Option) -> Result<(), RclrsError> { - let wait_set = WaitSet::new_for_node(node)?; +pub fn spin_once(node: Arc, timeout: Option) -> Result<(), RclrsError> { + let wait_set = WaitSet::new_for_node(&*node)?; let ready_entities = wait_set.wait(timeout)?; for ready_subscription in ready_entities.subscriptions { @@ -72,14 +72,16 @@ pub fn spin_once(node: &Node, timeout: Option) -> Result<(), RclrsErro /// Convenience function for calling [`spin_once`] in a loop. /// /// This function additionally checks that the context is still valid. -pub fn spin(node: &Node) -> Result<(), RclrsError> { +pub fn spin(node: Arc) -> Result<(), RclrsError> { // The context_is_valid functions exists only to abstract away ROS distro differences // SAFETY: No preconditions for this function. - let context_is_valid = - || unsafe { rcl_context_is_valid(&*node.rcl_context_mtx.lock().unwrap()) }; + let context_is_valid = { + let node = Arc::clone(&node); + move || unsafe { rcl_context_is_valid(&*node.rcl_context_mtx.lock().unwrap()) } + }; while context_is_valid() { - match spin_once(node, None) { + match spin_once(Arc::clone(&node), None) { Ok(_) | Err(RclrsError::RclError { code: RclReturnCode::Timeout,