Skip to content

Commit

Permalink
Small tweaks and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
operutka committed Jul 11, 2023
1 parent 80d7d31 commit bfbb8cb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/net/raw/utils.rs
Expand Up @@ -42,9 +42,9 @@ pub fn sum_type<T: Sized>(data: &T) -> u32 {
/// Sum a given slice of Sized type instances as 16-bit unsigned big endian
/// numbers.
pub fn sum_slice<T: Sized>(data: &[T]) -> u32 {
let size = mem::size_of::<T>();
let size = mem::size_of_val(data);
let ptr = data.as_ptr();
unsafe { sum_raw_be(ptr as *const u8, size * data.len()) }
unsafe { sum_raw_be(ptr as *const u8, size) }
}

/// Sum given raw data as 16-bit unsigned big endian numbers.
Expand Down
16 changes: 5 additions & 11 deletions src/net/utils.rs
Expand Up @@ -14,8 +14,6 @@

//! Common networking utils.

use std::mem;

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs};

use crate::utils::RuntimeError;
Expand Down Expand Up @@ -45,19 +43,15 @@ impl Ipv4AddrEx for Ipv4Addr {
fn from_slice(bytes: &[u8]) -> Ipv4Addr {
assert_eq!(bytes.len(), 4);

#[allow(clippy::cast_ptr_alignment)]
let ptr = bytes.as_ptr() as *const u32;
let addr = unsafe { u32::from_be(*ptr) };
let mut tmp = [0u8; 4];

tmp.copy_from_slice(bytes);

Self::from(addr)
Self::from(u32::from_be_bytes(tmp))
}

fn as_u32(&self) -> u32 {
let octets = self.octets();

let nr: u32 = unsafe { mem::transmute(octets) };

nr.to_be()
u32::from_be_bytes(self.octets())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/scanner/discovery.rs
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///! Network scanner for RTSP streams.
//! Network scanner for RTSP streams.
use std::fmt;
use std::io;
use std::result;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/mod.rs
Expand Up @@ -87,8 +87,8 @@ pub fn as_bytes<T: Sized>(val: &T) -> &[u8] {
/// Convert a given slice of Sized type instances to a slice of bytes.
pub fn slice_as_bytes<T: Sized>(data: &[T]) -> &[u8] {
let ptr = data.as_ptr();
let size = mem::size_of::<T>();
unsafe { slice::from_raw_parts(ptr as *const u8, size * data.len()) }
let size = mem::size_of_val(data);
unsafe { slice::from_raw_parts(ptr as *const u8, size) }
}

/// Convert a given typed pointer into a new vector (copying the data).
Expand Down

0 comments on commit bfbb8cb

Please sign in to comment.