summary refs log tree commit diff
path: root/data_model
diff options
context:
space:
mode:
authorDylan Reid <dgreid@chromium.org>2017-06-20 13:44:40 -0700
committerchrome-bot <chrome-bot@chromium.org>2017-06-26 15:28:36 -0700
commit045c7133dd22e4cc5fe62af136c15b04a8b8a485 (patch)
tree7ff0f5b65f2e45f84ffe70dfece3c1a76809d506 /data_model
parentbe4a4c97be4afa6ac4808808841a97bdf5661e04 (diff)
downloadcrosvm-045c7133dd22e4cc5fe62af136c15b04a8b8a485.tar
crosvm-045c7133dd22e4cc5fe62af136c15b04a8b8a485.tar.gz
crosvm-045c7133dd22e4cc5fe62af136c15b04a8b8a485.tar.bz2
crosvm-045c7133dd22e4cc5fe62af136c15b04a8b8a485.tar.lz
crosvm-045c7133dd22e4cc5fe62af136c15b04a8b8a485.tar.xz
crosvm-045c7133dd22e4cc5fe62af136c15b04a8b8a485.tar.zst
crosvm-045c7133dd22e4cc5fe62af136c15b04a8b8a485.zip
Add data_model with DataInit trait
The data_model crate is created to hold the DataInit trait.  Types
implementing this unsafe trait must guarantee that the type can be
initialized with random data and the resulting object will be valid.

Change-Id: Id6314d114805ec502adabe50a8bd6aa42fdb2c52
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/541681
Reviewed-by: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'data_model')
-rw-r--r--data_model/Cargo.toml6
-rw-r--r--data_model/src/lib.rs43
2 files changed, 49 insertions, 0 deletions
diff --git a/data_model/Cargo.toml b/data_model/Cargo.toml
new file mode 100644
index 0000000..ca85b0b
--- /dev/null
+++ b/data_model/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "data_model"
+version = "0.1.0"
+authors = ["The Chromium OS Authors"]
+
+[dependencies]
diff --git a/data_model/src/lib.rs b/data_model/src/lib.rs
new file mode 100644
index 0000000..895f9ad
--- /dev/null
+++ b/data_model/src/lib.rs
@@ -0,0 +1,43 @@
+// Copyright 2017 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/// Types for which it is safe to initialize from raw data.
+///
+/// A type `T` is `DataInit` if and only if it can be initialized by reading its contents from a
+/// byte array.  This is generally true for all plain-old-data structs.  It is notably not true for
+/// any type that includes a reference.
+///
+/// Implementing this trait guarantees that it is safe to instantiate the struct with random data.
+pub unsafe trait DataInit: Copy + Send + Sync {}
+
+// All intrinsic types and arays of intrinsic types are DataInit.  They are just numbers.
+macro_rules! array_data_init {
+    ($T:ty, $($N:expr)+) => {
+        $(
+            unsafe impl DataInit for [$T; $N] {}
+        )+
+    }
+}
+macro_rules! data_init_type {
+    ($T:ty) => {
+        unsafe impl DataInit for $T {}
+        array_data_init! {
+            $T,
+            0  1  2  3  4  5  6  7  8  9
+            10 11 12 13 14 15 16 17 18 19
+            20 21 22 23 24 25 26 27 28 29
+            30 31 32
+        }
+    }
+}
+data_init_type!(u8);
+data_init_type!(u16);
+data_init_type!(u32);
+data_init_type!(u64);
+data_init_type!(usize);
+data_init_type!(i8);
+data_init_type!(i16);
+data_init_type!(i32);
+data_init_type!(i64);
+data_init_type!(isize);