From fbdce42530419c112a4c82537488fb94d446e292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ho=C3=A0ng=20=C4=90=E1=BB=A9c=20Hi=E1=BA=BFu?= Date: Wed, 2 May 2018 16:01:48 +0700 Subject: [PATCH] tests: fix safe_packed_borrows warning Close #24 --- src/greater.rs | 7 +++++-- src/lesser.rs | 7 +++++-- tests/api.rs | 16 ++++++++++------ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/greater.rs b/src/greater.rs index 9304b06..540d8da 100644 --- a/src/greater.rs +++ b/src/greater.rs @@ -26,8 +26,11 @@ use ctx::{FromCtx, IntoCtx}; /// /// let bytes = [0xff, 0xff, 0xff, 0xff, 0xef,0xbe,0xad,0xde,]; /// let bar = bytes.cread_with::(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 : Index + Index> where diff --git a/src/lesser.rs b/src/lesser.rs index de02d46..60b48f1 100644 --- a/src/lesser.rs +++ b/src/lesser.rs @@ -43,8 +43,11 @@ use ctx::{FromCtx, IntoCtx, SizeWith}; /// assert!(error.is_err()); /// let mut bytes = Cursor::new(bytes_); /// let foo_ = bytes.ioread_with::(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 : Read diff --git a/tests/api.rs b/tests/api.rs index 3e31290..b29a94a 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -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; @@ -202,8 +206,8 @@ fn ioread_api() { assert!(error.is_err()); let mut bytes = Cursor::new(bytes_); let foo_ = bytes.ioread_with::(LE).unwrap(); - assert_eq!(foo_.foo, foo); - assert_eq!(foo_.bar, bar); + assert_eq!({foo_.foo}, foo); + assert_eq!({foo_.bar}, bar); } #[repr(packed)] @@ -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::(0, LE); - assert_eq!(bar.foo, -1); - assert_eq!(bar.bar, 0xdeadbeef); + assert_eq!({bar.foo}, -1); + assert_eq!({bar.bar}, 0xdeadbeef); } #[test] @@ -272,7 +276,7 @@ fn cwrite_api_customtype() { let mut bytes = [0x0; 16]; &bytes[..].cwrite::(bar, 0); let bar = bytes.cread::(0); - assert_eq!(bar.foo, -1); - assert_eq!(bar.bar, 0xdeadbeef); + assert_eq!({bar.foo}, -1); + assert_eq!({bar.bar}, 0xdeadbeef); }