summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Tolnay <dtolnay@chromium.org>2018-12-07 11:32:09 -0800
committerchrome-bot <chrome-bot@chromium.org>2018-12-09 00:48:17 -0800
commit088e7f30258a6b55779ccb5551f01322a325f1a8 (patch)
tree72753bd444d7b2266c453b51ddd71a925a82ae5a
parent3c0aac44d7d7150bbfb2981b92a4ed98a64a1a49 (diff)
downloadcrosvm-088e7f30258a6b55779ccb5551f01322a325f1a8.tar
crosvm-088e7f30258a6b55779ccb5551f01322a325f1a8.tar.gz
crosvm-088e7f30258a6b55779ccb5551f01322a325f1a8.tar.bz2
crosvm-088e7f30258a6b55779ccb5551f01322a325f1a8.tar.lz
crosvm-088e7f30258a6b55779ccb5551f01322a325f1a8.tar.xz
crosvm-088e7f30258a6b55779ccb5551f01322a325f1a8.tar.zst
crosvm-088e7f30258a6b55779ccb5551f01322a325f1a8.zip
assertions: Use compile-time assertion macro
This depends on the `assertions` crate added in CL:1366819.

`const_assert!(boolean expression)` is a compile-time assertion that
fails to compile if the expression is false.

TEST=`cargo check` each of the modified crates

Change-Id: I559884baf2275b1b506619693cd100a4ffc8adcd
Reviewed-on: https://chromium-review.googlesource.com/1368364
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: David Tolnay <dtolnay@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
-rw-r--r--9s/Cargo.toml1
-rw-r--r--9s/src/main.rs1
-rw-r--r--9s/src/vsock.rs4
-rw-r--r--Cargo.lock9
-rw-r--r--data_model/Cargo.toml1
-rw-r--r--data_model/src/endian.rs20
-rw-r--r--data_model/src/lib.rs2
-rw-r--r--usb_util/Cargo.toml1
-rw-r--r--usb_util/src/lib.rs1
-rw-r--r--usb_util/src/types.rs18
10 files changed, 34 insertions, 24 deletions
diff --git a/9s/Cargo.toml b/9s/Cargo.toml
index 3d6076b..1aa5e6a 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 ab3f291..39956de 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 acf263e..64911c0 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<VsockListener> {
-        // The compiler should optimize this out since these are both compile-time constants.
-        assert_eq!(size_of::<sockaddr_vm>(), size_of::<sockaddr>());
+        const_assert!(size_of::<sockaddr_vm>() == size_of::<sockaddr>());
 
         // 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 d4b6b07..3aa7f41 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",
@@ -41,6 +42,10 @@ dependencies = [
 ]
 
 [[package]]
+name = "assertions"
+version = "0.1.0"
+
+[[package]]
 name = "bit_field"
 version = "0.1.0"
 dependencies = [
@@ -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 ca85b0b..549d817 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 82799b8..19c8d6e 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 0e1bf80..a3c9284 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 c455981..bdb11f0 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 5c7889d..f92511c 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 59bb5ed..2fc1723 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::<UsbRequestSetup>() == 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::<UsbRequestSetup>(), 8);
-    }
-}