HTML 5544Rust 88Markdown 11TOML 91926 Definiciones
Return a Result from Taffy::remove (#106)
* Return a result from Sprawl::remove
* Update `RELEASES.md`
* Fix clippy error, fix compiler warnings
Deciding to unwrap in the tests, so that the tests fail if the remove fails.
* Run cargo fmt
* Move release note to new "0.2.0" section
* Add blank lines to satiate the Markdown Lint gods
* Remove pointless let _ = call
* Fix merge error
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
diff --git a/RELEASES.md b/RELEASES.md
index 6b2c60724..3ef21d4e1 100644
--- a/RELEASES.md
+++ b/RELEASES.md
@@ -8,6 +8,7 @@
### 0.2.0 Changed
+- `Sprawl::remove` now returns a `Result<usize, Error>`, to indicate if the operation was sucessful, and if it was, which ID was invalidated.
- renamed `taffy::forest::Forest.new-node(..)` `taffy::forest::Forest.new_with_children(..)`
- renamed `taffy::node::Taffy.new-node(..)` -> `taffy::node::Taffy.new_with_children(..)`
- renamed `taffy::style::Style` -> `taffy::style::FlexboxLayout` to more precicely indicate its purpose
diff --git a/src/node.rs b/src/node.rs
index 6fbac2230..97bfd1a40 100644
--- a/src/node.rs
+++ b/src/node.rs
@@ -123,9 +123,9 @@ impl Taffy {
/// Remove a specific [`Node`] from the tree
///
- /// Its [`Id`] is marked as invalid.
- pub fn remove(&mut self, node: Node) {
- let id = if let Ok(id) = self.find_node(node) { id } else { return };
+ /// Its [`Id`] is marked as invalid. Returns the id of the node removed.
+ pub fn remove(&mut self, node: Node) -> Result<usize, Error> {
+ let id = self.find_node(node)?;
self.nodes_to_ids.remove(&node);
self.ids_to_nodes.remove(&id);
@@ -135,6 +135,8 @@ impl Taffy {
let _ = self.nodes_to_ids.insert(new, id);
let _ = self.ids_to_nodes.insert(id, new);
}
+
+ Ok(id)
}
/// Sets the [`MeasureFunc`] of the associated node
diff --git a/tests/node.rs b/tests/node.rs
index 6999f85d1..38d7c8adf 100644
--- a/tests/node.rs
+++ b/tests/node.rs
@@ -111,7 +111,7 @@ mod node {
assert_eq!(taffy.children(node0).unwrap().as_slice(), &[node1]);
// Disconnect the tree: <0> <2>
- taffy.remove(node1);
+ taffy.remove(node1).unwrap();
assert!(taffy.style(node1).is_err());
@@ -186,7 +186,7 @@ mod node {
let child = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
taffy.add_child(parent, child).unwrap();
- taffy.remove(child);
- taffy.remove(parent);
+ taffy.remove(child).unwrap();
+ taffy.remove(parent).unwrap();
}
}