Skip to content

Commit

Permalink
downgrade Node, Context and Object document references to Weak
Browse files Browse the repository at this point in the history
  • Loading branch information
dginev committed Sep 24, 2018
1 parent 3d21d1d commit bd4b120
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/tree/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::{CStr, CString};
use std::ptr;
use std::rc::Rc;
use std::rc::{Rc, Weak};
use std::str;

use bindings::*;
Expand All @@ -16,6 +16,7 @@ use c_helpers::*;
use tree::node::Node;

pub(crate) type DocumentRef = Rc<RefCell<_Document>>;
pub(crate) type DocumentWeak = Weak<RefCell<_Document>>;

#[derive(Debug)]
pub(crate) struct _Document {
Expand Down
2 changes: 1 addition & 1 deletion src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod node;
mod nodetype;

pub use tree::document::Document;
pub(crate) use tree::document::DocumentRef;
pub(crate) use tree::document::{DocumentRef, DocumentWeak};
pub use tree::namespace::Namespace;
pub use tree::node::set_node_rc_guard;
pub use tree::node::{Node, NODE_RC_MAX_GUARD};
Expand Down
14 changes: 7 additions & 7 deletions src/tree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::ptr;
use std::rc::Rc;
use std::str;

use tree::document::{Document, DocumentRef};
use tree::document::{Document, DocumentRef, DocumentWeak};
use tree::namespace::Namespace;
use tree::nodetype::NodeType;
use xpath::Context;
Expand All @@ -38,7 +38,7 @@ struct _Node {
/// libxml's xmlNodePtr
node_ptr: xmlNodePtr,
/// Reference to parent `Document`
document: DocumentRef,
document: DocumentWeak,
/// Bookkeep removal from a parent
unlinked: bool,
}
Expand Down Expand Up @@ -139,7 +139,7 @@ impl Node {
// If newly encountered pointer, wrap
let node = _Node {
node_ptr,
document: document.clone(),
document: Rc::downgrade(&document),
unlinked: false,
};
let wrapped_node = Node(Rc::new(RefCell::new(node)));
Expand Down Expand Up @@ -172,7 +172,7 @@ impl Node {
unsafe { mem::transmute::<xmlNodePtr, usize>(self.node_ptr()) }
}

pub(crate) fn get_docref(&self) -> DocumentRef {
pub(crate) fn get_docref(&self) -> DocumentWeak {
self.0.borrow().document.clone()
}

Expand Down Expand Up @@ -644,7 +644,7 @@ impl Node {
c_name.as_bytes().as_ptr(),
ptr::null(),
);
Ok(Node::wrap(new_ptr, self.get_docref()))
Ok(Node::wrap(new_ptr, self.get_docref().upgrade().unwrap()))
}
}

Expand All @@ -668,7 +668,7 @@ impl Node {
c_name.as_bytes().as_ptr(),
c_content.as_bytes().as_ptr(),
);
Ok(Node::wrap(new_ptr, self.get_docref()))
Ok(Node::wrap(new_ptr, self.get_docref().upgrade().unwrap()))
}
}

Expand Down Expand Up @@ -722,7 +722,7 @@ impl Node {
if node_ptr.is_null() {
None
} else {
let new_node = Node::wrap(node_ptr, self.get_docref());
let new_node = Node::wrap(node_ptr, self.get_docref().upgrade().unwrap());
Some(new_node)
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/xpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::cell::RefCell;
use std::ffi::{CStr, CString};
use std::rc::Rc;
use std::str;
use tree::{Document, DocumentRef, Node};
use tree::{Document, DocumentRef, DocumentWeak, Node};

///Thinly wrapped libxml2 xpath context
pub(crate) type ContextRef = Rc<RefCell<_Context>>;
Expand All @@ -31,14 +31,14 @@ pub struct Context {
/// Safe reference to the libxml2 context pointer
pub(crate) context_ptr: ContextRef,
///Document contains pointer, needed for ContextPtr, so we need to borrow Document to prevent it's freeing
pub(crate) document: DocumentRef,
pub(crate) document: DocumentWeak,
}

///Essentially, the result of the evaluation of some xpath expression
pub struct Object {
///libxml's `ObjectPtr`
pub ptr: xmlXPathObjectPtr,
document: DocumentRef,
document: DocumentWeak,
}

impl Context {
Expand All @@ -50,7 +50,7 @@ impl Context {
} else {
Ok(Context {
context_ptr: Rc::new(RefCell::new(_Context(ctxtptr))),
document: doc.0.clone(),
document: Rc::downgrade(&doc.0),
})
}
}
Expand All @@ -61,7 +61,7 @@ impl Context {
} else {
Ok(Context {
context_ptr: Rc::new(RefCell::new(_Context(ctxtptr))),
document: docref.clone(),
document: Rc::downgrade(&docref),
})
}
}
Expand All @@ -74,7 +74,7 @@ impl Context {
/// Instantiate a new Context for the Document of a given Node.
/// Note: the Context is root-level for that document, use `.set_context_node` to limit scope to this node
pub fn from_node(node: &Node) -> Result<Context, ()> {
let docref = node.get_docref().clone();
let docref = node.get_docref().upgrade().unwrap();
Context::new_ptr(docref)
}

Expand Down Expand Up @@ -198,7 +198,7 @@ impl Object {
if ptr.is_null() {
panic!("rust-libxml: xpath: found null pointer result set");
}
let node = Node::wrap(ptr, self.document.clone());
let node = Node::wrap(ptr, self.document.upgrade().unwrap());
vec.push(node);
}
vec
Expand Down

0 comments on commit bd4b120

Please sign in to comment.