Skip to content

Commit

Permalink
Assert there are no circular references (#3025)
Browse files Browse the repository at this point in the history
* assert there are no circular references

the check is costly in release builds and should always fail
note that the current PartialEq impl is *not* symmetric!
should be fixed as well, with an improved design

* remove internal test for cyclic node refs

wasm_bindgen does not yet support #[should_panic]
see also rustwasm/wasm-bindgen#2286
  • Loading branch information
WorldSEnder authored Dec 10, 2022
1 parent d7f9880 commit 57f45e2
Showing 1 changed file with 4 additions and 30 deletions.
34 changes: 4 additions & 30 deletions packages/yew/src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ mod feat_csr {
/// Link a downstream `NodeRef`
pub(crate) fn link(&self, node_ref: Self) {
// Avoid circular references
if self == &node_ref {
return;
}
debug_assert!(
self != &node_ref,
"no circular references allowed! Report this as a bug in yew"
);

let mut this = self.0.borrow_mut();
this.node = None;
Expand Down Expand Up @@ -203,30 +204,3 @@ mod feat_hydration {
pub fn create_portal(child: Html, host: Element) -> Html {
VNode::VPortal(VPortal::new(child, host))
}

#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod tests {
use gloo::utils::document;
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};

use super::*;

wasm_bindgen_test_configure!(run_in_browser);

#[test]
fn self_linking_node_ref() {
let node: Node = document().create_text_node("test node").into();
let node_ref = NodeRef::new(node.clone());
let node_ref_2 = NodeRef::new(node.clone());

// Link to self
node_ref.link(node_ref.clone());
assert_eq!(node, node_ref.get().unwrap());

// Create cycle of two node refs
node_ref.link(node_ref_2.clone());
node_ref_2.link(node_ref);
assert_eq!(node, node_ref_2.get().unwrap());
}
}

0 comments on commit 57f45e2

Please sign in to comment.