sekineh / binary-heap-plus-rs

Enhancement over Rust's `std::collections::BinaryHeap`. Supports other than max heap.
MIT License
57 stars 18 forks source link

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

Closed peamaeq closed 2 years ago

peamaeq commented 2 years ago

In this function you use the unsafe keyword for almost the entrie function body.

We need to mark unsafe operations more precisely using unsafe keyword. Keeping unsafe blocks small can bring many benefits. For example, when mistakes happen, we can locate any errors related to memory safety within an unsafe block. This is the balance between Safe and Unsafe Rust. The separation is designed to make using Safe Rust as ergonomic as possible, but requires extra effort and care when writing Unsafe Rust.

Hope this PR can help you. Best regards. References https://doc.rust-lang.org/nomicon/safe-unsafe-meaning.html https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html

sekineh commented 2 years ago

Thanks, it looks good. But CI failed because the dependency atoi requires MaybeUninit which was introduced in rustc 1.36. So we need to bump the MSRV in the following files.

We also need a CHANGELOG entry.

clint-white commented 2 years ago

As the CI failures are unrelated to this PR (same errors on master), I openned a separate PR: #33.

Regarding the changes introduced in this PR, these are a subset of those found in rust-lang/rust#81706. The upstream PR also makes several of the private functions unsafe (sift_up, sift_down, sift_down_range, sift_down_to_bottom) and adds SAFETY comments before every use of unsafe. It would be worth incorporating those.

It may also be worth porting a more recent version from std to include other changes such as rust-lang/rust#81127 which improved the performance of BinaryHeap::into_sorted_vec().

clint-white commented 2 years ago

This would now conflict with #34, but very similar changes to those proposed here are among those proposed there.

sekineh commented 2 years ago

These big unsafe blocks are fixed in #34.