diff --git a/9s/Cargo.toml b/9s/Cargo.toml index 3d6076bb0d..1aa5e6a070 100644 --- a/9s/Cargo.toml +++ b/9s/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["The Chromium OS Authors"] [dependencies] +assertions = { path = "../assertions" } getopts = "=0.2.17" libc = "=0.2.44" p9 = { path = "../p9" } diff --git a/9s/src/main.rs b/9s/src/main.rs index ab3f291fd6..39956de977 100644 --- a/9s/src/main.rs +++ b/9s/src/main.rs @@ -5,6 +5,7 @@ /// Runs a [9P] server. /// /// [9P]: http://man.cat-v.org/plan_9/5/0intro +extern crate assertions; extern crate getopts; extern crate libc; extern crate p9; diff --git a/9s/src/vsock.rs b/9s/src/vsock.rs index acf263eb70..64911c06fd 100644 --- a/9s/src/vsock.rs +++ b/9s/src/vsock.rs @@ -8,6 +8,7 @@ use std::mem::{self, size_of}; use std::os::raw::{c_int, c_uchar, c_uint, c_ushort}; use std::os::unix::io::RawFd; +use assertions::const_assert; use libc::{self, c_void, sa_family_t, size_t, sockaddr, socklen_t}; // The domain for vsock sockets. @@ -113,8 +114,7 @@ impl VsockListener { /// Creates a new `VsockListener` bound to the specified port on the current virtual socket /// endpoint. pub fn bind(port: c_uint) -> io::Result { - // The compiler should optimize this out since these are both compile-time constants. - assert_eq!(size_of::(), size_of::()); + const_assert!(size_of::() == size_of::()); // Safe because this doesn't modify any memory and we check the return value. let fd: RawFd = diff --git a/Cargo.lock b/Cargo.lock index d4b6b07428..3aa7f411ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,7 @@ name = "9s" version = "0.1.0" dependencies = [ + "assertions 0.1.0", "getopts 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)", "p9 0.1.0", @@ -40,6 +41,10 @@ dependencies = [ "sys_util 0.1.0", ] +[[package]] +name = "assertions" +version = "0.1.0" + [[package]] name = "bit_field" version = "0.1.0" @@ -125,6 +130,9 @@ dependencies = [ [[package]] name = "data_model" version = "0.1.0" +dependencies = [ + "assertions 0.1.0", +] [[package]] name = "devices" @@ -434,6 +442,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "usb_util" version = "0.1.0" dependencies = [ + "assertions 0.1.0", "data_model 0.1.0", "pkg-config 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)", "sync 0.1.0", diff --git a/data_model/Cargo.toml b/data_model/Cargo.toml index ca85b0b5a7..549d8178e0 100644 --- a/data_model/Cargo.toml +++ b/data_model/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" authors = ["The Chromium OS Authors"] [dependencies] +assertions = { path = "../assertions" } diff --git a/data_model/src/endian.rs b/data_model/src/endian.rs index 82799b8546..19c8d6e7f6 100644 --- a/data_model/src/endian.rs +++ b/data_model/src/endian.rs @@ -30,6 +30,9 @@ //! assert_ne!(b_trans, l_trans); //! ``` +use assertions::const_assert; +use std::mem::{align_of, size_of}; + use DataInit; macro_rules! endian_type { @@ -41,6 +44,11 @@ macro_rules! endian_type { pub struct $new_type($old_type); impl $new_type { + fn _assert() { + const_assert!(align_of::<$new_type>() == align_of::<$old_type>()); + const_assert!(size_of::<$new_type>() == size_of::<$old_type>()); + } + /// Converts `self` to the native endianness. pub fn to_native(self) -> $old_type { $old_type::$from_new(self.0) @@ -89,7 +97,7 @@ mod tests { use super::*; use std::convert::From; - use std::mem::{align_of, size_of, transmute}; + use std::mem::transmute; #[cfg(target_endian = "little")] const NATIVE_LITTLE: bool = true; @@ -102,16 +110,6 @@ mod tests { mod $test_name { use super::*; - #[test] - fn align() { - assert_eq!(align_of::<$new_type>(), align_of::<$old_type>()); - } - - #[test] - fn size() { - assert_eq!(size_of::<$new_type>(), size_of::<$old_type>()); - } - #[allow(overflowing_literals)] #[test] fn equality() { diff --git a/data_model/src/lib.rs b/data_model/src/lib.rs index 0e1bf80d82..a3c9284276 100644 --- a/data_model/src/lib.rs +++ b/data_model/src/lib.rs @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +extern crate assertions; + use std::mem::size_of; use std::slice::{from_raw_parts, from_raw_parts_mut}; diff --git a/usb_util/Cargo.toml b/usb_util/Cargo.toml index c4559819ae..bdb11f0c06 100644 --- a/usb_util/Cargo.toml +++ b/usb_util/Cargo.toml @@ -5,6 +5,7 @@ authors = ["The Chromium OS Authors"] build = "build.rs" [dependencies] +assertions = { path = "../assertions" } data_model = { path = "../data_model" } sync = { path = "../sync" } diff --git a/usb_util/src/lib.rs b/usb_util/src/lib.rs index 5c7889d405..f92511c5ec 100644 --- a/usb_util/src/lib.rs +++ b/usb_util/src/lib.rs @@ -10,6 +10,7 @@ #[cfg_attr(feature = "cargo-clippy", allow(clippy))] mod bindings; +extern crate assertions; extern crate data_model; extern crate sync; diff --git a/usb_util/src/types.rs b/usb_util/src/types.rs index 59bb5ed91f..2fc1723919 100644 --- a/usb_util/src/types.rs +++ b/usb_util/src/types.rs @@ -2,9 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +use assertions::const_assert; use bindings; use data_model::DataInit; +use std::mem::size_of; + /// Speed of usb device. See usb spec for more details. #[derive(Debug)] pub enum Speed { @@ -114,6 +117,10 @@ pub struct UsbRequestSetup { pub length: u16, // wLength } +fn _assert() { + const_assert!(size_of::() == 8); +} + unsafe impl DataInit for UsbRequestSetup {} impl UsbRequestSetup { @@ -185,14 +192,3 @@ impl UsbRequestSetup { } } } - -#[cfg(test)] -mod tests { - use super::*; - use std::mem::size_of; - - #[test] - fn check_request_setup_size() { - assert_eq!(size_of::(), 8); - } -}