diff options
| author | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-03-23 11:34:07 +1100 |
|---|---|---|
| committer | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-04-29 18:50:31 -0700 |
| commit | a3d9980b25a239da612df9f8a38dec95f57adf4a (patch) | |
| tree | b0862f3726a600c1acf3d2f0e55550eb23679822 | |
| parent | 8b58981871660bd83454878af50de8157ed3ea5e (diff) | |
| download | rust-a3d9980b25a239da612df9f8a38dec95f57adf4a.tar.gz rust-a3d9980b25a239da612df9f8a38dec95f57adf4a.zip | |
Document how generated bitflags can be extended with type and trait implementations
| -rw-r--r-- | src/libcollections/bitflags.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/libcollections/bitflags.rs b/src/libcollections/bitflags.rs index 294d5fe6d8e..22b98faf6bf 100644 --- a/src/libcollections/bitflags.rs +++ b/src/libcollections/bitflags.rs @@ -38,6 +38,40 @@ //! } //! ~~~ //! +//! The generated `struct`s can also be extended with type and trait implementations: +//! +//! ~~~rust +//! #[feature(phase)]; +//! #[phase(syntax)] extern crate collections; +//! +//! use std::fmt; +//! +//! bitflags!(Flags: u32 { +//! FlagA = 0x00000001, +//! FlagB = 0x00000010 +//! }) +//! +//! impl Flags { +//! pub fn clear(&mut self) { +//! self.bits = 0; // The `bits` field can be accessed from within the +//! // same module where the `bitflags!` macro was invoked. +//! } +//! } +//! +//! impl fmt::Show for Flags { +//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { +//! write!(f.buf, "hi!") +//! } +//! } +//! +//! fn main() { +//! let mut flags = FlagA | FlagB; +//! flags.clear(); +//! assert!(flags.is_empty()); +//! assert_eq!(format!("{}", flags), ~"hi!"); +//! } +//! ~~~ +//! //! # Operators //! //! The following operator traits are implemented for the generated `struct`: |
