Skip to content

Commit

Permalink
error, tests: inline format args (better perf) (#91)
Browse files Browse the repository at this point in the history
Make code a bit easier to read, and also, per rust issue 112156, there is a 6% perf hit when using refs with format, so this PR also fixes that.
  • Loading branch information
nyurik authored Nov 13, 2023
1 parent f5beda1 commit 30f49c7
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion examples/data_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ fn main() {
let data = bytes.pread_with::<Data>(0, BE).unwrap();
assert_eq!(data.id, 0x01020304);
assert_eq!(data.name.to_string(), "UserName".to_string());
println!("Data: {:?}", &data);
println!("Data: {data:?}");
}
2 changes: 1 addition & 1 deletion scroll_derive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use scroll::{Pread, Pwrite, Cread, LE};
fn main (){
let bytes = [0xefu8, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0, 0, 224, 63, 0xad, 0xde, 0xef, 0xbe];
let data: Data = bytes.pread_with(0, LE).unwrap();
println!("data: {:?}", &data);
println!("data: {data:?}");
assert_eq!(data.id, 0xdeadbeefu32);
let mut bytes2 = vec![0; ::std::mem::size_of::<Data>()];
bytes2.pwrite_with(data, 0, LE).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion scroll_derive/examples/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
0xefu8, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0, 0, 224, 63, 0xad, 0xde, 0xef, 0xbe,
];
let data: Data = bytes.pread_with(0, LE).unwrap();
println!("data: {:?}", &data);
println!("data: {data:?}");
assert_eq!(data.id, 0xdeadbeefu32);
let mut bytes2 = vec![0; ::std::mem::size_of::<Data>()];
bytes2.pwrite_with(data, 0, LE).unwrap();
Expand Down
26 changes: 13 additions & 13 deletions scroll_derive/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Data {
fn test_data() {
let bytes = [0xefu8, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0, 0, 224, 63];
let data: Data = bytes.pread_with(0, LE).unwrap();
println!("data: {:?}", &data);
println!("data: {data:?}");
assert_eq!(data.id, 0xdeadbeefu32);
assert_eq!(data.timestamp, 0.5f64);
let mut bytes2 = vec![0; ::std::mem::size_of::<Data>()];
Expand All @@ -49,7 +49,7 @@ struct Data2 {
fn test_array() {
let bytes = [0u8; 64];
let data: Data2 = bytes.pread_with(0, LE).unwrap();
println!("data: {:?}", &data);
println!("data: {data:?}");
}

#[derive(Debug, PartialEq, Pread, Pwrite, SizeWith)]
Expand All @@ -61,7 +61,7 @@ struct Data3 {
fn test_sizewith() {
let bytes = [0u8; 64];
let data: Data3 = bytes.gread_with(&mut 0, LE).unwrap();
println!("data: {:?}", &data);
println!("data: {data:?}");
}

#[derive(Debug, PartialEq, IOread, IOwrite, SizeWith)]
Expand All @@ -75,7 +75,7 @@ struct Data4 {
fn test_ioread() {
let bytes = [0, 1, 2, 3, 0xde, 0xed, 0xbe, 0xaf];
let data: Data4 = bytes.cread_with(0, LE);
println!("data: {:?}", &data);
println!("data: {data:?}");
assert_eq!(data.name, 50462976);
assert_eq!(data.j, 0xedde);
assert_eq!(data.arr, [0xbe, 0xaf]);
Expand All @@ -85,21 +85,21 @@ fn test_ioread() {
fn test_iowrite() {
let bytes = [0, 1, 2, 3, 0xde, 0xed, 0xbe, 0xaf];
let data: Data4 = bytes.cread_with(0, LE);
println!("data: {:?}", &data);
println!("data: {data:?}");
assert_eq!(data.name, 50462976);
assert_eq!(data.j, 0xedde);
assert_eq!(data.arr, [0xbe, 0xaf]);

let mut bytes_null = [0u8; 8];
bytes_null.cwrite_with(&data, 0, LE);
println!("bytes_null: {:?}", &bytes_null);
println!("bytes : {:?}", &bytes);
println!("bytes_null: {bytes_null:?}");
println!("bytes : {bytes:?}");
assert_eq!(bytes_null, bytes);

let mut bytes_null = [0u8; 8];
bytes_null.cwrite_with(data, 0, LE);
println!("bytes_null: {:?}", &bytes_null);
println!("bytes : {:?}", &bytes);
println!("bytes_null: {bytes_null:?}");
println!("bytes : {bytes:?}");
assert_eq!(bytes_null, bytes);
}

Expand All @@ -116,13 +116,13 @@ struct Data5 {
fn test_pread_arrays() {
let bytes = [0, 1, 2, 3, 0, 0, 0xde, 0xed, 0xad, 0xde, 0xef, 0xbe];
let data: Data5 = bytes.pread_with(0, LE).unwrap();
println!("data: {:?}", &data);
println!("data: {data:?}");
assert_eq!(data.name, 50462976);
assert_eq!(data.arr1, [0xde, 0xed]);
assert_eq!(data.arr2, [0xdead, 0xbeef]);
let offset = &mut 0;
let data: Data5 = bytes.gread_with(offset, LE).unwrap();
println!("data: {:?}", &data);
println!("data: {data:?}");
assert_eq!(data.name, 50462976);
assert_eq!(data.arr1, [0xde, 0xed]);
assert_eq!(data.arr2, [0xdead, 0xbeef]);
Expand All @@ -141,8 +141,8 @@ fn test_array_copy() {
let bytes = [0xde, 0xed, 0xef, 0xbe, 0x68, 0x65, 0x6c, 0x6c, 0x0];
let data: Data6 = bytes.pread_with(0, LE).unwrap();
let name: &str = data.name.pread(0).unwrap();
println!("data: {:?}", &data);
println!("data.name: {:?}", name);
println!("data: {data:?}");
println!("data.name: {name:?}");
assert_eq!(data.id, 0xbeefedde);
assert_eq!(name, "hell");
}
Expand Down
2 changes: 1 addition & 1 deletion src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ impl<'a> TryIntoCtx for &'a [u8] {
let src_len = self.len() as isize;
let dst_len = dst.len() as isize;
// if src_len < 0 || dst_len < 0 || offset < 0 {
// return Err(error::Error::BadOffset(format!("requested operation has negative casts: src len: {} dst len: {} offset: {}", src_len, dst_len, offset)).into())
// return Err(error::Error::BadOffset(format!("requested operation has negative casts: src len: {src_len} dst len: {dst_len} offset: {offset}")).into())
// }
if src_len > dst_len {
Err(error::Error::TooBig {
Expand Down
10 changes: 5 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ impl Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::TooBig { ref size, ref len } => {
write!(fmt, "type is too big ({}) for {}", size, len)
write!(fmt, "type is too big ({size}) for {len}")
}
Error::BadOffset(ref offset) => {
write!(fmt, "bad offset {}", offset)
write!(fmt, "bad offset {offset}")
}
Error::BadInput { ref msg, ref size } => {
write!(fmt, "bad input {} ({})", msg, size)
write!(fmt, "bad input {msg} ({size})")
}
#[cfg(feature = "std")]
Error::Custom(ref msg) => {
write!(fmt, "{}", msg)
write!(fmt, "{msg}")
}
#[cfg(feature = "std")]
Error::IO(ref err) => {
write!(fmt, "{}", err)
write!(fmt, "{err}")
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/leb128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,23 +184,23 @@ mod tests {
let bytes = &buf[..];
let num = bytes.pread::<Uleb128>(0).unwrap();
#[cfg(feature = "std")]
println!("num: {:?}", &num);
println!("num: {num:?}");
assert_eq!(130u64, num.into());
assert_eq!(num.size(), 2);

let buf = [0x00, 0x01];
let bytes = &buf[..];
let num = bytes.pread::<Uleb128>(0).unwrap();
#[cfg(feature = "std")]
println!("num: {:?}", &num);
println!("num: {num:?}");
assert_eq!(0u64, num.into());
assert_eq!(num.size(), 1);

let buf = [0x21];
let bytes = &buf[..];
let num = bytes.pread::<Uleb128>(0).unwrap();
#[cfg(feature = "std")]
println!("num: {:?}", &num);
println!("num: {num:?}");
assert_eq!(0x21u64, num.into());
assert_eq!(num.size(), 1);
}
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,22 +358,22 @@ mod tests {
let b = &bytes[..];
let s: &str = b.pread(0).unwrap();
#[cfg(feature = "std")]
println!("str: {}", s);
println!("str: {s}");
assert_eq!(s.len(), bytes[..].len() - 1);
let bytes: &[u8] = b"hello, world!\0some_other_things";
let hello_world: &str = bytes.pread_with(0, StrCtx::Delimiter(NULL)).unwrap();
#[cfg(feature = "std")]
println!("{:?}", &hello_world);
println!("{hello_world:?}");
assert_eq!(hello_world.len(), 13);
let hello: &str = bytes.pread_with(0, StrCtx::Delimiter(SPACE)).unwrap();
#[cfg(feature = "std")]
println!("{:?}", &hello);
println!("{hello:?}");
assert_eq!(hello.len(), 6);
// this could result in underflow so we just try it
let _error = bytes.pread_with::<&str>(6, StrCtx::Delimiter(SPACE));
let error = bytes.pread_with::<&str>(7, StrCtx::Delimiter(SPACE));
#[cfg(feature = "std")]
println!("{:?}", &error);
println!("{error:?}");
assert!(error.is_ok());
}

Expand All @@ -384,16 +384,16 @@ mod tests {
let bytes: &[u8] = b"";
let hello_world = bytes.pread_with::<&str>(0, StrCtx::Delimiter(NULL));
#[cfg(feature = "std")]
println!("1 {:?}", &hello_world);
assert_eq!(hello_world.is_err(), true);
println!("1 {hello_world:?}");
assert!(hello_world.is_err());
let error = bytes.pread_with::<&str>(7, StrCtx::Delimiter(SPACE));
#[cfg(feature = "std")]
println!("2 {:?}", &error);
println!("2 {error:?}");
assert!(error.is_err());
let bytes: &[u8] = b"\0";
let null = bytes.pread::<&str>(0).unwrap();
#[cfg(feature = "std")]
println!("3 {:?}", &null);
println!("3 {null:?}");
assert_eq!(null.len(), 0);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ fn lifetime_passthrough_<'a>(segments: &Segments<'a>, section_name: &str) -> Opt
let segment_name = "__TEXT";
for segment in &segments.segments {
if let Ok(name) = segment.name() {
println!("segment.name: {}", name);
println!("segment.name: {name}");
if name == segment_name {
if let Ok(sections) = segment.sections() {
for section in sections {
let sname = section.name().unwrap();
println!("section.name: {}", sname);
println!("section.name: {sname}");
if section_name == sname {
return Some(section.data);
}
Expand Down

0 comments on commit 30f49c7

Please sign in to comment.