bv/macros.rs
1/// Like `vec!` but for [`BitVec`](struct.BitVec.html).
2///
3/// The `bit_vec!` macro creates a `BitVec` literal. It takes two forms:
4///
5/// - A single `bool`, followed by a semicolon and number of times to repeat. This is
6/// equivalent to a call to [`BitVec::new_fill`](struct.BitVec.html#method.new_fill).
7///
8/// - A sequence of comma-separated `bool`s; this creates a `BitVec` and pushes each `bool` in
9/// turn.
10///
11/// # Examples
12///
13/// ```
14/// # #[macro_use] extern crate bv;
15/// use bv::*;
16///
17/// fn main() {
18/// let mut bv1: BitVec = bit_vec![ true; 3 ];
19/// let bv2: BitVec = bit_vec![ true, false, true ];
20///
21/// assert_ne!(bv1, bv2);
22/// bv1.set_bit(1, false);
23/// assert_eq!(bv1, bv2);
24/// }
25/// ```
26#[macro_export]
27macro_rules! bit_vec {
28 ( $e:expr ; $n:expr ) => {
29 $crate::BitVec::new_fill($e, $n)
30 };
31
32 ( $( $e:expr ),* ) => {
33 {
34 let mut result = $crate::BitVec::new();
35 let _ = &mut result;
36 $(
37 result.push($e);
38 )*
39 result
40 }
41 };
42
43 ( $( $e:expr, )* ) => {
44 bit_vec![ $($e),* ]
45 };
46}
47
48#[test]
49fn bit_vec_macro_allows_trailing_comma() {
50 let bv1: super::BitVec = bit_vec![true, false, true];
51 let bv2: super::BitVec = bit_vec![true, false, true,];
52 assert_eq!( bv1, bv2 );
53}
54
55#[test]
56fn type_1_hygiene() {
57 let result = true;
58 let bv: super::BitVec = bit_vec![result];
59 assert!( bv[0] );
60}
61
62// Implements Index for any type that implements Bits.
63macro_rules! impl_index_from_bits {
64 (
65 $(
66 impl[ $($param:tt)* ] Index<$ix:ty> for $bv:ty ;
67 )+
68 )=> {
69 $(
70 impl<$($param)*> ::std::ops::Index<$ix> for $bv {
71 type Output = bool;
72
73 fn index(&self, index: $ix) -> &bool {
74 use $crate::Bits;
75
76 static TRUE: bool = true;
77 static FALSE: bool = false;
78
79 if self.get_bit(index) {&TRUE} else {&FALSE}
80 }
81 }
82 )+
83 };
84}
85