From 1612ff7a83ad27837d0ce19cc4558da09e20d4e6 Mon Sep 17 00:00:00 2001 From: Jingkui Wang Date: Fri, 29 Mar 2019 11:16:16 -0700 Subject: 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 Tested-by: kokoro Reviewed-by: David Tolnay --- bit_field/src/lib.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'bit_field/src') 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. //! -- cgit 1.4.1