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 140 141 142 143 144 145 146 147 148
#![no_std] /*! # Bit Op This crate provide you constant for each bit of each unsigned type. And if you import `BitOp` trait you can call `set`, `reset`, `toggle`, `get` on all unsigned integers ## Basic usage: ``` use bit_op::BitOp; use bit_op::bit_u8::B0 as HELLO; use bit_op::bit_u8::B1 as WORLD; let mut config: u8 = 0; config.set(HELLO); config.set(WORLD); let mut output = String::new(); if config.get(HELLO) != 0 { output += "Hello "; } if config.get(WORLD) != 0 { output += "World!"; } assert_eq!(output, "Hello World!"); ``` */ /// Definitions of constants for each bit of u8 pub mod bit_u8; /// Definitions of constants for each bit of u16 pub mod bit_u16; /// Definitions of constants for each bit of u32 pub mod bit_u32; /// Definitions of constants for each bit of u64 pub mod bit_u64; /// Definitions of constants for each bit of u128 pub mod bit_u128; use core::ops::{BitAnd, BitAndAssign, BitOrAssign, BitXorAssign, Not}; // Provide the methods `set`, `reset`, `toggle`, `get` pub trait BitOp: Not<Output = Self> + BitOrAssign<Self> + BitAndAssign<Self> + BitXorAssign<Self> + BitAnd<Self, Output = Self> + Sized { /** Set desired bits # Examples ``` use bit_op::{BitOp, bit_u8::*}; let mut x = 0b00001111u8; x.set(B7); assert_eq!(x, 0b10001111); let mut y = 0u8; y.set(B7 | B0); assert_eq!(y, 0b10000001); ``` */ fn set(&mut self, rhs: Self) { *self |= rhs; } /** Reset desired bits # Examples ``` use bit_op::{BitOp, bit_u8::*}; let mut x = 0b00001111u8; x.reset(B0 | B1 | B2 | B3); assert_eq!(x, 0); let mut y = 0b11111111u8; y.reset(B7 | B0); assert_eq!(y, 0b01111110); ``` */ fn reset(&mut self, rhs: Self) { *self &= !rhs; } /** Toggle desired bits # Examples ``` use bit_op::{BitOp, bit_u8::*}; let mut x = 0b00001111u8; x.toggle(B5 | B4 | B3 | B2); assert_eq!(x, 0b00110011); x.toggle(B5 | B4 | B3 | B2); assert_eq!(x, 0b00001111); ``` */ fn toggle(&mut self, rhs: Self) { *self ^= rhs; } /** Get desired bits # Examples ``` use bit_op::{BitOp, bit_u8::*}; let mut x = 0b10000001u8; assert_eq!(x.get(B7), 0b10000000); assert_eq!(x.get(B6), 0b00000000); assert_eq!(x.get(B0), 0b00000001); ``` */ fn get(self, rhs: Self) -> Self { self & rhs } } impl BitOp for u8 {} impl BitOp for u16 {} impl BitOp for u32 {} impl BitOp for u64 {} impl BitOp for u128 {}