fitzgen / bumpalo

A fast bump allocation arena for Rust
https://docs.rs/bumpalo
Apache License 2.0
1.36k stars 111 forks source link

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

Closed peamaeq closed 3 months ago

peamaeq commented 2 years ago

In this function you use the unsafe keyword for almost the entrie function body. However, I found that only 6 functions and 1 operation are real unsafe operations (see the list below).

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. Real unsafe operation list:

  1. the layout_from_size_align()/alloc()/new_unchecked()/write()/as_ref()/sub() function(these are unsafe functions)
  2. dereferencing raw pointers(&mut *p)

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

fitzgen commented 3 months ago

Closing old pull requests.