Skip to content

Commit a3d9980

Browse files
committed
Document how generated bitflags can be extended with type and trait implementations
1 parent 8b58981 commit a3d9980

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/libcollections/bitflags.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,40 @@
3838
//! }
3939
//! ~~~
4040
//!
41+
//! The generated `struct`s can also be extended with type and trait implementations:
42+
//!
43+
//! ~~~rust
44+
//! #[feature(phase)];
45+
//! #[phase(syntax)] extern crate collections;
46+
//!
47+
//! use std::fmt;
48+
//!
49+
//! bitflags!(Flags: u32 {
50+
//! FlagA = 0x00000001,
51+
//! FlagB = 0x00000010
52+
//! })
53+
//!
54+
//! impl Flags {
55+
//! pub fn clear(&mut self) {
56+
//! self.bits = 0; // The `bits` field can be accessed from within the
57+
//! // same module where the `bitflags!` macro was invoked.
58+
//! }
59+
//! }
60+
//!
61+
//! impl fmt::Show for Flags {
62+
//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63+
//! write!(f.buf, "hi!")
64+
//! }
65+
//! }
66+
//!
67+
//! fn main() {
68+
//! let mut flags = FlagA | FlagB;
69+
//! flags.clear();
70+
//! assert!(flags.is_empty());
71+
//! assert_eq!(format!("{}", flags), ~"hi!");
72+
//! }
73+
//! ~~~
74+
//!
4175
//! # Operators
4276
//!
4377
//! The following operator traits are implemented for the generated `struct`:

0 commit comments

Comments
 (0)