Skip to content

Commit

Permalink
Merge pull request #107 from MaxVerevkin/misc
Browse files Browse the repository at this point in the history
avoid raw pointre casts using "as"; perfer extend_from_slice
  • Loading branch information
KillingSpark authored Feb 24, 2024
2 parents bf03354 + ae6c4f4 commit 28127b3
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion rustbus/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn read_message(stream: &mut UnixStream, buf: &mut Vec<u8>) -> std::io::Result<S
let mut tmpbuf = [0u8; 512];
while !has_line_ending(buf) {
let bytes = stream.read(&mut tmpbuf[..])?;
buf.extend(&tmpbuf[..bytes])
buf.extend_from_slice(&tmpbuf[..bytes])
}
let idx = find_line_ending(buf).unwrap();
let line = buf.drain(0..idx).collect::<Vec<_>>();
Expand Down
2 changes: 1 addition & 1 deletion rustbus/src/connection/ll_conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl RecvConn {

self.cmsgs_in.extend(msg.cmsgs());
let bytes = msg.bytes;
self.msg_buf_in.extend(&mut tmpbuf[..bytes].iter().copied());
self.msg_buf_in.extend_from_slice(&tmpbuf[..bytes]);
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion rustbus/src/wire/marshal/traits/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl<E: Marshal> Marshal for &[E] {
assert!(len <= u32::MAX as usize);
write_u32(len as u32, ctx.byteorder, ctx.buf);
ctx.align_to(alignment);
let ptr = *self as *const [E] as *const u8;
let ptr = self.as_ptr().cast::<u8>();
let slice = std::slice::from_raw_parts(ptr, len);
ctx.buf.extend_from_slice(slice);
return Ok(());
Expand Down
2 changes: 1 addition & 1 deletion rustbus/src/wire/unmarshal/traits/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ where

// cast the slice from u8 to the target type
let elem_cnt = bytes_in_array / alignment;
let ptr = content_slice as *const [u8] as *const E;
let ptr = content_slice.as_ptr().cast::<E>();
let slice = std::slice::from_raw_parts(ptr, elem_cnt);

ctx.offset += bytes_in_array;
Expand Down
12 changes: 6 additions & 6 deletions rustbus/src/wire/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ pub fn pad_to_align(align_to: usize, buf: &mut Vec<u8>) {

pub fn write_u16(val: u16, byteorder: ByteOrder, buf: &mut Vec<u8>) {
match byteorder {
ByteOrder::LittleEndian => buf.extend(&val.to_le_bytes()[..]),
ByteOrder::BigEndian => buf.extend(&val.to_be_bytes()[..]),
ByteOrder::LittleEndian => buf.extend_from_slice(&val.to_le_bytes()),
ByteOrder::BigEndian => buf.extend_from_slice(&val.to_be_bytes()),
}
}
#[inline]
pub fn write_u32(val: u32, byteorder: ByteOrder, buf: &mut Vec<u8>) {
match byteorder {
ByteOrder::LittleEndian => buf.extend(&val.to_le_bytes()[..]),
ByteOrder::BigEndian => buf.extend(&val.to_be_bytes()[..]),
ByteOrder::LittleEndian => buf.extend_from_slice(&val.to_le_bytes()),
ByteOrder::BigEndian => buf.extend_from_slice(&val.to_be_bytes()),
}
}
pub fn write_u64(val: u64, byteorder: ByteOrder, buf: &mut Vec<u8>) {
match byteorder {
ByteOrder::LittleEndian => buf.extend(&val.to_le_bytes()[..]),
ByteOrder::BigEndian => buf.extend(&val.to_be_bytes()[..]),
ByteOrder::LittleEndian => buf.extend_from_slice(&val.to_le_bytes()),
ByteOrder::BigEndian => buf.extend_from_slice(&val.to_be_bytes()),
}
}

Expand Down

0 comments on commit 28127b3

Please sign in to comment.