Skip to content

The scope of the unsafe block can be appropriately reduced #1

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/ll/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ impl<T> LinkedList<T> {
let mut node = Node::new(element);
unsafe {
node.as_mut().next = None;
}

match self.tail {
Some(mut tail) => tail.as_mut().next = Some(node),
Some(mut tail) => unsafe { tail.as_mut().next = Some(node) },
None => self.head = Some(node),
}
}

self.tail = Some(node);
self.len += 1;
Expand Down Expand Up @@ -133,9 +133,9 @@ impl<'a, T> Iterator for Iter<'a, T> {
if self.len == 0 {
None
} else {
self.head.map(|node| unsafe {
self.head.map(|node| {
// Need an unbound lifetime to get 'a
let node = &*node.as_ptr();
let node = unsafe { &*node.as_ptr() };
self.len -= 1;
self.head = node.next;
&node.element
Expand Down
4 changes: 2 additions & 2 deletions src/tree/binary/builder/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ fn build<K: std::str::FromStr, V>(vec: &[&str]) -> Tree<K, V> {
}

if let Some(mut node) = node {
let parent = binary_tree::parent(i);
let mut parent_node = aux[parent].unwrap();
unsafe {
let parent = binary_tree::parent(i);
let mut parent_node = aux[parent].unwrap();
node.as_mut().parent = Some(parent_node);
if binary_tree::left(parent) == i {
parent_node.as_mut().left = Some(node);
Expand Down
16 changes: 8 additions & 8 deletions src/tree/binary/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,27 +134,27 @@ impl<'a, K: 'a, V: 'a> NodeQuery<K, V> {
}

pub fn set_left(&mut self, node: Option<NonNull<Node<K, V>>>) {
unsafe {

if let Some(mut p) = self.node {
p.as_mut().left = node;
unsafe { p.as_mut().left = node };
}

if let Some(mut p) = node {
p.as_mut().parent = self.node;
unsafe { p.as_mut().parent = self.node };
}
}

}

pub fn set_right(&mut self, node: Option<NonNull<Node<K, V>>>) {
unsafe {

if let Some(mut p) = self.node {
p.as_mut().right = node;
unsafe { p.as_mut().right = node };
}

if let Some(mut p) = node {
p.as_mut().parent = self.node;
unsafe { p.as_mut().parent = self.node };
}
}

}

pub fn set_children(&mut self, l: Option<NonNull<Node<K, V>>>, r: Option<NonNull<Node<K, V>>>) {
Expand Down
6 changes: 3 additions & 3 deletions src/tree/binary/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ impl<K, V> Drop for Tree<K, V> {
}

fn height<K, V>(node: Option<NonNull<Node<K, V>>>) -> usize {
node.map_or(0, |node| unsafe {
let lh = height(node.as_ref().left);
let rh = height(node.as_ref().right);
node.map_or(0, |node| {
let lh = height(unsafe { node.as_ref().left });
let rh = height(unsafe { node.as_ref().right });
1 + std::cmp::max(lh, rh)
})
}