Skip to content
  • Federico Mena Quintero's avatar
    Turn NodeData into an enum with variants for Element and Text · 741a0c0b
    Federico Mena Quintero authored
    pub enum NodeData {
        Element(Element),
        Text(NodeChars),
    }
    
    The old NodeData is now Element.
    
    This will let us box the Element, so that it takes as little space as
    possible for the enum when it has a NodeData::Text value.
    
    To make this easier for the rest of the code, now there is a
    NodeBorrow trait, implemented for RsvgNode and imported pretty much
    everywhere, with the following methods:
    
    pub trait NodeBorrow {
        /// Returns `false` for NodeData::Text, `true` otherwise.
        fn is_element(&self) -> bool;
    
        /// Borrows a `NodeChars` reference.
        ///
        /// Panics: will panic if `&self` is not a `NodeData::Text` node
        fn borrow_chars(&self) -> Ref<NodeChars>;
    
        /// Borrows an `Element` reference
        ///
        /// Panics: will panic if `&self` is not a `NodeData::Element` node
        fn borrow_element(&self) -> Ref<Element>;
    
        /// Borrows an `Element` reference mutably
        ///
        /// Panics: will panic if `&self` is not a `NodeData::Element` node
        fn borrow_element_mut(&mut self) -> RefMut<Element>;
    }
    
    So, instead of doing node.borrow() -> Ref<NodeData>, we now can do
    node.borrow_element() directly, for example.
    
    The calling code is still supposed to first ensure that a node
    is_element() or not, just like it was doing
    `get_type() == NodeType::Foo` before doing get_impl::<Foo>().
    741a0c0b