Skip to content

Commit

Permalink
Resize refactor (#696)
Browse files Browse the repository at this point in the history
* use checked_sub

* return when additional == 0

* move safe operation out of unsafe block

* use spare_capacity_mut instead of chunk_mut

We don't need to check capacity because it's already been reserved
above.

* Add safety comments

* refactor to use guard clauses

This would be better written with let-else, but we won't get that until
`MSRV >= 1.65.x`.

* use if-let instead of unwrap

* reduce scope of unsafe blocks

Co-authored-by: Alice Ryhl <[email protected]>

---------

Co-authored-by: Alice Ryhl <[email protected]>
  • Loading branch information
braddunbar and Darksonn authored Apr 24, 2024
1 parent 4e2c9c0 commit 9d3ec1c
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/bytes_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,18 +468,26 @@ impl BytesMut {
/// assert_eq!(&buf[..], &[0x1, 0x1, 0x3, 0x3]);
/// ```
pub fn resize(&mut self, new_len: usize, value: u8) {
let len = self.len();
if new_len > len {
let additional = new_len - len;
self.reserve(additional);
unsafe {
let dst = self.chunk_mut().as_mut_ptr();
ptr::write_bytes(dst, value, additional);
self.set_len(new_len);
}
let additional = if let Some(additional) = new_len.checked_sub(self.len()) {
additional
} else {
self.truncate(new_len);
return;
};

if additional == 0 {
return;
}

self.reserve(additional);
let dst = self.spare_capacity_mut().as_mut_ptr();
// 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.
unsafe { self.set_len(new_len) };
}

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

0 comments on commit 9d3ec1c

Please sign in to comment.