1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
use storage::BlockType; use traits::*; macro_rules! impl_bits_prim { ( $t:ident ) => { impl Bits for $t { type Block = $t; #[inline] fn bit_len(&self) -> u64 { Self::nbits() as u64 } #[inline] fn block_len(&self) -> usize { 1 } #[inline] fn get_bit(&self, position: u64) -> bool { assert!(position < self.bit_len(), "prim::get_bit: out of bounds"); BlockType::get_bit(*self, position as usize) } #[inline] fn get_block(&self, position: usize) -> Self::Block { assert!(position == 0, "prim::get_block: out of bounds"); *self } #[inline] fn get_bits(&self, start: u64, count: usize) -> Self { assert!(start + count as u64 <= Self::nbits() as u64, "prim::get_bits: out of bounds"); BlockType::get_bits(*self, start as usize, count) } } impl BitsMut for $t { #[inline] fn set_bit(&mut self, position: u64, value: bool) { assert!(position < self.bit_len(), "prim::set_bit: out of bounds"); *self = self.with_bit(position as usize, value); } #[inline] fn set_block(&mut self, position: usize, value: Self::Block) { assert!(position == 0, "prim::set_block: out of bounds"); *self = value; } #[inline] fn set_bits(&mut self, start: u64, count: usize, value: Self::Block) { assert!(start + count as u64 <= Self::nbits() as u64, "prim::set_bits: out of bounds"); *self = self.with_bits(start as usize, count, value); } } } } impl_bits_prim!(u8); impl_bits_prim!(u16); impl_bits_prim!(u32); impl_bits_prim!(u64); #[cfg(int_128)] impl_bits_prim!(u128); impl_bits_prim!(usize); #[cfg(test)] mod test { use super::*; #[test] fn bit_len() { assert_eq!( 30u8.bit_len(), 8 ); assert_eq!( 30u16.bit_len(), 16 ); assert_eq!( 30u32.bit_len(), 32 ); assert_eq!( 30u64.bit_len(), 64 ); } #[test] fn block_len() { assert_eq!( 30u8.block_len(), 1 ); assert_eq!( 30u16.block_len(), 1 ); assert_eq!( 30u32.block_len(), 1 ); assert_eq!( 30u64.block_len(), 1 ); } #[test] fn get_bit() { assert_eq!( 0b01010101u8.get_bit(0), true ); assert_eq!( 0b01010101u8.get_bit(1), false ); assert_eq!( 0b01010101u8.get_bit(2), true ); assert_eq!( 0b01010101u8.get_bit(3), false ); } quickcheck! { fn prop_get_block_u8(value: u8) -> bool { value.get_block(0) == value } fn prop_get_block_u32(value: u32) -> bool { value.get_block(0) == value } } #[test] fn get_bits() { assert_eq!( 0b01010101u8.get_bits(1, 4), 0b00001010 ); assert_eq!( 0b01010101u8.get_bits(2, 4), 0b00000101 ); } #[test] fn set_bit() { let mut x = 0b01010101u8; x.set_bit(0, false); assert_eq!( x, 0b01010100 ); x.set_bit(1, true); assert_eq!( x, 0b01010110 ); x.set_block(0, 0b00000000); assert_eq!( x, 0b00000000 ); x.set_bits(2, 4, 0b00001111); assert_eq!( x, 0b00111100 ); } #[cfg(int_128)] #[test] fn u128_feature () { assert_eq!( 30u128.block_len(), 1 ); } }