Skip to content

Commit

Permalink
Merge pull request #28 from hdhoang/unsafe_packed_borrows
Browse files Browse the repository at this point in the history
tests: fix safe_packed_borrows warning
  • Loading branch information
m4b authored May 6, 2018
2 parents 37a782d + fbdce42 commit 2bfd25e
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 2bfd25e

Please sign in to comment.