Skip to content

Commit a8b1368

Browse files
committed
Replace constructor method procedure
For backward compatibility, it is better to retain constructor method but deperecated. As for now, constructors only use NodeBuilder.
1 parent 2d6a916 commit a8b1368

File tree

2 files changed

+26
-35
lines changed

2 files changed

+26
-35
lines changed

rclrs/src/context.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::rcl_bindings::*;
2-
use crate::{Node, RclReturnCode, ToResult};
2+
use crate::{Node, NodeBuilder, RclReturnCode, ToResult};
33

44
use std::ffi::CString;
55
use std::os::raw::c_char;
@@ -111,7 +111,7 @@ impl Context {
111111
/// assert!(node.is_ok());
112112
/// ```
113113
pub fn create_node(&self, node_name: &str) -> Result<Node, RclReturnCode> {
114-
Node::new(node_name, self)
114+
NodeBuilder::new(node_name, self).build()
115115
}
116116

117117
/// Creates a node in a namespace.
@@ -132,7 +132,24 @@ impl Context {
132132
node_namespace: &str,
133133
node_name: &str,
134134
) -> Result<Node, RclReturnCode> {
135-
Node::new_with_namespace(node_namespace, node_name, self)
135+
NodeBuilder::new(node_name, self)
136+
.namespace(node_namespace)
137+
.build()
138+
}
139+
140+
/// Creates a [`NodeBuilder`][1]
141+
///
142+
/// [1]: crate::NodeBuilder
143+
///
144+
/// # Example
145+
/// ```
146+
/// # use rclrs::Context;
147+
/// let ctx = Context::new([]).unwrap();
148+
/// let builder = ctx.node_builder("foo_node");
149+
/// let node = builder.build();
150+
/// ```
151+
pub fn create_node_builder<'a>(&'a self, node_name: &'a str) -> NodeBuilder<'a> {
152+
NodeBuilder::new(node_name, self)
136153
}
137154

138155
/// Checks if the context is still valid.

rclrs/src/node/mod.rs

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,51 +64,25 @@ pub struct NodeBuilder<'a> {
6464

6565
impl Node {
6666
/// Creates a new node in the empty namespace.
67+
#[deprecated(since = "0.2.0", note = "please use `NodeBuilder` instead")]
6768
#[allow(clippy::new_ret_no_self)]
6869
pub fn new(node_name: &str, context: &Context) -> Result<Node, RclReturnCode> {
69-
Self::new_with_namespace("", node_name, context)
70+
NodeBuilder::new(node_name, context).build()
7071
}
7172

7273
/// Creates a new node in a namespace.
7374
///
7475
/// A namespace without a leading forward slash is automatically changed to have a leading
7576
/// forward slash.
77+
#[deprecated(since = "0.2.0", note = "please use `NodeBuilder` instead")]
7678
pub fn new_with_namespace(
7779
node_ns: &str,
7880
node_name: &str,
7981
context: &Context,
8082
) -> Result<Node, RclReturnCode> {
81-
let raw_node_name = CString::new(node_name).unwrap();
82-
let raw_node_ns = CString::new(node_ns).unwrap();
83-
84-
// SAFETY: Getting a zero-initialized value is always safe.
85-
let mut node_handle = unsafe { rcl_get_zero_initialized_node() };
86-
let context_handle = &mut *context.handle.lock();
87-
88-
unsafe {
89-
// SAFETY: No preconditions for this function.
90-
let node_options = rcl_node_get_default_options();
91-
// SAFETY: The node handle is zero-initialized as expected by this function.
92-
// The strings and node options are copied by this function, so we don't need
93-
// to keep them alive.
94-
// The context handle is kept alive because it is co-owned by the node.
95-
rcl_node_init(
96-
&mut node_handle,
97-
raw_node_name.as_ptr(),
98-
raw_node_ns.as_ptr(),
99-
context_handle,
100-
&node_options,
101-
)
102-
.ok()?;
103-
}
104-
105-
let handle = Arc::new(Mutex::new(node_handle));
106-
107-
Ok(Node {
108-
handle,
109-
context: context.handle.clone(),
110-
subscriptions: std::vec![],
111-
})
83+
NodeBuilder::new(node_name, context)
84+
.namespace(node_ns)
85+
.build()
11286
}
11387

11488
/// Returns the name of the node.

0 commit comments

Comments
 (0)