-
Notifications
You must be signed in to change notification settings - Fork 162
Add NodeBuilder for node instantiation #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0e21772
Add NodeBuilder
Soya-Onishi b25b466
Replace constructor method procedure
Soya-Onishi bacc6da
Fix doc to compile example successfully
Soya-Onishi 3366daa
Fix doc to work test successfully
Soya-Onishi 26a8ccb
Fix field types for more simplicity
Soya-Onishi 8e55d24
Remove unused code
Soya-Onishi adcc2d7
Fix doc comments for cleanliness
Soya-Onishi 15373e4
Fix doc example for more easier to understand how to use
Soya-Onishi fc04e9a
Fix doc for more cleanliness
Soya-Onishi 1ee051a
Fix docs, Replace err types and Delete some methods
Soya-Onishi 1ddb53f
Merge branch 'master' into node_builder
Soya-Onishi 9596b49
Fix docs reference
Soya-Onishi 3bfb2c6
Make documentation more consistent
nnmm 853d7b4
Move NodeBuilder to a dedicated file
Soya-Onishi 3982810
Fix doc examples and use Node::builder instead NodeBuilder
Soya-Onishi ac7404a
Fix doc reference and re-add pub to NodeBuilder::new
Soya-Onishi 30778f2
Swap arguments to match other related methods
Soya-Onishi dcb17dd
Fix doc to appropriate text
Soya-Onishi 8a67e7d
Add description about taking name
Soya-Onishi c31fe6e
Fix reference
Soya-Onishi aa1b450
Fix entity for reference
Soya-Onishi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
use crate::rcl_bindings::*; | ||
use crate::{Context, Node, RclrsError, ToResult}; | ||
|
||
use std::ffi::CString; | ||
|
||
use parking_lot::Mutex; | ||
use std::sync::Arc; | ||
|
||
/// A builder for creating a [`Node`][1]. | ||
/// | ||
/// The builder pattern allows selectively setting some fields, and leaving all others at their default values. | ||
/// This struct instance can be created via [`Node::builder()`][2]. | ||
/// | ||
/// The default values for optional fields are: | ||
/// - `namespace: "/"` | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// # use rclrs::{Context, NodeBuilder, Node, RclrsError}; | ||
/// let context = Context::new([])?; | ||
/// // Building a node in a single expression | ||
/// let node = NodeBuilder::new(&context, "foo_node").namespace("/bar").build()?; | ||
/// assert_eq!(node.name(), "foo_node"); | ||
/// assert_eq!(node.namespace(), "/bar"); | ||
/// // Building a node via Node::builder() | ||
/// let node = Node::builder(&context, "bar_node").build()?; | ||
/// assert_eq!(node.name(), "bar_node"); | ||
/// // Building a node step-by-step | ||
/// let mut builder = Node::builder(&context, "goose"); | ||
/// builder = builder.namespace("/duck/duck"); | ||
/// let node = builder.build()?; | ||
/// assert_eq!(node.fully_qualified_name(), "/duck/duck/goose"); | ||
/// # Ok::<(), RclrsError>(()) | ||
/// ``` | ||
/// | ||
/// [1]: crate::Node | ||
/// [2]: crate::Node::builder | ||
/// | ||
pub struct NodeBuilder { | ||
context: Arc<Mutex<rcl_context_t>>, | ||
name: String, | ||
namespace: String, | ||
} | ||
|
||
impl NodeBuilder { | ||
/// Creates a builder for a node with the given name. | ||
/// | ||
/// See the [`Node` docs][1] for general information on node names. | ||
/// | ||
/// # Rules for valid node names | ||
/// | ||
/// The rules for a valid node name are checked by the [`rmw_validate_node_name()`][2] | ||
/// function. They are: | ||
/// - Must contain only the `a-z`, `A-Z`, `0-9`, and `_` characters | ||
/// - Must not be empty and not be longer than `RMW_NODE_NAME_MAX_NAME_LENGTH` | ||
/// - Must not start with a number | ||
/// | ||
/// Note that node name validation is delayed until [`NodeBuilder::build()`][3]. | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// # use rclrs::{Context, NodeBuilder, RclrsError, RclReturnCode, NodeErrorCode}; | ||
/// let context = Context::new([])?; | ||
/// // This is a valid node name | ||
/// assert!(NodeBuilder::new(&context, "my_node").build().is_ok()); | ||
/// // This is another valid node name (although not a good one) | ||
/// assert!(NodeBuilder::new(&context, "_______").build().is_ok()); | ||
/// // This is an invalid node name | ||
/// assert_eq!( | ||
/// NodeBuilder::new(&context, "röböt") | ||
/// .build() | ||
/// .unwrap_err() | ||
/// .code, | ||
/// RclReturnCode::NodeError(NodeErrorCode::NodeInvalidName) | ||
/// ); | ||
/// # Ok::<(), RclrsError>(()) | ||
/// ``` | ||
/// | ||
/// [1]: crate::Node#naming | ||
/// [2]: https://docs.ros2.org/latest/api/rmw/validate__node__name_8h.html#a5690a285aed9735f89ef11950b6e39e3 | ||
/// [3]: NodeBuilder::build | ||
pub fn new(context: &Context, name: &str) -> NodeBuilder { | ||
NodeBuilder { | ||
context: context.handle.clone(), | ||
name: name.to_string(), | ||
namespace: "/".to_string(), | ||
} | ||
} | ||
|
||
/// Sets the node namespace. | ||
/// | ||
/// See the [`Node` docs][1] for general information on namespaces. | ||
/// | ||
/// # Rules for valid namespaces | ||
/// | ||
/// The rules for a valid node namespace are based on the [rules for a valid topic][2] | ||
/// and are checked by the [`rmw_validate_namespace()`][3] function. However, a namespace | ||
/// without a leading forward slash is automatically changed to have a leading forward slash | ||
/// before it is checked with this function. | ||
/// | ||
/// Thus, the effective rules are: | ||
/// - Must contain only the `a-z`, `A-Z`, `0-9`, `_`, and `/` characters | ||
/// - Must not have a number at the beginning, or after a `/` | ||
/// - Must not contain two or more `/` characters in a row | ||
/// - Must not have a `/` character at the end, except if `/` is the full namespace | ||
/// | ||
/// Note that namespace validation is delayed until [`NodeBuilder::build()`][4]. | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// # use rclrs::{Context, Node, RclrsError, RclReturnCode, NodeErrorCode}; | ||
/// let context = Context::new([])?; | ||
/// // This is a valid namespace | ||
/// let builder_ok_ns = Node::builder(&context, "my_node").namespace("/some/nested/namespace"); | ||
/// assert!(builder_ok_ns.build().is_ok()); | ||
/// // This is an invalid namespace | ||
/// assert_eq!( | ||
/// Node::builder(&context, "my_node") | ||
/// .namespace("/10_percent_luck/20_percent_skill") | ||
/// .build() | ||
/// .unwrap_err() | ||
/// .code, | ||
/// RclReturnCode::NodeError(NodeErrorCode::NodeInvalidNamespace) | ||
/// ); | ||
/// // A missing forward slash at the beginning is automatically added | ||
/// assert_eq!( | ||
/// Node::builder(&context, "my_node") | ||
/// .namespace("foo") | ||
/// .build()? | ||
/// .namespace(), | ||
/// "/foo" | ||
/// ); | ||
/// # Ok::<(), RclrsError>(()) | ||
/// ``` | ||
/// | ||
/// [1]: crate::Node#naming | ||
/// [2]: http://design.ros2.org/articles/topic_and_service_names.html | ||
/// [3]: https://docs.ros2.org/latest/api/rmw/validate__namespace_8h.html#a043f17d240cf13df01321b19a469ee49 | ||
/// [4]: NodeBuilder::build | ||
pub fn namespace(mut self, namespace: &str) -> Self { | ||
self.namespace = namespace.to_string(); | ||
self | ||
} | ||
|
||
/// Builds the node instance. | ||
/// | ||
/// Node name and namespace validation is performed in this method. | ||
/// | ||
/// For example usage, see the [`NodeBuilder`][1] docs. | ||
/// | ||
/// # Panics | ||
/// When the node name or namespace contain null bytes. | ||
/// | ||
/// [1]: crate::NodeBuilder | ||
pub fn build(&self) -> Result<Node, RclrsError> { | ||
let node_name = CString::new(self.name.as_str()).unwrap(); | ||
let node_namespace = CString::new(self.namespace.as_str()).unwrap(); | ||
|
||
// SAFETY: No preconditions for this function. | ||
let mut node_handle = unsafe { rcl_get_zero_initialized_node() }; | ||
|
||
unsafe { | ||
// SAFETY: No preconditions for this function. | ||
let context_handle = &mut *self.context.lock(); | ||
// SAFETY: No preconditions for this function. | ||
let node_options = rcl_node_get_default_options(); | ||
|
||
// SAFETY: The node handle is zero-initialized as expected by this function. | ||
// The strings and node options are copied by this function, so we don't need | ||
// to keep them alive. | ||
// The context handle has to be kept alive because it is co-owned by the node. | ||
rcl_node_init( | ||
&mut node_handle, | ||
node_name.as_ptr(), | ||
node_namespace.as_ptr(), | ||
context_handle, | ||
&node_options, | ||
) | ||
.ok()?; | ||
}; | ||
|
||
let handle = Arc::new(Mutex::new(node_handle)); | ||
|
||
Ok(Node { | ||
handle, | ||
context: self.context.clone(), | ||
subscriptions: std::vec![], | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.