Skip to content

Commit

Permalink
reduce scope of unsafe blocks
Browse files Browse the repository at this point in the history
Co-authored-by: Alice Ryhl <aliceryhl@google.com>
  • Loading branch information
braddunbar and Darksonn committed Apr 22, 2024
1 parent 63bca89 commit 434166a
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/bytes_mut.rs
Expand Up @@ -481,15 +481,13 @@ impl BytesMut {

self.reserve(additional);
let dst = self.spare_capacity_mut().as_mut_ptr();
unsafe {
// SAFETY: `spare_capacity_mut` returns a valid, properly aligned pointer and we've
// reserved enough space to write `additional` bytes.
ptr::write_bytes(dst, value, additional);
// SAFETY: `spare_capacity_mut` returns a valid, properly aligned pointer and we've
// reserved enough space to write `additional` bytes.
unsafe { ptr::write_bytes(dst, value, additional) };

// SAFETY: There are at least `new_len` initialized bytes in the buffer so no
// uninitialized bytes are being exposed.
self.set_len(new_len);
}
// SAFETY: There are at least `new_len` initialized bytes in the buffer so no
// uninitialized bytes are being exposed.
unsafe { self.set_len(new_len) };
}

/// Sets the length of the buffer.
Expand Down

0 comments on commit 434166a

Please sign in to comment.