Skip to content

Commit

Permalink
Fix clippy warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frostie314159 committed Nov 5, 2023
1 parent 8f60b4b commit 6f4b34c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 44 deletions.
2 changes: 1 addition & 1 deletion examples/data_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl<'a> ctx::TryFromCtx<'a, Endian> for Data<'a> {
fn try_from_ctx(src: &'a [u8], endian: Endian) -> Result<(Self, usize), Self::Error> {
let name = src.pread::<&'a str>(0)?;
let id = src.pread_with(name.len() + 1, endian)?;
Ok((Data { name: name, id: id }, name.len() + 4))
Ok((Data { name, id }, name.len() + 4))
}
}

Expand Down
22 changes: 6 additions & 16 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@
//! ```

use core::mem::size_of;
use core::mem::transmute;
use core::ptr::copy_nonoverlapping;
use core::result;
use core::str;
Expand Down Expand Up @@ -249,11 +248,7 @@ impl StrCtx {
}

pub fn is_empty(&self) -> bool {
if let StrCtx::Length(_) = *self {
true
} else {
false
}
matches!(*self, StrCtx::Length(_))
}
}

Expand Down Expand Up @@ -407,11 +402,12 @@ macro_rules! write_into {
($typ:ty, $size:expr, $n:expr, $dst:expr, $endian:expr) => {{
unsafe {
assert!($dst.len() >= $size);
let bytes = transmute::<$typ, [u8; $size]>(if $endian.is_little() {
let bytes = if $endian.is_little() {
$n.to_le()
} else {
$n.to_be()
});
}
.to_ne_bytes();
copy_nonoverlapping((&bytes).as_ptr(), $dst.as_mut_ptr(), $size);
}
}};
Expand Down Expand Up @@ -572,7 +568,7 @@ macro_rules! from_ctx_float_impl {
&mut data as *mut signed_to_unsigned!($typ) as *mut u8,
$size,
);
transmute(if le.is_little() {
$typ::from_bits(if le.is_little() {
data.to_le()
} else {
data.to_be()
Expand Down Expand Up @@ -623,13 +619,7 @@ macro_rules! into_ctx_float_impl {
#[inline]
fn into_ctx(self, dst: &mut [u8], le: Endian) {
assert!(dst.len() >= $size);
write_into!(
signed_to_unsigned!($typ),
$size,
transmute::<$typ, signed_to_unsigned!($typ)>(self),
dst,
le
);
write_into!(signed_to_unsigned!($typ), $size, self.to_bits(), dst, le);
}
}
impl<'a> IntoCtx<Endian> for &'a $typ {
Expand Down
5 changes: 1 addition & 4 deletions src/endian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ impl Endian {
}
#[inline]
pub fn is_little(&self) -> bool {
match *self {
LE => true,
_ => false,
}
matches!(*self, LE)
}
}
4 changes: 2 additions & 2 deletions src/lesser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ pub trait IOread<Ctx: Copy>: Read {
fn ioread_with<N: FromCtx<Ctx> + SizeWith<Ctx>>(&mut self, ctx: Ctx) -> Result<N> {
let mut scratch = [0u8; 256];
let size = N::size_with(&ctx);
let mut buf = &mut scratch[0..size];
self.read_exact(&mut buf)?;
let buf = &mut scratch[0..size];
self.read_exact(buf)?;
Ok(N::from_ctx(buf, ctx))
}
}
Expand Down
34 changes: 15 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ doc_comment!(include_str!("../README.md"));

#[cfg(test)]
mod tests {
#[allow(overflowing_literals)]
use super::LE;

#[test]
Expand Down Expand Up @@ -378,14 +377,14 @@ mod tests {
let bytes: &[u8] = b"";
let hello_world = bytes.pread_with::<&str>(0, StrCtx::Delimiter(NULL));
println!("1 {:?}", &hello_world);
assert_eq!(hello_world.is_err(), true);
assert!(hello_world.is_err());
let error = bytes.pread_with::<&str>(7, StrCtx::Delimiter(SPACE));
println!("2 {:?}", &error);
assert!(error.is_err());
let bytes: &[u8] = b"\0";
let null = bytes.pread::<&str>(0).unwrap();
println!("3 {:?}", &null);
assert_eq!(null.len(), 0);
assert!(null.is_empty());
}

#[test]
Expand Down Expand Up @@ -435,11 +434,8 @@ mod tests {
}

impl From<super::Error> for ExternalError {
fn from(err: super::Error) -> Self {
//use super::Error::*;
match err {
_ => ExternalError {},
}
fn from(_err: super::Error) -> Self {
ExternalError {}
}
}

Expand All @@ -451,7 +447,7 @@ mod tests {
fn try_into_ctx(self, this: &mut [u8], le: super::Endian) -> Result<usize, Self::Error> {
use super::Pwrite;
if this.len() < 2 {
return Err((ExternalError {}).into());
return Err(ExternalError {});
}
this.pwrite_with(self.0, 0, le)?;
Ok(2)
Expand All @@ -463,7 +459,7 @@ mod tests {
fn try_from_ctx(this: &'a [u8], le: super::Endian) -> Result<(Self, usize), Self::Error> {
use super::Pread;
if this.len() > 2 {
return Err((ExternalError {}).into());
return Err(ExternalError {});
}
let n = this.pread_with(0, le)?;
Ok((Foo(n), 2))
Expand All @@ -477,9 +473,9 @@ mod tests {
let bytes_from: [u8; 8] = [1, 2, 3, 4, 5, 6, 7, 8];
let bytes_to = &mut bytes_to[..];
let bytes_from = &bytes_from[..];
for i in 0..bytes_from.len() {
bytes_to[i] = bytes_from.pread(i).unwrap();
}
bytes_to.iter_mut().enumerate().for_each(|(i, item)| {
*item = bytes_from.pread(i).unwrap();
});
assert_eq!(bytes_to, bytes_from);
}

Expand Down Expand Up @@ -571,10 +567,10 @@ mod tests {
let bytes_from: [u8; 8] = [1, 2, 3, 4, 5, 6, 7, 8];
let bytes_to = &mut bytes_to[..];
let bytes_from = &bytes_from[..];
let mut offset = &mut 0;
for i in 0..bytes_from.len() {
bytes_to[i] = bytes_from.gread(&mut offset).unwrap();
}
let offset = &mut 0;
bytes_to.iter_mut().for_each(|item| {
*item = bytes_from.gread(offset).unwrap();
});
assert_eq!(bytes_to, bytes_from);
assert_eq!(*offset, bytes_to.len());
}
Expand Down Expand Up @@ -612,11 +608,11 @@ mod tests {
let res = b.gread_with::<&str>(offset, StrCtx::Length(3));
assert!(res.is_err());
*offset = 0;
let astring: [u8; 3] = [0x45, 042, 0x44];
let astring: [u8; 3] = [0x45, 0x2a, 0x44];
let string = astring.gread_with::<&str>(offset, StrCtx::Length(2));
match &string {
&Ok(_) => {}
&Err(ref err) => {
Err(err) => {
println!("{}", &err);
panic!();
}
Expand Down
9 changes: 7 additions & 2 deletions tests/api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::disallowed_names)]
// this exists primarily to test various API usages of scroll; e.g., must compile

// #[macro_use] extern crate scroll_derive;
Expand Down Expand Up @@ -83,7 +84,7 @@ pub struct Segment<'a> {

impl<'a> Segment<'a> {
pub fn name(&self) -> Result<&str> {
Ok(self.segname.pread::<&str>(0)?)
self.segname.pread::<&str>(0)
}
pub fn sections(&self) -> Result<Vec<Section<'a>>> {
let nsects = self.nsects as usize;
Expand Down Expand Up @@ -137,6 +138,11 @@ impl<'a> Segments<'a> {
Ok(sections)
}
}
impl<'a> Default for Segments<'a> {
fn default() -> Self {
Self::new()
}
}

fn lifetime_passthrough_<'a>(segments: &Segments<'a>, section_name: &str) -> Option<&'a [u8]> {
let segment_name = "__TEXT";
Expand All @@ -163,7 +169,6 @@ fn lifetime_passthrough_<'a>(segments: &Segments<'a>, section_name: &str) -> Opt
fn lifetime_passthrough() {
let segments = Segments::new();
let _res = lifetime_passthrough_(&segments, "__text");
assert!(true)
}

#[derive(Default)]
Expand Down

0 comments on commit 6f4b34c

Please sign in to comment.