Skip to content

Commit

Permalink
VirtualNode: Add as_X_variant_ref* methods
Browse files Browse the repository at this point in the history
This allows downcasting into the variants.
  • Loading branch information
dbrgn committed Feb 5, 2019
1 parent 0af49f7 commit 06ee630
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions crates/virtual-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,38 @@ impl VirtualNode {
VirtualNodeText { text: text.into() }
}

/// Cast into a `VirtualNodeElement` reference, if this is an `Element` variant.
pub fn as_element_variant_ref(&self) -> Option<&VirtualNodeElement> {
match self {
VirtualNode::Element(ref element_node) => Some(element_node),
_ => None,
}
}

/// Cast into a mutable `VirtualNodeElement` reference, if this is an `Element` variant.
pub fn as_element_variant_ref_mut(&mut self) -> Option<&mut VirtualNodeElement> {
match self {
VirtualNode::Element(ref mut element_node) => Some(element_node),
_ => None,
}
}

/// Cast into a `VirtualNodeText` reference, if this is a `Text` variant.
pub fn as_text_variant_ref(&self) -> Option<&VirtualNodeText> {
match self {
VirtualNode::Text(ref text_node) => Some(text_node),
_ => None,
}
}

/// Cast into a mutable `VirtualNodeText` reference, if this is a `Text` variant.
pub fn as_text_variant_ref_mut(&mut self) -> Option<&mut VirtualNodeText> {
match self {
VirtualNode::Text(ref mut text_node) => Some(text_node),
_ => None,
}
}

/// Create and return a `CreatedNode` instance (containing a DOM `Node`
/// together with potentially related closures) for this virtual node.
pub fn create_dom_node(&self) -> CreatedNode {
Expand Down

0 comments on commit 06ee630

Please sign in to comment.