diff --git a/Cargo.toml b/Cargo.toml index 39e7969..9748597 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,13 +12,13 @@ description = "A suite of powerful, extensible, generic, endian-aware Read/Write include = ["src/**/*", "Cargo.toml", "LICENSE", "README.md"] rust-version = "1.63" -[dependencies] -scroll_derive = { version = "0.11", optional = true, path = "scroll_derive" } - [features] default = ["std"] std = [] -derive = ["scroll_derive"] +derive = ["dep:scroll_derive"] + +[dependencies] +scroll_derive = { version = "0.11", optional = true, path = "scroll_derive" } [dev-dependencies] rayon = "1" diff --git a/examples/data_ctx.rs b/examples/data_ctx.rs index 667f4b1..d9ee212 100644 --- a/examples/data_ctx.rs +++ b/examples/data_ctx.rs @@ -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)) } } diff --git a/src/ctx.rs b/src/ctx.rs index 2f6c7ed..9a8f471 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -180,12 +180,9 @@ //! } //! ``` -use core::mem::size_of; -use core::mem::transmute; +use core::mem::{size_of, transmute}; use core::ptr::copy_nonoverlapping; -use core::result; -use core::str; - +use core::{result, str}; #[cfg(feature = "std")] use std::ffi::{CStr, CString}; @@ -240,18 +237,14 @@ impl Default for StrCtx { impl StrCtx { pub fn len(&self) -> usize { - match *self { + match self { StrCtx::Delimiter(_) | StrCtx::DelimiterUntil(_, _) => 1, StrCtx::Length(_) => 0, } } pub fn is_empty(&self) -> bool { - if let StrCtx::Length(_) = *self { - true - } else { - false - } + matches!(self, StrCtx::Length(_)) } } @@ -867,11 +860,11 @@ impl TryIntoCtx for CString { // } #[cfg(test)] +#[cfg(feature = "std")] mod tests { use super::*; #[test] - #[cfg(feature = "std")] fn parse_a_cstr() { let src = CString::new("Hello World").unwrap(); let as_bytes = src.as_bytes_with_nul(); @@ -883,7 +876,6 @@ mod tests { } #[test] - #[cfg(feature = "std")] fn round_trip_a_c_str() { let src = CString::new("Hello World").unwrap(); let src = src.as_c_str(); diff --git a/src/endian.rs b/src/endian.rs index 06d7a1d..7b83c34 100644 --- a/src/endian.rs +++ b/src/endian.rs @@ -43,9 +43,6 @@ impl Endian { } #[inline] pub fn is_little(&self) -> bool { - match *self { - LE => true, - _ => false, - } + *self == LE } } diff --git a/src/error.rs b/src/error.rs index cf413be..7ed7d03 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,10 +1,7 @@ use core::fmt::{self, Display}; use core::result; - -#[cfg(feature = "std")] -use std::error; #[cfg(feature = "std")] -use std::io; +use std::{error, io}; #[derive(Debug)] /// A custom Scroll error @@ -20,7 +17,8 @@ pub enum Error { size: usize, msg: &'static str, }, - /// A custom Scroll error for reporting messages to clients + /// A custom Scroll error for reporting messages to clients. + /// For no-std, use [`Error::BadInput`] with a static string. #[cfg(feature = "std")] Custom(String), /// Returned when IO based errors are encountered @@ -31,7 +29,7 @@ pub enum Error { #[cfg(feature = "std")] impl error::Error for Error { fn description(&self) -> &str { - match *self { + match self { Error::TooBig { .. } => "TooBig", Error::BadOffset(_) => "BadOffset", Error::BadInput { .. } => "BadInput", @@ -40,7 +38,7 @@ impl error::Error for Error { } } fn cause(&self) -> Option<&dyn error::Error> { - match *self { + match self { Error::TooBig { .. } => None, Error::BadOffset(_) => None, Error::BadInput { .. } => None, @@ -59,7 +57,7 @@ impl From for Error { impl Display for Error { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match *self { + match self { Error::TooBig { ref size, ref len } => { write!(fmt, "type is too big ({}) for {}", size, len) } diff --git a/src/leb128.rs b/src/leb128.rs index 52050d6..e6490c6 100644 --- a/src/leb128.rs +++ b/src/leb128.rs @@ -1,9 +1,8 @@ -use crate::ctx::TryFromCtx; -use crate::error; -use crate::Pread; use core::convert::{AsRef, From}; -use core::result; -use core::u8; +use core::{result, u8}; + +use crate::ctx::TryFromCtx; +use crate::{error, Pread}; #[derive(Debug, PartialEq, Copy, Clone)] /// An unsigned leb128 integer diff --git a/src/lesser.rs b/src/lesser.rs index 46ef4c5..636bf25 100644 --- a/src/lesser.rs +++ b/src/lesser.rs @@ -1,6 +1,7 @@ -use crate::ctx::{FromCtx, IntoCtx, SizeWith}; use std::io::{Read, Result, Write}; +use crate::ctx::{FromCtx, IntoCtx, SizeWith}; + /// An extension trait to `std::io::Read` streams; mainly targeted at reading primitive types with /// a known size. /// @@ -104,8 +105,8 @@ pub trait IOread: Read { fn ioread_with + SizeWith>(&mut self, ctx: Ctx) -> Result { 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)) } } diff --git a/src/lib.rs b/src/lib.rs index 2b29d23..883ef2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -253,8 +253,7 @@ pub use crate::pwrite::*; #[doc(hidden)] pub mod export { - pub use ::core::mem; - pub use ::core::result; + pub use ::core::{mem, result}; } #[allow(unused)] @@ -271,7 +270,9 @@ doc_comment!(include_str!("../README.md")); #[cfg(test)] mod tests { - #[allow(overflowing_literals)] + // FIXME: is this needed? It used to be incorrectly declared. + #![allow(overflowing_literals)] + use super::LE; #[test] @@ -462,7 +463,7 @@ mod tests { fn try_into_ctx(self, this: &mut [u8], le: super::Endian) -> Result { use super::Pwrite; if this.len() < 2 { - return Err((ExternalError {}).into()); + return Err(ExternalError {}); } this.pwrite_with(self.0, 0, le)?; Ok(2) @@ -474,7 +475,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)) diff --git a/tests/api.rs b/tests/api.rs index 8014287..68a90e9 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -1,14 +1,7 @@ -// this exists primarily to test various API usages of scroll; e.g., must compile - -// guard against potential undefined behaviour when borrowing from -// packed structs. See https://github.com/rust-lang/rust/issues/46043 -#![deny(unaligned_references)] - -// #[macro_use] extern crate scroll_derive; +use std::ops::{Deref, DerefMut}; -use scroll::ctx::SizeWith; +use scroll::ctx::SizeWith as _; use scroll::{ctx, Cread, Pread, Result}; -use std::ops::{Deref, DerefMut}; #[derive(Default)] pub struct Section<'a> { @@ -87,7 +80,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>> { let nsects = self.nsects as usize; @@ -170,6 +163,7 @@ fn lifetime_passthrough() { assert!(true) } +#[cfg(feature = "std")] #[derive(Default)] #[repr(packed)] struct Foo { @@ -177,6 +171,7 @@ struct Foo { bar: u32, } +#[cfg(feature = "std")] impl scroll::ctx::FromCtx for Foo { fn from_ctx(bytes: &[u8], ctx: scroll::Endian) -> Self { Foo { @@ -186,6 +181,7 @@ impl scroll::ctx::FromCtx for Foo { } } +#[cfg(feature = "std")] impl scroll::ctx::SizeWith for Foo { fn size_with(_: &scroll::Endian) -> usize { ::std::mem::size_of::() @@ -197,6 +193,7 @@ impl scroll::ctx::SizeWith for Foo { fn ioread_api() { use scroll::{IOread, LE}; use std::io::Cursor; + let bytes_ = [ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0x00, 0x00, ]; @@ -261,8 +258,8 @@ fn cread_api_badindex() { #[test] fn cwrite_api() { - use scroll::Cread; - use scroll::Cwrite; + use scroll::{Cread, Cwrite}; + let mut bytes = [0x0; 16]; bytes.cwrite::(42, 0); bytes.cwrite::(0xdeadbeef, 8);