Skip to content

Commit 30e33ed

Browse files
authored
Make code a little nicer
1 parent 514ab2a commit 30e33ed

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/main.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,23 @@ type TNodePool = TNonFreePooledMemManager<TNode, 64>;
3030
impl TNode {
3131
#[inline(always)]
3232
fn check_node(node: *mut TNode) -> i32 {
33-
unsafe {
34-
// `node` is never itself null when passed into this function. Also, anecdotally, IMO Rust's
35-
// syntax for dereferencing raw pointers could not possibly be worse than what it is.
36-
if !((*node).right.is_null() && (*node).left.is_null()) {
37-
return 1 + TNode::check_node((*node).right) + TNode::check_node((*node).left);
38-
}
33+
// `node` is never itself null when passed into this function.
34+
let node_ref = unsafe { &mut *node };
35+
if !(node_ref.right.is_null() && node_ref.left.is_null()) {
36+
return 1 + TNode::check_node(node_ref.right) + TNode::check_node(node_ref.left);
3937
}
4038
1
4139
}
4240

4341
// This can't be `&mut` instead of `*mut` due to the lifetime / borrowing rules.
4442
#[inline(always)]
4543
fn make_tree(depth: i32, node_pool: *mut TNodePool) -> *mut TNode {
46-
unsafe {
47-
let result = (*node_pool).new_item();
48-
if depth > 0 {
49-
(*result).right = TNode::make_tree(depth - 1, node_pool);
50-
(*result).left = TNode::make_tree(depth - 1, node_pool);
51-
}
52-
result
44+
let res_ref = unsafe { &mut *((*node_pool).new_item()) };
45+
if depth > 0 {
46+
res_ref.right = TNode::make_tree(depth - 1, node_pool);
47+
res_ref.left = TNode::make_tree(depth - 1, node_pool);
5348
}
49+
res_ref as *mut T
5450
}
5551
}
5652

0 commit comments

Comments
 (0)