aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorJoey Sacchini <joey@sacchini.net>2021-01-04 21:19:52 -0500
committerJoey Sacchini <joey@sacchini.net>2021-01-04 21:19:52 -0500
commit6ec44bd5b6a354c55f3f5175f55d26534ee5b886 (patch)
tree94a864e5aa6a3baaa72de09340cde3597965571d /src/util.rs
parentdccbf0f736916f4ff4c36a2ea321397dee518a63 (diff)
downloadcraftio-rs-6ec44bd5b6a354c55f3f5175f55d26534ee5b886.tar.gz
craftio-rs-6ec44bd5b6a354c55f3f5175f55d26534ee5b886.tar.bz2
craftio-rs-6ec44bd5b6a354c55f3f5175f55d26534ee5b886.zip
remove some unsafe code and add max packet size (also add some debug assertions)
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs28
1 files changed, 4 insertions, 24 deletions
diff --git a/src/util.rs b/src/util.rs
index e33fc10..3f4bd9f 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -17,18 +17,8 @@ pub(crate) fn get_sized_buf(buf: &mut Option<Vec<u8>>, offset: usize, size: usiz
}
fn ensure_buf_has_size(buf: &mut Vec<u8>, total_size: usize) {
- let cur_len = buf.len();
- if cur_len >= total_size {
- return;
- }
-
- let additional = total_size - cur_len;
- buf.reserve(additional);
- unsafe {
- let start_at = buf.as_mut_ptr();
- let start_write_at = start_at.offset(cur_len as isize);
- std::ptr::write_bytes(start_write_at, 0, additional);
- buf.set_len(total_size);
+ if total_size > buf.len() {
+ buf.resize(total_size, 0u8);
}
}
@@ -42,15 +32,5 @@ pub(crate) fn move_data_rightwards(target: &mut [u8], size: usize, shift_amount:
)
}
- unsafe { move_data_rightwards_unchecked(target, size, shift_amount) }
-}
-
-unsafe fn move_data_rightwards_unchecked(target: &mut [u8], size: usize, shift_amount: usize) {
- if shift_amount == 0 {
- return;
- }
-
- let src_ptr = target.as_mut_ptr();
- let dst_ptr = src_ptr.offset(shift_amount as isize);
- std::ptr::copy(src_ptr, dst_ptr, size);
-}
+ target.copy_within(0..size, shift_amount);
+} \ No newline at end of file