Skip to content

Commit

Permalink
refactor to use guard clauses
Browse files Browse the repository at this point in the history
This would be better written with let-else, but we won't get that until
`MSRV >= 1.65.x`.
  • Loading branch information
braddunbar committed Apr 21, 2024
1 parent 0096b25 commit a1d0cc6
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/bytes_mut.rs
Expand Up @@ -468,24 +468,28 @@ impl BytesMut {
/// assert_eq!(&buf[..], &[0x1, 0x1, 0x3, 0x3]);
/// ```
pub fn resize(&mut self, new_len: usize, value: u8) {
if let Some(additional) = new_len.checked_sub(self.len()) {
if additional == 0 {
return;
}

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);
let additional = new_len.checked_sub(self.len());

// SAFETY: There are at least `new_len` initialized bytes in the buffer so no
// uninitialized bytes are being exposed.
self.set_len(new_len);
}
} else {
if additional.is_none() {
self.truncate(new_len);
return;
}

if additional == Some(0) {
return;
}

let additional = additional.unwrap();
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: There are at least `new_len` initialized bytes in the buffer so no
// uninitialized bytes are being exposed.
self.set_len(new_len);
}
}

Expand Down

0 comments on commit a1d0cc6

Please sign in to comment.