Skip to content

Commit

Permalink
tests: fix safe_packed_borrows warning
Browse files Browse the repository at this point in the history
Close m4b#24
  • Loading branch information
hdhoang committed May 2, 2018
1 parent 6873fa9 commit dc960ec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/greater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ use ctx::{FromCtx, IntoCtx};
///
/// let bytes = [0xff, 0xff, 0xff, 0xff, 0xef,0xbe,0xad,0xde,];
/// let bar = bytes.cread_with::<Bar>(0, LE);
/// assert_eq!(bar.foo, -1);
/// assert_eq!(bar.bar, 0xdeadbeef);
/// // Remember that you need to copy out fields from packed structs
/// // with a `{}` block instead of borrowing them directly
/// // ref: https://github.com/rust-lang/rust/issues/46043
/// assert_eq!({bar.foo}, -1);
/// assert_eq!({bar.bar}, 0xdeadbeef);
/// ```
pub trait Cread<Ctx, I = usize> : Index<I> + Index<RangeFrom<I>>
where
Expand Down
7 changes: 5 additions & 2 deletions src/lesser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ use ctx::{FromCtx, IntoCtx, SizeWith};
/// assert!(error.is_err());
/// let mut bytes = Cursor::new(bytes_);
/// let foo_ = bytes.ioread_with::<Foo>(LE).unwrap();
/// assert_eq!(foo_.foo, foo);
/// assert_eq!(foo_.bar, bar);
/// // Remember that you need to copy out fields from packed structs
/// // with a `{}` block instead of borrowing them directly
/// // ref: https://github.com/rust-lang/rust/issues/46043
/// assert_eq!({foo_.foo}, foo);
/// assert_eq!({foo_.bar}, bar);
/// ```
///
pub trait IOread<Ctx: Copy> : Read
Expand Down
16 changes: 10 additions & 6 deletions tests/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// 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(safe_packed_borrows)]

extern crate scroll;

// #[macro_use] extern crate scroll_derive;
Expand Down Expand Up @@ -202,8 +206,8 @@ fn ioread_api() {
assert!(error.is_err());
let mut bytes = Cursor::new(bytes_);
let foo_ = bytes.ioread_with::<Foo>(LE).unwrap();
assert_eq!(foo_.foo, foo);
assert_eq!(foo_.bar, bar);
assert_eq!({foo_.foo}, foo);
assert_eq!({foo_.bar}, bar);
}

#[repr(packed)]
Expand Down Expand Up @@ -234,8 +238,8 @@ fn cread_api_customtype() {
use scroll::{LE, Cread};
let bytes = [0xff, 0xff, 0xff, 0xff, 0xef,0xbe,0xad,0xde,];
let bar = &bytes[..].cread_with::<Bar>(0, LE);
assert_eq!(bar.foo, -1);
assert_eq!(bar.bar, 0xdeadbeef);
assert_eq!({bar.foo}, -1);
assert_eq!({bar.bar}, 0xdeadbeef);
}

#[test]
Expand Down Expand Up @@ -272,7 +276,7 @@ fn cwrite_api_customtype() {
let mut bytes = [0x0; 16];
&bytes[..].cwrite::<Bar>(bar, 0);
let bar = bytes.cread::<Bar>(0);
assert_eq!(bar.foo, -1);
assert_eq!(bar.bar, 0xdeadbeef);
assert_eq!({bar.foo}, -1);
assert_eq!({bar.bar}, 0xdeadbeef);
}

0 comments on commit dc960ec

Please sign in to comment.