mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2024-12-24 11:58:41 +00:00
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>
This commit is contained in:
parent
3c0aac44d7
commit
088e7f3025
10 changed files with 34 additions and 24 deletions
|
@ -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" }
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 =
|
||||
|
|
9
Cargo.lock
generated
9
Cargo.lock
generated
|
@ -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",
|
||||
|
|
|
@ -4,3 +4,4 @@ version = "0.1.0"
|
|||
authors = ["The Chromium OS Authors"]
|
||||
|
||||
[dependencies]
|
||||
assertions = { path = "../assertions" }
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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};
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ authors = ["The Chromium OS Authors"]
|
|||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
assertions = { path = "../assertions" }
|
||||
data_model = { path = "../data_model" }
|
||||
sync = { path = "../sync" }
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#[cfg_attr(feature = "cargo-clippy", allow(clippy))]
|
||||
mod bindings;
|
||||
|
||||
extern crate assertions;
|
||||
extern crate data_model;
|
||||
extern crate sync;
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue