Skip to content

Commit

Permalink
Fixed a few issues and implemented register_as_singleton directly o…
Browse files Browse the repository at this point in the history
…n `NodeBase`.
  • Loading branch information
LunaticWyrm467 committed Sep 23, 2024
1 parent 1a0eedd commit f7d3869
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node_tree_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node_tree"
version = "0.6.1"
version = "0.6.2"
edition = "2021"
rust-version = "1.78"

Expand Down
38 changes: 17 additions & 21 deletions node_tree_core/src/structs/node_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,36 +86,32 @@ impl NodeBase {
}

/// Sets the name of the node.
/// This will fail if the name is not unique within the context of the parent's children
/// vector.
/// Returns false if the operation fails.
pub fn set_name(&mut self, name: &str) -> bool {
/// If the name is not unique among the node's siblings, then it will be made into a unique name.
pub fn set_name(&mut self, name: &str) {
if let (Some(parent), Some(tree)) = (self.parent, self.tree()) {
let mut is_unique: bool = true;
let parent: &dyn Node = unsafe { tree.get_node(parent).unwrap_unchecked() };
let siblings: &[String] = &parent.children().iter().map(|a| a.name().to_string()).collect::<Vec<_>>();

for sibling_name in siblings {
let self_name: &str = self.name();
if sibling_name == self_name { // Ignore THIS node.
continue;
} else if sibling_name == name {
is_unique = false;
break;
}
}

if is_unique {
unsafe {
self.set_name_unchecked(name);
}
unsafe {
self.set_name_unchecked(&ensure_unique_name(name, siblings));
}
is_unique
} else {
unsafe {
self.set_name_unchecked(name);
}
true
}
}

/// Registers this node as a singleton.
/// Returns whether the name was set successfully.
///
/// # Panics
/// Panics if this Node is not connected to a `NodeTree`.
pub fn register_as_singleton(&mut self, name: String) -> bool {
let rid: RID = self.rid;
match self.tree_mut() {
None => panic!("Cannot register a node that is not apart of the Nodetree as a singleton!"),
Some(tree) => tree.register_as_singleton(rid, name).unwrap()
}
}

Expand Down

0 comments on commit f7d3869

Please sign in to comment.