summary refs log tree commit diff
path: root/bit_field/src
diff options
context:
space:
mode:
authorJingkui Wang <jkwang@google.com>2019-03-29 11:16:16 -0700
committerchrome-bot <chrome-bot@chromium.org>2019-04-03 18:13:52 -0700
commit1612ff7a83ad27837d0ce19cc4558da09e20d4e6 (patch)
treef5aaafdb5a0810fb422aa62845943d5e3967dd65 /bit_field/src
parent6bfc6a0304910701e55e56c4feaf0d73c07d33e5 (diff)
downloadcrosvm-1612ff7a83ad27837d0ce19cc4558da09e20d4e6.tar
crosvm-1612ff7a83ad27837d0ce19cc4558da09e20d4e6.tar.gz
crosvm-1612ff7a83ad27837d0ce19cc4558da09e20d4e6.tar.bz2
crosvm-1612ff7a83ad27837d0ce19cc4558da09e20d4e6.tar.lz
crosvm-1612ff7a83ad27837d0ce19cc4558da09e20d4e6.tar.xz
crosvm-1612ff7a83ad27837d0ce19cc4558da09e20d4e6.tar.zst
crosvm-1612ff7a83ad27837d0ce19cc4558da09e20d4e6.zip
improve bitfield type safety by allowing tuple struct field
user-defined tuple struct could be used to improve type safety.

TEST=cargo test
BUG=None

Change-Id: I8ce10fc51b79c277ab23029513b707f3dd621af5
Reviewed-on: https://chromium-review.googlesource.com/1546432
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
Diffstat (limited to 'bit_field/src')
-rw-r--r--bit_field/src/lib.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/bit_field/src/lib.rs b/bit_field/src/lib.rs
index 64a309b..bd11c3c 100644
--- a/bit_field/src/lib.rs
+++ b/bit_field/src/lib.rs
@@ -101,6 +101,30 @@
 //! }
 //! ```
 //!
+//! Fields may be user-defined single element tuple struct with primitive types. Use must specify
+//! the width with `#[bits = N]`. This should be used to improve type safety.
+//!
+//! ```
+//! extern crate bit_field;
+//!
+//! use bit_field::*;
+//!
+//! #[bitfield]
+//! #[bits = 60]
+//! struct AddressField(u64);
+//!
+//! impl AddressField {
+//!     pub fn new(addr: u64) -> AddressField {
+//!         AddressField(addr >> 4)
+//!     }
+//!
+//!     pub fn get_addr(&self) -> u64 {
+//!         self.0 << 4
+//!     }
+//! }
+//!
+//! ```
+//!
 //! Finally, fields may be of user-defined enum types. The enum must satisfy one of the following
 //! requirements.
 //!