Skip to content

Commit b25b466

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 0e21772 commit b25b466

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-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;
@@ -115,7 +115,7 @@ impl Context {
115115
/// # Ok::<(), RclReturnCode>(())
116116
/// ```
117117
pub fn create_node(&self, node_name: &str) -> Result<Node, RclReturnCode> {
118-
Node::new(node_name, self)
118+
NodeBuilder::new(node_name, self).build()
119119
}
120120

121121
/// Creates a new node in a namespace.
@@ -138,7 +138,24 @@ impl Context {
138138
node_namespace: &str,
139139
node_name: &str,
140140
) -> Result<Node, RclReturnCode> {
141-
Node::new_with_namespace(node_namespace, node_name, self)
141+
NodeBuilder::new(node_name, self)
142+
.namespace(node_namespace)
143+
.build()
144+
}
145+
146+
/// Creates a [`NodeBuilder`][1]
147+
///
148+
/// [1]: crate::NodeBuilder
149+
///
150+
/// # Example
151+
/// ```
152+
/// # use rclrs::Context;
153+
/// let ctx = Context::new([]).unwrap();
154+
/// let builder = ctx.node_builder("foo_node");
155+
/// let node = builder.build();
156+
/// ```
157+
pub fn create_node_builder<'a>(&'a self, node_name: &'a str) -> NodeBuilder<'a> {
158+
NodeBuilder::new(node_name, self)
142159
}
143160

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

rclrs/src/node/mod.rs

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,22 @@ impl fmt::Debug for Node {
8080

8181
impl Node {
8282
/// Creates a new node in the empty namespace.
83+
<<<<<<< HEAD
8384
///
8485
/// See [`Node::new_with_namespace`] for documentation.
86+
=======
87+
#[deprecated(since = "0.2.0", note = "please use `NodeBuilder` instead")]
88+
>>>>>>> Replace constructor method procedure
8589
#[allow(clippy::new_ret_no_self)]
8690
pub fn new(node_name: &str, context: &Context) -> Result<Node, RclReturnCode> {
87-
Self::new_with_namespace("", node_name, context)
91+
NodeBuilder::new(node_name, context).build()
8892
}
8993

9094
/// Creates a new node in a namespace.
9195
///
9296
/// A namespace without a leading forward slash is automatically changed to have a leading
9397
/// forward slash.
98+
<<<<<<< HEAD
9499
///
95100
/// # Naming
96101
/// The node namespace will be prefixed to the node name itself to form the *fully qualified
@@ -138,42 +143,17 @@ impl Node {
138143
/// [1]: https://docs.ros.org/en/rolling/How-To-Guides/Node-arguments.html
139144
/// [2]: https://docs.ros2.org/latest/api/rmw/validate__namespace_8h.html
140145
/// [3]: https://docs.ros2.org/latest/api/rmw/validate__node__name_8h.html
146+
=======
147+
#[deprecated(since = "0.2.0", note = "please use `NodeBuilder` instead")]
148+
>>>>>>> Replace constructor method procedure
141149
pub fn new_with_namespace(
142150
node_ns: &str,
143151
node_name: &str,
144152
context: &Context,
145153
) -> Result<Node, RclReturnCode> {
146-
let raw_node_name = CString::new(node_name).unwrap();
147-
let raw_node_ns = CString::new(node_ns).unwrap();
148-
149-
// SAFETY: Getting a zero-initialized value is always safe.
150-
let mut node_handle = unsafe { rcl_get_zero_initialized_node() };
151-
let context_handle = &mut *context.handle.lock();
152-
153-
unsafe {
154-
// SAFETY: No preconditions for this function.
155-
let node_options = rcl_node_get_default_options();
156-
// SAFETY: The node handle is zero-initialized as expected by this function.
157-
// The strings and node options are copied by this function, so we don't need
158-
// to keep them alive.
159-
// The context handle is kept alive because it is co-owned by the node.
160-
rcl_node_init(
161-
&mut node_handle,
162-
raw_node_name.as_ptr(),
163-
raw_node_ns.as_ptr(),
164-
context_handle,
165-
&node_options,
166-
)
167-
.ok()?;
168-
}
169-
170-
let handle = Arc::new(Mutex::new(node_handle));
171-
172-
Ok(Node {
173-
handle,
174-
context: context.handle.clone(),
175-
subscriptions: std::vec![],
176-
})
154+
NodeBuilder::new(node_name, context)
155+
.namespace(node_ns)
156+
.build()
177157
}
178158

179159
/// Returns the name of the node.

0 commit comments

Comments
 (0)