Skip to content

Commit

Permalink
Journal: fix an incomplete sentence in 'Using Drop for Safety in Rust'
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskrycho committed Dec 12, 2024
1 parent 5f485fc commit 7865c68
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ The next block of comments and the first line in the unsafe block get into somet

Here, the point is to make sure we have a valid pointer provenance to use when dropping any values that are unused at the end of the original range specified when calling `drain`: Miri will (rightly!) complain otherwise.

Once we have pointers with valid provenances, we get a “raw slice” with [the `std::ptr::slice_from_raw_parts_mut` function][sfrpm]. A “raw slice” is *basically* just a chunk of memory interpreted as a contiguous sequence of items of a given type. It is called “raw” because it is unsafe: it is constructed directly from a pointer and size; it is on the caller to make sure it is valid.[^basically-c] We use the `_mut` version of the function here because the next, and next-to-last, thing we do is call [the `std::ptr::drop_in_place` function][dip], which runs the `Drop` implementation for whatever it is called on *without* moving them. Called on a slice, that will in turn recursively call the `Drop` on each item in.
Once we have pointers with valid provenances, we get a “raw slice” with [the `std::ptr::slice_from_raw_parts_mut` function][sfrpm]. A “raw slice” is *basically* just a chunk of memory interpreted as a contiguous sequence of items of a given type. It is called “raw” because it is unsafe: it is constructed directly from a pointer and size; it is on the caller to make sure it is valid.[^basically-c] We use the `_mut` version of the function here because the next, and next-to-last, thing we do is call [the `std::ptr::drop_in_place` function][dip], which runs the `Drop` implementation for whatever it is called on *without* moving them. Called on a slice, that will in turn recursively call the `Drop` on each item in the slice.

[sfrpm]: https://doc.rust-lang.org/1.83.0/std/ptr/fn.slice_from_raw_parts_mut.html
[dip]: https://doc.rust-lang.org/1.83.0/std/primitive.pointer.html#method.drop_in_place
Expand Down

0 comments on commit 7865c68

Please sign in to comment.