Hi all. I'm new to rust programming language and start my journey to become a more advanced user. So I'm trying to implement a DOM tree to master smart pointers but i face a compilation error. I try to solve myself this error and also try to use chatgpt but the answer was the same code that's mine.
Here the code:
Html5ever internal struct:
pub struct ExpandedName<'a> {
pub ns: &'a Atom<NamespaceStaticSet>,
pub local: &'a Atom<LocalNameStaticSet>,
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone)]
#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))]
pub struct QualName {
pub prefix: Option<Prefix>,
pub ns: Namespace,
pub local: LocalName,
}
And my code to implement TreeSink, structs:
pub enum NodeData {
Document,
Doctype {
name: StrTendril,
public_id: StrTendril,
system_id: StrTendril,
},
Text {
contents: RefCell<StrTendril>,
},
Comment {
contents: StrTendril,
},
Element {
name: QualName,
attributes: RefCell<Vec<Attribute>>,
template_contents: Option<Link>,
mathml_annotation_xml_integration_point: bool,
},
ProcessingInstruction {
target: StrTendril,
contents: StrTendril,
},
}
type Link = Rc<RefCell<Node>>;
type WeakLink = Weak<RefCell<Node>>;
pub struct Node {
pub data: NodeData,
pub parent: Option<WeakLink>,
pub children: Rc<RefCell<Vec<Link>>>,
}
The method that cause the error when implementing trait TreeSink for my tree:
...
type Handle = Link;
type Output = Self;
fn elem_name<'a>(&'a self, target: &'a Self::Handle) -> ExpandedName<'a> {
return match target.borrow().data {
NodeData::Element { ref name, .. } => name.expanded(),
_ => panic!("not an element!"),
};
}
I've got this error on name.expanded() : "returns a value referencing data owned by the current function". I tried to break down the problem and I manage to write a function returning a QualName but the problem seems to be with the ExpandedName returned by expanded() method with references &'a Atom despite the lifetime 'a. Any idea on how to solve the error ? Thanks.