Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename method fn value() on NodeMut<…> to value_mut(), re-introducing the former as an immutable getter #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 51 additions & 42 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,28 +360,37 @@ impl<'a, T: 'a> NodeMut<'a, T> {
self.tree
}

fn node(&mut self) -> &mut Node<T> {
fn node(&self) -> &Node<T> {
unsafe { self.tree.node(self.id) }
}

fn node_mut(&mut self) -> &mut Node<T> {
unsafe { self.tree.node_mut(self.id) }
}

/// Returns the value of this node.
pub fn value(&mut self) -> &mut T {
&mut self.node().value
/// Returns a reference to the value of this node.
pub fn value(&self) -> &T {
&self.node().value
}

/// Returns a mutable reference to the value of this node.
pub fn value_mut(&mut self) -> &mut T {
&mut self.node_mut().value
}

fn axis<F>(&mut self, f: F) -> Option<NodeMut<T>>
where
F: FnOnce(&mut Node<T>) -> Option<NodeId>,
{
let id = f(self.node());
let id = f(self.node_mut());
id.map(move |id| unsafe { self.tree.get_unchecked_mut(id) })
}

fn into_axis<F>(mut self, f: F) -> Result<Self, Self>
where
F: FnOnce(&mut Node<T>) -> Option<NodeId>,
{
let id = f(self.node());
let id = f(self.node_mut());
match id {
Some(id) => Ok(unsafe { self.tree.get_unchecked_mut(id) }),
None => Err(self),
Expand Down Expand Up @@ -509,17 +518,17 @@ impl<'a, T: 'a> NodeMut<'a, T> {

/// Detaches this node from its parent.
pub fn detach(&mut self) {
let parent_id = match self.node().parent {
let parent_id = match self.node_mut().parent {
Some(id) => id,
None => return,
};
let prev_sibling_id = self.node().prev_sibling;
let next_sibling_id = self.node().next_sibling;
let prev_sibling_id = self.node_mut().prev_sibling;
let next_sibling_id = self.node_mut().next_sibling;

{
self.node().parent = None;
self.node().prev_sibling = None;
self.node().next_sibling = None;
self.node_mut().parent = None;
self.node_mut().prev_sibling = None;
self.node_mut().next_sibling = None;
}

if let Some(id) = prev_sibling_id {
Expand Down Expand Up @@ -556,14 +565,14 @@ impl<'a, T: 'a> NodeMut<'a, T> {
"Cannot append node as a child to itself"
);

let last_child_id = self.node().children.map(|(_, id)| id);
let last_child_id = self.node_mut().children.map(|(_, id)| id);

if last_child_id != Some(new_child_id) {
{
let mut new_child = self.tree.get_mut(new_child_id).unwrap();
new_child.detach();
new_child.node().parent = Some(self.id);
new_child.node().prev_sibling = last_child_id;
new_child.node_mut().parent = Some(self.id);
new_child.node_mut().prev_sibling = last_child_id;
}

if let Some(id) = last_child_id {
Expand All @@ -573,7 +582,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
}

{
self.node().children = match self.node().children {
self.node_mut().children = match self.node_mut().children {
Some((first_child_id, _)) => Some((first_child_id, new_child_id)),
None => Some((new_child_id, new_child_id)),
};
Expand All @@ -595,14 +604,14 @@ impl<'a, T: 'a> NodeMut<'a, T> {
"Cannot prepend node as a child to itself"
);

let first_child_id = self.node().children.map(|(id, _)| id);
let first_child_id = self.node_mut().children.map(|(id, _)| id);

if first_child_id != Some(new_child_id) {
{
let mut new_child = self.tree.get_mut(new_child_id).unwrap();
new_child.detach();
new_child.node().parent = Some(self.id);
new_child.node().next_sibling = first_child_id;
new_child.node_mut().parent = Some(self.id);
new_child.node_mut().next_sibling = first_child_id;
}

if let Some(id) = first_child_id {
Expand All @@ -612,7 +621,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
}

{
self.node().children = match self.node().children {
self.node_mut().children = match self.node_mut().children {
Some((_, last_child_id)) => Some((new_child_id, last_child_id)),
None => Some((new_child_id, new_child_id)),
};
Expand All @@ -635,15 +644,15 @@ impl<'a, T: 'a> NodeMut<'a, T> {
"Cannot insert node as a sibling of itself"
);

let parent_id = self.node().parent.unwrap();
let prev_sibling_id = self.node().prev_sibling;
let parent_id = self.node_mut().parent.unwrap();
let prev_sibling_id = self.node_mut().prev_sibling;

{
let mut new_sibling = self.tree.get_mut(new_sibling_id).unwrap();
new_sibling.detach();
new_sibling.node().parent = Some(parent_id);
new_sibling.node().prev_sibling = prev_sibling_id;
new_sibling.node().next_sibling = Some(self.id);
new_sibling.node_mut().parent = Some(parent_id);
new_sibling.node_mut().prev_sibling = prev_sibling_id;
new_sibling.node_mut().next_sibling = Some(self.id);
}

if let Some(id) = prev_sibling_id {
Expand All @@ -652,7 +661,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
}
}

self.node().prev_sibling = Some(new_sibling_id);
self.node_mut().prev_sibling = Some(new_sibling_id);

{
let parent = unsafe { self.tree.node_mut(parent_id) };
Expand All @@ -678,15 +687,15 @@ impl<'a, T: 'a> NodeMut<'a, T> {
"Cannot insert node as a sibling of itself"
);

let parent_id = self.node().parent.unwrap();
let next_sibling_id = self.node().next_sibling;
let parent_id = self.node_mut().parent.unwrap();
let next_sibling_id = self.node_mut().next_sibling;

{
let mut new_sibling = self.tree.get_mut(new_sibling_id).unwrap();
new_sibling.detach();
new_sibling.node().parent = Some(parent_id);
new_sibling.node().prev_sibling = Some(self.id);
new_sibling.node().next_sibling = next_sibling_id;
new_sibling.node_mut().parent = Some(parent_id);
new_sibling.node_mut().prev_sibling = Some(self.id);
new_sibling.node_mut().next_sibling = next_sibling_id;
}

if let Some(id) = next_sibling_id {
Expand All @@ -695,7 +704,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
}
}

self.node().next_sibling = Some(new_sibling_id);
self.node_mut().next_sibling = Some(new_sibling_id);

{
let parent = unsafe { self.tree.node_mut(parent_id) };
Expand All @@ -722,7 +731,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {

let new_child_ids = {
let mut from = self.tree.get_mut(from_id).unwrap();
match from.node().children.take() {
match from.node_mut().children.take() {
Some(ids) => ids,
None => return,
}
Expand All @@ -733,18 +742,18 @@ impl<'a, T: 'a> NodeMut<'a, T> {
self.tree.node_mut(new_child_ids.1).parent = Some(self.id);
}

if self.node().children.is_none() {
self.node().children = Some(new_child_ids);
if self.node_mut().children.is_none() {
self.node_mut().children = Some(new_child_ids);
return;
}

let old_child_ids = self.node().children.unwrap();
let old_child_ids = self.node_mut().children.unwrap();
unsafe {
self.tree.node_mut(old_child_ids.1).next_sibling = Some(new_child_ids.0);
self.tree.node_mut(new_child_ids.0).prev_sibling = Some(old_child_ids.1);
}

self.node().children = Some((old_child_ids.0, new_child_ids.1));
self.node_mut().children = Some((old_child_ids.0, new_child_ids.1));
}

/// Reparents the children of a node, prepending them to this node.
Expand All @@ -761,7 +770,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {

let new_child_ids = {
let mut from = self.tree.get_mut(from_id).unwrap();
match from.node().children.take() {
match from.node_mut().children.take() {
Some(ids) => ids,
None => return,
}
Expand All @@ -772,18 +781,18 @@ impl<'a, T: 'a> NodeMut<'a, T> {
self.tree.node_mut(new_child_ids.1).parent = Some(self.id);
}

if self.node().children.is_none() {
self.node().children = Some(new_child_ids);
if self.node_mut().children.is_none() {
self.node_mut().children = Some(new_child_ids);
return;
}

let old_child_ids = self.node().children.unwrap();
let old_child_ids = self.node_mut().children.unwrap();
unsafe {
self.tree.node_mut(old_child_ids.0).prev_sibling = Some(new_child_ids.1);
self.tree.node_mut(new_child_ids.1).next_sibling = Some(old_child_ids.0);
}

self.node().children = Some((new_child_ids.0, old_child_ids.1));
self.node_mut().children = Some((new_child_ids.0, old_child_ids.1));
}
}

Expand Down
Loading