Skip to content

Commit f1a0f41

Browse files
committed
Shared state pattern (spin-off of #427) (#430)
* Migrate Node to shared state pattern Signed-off-by: Michael X. Grey <greyxmike@gmail.com> * Migrate primitives to state pattern Signed-off-by: Michael X. Grey <greyxmike@gmail.com> * Fix examples Signed-off-by: Michael X. Grey <greyxmike@gmail.com> * Fix merge errors Signed-off-by: Michael X. Grey <mxgrey@intrinsic.ai> * Fix style Signed-off-by: Michael X. Grey <mxgrey@intrinsic.ai> * Fix doc Signed-off-by: Michael X. Grey <mxgrey@intrinsic.ai> --------- Signed-off-by: Michael X. Grey <greyxmike@gmail.com> Signed-off-by: Michael X. Grey <mxgrey@intrinsic.ai>
1 parent c048a8a commit f1a0f41

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

rclrs/minimal_pub_sub/src/minimal_two_nodes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use anyhow::{Error, Result};
88

99
struct MinimalSubscriber {
1010
num_messages: AtomicU32,
11-
node: Arc<Node>,
12-
subscription: Mutex<Option<Arc<Subscription<std_msgs::msg::String>>>>,
11+
node: Node,
12+
subscription: Mutex<Option<Subscription<std_msgs::msg::String>>>,
1313
}
1414

1515
impl MinimalSubscriber {

rclrs/rust_pubsub/src/simple_publisher.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use rclrs::*;
2-
use std::{sync::Arc, thread, time::Duration};
2+
use std::{thread, time::Duration};
33
use std_msgs::msg::String as StringMsg;
44

55
struct SimplePublisher {
6-
publisher: Arc<Publisher<StringMsg>>,
6+
publisher: Publisher<StringMsg>,
77
}
88

99
impl SimplePublisher {
@@ -24,12 +24,11 @@ impl SimplePublisher {
2424

2525
fn main() -> Result<(), RclrsError> {
2626
let mut executor = Context::default_from_env().unwrap().create_basic_executor();
27-
let publisher = Arc::new(SimplePublisher::new(&executor).unwrap());
28-
let publisher_other_thread = Arc::clone(&publisher);
27+
let publisher = SimplePublisher::new(&executor).unwrap();
2928
let mut count: i32 = 0;
3029
thread::spawn(move || loop {
3130
thread::sleep(Duration::from_millis(1000));
32-
count = publisher_other_thread.publish_data(count).unwrap();
31+
count = publisher.publish_data(count).unwrap();
3332
});
3433
executor.spin(SpinOptions::default()).first_error()
3534
}

rclrs/rust_pubsub/src/simple_subscriber.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
use std_msgs::msg::String as StringMsg;
88

99
pub struct SimpleSubscriptionNode {
10-
_subscriber: Arc<Subscription<StringMsg>>,
10+
_subscriber: Subscription<StringMsg>,
1111
data: Arc<Mutex<Option<StringMsg>>>,
1212
}
1313

@@ -34,11 +34,10 @@ impl SimpleSubscriptionNode {
3434
}
3535
fn main() -> Result<(), RclrsError> {
3636
let mut executor = Context::default_from_env().unwrap().create_basic_executor();
37-
let subscription = Arc::new(SimpleSubscriptionNode::new(&executor).unwrap());
38-
let subscription_other_thread = Arc::clone(&subscription);
37+
let subscription = SimpleSubscriptionNode::new(&executor).unwrap();
3938
thread::spawn(move || loop {
4039
thread::sleep(Duration::from_millis(1000));
41-
subscription_other_thread.data_callback().unwrap()
40+
subscription.data_callback().unwrap()
4241
});
4342
executor.spin(SpinOptions::default()).first_error()
4443
}

0 commit comments

Comments
 (0)