Skip to content

Commit

Permalink
fix: unsafe issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed May 19, 2023
1 parent 958ff56 commit 7f77401
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
4 changes: 2 additions & 2 deletions rust/crates/tidy-tree/src/layout/basic_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ impl Layout for BasicLayout {
self.update_meta(node);
});
root.pre_order_traversal_mut(|node| {
if let Some(parent) = node.parent {
let parent = unsafe { parent.as_ref() };
if let Some(mut parent) = node.parent {
let parent = unsafe { parent.as_mut() };
node.x = parent.x + node.relative_x;
node.y = parent.y + node.relative_y;
}
Expand Down
18 changes: 9 additions & 9 deletions rust/crates/tidy-tree/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl Node {
self.tidy.as_ref().unwrap()
}

fn reset_parent_link(&mut self) {
fn reset_parent_link_of_children(&mut self) {
if self.children.is_empty() {
return;
}
Expand All @@ -161,8 +161,8 @@ impl Node {
pub fn append_child(&mut self, mut child: Self) -> NonNull<Self> {
child.parent = Some(self.into());
let mut boxed = Box::new(child);
boxed.reset_parent_link();
let ptr = boxed.as_ref().into();
boxed.reset_parent_link_of_children();
let ptr = boxed.as_mut().into();
self.children.push(boxed);
ptr
}
Expand Down Expand Up @@ -202,7 +202,7 @@ impl Node {

stack.push((node_ptr, false));
for child in node.children.iter_mut() {
stack.push((child.as_ref().into(), true));
stack.push((child.as_mut().into(), true));
}
}
}
Expand All @@ -221,7 +221,7 @@ impl Node {

stack.push((node_ptr, false));
for child in node.children.iter_mut() {
stack.push((child.as_ref().into(), true));
stack.push((child.as_mut().into(), true));
}
}
}
Expand All @@ -235,7 +235,7 @@ impl Node {
let node = unsafe { node.as_mut() };
f(node);
for child in node.children.iter_mut() {
stack.push(child.as_ref().into());
stack.push(child.as_mut().into());
}
}
}
Expand All @@ -249,7 +249,7 @@ impl Node {
let node = unsafe { node.as_mut() };
f(node);
for child in node.children.iter_mut() {
stack.push(child.as_ref().into());
stack.push(child.as_mut().into());
}
}
}
Expand All @@ -264,7 +264,7 @@ impl Node {
let node = unsafe { node.as_mut() };
f(node, depth);
for child in node.children.iter_mut() {
queue.push_back((child.as_ref().into(), depth + 1));
queue.push_back((child.as_mut().into(), depth + 1));
}
}
}
Expand All @@ -285,7 +285,7 @@ impl Node {
let node = unsafe { node.as_mut() };
f(node, depth);
for child in node.children.iter_mut() {
stack.push((child.as_ref().into(), depth + 1));
stack.push((child.as_mut().into(), depth + 1));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions rust/crates/tidy-tree/tests/aesthetic_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub fn check_y_position_in_same_level(root: &Node) {

pub fn assert_no_crossed_lines(root: &Node) {
let mut lines: Vec<Line> = vec![];
// println!("{}", &root.str());
root.post_order_traversal(|node| {
for child in node.children.iter() {
let line = Line {
Expand Down
14 changes: 7 additions & 7 deletions rust/crates/tidy-tree/tests/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ pub fn gen_node(rng: &mut StdRng) -> Node {
}
}

pub fn gen_tree(rng: &mut StdRng, num: usize) -> Node {
let root = gen_node(rng);
let mut nodes: Vec<NonNull<Node>> = vec![(&root).into()];
pub fn gen_tree(rng: &mut StdRng, num: usize) -> Box<Node> {
let mut root = Box::new(gen_node(rng));
let mut nodes: Vec<NonNull<Node>> = vec![(&mut *root).into()];
for _ in 0..num {
let parent_index = rng.gen_range(0..nodes.len());
let parent = unsafe { nodes[parent_index].as_mut() };
let node = gen_node(rng);
parent.append_child(node);
nodes.push(parent.children.last().unwrap().as_ref().into());
nodes.push(parent.children.last_mut().unwrap().as_mut().into());
}

root
}

pub fn prepare_tree(rng: &mut StdRng) -> (Node, Vec<NonNull<Node>>) {
let root = gen_node(rng);
let nodes: Vec<NonNull<Node>> = vec![(&root).into()];
pub fn prepare_tree(rng: &mut StdRng) -> (Box<Node>, Vec<NonNull<Node>>) {
let mut root = Box::new(gen_node(rng));
let nodes: Vec<NonNull<Node>> = vec![(&mut *root).into()];
(root, nodes)
}

Expand Down
2 changes: 1 addition & 1 deletion rust/crates/tidy-tree/tests/layout_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mod aesthetic_rules;
mod gen;

use std::{time::Instant};
use std::time::Instant;

use rand::{prelude::StdRng, SeedableRng};
use tidy_tree::{BasicLayout, Layout, TidyLayout};
Expand Down

0 comments on commit 7f77401

Please sign in to comment.