| 1 |
#![recursion_limit = "128"] |
| 2 |
#![allow(clippy::cognitive_complexity)] |
| 3 |
|
| 4 |
use bitfield::{bitfield, bitfield_fields}; |
| 5 |
|
| 6 |
// We use a constant to make sure bits positions don't need to be literals but |
| 7 |
// can also be constants or expressions. |
| 8 |
const THREE: usize = 3; |
| 9 |
|
| 10 |
#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 11 |
pub struct Foo(u16); |
| 12 |
impl From<u8> for Foo { |
| 13 |
fn from(value: u8) -> Foo { |
| 14 |
Foo(u16::from(value)) |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
impl From<Foo> for u8 { |
| 19 |
fn from(value: Foo) -> u8 { |
| 20 |
value.0 as u8 |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 25 |
pub struct EvenU8(u8); |
| 26 |
|
| 27 |
impl From<EvenU8> for u8 { |
| 28 |
fn from(value: EvenU8) -> u8 { |
| 29 |
value.0 |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
impl TryFrom<u8> for EvenU8 { |
| 34 |
type Error = (); |
| 35 |
|
| 36 |
fn try_from(value: u8) -> Result<Self, Self::Error> { |
| 37 |
if value % 2 == 0 { |
| 38 |
Ok(EvenU8(value)) |
| 39 |
} else { |
| 40 |
Err(()) |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
bitfield! { |
| 46 |
#[derive(Copy, Clone)] |
| 47 |
/// documentation comments also work! |
| 48 |
struct FooBar(u32); |
| 49 |
impl Debug; |
| 50 |
impl BitOr; |
| 51 |
foo1, set_foo1: 0, 0; |
| 52 |
u8; |
| 53 |
foo2, set_foo2: 31, 31; |
| 54 |
foo3, set_foo3: THREE, 0; |
| 55 |
// We make sure attributes are applied to fields. If attributes were not |
| 56 |
// applied, the compilation would fail with a `duplicate definition` |
| 57 |
// error. |
| 58 |
#[cfg(not(test))] |
| 59 |
foo3, set_foo3: 3, 0; |
| 60 |
u16, foo4, set_foo4: 31, 28; |
| 61 |
foo5, set_foo5: 0, 0, 32; |
| 62 |
u32; |
| 63 |
foo6, set_foo6: 5, THREE, THREE; |
| 64 |
mask GETTER_MASK(u32), getter_only, _: 3, 1; |
| 65 |
mask SETTER_MASK(u32), _, setter_only: 2*2, 2; |
| 66 |
pub mask PUB_GETTER_MASK(u32), pub_getter_only, _: 3, 1; |
| 67 |
pub mask PUB_SETTER_MASK(u32), _, pub_setter_only: 2*2, 2; |
| 68 |
getter_only_array, _: 5, 3, 3; |
| 69 |
_, setter_only_array: 2*THREE, 4, 3; |
| 70 |
all_bits, set_all_bits: 31, 0; |
| 71 |
mask SINGLE_BIT_MASK(u32), single_bit, set_single_bit: 3; |
| 72 |
u8, into Foo, into_foo1, set_into_foo1: 31, 31; |
| 73 |
pub u8, mask PUB_MASK(u32), into Foo, into_foo2, set_into_foo2: 31, 31; |
| 74 |
u8, from into Foo, from_foo1, set_from_foo1: 31, 31; |
| 75 |
u8, from into Foo, _, set_from_foo2: 31, 31; |
| 76 |
u8; |
| 77 |
into Foo, into_foo3, set_into_foo3: 31, 31; |
| 78 |
pub into Foo, into_foo4, set_into_foo4: 31, 31; |
| 79 |
into Foo, _, set_into_foo5: 31, 31; |
| 80 |
into Foo, into_foo6, _: 29, 29, 3; |
| 81 |
from into Foo, from_foo3, set_from_foo3: 31, 31; |
| 82 |
from into Foo, _, set_from_foo4: 31, 31; |
| 83 |
from into Foo, from_foo5, set_from_foo5: 29, 29, 3; |
| 84 |
from into Foo, from_foo6, _: 31, 31; |
| 85 |
from try_into EvenU8, from_foo7, set_from_foo7: 31, 30; |
| 86 |
try_into EvenU8, from_foo8, set_from_foo8: 31, 30; |
| 87 |
try_into EvenU8, from_foo9, set_from_foo9: 8, 8, 3; |
| 88 |
|
| 89 |
i8; |
| 90 |
signed_single_bit, set_signed_single_bit: 0, 0; |
| 91 |
signed_two_bits, set_signed_two_bits: 1, 0; |
| 92 |
signed_eight_bits, set_signed_eight_bits: 7, 0; |
| 93 |
signed_eight_bits_unaligned, set_signed_eight_bits_unaligned: 8, 1; |
| 94 |
u128, mask U128_MASK(u128), u128_getter, set_u128: 8, 1; |
| 95 |
i128, mask I128_MASK(i128), i128_getter, set_i128: 8, 1; |
| 96 |
bool, bool_array_getter, bool_array_setter: 8, 8, 3; |
| 97 |
} |
| 98 |
|
| 99 |
impl FooBar { |
| 100 |
bitfield_fields! { |
| 101 |
// Boolean field don't need a type |
| 102 |
foo7, _: 1; |
| 103 |
} |
| 104 |
|
| 105 |
bitfield_fields! { |
| 106 |
// If all fields have a type, we don't need to specify a default type |
| 107 |
u8, foo8,_: 1, 0; |
| 108 |
u32, foo9, _: 2, 0; |
| 109 |
} |
| 110 |
|
| 111 |
bitfield_fields! { |
| 112 |
// We can still set a default type |
| 113 |
u16; |
| 114 |
foo10, _: 2, 0; |
| 115 |
u32, foo11, _: 2, 0; |
| 116 |
foo12, _: 2, 0; |
| 117 |
} |
| 118 |
|
| 119 |
// Check if an empty bitfield_fields compiles without errors. |
| 120 |
bitfield_fields! {} |
| 121 |
|
| 122 |
// Check if mask, from and into are allowed as getter names |
| 123 |
bitfield_fields! { |
| 124 |
u8, mask, _: 2,0; |
| 125 |
u8, from, _: 2,0; |
| 126 |
u8, into, _: 2,0; |
| 127 |
} |
| 128 |
|
| 129 |
bitfield_fields! { |
| 130 |
u8, lsb_msb_inverted, set_lsb_msb_inverted: 1, 3; |
| 131 |
u8, lsb_msb_inverted_array, set_lsb_msb_inverted_array: 2, 3, 2; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn test_single_bit() { |
| 137 |
let mut fb = FooBar(0); |
| 138 |
|
| 139 |
fb.set_foo1(1); |
| 140 |
assert_eq!(0x1, fb.0); |
| 141 |
assert_eq!(0x1, fb.foo1()); |
| 142 |
assert_eq!(0x0, fb.foo2()); |
| 143 |
assert!(!fb.single_bit()); |
| 144 |
assert_eq!(-1, fb.signed_single_bit()); |
| 145 |
|
| 146 |
fb.set_foo2(1); |
| 147 |
assert_eq!(0x8000_0001, fb.0); |
| 148 |
assert_eq!(0x1, fb.foo1()); |
| 149 |
assert_eq!(0x1, fb.foo2()); |
| 150 |
assert!(!fb.single_bit()); |
| 151 |
assert_eq!(-1, fb.signed_single_bit()); |
| 152 |
|
| 153 |
fb.set_foo1(0); |
| 154 |
assert_eq!(0x8000_0000, fb.0); |
| 155 |
assert_eq!(0x0, fb.foo1()); |
| 156 |
assert_eq!(0x1, fb.foo2()); |
| 157 |
assert!(!fb.single_bit()); |
| 158 |
assert_eq!(0, fb.signed_single_bit()); |
| 159 |
|
| 160 |
fb.set_single_bit(true); |
| 161 |
assert_eq!(0x8000_0008, fb.0); |
| 162 |
assert_eq!(0x0, fb.foo1()); |
| 163 |
assert_eq!(0x1, fb.foo2()); |
| 164 |
assert!(fb.single_bit()); |
| 165 |
assert_eq!(0, fb.signed_single_bit()); |
| 166 |
|
| 167 |
fb.set_signed_single_bit(-1); |
| 168 |
assert_eq!(0x8000_0009, fb.0); |
| 169 |
assert_eq!(0x1, fb.foo1()); |
| 170 |
assert_eq!(0x1, fb.foo2()); |
| 171 |
assert!(fb.single_bit()); |
| 172 |
assert_eq!(-1, fb.signed_single_bit()); |
| 173 |
} |
| 174 |
|
| 175 |
#[test] |
| 176 |
fn test_single_bit_plus_garbage() { |
| 177 |
let mut fb = FooBar(0); |
| 178 |
|
| 179 |
fb.set_foo1(0b10); |
| 180 |
assert_eq!(0x0, fb.0); |
| 181 |
assert_eq!(0x0, fb.foo1()); |
| 182 |
assert_eq!(0x0, fb.foo2()); |
| 183 |
|
| 184 |
fb.set_foo1(0b11); |
| 185 |
assert_eq!(0x1, fb.0); |
| 186 |
assert_eq!(0x1, fb.foo1()); |
| 187 |
assert_eq!(0x0, fb.foo2()); |
| 188 |
} |
| 189 |
|
| 190 |
#[test] |
| 191 |
fn test_multiple_bit() { |
| 192 |
let mut fb = FooBar(0); |
| 193 |
|
| 194 |
fb.set_foo3(0x0F); |
| 195 |
assert_eq!(0xF, fb.0); |
| 196 |
assert_eq!(0xF, fb.foo3()); |
| 197 |
assert_eq!(0x0, fb.foo4()); |
| 198 |
|
| 199 |
fb.set_foo4(0x0F); |
| 200 |
assert_eq!(0xF000_000F, fb.0); |
| 201 |
assert_eq!(0xF, fb.foo3()); |
| 202 |
assert_eq!(0xF, fb.foo4()); |
| 203 |
|
| 204 |
fb.set_foo3(0); |
| 205 |
assert_eq!(0xF000_0000, fb.0); |
| 206 |
assert_eq!(0x0, fb.foo3()); |
| 207 |
assert_eq!(0xF, fb.foo4()); |
| 208 |
|
| 209 |
fb.set_foo3(0xA); |
| 210 |
assert_eq!(0xF000_000A, fb.0); |
| 211 |
assert_eq!(0xA, fb.foo3()); |
| 212 |
assert_eq!(0xF, fb.foo4()); |
| 213 |
} |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn test_bool_array_field() { |
| 217 |
let mut fb = FooBar(0); |
| 218 |
|
| 219 |
assert!(!fb.bool_array_getter(0)); |
| 220 |
assert!(!fb.bool_array_getter(1)); |
| 221 |
assert!(!fb.bool_array_getter(2)); |
| 222 |
|
| 223 |
fb.bool_array_setter(1, true); |
| 224 |
|
| 225 |
assert_eq!(1 << 9, fb.0); |
| 226 |
assert!(!fb.bool_array_getter(0)); |
| 227 |
assert!(fb.bool_array_getter(1)); |
| 228 |
assert!(!fb.bool_array_getter(2)); |
| 229 |
} |
| 230 |
|
| 231 |
#[test] |
| 232 |
fn test_try_into() { |
| 233 |
let mut fb = FooBar(0); |
| 234 |
assert_eq!(fb.from_foo7(), Ok(EvenU8(0))); |
| 235 |
assert_eq!(fb.from_foo8(), Ok(EvenU8(0))); |
| 236 |
fb.set_from_foo7(EvenU8(1)); |
| 237 |
assert_eq!(fb.from_foo7(), Err(())); |
| 238 |
assert_eq!(fb.from_foo8(), Err(())); |
| 239 |
fb.set_from_foo8(2); |
| 240 |
assert_eq!(fb.from_foo7(), Ok(EvenU8(2))); |
| 241 |
assert_eq!(fb.from_foo8(), Ok(EvenU8(2))); |
| 242 |
} |
| 243 |
|
| 244 |
#[test] |
| 245 |
#[should_panic(expected = "the MSB (1) is smaller than the LSB (3), you likely inverted them")] |
| 246 |
fn lsb_msb_inverted_should_panic_on_access_1() { |
| 247 |
let fb = FooBar(0); |
| 248 |
fb.lsb_msb_inverted(); |
| 249 |
} |
| 250 |
|
| 251 |
#[test] |
| 252 |
#[should_panic(expected = "the MSB (1) is smaller than the LSB (3), you likely inverted them")] |
| 253 |
fn lsb_msb_inverted_should_panic_on_access_2() { |
| 254 |
let mut fb = FooBar(0); |
| 255 |
fb.set_lsb_msb_inverted(1); |
| 256 |
} |
| 257 |
|
| 258 |
#[test] |
| 259 |
#[should_panic(expected = "the MSB (2) is smaller than the LSB (3), you likely inverted them")] |
| 260 |
fn lsb_msb_inverted_should_panic_on_access_3() { |
| 261 |
let fb = FooBar(0); |
| 262 |
fb.lsb_msb_inverted_array(0); |
| 263 |
} |
| 264 |
|
| 265 |
#[test] |
| 266 |
#[should_panic(expected = "the MSB (2) is smaller than the LSB (3), you likely inverted them")] |
| 267 |
fn lsb_msb_inverted_should_panic_on_access_4() { |
| 268 |
let mut fb = FooBar(0); |
| 269 |
fb.set_lsb_msb_inverted_array(0, 1); |
| 270 |
} |
| 271 |
|
| 272 |
bitfield! { |
| 273 |
#[derive(Clone, Copy)] |
| 274 |
struct FourFields(u8); |
| 275 |
impl BitOr; |
| 276 |
impl BitAnd; |
| 277 |
impl BitXor; |
| 278 |
impl new; |
| 279 |
a, set_a: 0; |
| 280 |
b, set_b: 1; |
| 281 |
c, set_c: 2; |
| 282 |
d, set_d: 3; |
| 283 |
} |
| 284 |
|
| 285 |
#[test] |
| 286 |
fn test_bitwise_ops() { |
| 287 |
let mut ff1 = FourFields(0); |
| 288 |
ff1.set_a(true); |
| 289 |
ff1.set_b(true); |
| 290 |
let mut ff2 = FourFields(0); |
| 291 |
ff2.set_a(true); |
| 292 |
ff2.set_c(true); |
| 293 |
|
| 294 |
let ffand = ff1 & ff2; |
| 295 |
assert!(ffand.a()); |
| 296 |
assert!(!ffand.b()); |
| 297 |
assert!(!ffand.c()); |
| 298 |
assert!(!ffand.d()); |
| 299 |
|
| 300 |
let ffor = ff1 | ff2; |
| 301 |
assert!(ffor.a()); |
| 302 |
assert!(ffor.b()); |
| 303 |
assert!(ffor.c()); |
| 304 |
assert!(!ffor.d()); |
| 305 |
|
| 306 |
let ffxor = ff1 ^ ff2; |
| 307 |
assert!(!ffxor.a()); |
| 308 |
assert!(ffxor.b()); |
| 309 |
assert!(ffxor.c()); |
| 310 |
assert!(!ffxor.d()); |
| 311 |
|
| 312 |
ff1 ^= ff2; |
| 313 |
assert!(!ff1.a()); |
| 314 |
assert!(ff1.b()); |
| 315 |
assert!(ff1.c()); |
| 316 |
assert!(!ff1.d()); |
| 317 |
} |
| 318 |
|
| 319 |
#[test] |
| 320 |
fn test_constructor() { |
| 321 |
let ff1 = FourFields::new(true, false, true, false); |
| 322 |
assert!(ff1.a()); |
| 323 |
assert!(!ff1.b()); |
| 324 |
assert!(ff1.c()); |
| 325 |
assert!(!ff1.d()); |
| 326 |
} |
| 327 |
|
| 328 |
#[test] |
| 329 |
fn test_getter_setter_only() { |
| 330 |
let mut fb = FooBar(0); |
| 331 |
fb.setter_only(0x7); |
| 332 |
assert_eq!(0x1C, fb.0); |
| 333 |
assert_eq!(0x6, fb.getter_only()); |
| 334 |
} |
| 335 |
|
| 336 |
#[test] |
| 337 |
fn test_array_field1() { |
| 338 |
let mut fb = FooBar(0); |
| 339 |
|
| 340 |
fb.set_foo5(0, 1); |
| 341 |
assert_eq!(0x1, fb.0); |
| 342 |
assert_eq!(1, fb.foo5(0)); |
| 343 |
|
| 344 |
fb.set_foo5(0, 0); |
| 345 |
assert_eq!(0x0, fb.0); |
| 346 |
assert_eq!(0, fb.foo5(0)); |
| 347 |
|
| 348 |
fb.set_foo5(0, 1); |
| 349 |
fb.set_foo5(6, 1); |
| 350 |
fb.set_foo5(31, 1); |
| 351 |
assert_eq!(0x8000_0041, fb.0); |
| 352 |
assert_eq!(1, fb.foo5(0)); |
| 353 |
assert_eq!(1, fb.foo5(6)); |
| 354 |
assert_eq!(1, fb.foo5(31)); |
| 355 |
assert_eq!(0, fb.foo5(1)); |
| 356 |
assert_eq!(0, fb.foo5(5)); |
| 357 |
assert_eq!(0, fb.foo5(7)); |
| 358 |
assert_eq!(0, fb.foo5(30)); |
| 359 |
} |
| 360 |
|
| 361 |
#[test] |
| 362 |
fn test_array_field2() { |
| 363 |
let mut fb = FooBar(0); |
| 364 |
|
| 365 |
fb.set_foo6(0, 1); |
| 366 |
assert_eq!(0x8, fb.0); |
| 367 |
assert_eq!(1, fb.foo6(0)); |
| 368 |
assert_eq!(0, fb.foo6(1)); |
| 369 |
assert_eq!(0, fb.foo6(2)); |
| 370 |
|
| 371 |
fb.set_foo6(0, 7); |
| 372 |
assert_eq!(0x38, fb.0); |
| 373 |
assert_eq!(7, fb.foo6(0)); |
| 374 |
assert_eq!(0, fb.foo6(1)); |
| 375 |
assert_eq!(0, fb.foo6(2)); |
| 376 |
|
| 377 |
fb.set_foo6(2, 7); |
| 378 |
assert_eq!(0xE38, fb.0); |
| 379 |
assert_eq!(7, fb.foo6(0)); |
| 380 |
assert_eq!(0, fb.foo6(1)); |
| 381 |
assert_eq!(7, fb.foo6(2)); |
| 382 |
|
| 383 |
fb.set_foo6(0, 0); |
| 384 |
assert_eq!(0xE00, fb.0); |
| 385 |
assert_eq!(0, fb.foo6(0)); |
| 386 |
assert_eq!(0, fb.foo6(1)); |
| 387 |
assert_eq!(7, fb.foo6(2)); |
| 388 |
} |
| 389 |
|
| 390 |
#[allow(clippy::identity_op)] |
| 391 |
#[allow(clippy::erasing_op)] |
| 392 |
#[test] |
| 393 |
fn test_setter_only_array() { |
| 394 |
let mut fb = FooBar(0); |
| 395 |
|
| 396 |
fb.setter_only_array(0, 0); |
| 397 |
assert_eq!(0x0, fb.0); |
| 398 |
|
| 399 |
fb.setter_only_array(0, 0b111); |
| 400 |
assert_eq!(0b111 << (4 + 0 * 2), fb.0); |
| 401 |
|
| 402 |
fb.setter_only_array(0, 0); |
| 403 |
fb.setter_only_array(1, 0b111); |
| 404 |
assert_eq!(0b111 << (4 + 1 * 3), fb.0); |
| 405 |
|
| 406 |
fb.setter_only_array(1, 0); |
| 407 |
fb.setter_only_array(2, 0b111); |
| 408 |
assert_eq!(0b111 << (4 + 2 * 3), fb.0); |
| 409 |
} |
| 410 |
|
| 411 |
#[test] |
| 412 |
fn test_getter_only_array() { |
| 413 |
let mut fb = FooBar(0); |
| 414 |
|
| 415 |
assert_eq!(0, fb.getter_only_array(0)); |
| 416 |
assert_eq!(0, fb.getter_only_array(1)); |
| 417 |
assert_eq!(0, fb.getter_only_array(2)); |
| 418 |
|
| 419 |
fb.0 = !(0x1FF << 3); |
| 420 |
assert_eq!(0, fb.getter_only_array(0)); |
| 421 |
assert_eq!(0, fb.getter_only_array(1)); |
| 422 |
assert_eq!(0, fb.getter_only_array(2)); |
| 423 |
|
| 424 |
fb.0 = 0xF << 3; |
| 425 |
assert_eq!(0b111, fb.getter_only_array(0)); |
| 426 |
assert_eq!(0b001, fb.getter_only_array(1)); |
| 427 |
assert_eq!(0, fb.getter_only_array(2)); |
| 428 |
|
| 429 |
fb.0 = 0xF << 6; |
| 430 |
assert_eq!(0, fb.getter_only_array(0)); |
| 431 |
assert_eq!(0b111, fb.getter_only_array(1)); |
| 432 |
assert_eq!(0b001, fb.getter_only_array(2)); |
| 433 |
|
| 434 |
fb.0 = 0xF << 8; |
| 435 |
assert_eq!(0, fb.getter_only_array(0)); |
| 436 |
assert_eq!(0b100, fb.getter_only_array(1)); |
| 437 |
assert_eq!(0b111, fb.getter_only_array(2)); |
| 438 |
|
| 439 |
fb.0 = 0b101_010_110 << 3; |
| 440 |
assert_eq!(0b110, fb.getter_only_array(0)); |
| 441 |
assert_eq!(0b010, fb.getter_only_array(1)); |
| 442 |
assert_eq!(0b101, fb.getter_only_array(2)); |
| 443 |
} |
| 444 |
|
| 445 |
#[test] |
| 446 |
fn test_signed() { |
| 447 |
let mut fb = FooBar(0); |
| 448 |
|
| 449 |
assert_eq!(0, fb.signed_two_bits()); |
| 450 |
assert_eq!(0, fb.signed_eight_bits()); |
| 451 |
assert_eq!(0, fb.signed_eight_bits_unaligned()); |
| 452 |
|
| 453 |
fb.set_signed_two_bits(-2); |
| 454 |
assert_eq!(0b10, fb.0); |
| 455 |
assert_eq!(-2, fb.signed_two_bits()); |
| 456 |
assert_eq!(2, fb.signed_eight_bits()); |
| 457 |
assert_eq!(1, fb.signed_eight_bits_unaligned()); |
| 458 |
|
| 459 |
fb.set_signed_two_bits(-1); |
| 460 |
assert_eq!(0b11, fb.0); |
| 461 |
assert_eq!(-1, fb.signed_two_bits()); |
| 462 |
assert_eq!(3, fb.signed_eight_bits()); |
| 463 |
assert_eq!(1, fb.signed_eight_bits_unaligned()); |
| 464 |
|
| 465 |
fb.set_signed_two_bits(0); |
| 466 |
assert_eq!(0, fb.0); |
| 467 |
assert_eq!(0, fb.signed_two_bits()); |
| 468 |
assert_eq!(0, fb.signed_eight_bits()); |
| 469 |
assert_eq!(0, fb.signed_eight_bits_unaligned()); |
| 470 |
|
| 471 |
fb.set_signed_two_bits(1); |
| 472 |
assert_eq!(1, fb.0); |
| 473 |
assert_eq!(1, fb.signed_two_bits()); |
| 474 |
assert_eq!(1, fb.signed_eight_bits()); |
| 475 |
assert_eq!(0, fb.signed_eight_bits_unaligned()); |
| 476 |
|
| 477 |
fb.set_signed_eight_bits(0); |
| 478 |
assert_eq!(0, fb.0); |
| 479 |
assert_eq!(0, fb.signed_two_bits()); |
| 480 |
assert_eq!(0, fb.signed_eight_bits()); |
| 481 |
assert_eq!(0, fb.signed_eight_bits_unaligned()); |
| 482 |
|
| 483 |
fb.set_signed_eight_bits(-1); |
| 484 |
assert_eq!(0xFF, fb.0); |
| 485 |
assert_eq!(-1, fb.signed_two_bits()); |
| 486 |
assert_eq!(-1, fb.signed_eight_bits()); |
| 487 |
assert_eq!(127, fb.signed_eight_bits_unaligned()); |
| 488 |
|
| 489 |
fb.set_signed_eight_bits(-128); |
| 490 |
assert_eq!(0x80, fb.0); |
| 491 |
assert_eq!(0, fb.signed_two_bits()); |
| 492 |
assert_eq!(-128, fb.signed_eight_bits()); |
| 493 |
assert_eq!(64, fb.signed_eight_bits_unaligned()); |
| 494 |
|
| 495 |
fb.set_signed_eight_bits(127); |
| 496 |
assert_eq!(0x7F, fb.0); |
| 497 |
assert_eq!(-1, fb.signed_two_bits()); |
| 498 |
assert_eq!(127, fb.signed_eight_bits()); |
| 499 |
assert_eq!(63, fb.signed_eight_bits_unaligned()); |
| 500 |
|
| 501 |
fb.set_signed_eight_bits_unaligned(0); |
| 502 |
assert_eq!(1, fb.0); |
| 503 |
assert_eq!(1, fb.signed_two_bits()); |
| 504 |
assert_eq!(1, fb.signed_eight_bits()); |
| 505 |
assert_eq!(0, fb.signed_eight_bits_unaligned()); |
| 506 |
|
| 507 |
fb.set_signed_eight_bits(0); |
| 508 |
fb.set_signed_eight_bits_unaligned(-1); |
| 509 |
assert_eq!(0x1FE, fb.0); |
| 510 |
assert_eq!(-2, fb.signed_two_bits()); |
| 511 |
assert_eq!(-2, fb.signed_eight_bits()); |
| 512 |
assert_eq!(-1, fb.signed_eight_bits_unaligned()); |
| 513 |
|
| 514 |
fb.set_signed_eight_bits_unaligned(-128); |
| 515 |
assert_eq!(0x100, fb.0); |
| 516 |
assert_eq!(0, fb.signed_two_bits()); |
| 517 |
assert_eq!(0, fb.signed_eight_bits()); |
| 518 |
assert_eq!(-128, fb.signed_eight_bits_unaligned()); |
| 519 |
fb.set_signed_eight_bits_unaligned(127); |
| 520 |
assert_eq!(0xFE, fb.0); |
| 521 |
assert_eq!(-2, fb.signed_two_bits()); |
| 522 |
assert_eq!(-2, fb.signed_eight_bits()); |
| 523 |
assert_eq!(127, fb.signed_eight_bits_unaligned()); |
| 524 |
} |
| 525 |
|
| 526 |
#[test] |
| 527 |
fn test_field_type() { |
| 528 |
let fb = FooBar(0); |
| 529 |
let _: u32 = fb.foo1(); |
| 530 |
let _: u8 = fb.foo2(); |
| 531 |
let _: u8 = fb.foo3(); |
| 532 |
let _: u16 = fb.foo4(); |
| 533 |
let _: u8 = fb.foo5(0); |
| 534 |
let _: u32 = fb.foo6(0); |
| 535 |
|
| 536 |
let _: bool = fb.foo7(); |
| 537 |
let _: u8 = fb.foo8(); |
| 538 |
let _: u32 = fb.foo9(); |
| 539 |
let _: u16 = fb.foo10(); |
| 540 |
let _: u32 = fb.foo11(); |
| 541 |
let _: u16 = fb.foo12(); |
| 542 |
|
| 543 |
let _: Foo = fb.into_foo1(); |
| 544 |
let _: Foo = fb.into_foo2(); |
| 545 |
let _: Foo = fb.into_foo3(); |
| 546 |
let _: Foo = fb.into_foo4(); |
| 547 |
let _: Foo = fb.into_foo6(0); |
| 548 |
|
| 549 |
let _: Foo = fb.from_foo1(); |
| 550 |
let _: Foo = fb.from_foo3(); |
| 551 |
let _: Foo = fb.from_foo5(0); |
| 552 |
|
| 553 |
let _: i8 = fb.signed_single_bit(); |
| 554 |
let _: i8 = fb.signed_two_bits(); |
| 555 |
let _: i8 = fb.signed_eight_bits(); |
| 556 |
let _: i8 = fb.signed_eight_bits_unaligned(); |
| 557 |
|
| 558 |
let _: u128 = fb.u128_getter(); |
| 559 |
let _: i128 = fb.i128_getter(); |
| 560 |
} |
| 561 |
|
| 562 |
#[test] |
| 563 |
fn test_into_setter() { |
| 564 |
let mut fb = FooBar(0); |
| 565 |
|
| 566 |
// We just check that the parameter type is correct |
| 567 |
fb.set_into_foo1(0u8); |
| 568 |
fb.set_into_foo2(0u8); |
| 569 |
fb.set_into_foo3(0u8); |
| 570 |
fb.set_into_foo4(0u8); |
| 571 |
} |
| 572 |
|
| 573 |
#[test] |
| 574 |
fn test_from_setter() { |
| 575 |
let mut fb = FooBar(0); |
| 576 |
assert_eq!(0, fb.0); |
| 577 |
|
| 578 |
fb.set_from_foo1(Foo(1)); |
| 579 |
assert_eq!(1 << 31, fb.0); |
| 580 |
fb.set_from_foo1(Foo(0)); |
| 581 |
assert_eq!(0, fb.0); |
| 582 |
|
| 583 |
fb.set_from_foo2(Foo(1)); |
| 584 |
assert_eq!(1 << 31, fb.0); |
| 585 |
fb.set_from_foo2(Foo(0)); |
| 586 |
assert_eq!(0, fb.0); |
| 587 |
|
| 588 |
fb.set_from_foo3(Foo(1)); |
| 589 |
assert_eq!(1 << 31, fb.0); |
| 590 |
fb.set_from_foo3(Foo(0)); |
| 591 |
assert_eq!(0, fb.0); |
| 592 |
|
| 593 |
fb.set_from_foo4(Foo(1)); |
| 594 |
assert_eq!(1 << 31, fb.0); |
| 595 |
fb.set_from_foo4(Foo(0)); |
| 596 |
assert_eq!(0, fb.0); |
| 597 |
|
| 598 |
fb.set_from_foo5(1, Foo(1)); |
| 599 |
assert_eq!(1 << 30, fb.0); |
| 600 |
} |
| 601 |
|
| 602 |
#[test] |
| 603 |
fn test_all_bits() { |
| 604 |
let mut fb = FooBar(0); |
| 605 |
|
| 606 |
assert_eq!(0, fb.all_bits()); |
| 607 |
|
| 608 |
fb.set_all_bits(!0u32); |
| 609 |
assert_eq!(!0u32, fb.0); |
| 610 |
assert_eq!(!0u32, fb.all_bits()); |
| 611 |
|
| 612 |
fb.0 = 0x8000_0001; |
| 613 |
assert_eq!(0x8000_0001, fb.all_bits()); |
| 614 |
} |
| 615 |
|
| 616 |
#[test] |
| 617 |
fn test_is_copy() { |
| 618 |
let a = FooBar(0); |
| 619 |
let _b = a; |
| 620 |
let _c = a; |
| 621 |
} |
| 622 |
|
| 623 |
#[test] |
| 624 |
fn test_debug() { |
| 625 |
let fb = FooBar(1_234_567_890); |
| 626 |
let expected = "FooBar { .0: 1234567890, foo1: 0, foo2: 0, foo3: 2, foo3: 2, foo4: 4, foo5: [0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0], foo6: [2, 3, 1], getter_only: 1, pub_getter_only: 1, getter_only_array: [2, 3, 1], all_bits: 1234567890, single_bit: false, into_foo1: Foo(0), into_foo2: Foo(0), from_foo1: Foo(0), into_foo3: Foo(0), into_foo4: Foo(0), into_foo6: [Foo(0), Foo(1), Foo(0)], from_foo3: Foo(0), from_foo5: [Foo(0), Foo(1), Foo(0)], from_foo6: Foo(0), from_foo7: Err(()), from_foo8: Err(()), from_foo9: [Ok(EvenU8(0)), Err(()), Ok(EvenU8(0))], signed_single_bit: 0, signed_two_bits: -2, signed_eight_bits: -46, signed_eight_bits_unaligned: 105, u128_getter: 105, i128_getter: 105, bool_array_getter: [false, true, false] }"; |
| 627 |
assert_eq!(expected, format!("{:?}", fb)) |
| 628 |
} |
| 629 |
|
| 630 |
bitfield! { |
| 631 |
#[derive(Clone, Copy)] |
| 632 |
struct ArrayBitfield([u8]); |
| 633 |
impl BitAnd; |
| 634 |
impl BitOr; |
| 635 |
impl BitXor; |
| 636 |
impl Debug; |
| 637 |
impl new; |
| 638 |
u32; |
| 639 |
foo1, set_foo1: 0, 0; |
| 640 |
foo2, set_foo2: 7, 0; |
| 641 |
foo3, set_foo3: 8, 1; |
| 642 |
foo4, set_foo4: 19, 4; |
| 643 |
i32; |
| 644 |
signed_foo1, set_signed_foo1: 0, 0; |
| 645 |
signed_foo2, set_signed_foo2: 7, 0; |
| 646 |
signed_foo3, set_signed_foo3: 8, 1; |
| 647 |
signed_foo4, set_signed_foo4: 19, 4; |
| 648 |
u128, u128_getter, set_u128: 19, 4; |
| 649 |
u8, from into Foo, into_from_foo1, set_into_from_foo1: 21, 20; |
| 650 |
u8, into Foo, into_foo2, set_into_foo2: 23, 22; |
| 651 |
u8, from Foo, from_foo3, set_from_foo3: 25, 24; |
| 652 |
} |
| 653 |
|
| 654 |
impl ArrayBitfield<[u8; 3]> { |
| 655 |
bitfield_fields! { |
| 656 |
u8, lsb_msb_inverted, set_lsb_msb_inverted: 3, 4; |
| 657 |
u8, lsb_msb_inverted_array, set_lsb_msb_inverted_array: 4, 5, 2; |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
#[test] |
| 662 |
fn test_arraybitfield() { |
| 663 |
let mut ab = ArrayBitfield([0; 3]); |
| 664 |
|
| 665 |
assert_eq!(0u32, ab.foo1()); |
| 666 |
assert_eq!(0u32, ab.foo2()); |
| 667 |
assert_eq!(0u32, ab.foo3()); |
| 668 |
assert_eq!(0u32, ab.foo4()); |
| 669 |
assert_eq!(0i32, ab.signed_foo1()); |
| 670 |
assert_eq!(0i32, ab.signed_foo2()); |
| 671 |
assert_eq!(0i32, ab.signed_foo3()); |
| 672 |
assert_eq!(0i32, ab.signed_foo4()); |
| 673 |
assert_eq!(0u128, ab.u128_getter()); |
| 674 |
|
| 675 |
ab.set_foo1(1); |
| 676 |
assert_eq!([1, 0, 0], ab.0); |
| 677 |
assert_eq!(1, ab.foo1()); |
| 678 |
assert_eq!(1, ab.foo2()); |
| 679 |
assert_eq!(0, ab.foo3()); |
| 680 |
assert_eq!(0, ab.foo4()); |
| 681 |
assert_eq!(-1, ab.signed_foo1()); |
| 682 |
assert_eq!(1, ab.signed_foo2()); |
| 683 |
assert_eq!(0, ab.signed_foo3()); |
| 684 |
assert_eq!(0, ab.signed_foo4()); |
| 685 |
assert_eq!(0, ab.u128_getter()); |
| 686 |
|
| 687 |
ab.set_foo1(0); |
| 688 |
ab.set_foo2(0xFF); |
| 689 |
assert_eq!([0xFF, 0, 0], ab.0); |
| 690 |
assert_eq!(1, ab.foo1()); |
| 691 |
assert_eq!(0xFF, ab.foo2()); |
| 692 |
assert_eq!(0x7F, ab.foo3()); |
| 693 |
assert_eq!(0x0F, ab.foo4()); |
| 694 |
assert_eq!(-1, ab.signed_foo1()); |
| 695 |
assert_eq!(-1, ab.signed_foo2()); |
| 696 |
assert_eq!(127, ab.signed_foo3()); |
| 697 |
assert_eq!(0x0F, ab.signed_foo4()); |
| 698 |
assert_eq!(0x0F, ab.u128_getter()); |
| 699 |
|
| 700 |
ab.set_foo2(0); |
| 701 |
ab.set_foo3(0xFF); |
| 702 |
assert_eq!([0xFE, 0x01, 0], ab.0); |
| 703 |
assert_eq!(0, ab.foo1()); |
| 704 |
assert_eq!(0xFE, ab.foo2()); |
| 705 |
assert_eq!(0xFF, ab.foo3()); |
| 706 |
assert_eq!(0x1F, ab.foo4()); |
| 707 |
assert_eq!(0, ab.signed_foo1()); |
| 708 |
assert_eq!(-2, ab.signed_foo2()); |
| 709 |
assert_eq!(-1, ab.signed_foo3()); |
| 710 |
assert_eq!(0x1F, ab.signed_foo4()); |
| 711 |
assert_eq!(0x1F, ab.u128_getter()); |
| 712 |
|
| 713 |
ab.set_foo3(0); |
| 714 |
ab.set_foo4(0xFFFF); |
| 715 |
assert_eq!([0xF0, 0xFF, 0x0F], ab.0); |
| 716 |
assert_eq!(0, ab.foo1()); |
| 717 |
assert_eq!(0xF0, ab.foo2()); |
| 718 |
assert_eq!(0xF8, ab.foo3()); |
| 719 |
assert_eq!(0xFFFF, ab.foo4()); |
| 720 |
assert_eq!(0, ab.signed_foo1()); |
| 721 |
assert_eq!(-16, ab.signed_foo2()); |
| 722 |
assert_eq!(-8, ab.signed_foo3()); |
| 723 |
assert_eq!(-1, ab.signed_foo4()); |
| 724 |
assert_eq!(0xFFFF, ab.u128_getter()); |
| 725 |
|
| 726 |
ab.set_foo4(0x0); |
| 727 |
ab.set_signed_foo1(0); |
| 728 |
assert_eq!([0x00, 0x00, 0x00], ab.0); |
| 729 |
|
| 730 |
ab.set_signed_foo1(-1); |
| 731 |
assert_eq!([0x01, 0x00, 0x00], ab.0); |
| 732 |
|
| 733 |
ab.set_signed_foo1(0); |
| 734 |
ab.set_signed_foo2(127); |
| 735 |
assert_eq!([0x7F, 0x00, 0x00], ab.0); |
| 736 |
|
| 737 |
ab.set_signed_foo2(-128); |
| 738 |
assert_eq!([0x80, 0x00, 0x00], ab.0); |
| 739 |
|
| 740 |
ab.set_signed_foo2(1); |
| 741 |
assert_eq!([0x01, 0x00, 0x00], ab.0); |
| 742 |
|
| 743 |
ab.set_signed_foo2(-1); |
| 744 |
assert_eq!([0xFF, 0x00, 0x00], ab.0); |
| 745 |
|
| 746 |
ab.set_signed_foo2(0); |
| 747 |
ab.set_signed_foo3(127); |
| 748 |
assert_eq!([0xFE, 0x00, 0x00], ab.0); |
| 749 |
|
| 750 |
ab.set_signed_foo3(-1); |
| 751 |
assert_eq!([0xFE, 0x01, 0x00], ab.0); |
| 752 |
|
| 753 |
ab.set_signed_foo3(0); |
| 754 |
ab.set_signed_foo4(-1); |
| 755 |
assert_eq!([0xF0, 0xFF, 0x0F], ab.0); |
| 756 |
|
| 757 |
ab.set_signed_foo4(0); |
| 758 |
ab.set_u128(0xFFFF); |
| 759 |
assert_eq!([0xF0, 0xFF, 0x0F], ab.0); |
| 760 |
} |
| 761 |
|
| 762 |
#[test] |
| 763 |
#[should_panic(expected = "the MSB (3) is smaller than the LSB (4), you likely inverted them")] |
| 764 |
fn lsb_msb_inverted_should_panic_on_access_with_array_bitfield_1() { |
| 765 |
let ab = ArrayBitfield([0; 3]); |
| 766 |
ab.lsb_msb_inverted(); |
| 767 |
} |
| 768 |
|
| 769 |
#[test] |
| 770 |
#[should_panic(expected = "the MSB (3) is smaller than the LSB (4), you likely inverted them")] |
| 771 |
fn lsb_msb_inverted_should_panic_on_access_with_array_bitfield_2() { |
| 772 |
let mut ab = ArrayBitfield([0; 3]); |
| 773 |
ab.set_lsb_msb_inverted(1); |
| 774 |
} |
| 775 |
|
| 776 |
#[test] |
| 777 |
#[should_panic(expected = "the MSB (4) is smaller than the LSB (5), you likely inverted them")] |
| 778 |
fn lsb_msb_inverted_should_panic_on_access_with_array_bitfield_3() { |
| 779 |
let ab = ArrayBitfield([0; 3]); |
| 780 |
ab.lsb_msb_inverted_array(0); |
| 781 |
} |
| 782 |
|
| 783 |
#[test] |
| 784 |
#[should_panic(expected = "the MSB (4) is smaller than the LSB (5), you likely inverted them")] |
| 785 |
fn lsb_msb_inverted_should_panic_on_access_with_array_bitfield_4() { |
| 786 |
let mut ab = ArrayBitfield([0; 3]); |
| 787 |
ab.set_lsb_msb_inverted_array(1, 1); |
| 788 |
} |
| 789 |
|
| 790 |
#[test] |
| 791 |
fn test_arraybitfield2() { |
| 792 |
// Check that the macro can be called from a function. |
| 793 |
bitfield! { |
| 794 |
struct ArrayBitfield2([u16]); |
| 795 |
impl Debug; |
| 796 |
u32; |
| 797 |
foo1, set_foo1: 0, 0; |
| 798 |
foo2, set_foo2: 7, 0; |
| 799 |
foo3, set_foo3: 8, 1; |
| 800 |
foo4, set_foo4: 20, 4; |
| 801 |
bool, bool_array_getter, bool_array_setter: 0, 0, 3; |
| 802 |
} |
| 803 |
let mut ab = ArrayBitfield2([0; 2]); |
| 804 |
|
| 805 |
assert_eq!(0, ab.foo1()); |
| 806 |
assert_eq!(0, ab.foo2()); |
| 807 |
assert_eq!(0, ab.foo3()); |
| 808 |
assert_eq!(0, ab.foo4()); |
| 809 |
|
| 810 |
ab.set_foo1(1); |
| 811 |
assert_eq!([1, 0], ab.0); |
| 812 |
assert_eq!(1, ab.foo1()); |
| 813 |
assert_eq!(1, ab.foo2()); |
| 814 |
assert_eq!(0, ab.foo3()); |
| 815 |
assert_eq!(0, ab.foo4()); |
| 816 |
|
| 817 |
ab.set_foo1(0); |
| 818 |
ab.set_foo2(0xFF); |
| 819 |
assert_eq!([0xFF, 0], ab.0); |
| 820 |
assert_eq!(1, ab.foo1()); |
| 821 |
assert_eq!(0xFF, ab.foo2()); |
| 822 |
assert_eq!(0x7F, ab.foo3()); |
| 823 |
assert_eq!(0x0F, ab.foo4()); |
| 824 |
|
| 825 |
ab.set_foo2(0); |
| 826 |
ab.set_foo3(0xFF); |
| 827 |
assert_eq!([0x1FE, 0x0], ab.0); |
| 828 |
assert_eq!(0, ab.foo1()); |
| 829 |
assert_eq!(0xFE, ab.foo2()); |
| 830 |
assert_eq!(0xFF, ab.foo3()); |
| 831 |
assert_eq!(0x1F, ab.foo4()); |
| 832 |
|
| 833 |
ab.set_foo3(0); |
| 834 |
ab.set_foo4(0xFFFF); |
| 835 |
assert_eq!([0xFFF0, 0xF], ab.0); |
| 836 |
assert_eq!(0, ab.foo1()); |
| 837 |
assert_eq!(0xF0, ab.foo2()); |
| 838 |
assert_eq!(0xF8, ab.foo3()); |
| 839 |
assert_eq!(0xFFFF, ab.foo4()); |
| 840 |
} |
| 841 |
|
| 842 |
bitfield! { |
| 843 |
pub(self) struct ArrayBitfieldMsb0(MSB0 [u8]); |
| 844 |
impl Debug; |
| 845 |
u32; |
| 846 |
foo1, set_foo1: 0, 0; |
| 847 |
foo2, set_foo2: 7, 0; |
| 848 |
foo3, set_foo3: 8, 1; |
| 849 |
foo4, set_foo4: 19, 4; |
| 850 |
i32; |
| 851 |
signed_foo1, set_signed_foo1: 0, 0; |
| 852 |
signed_foo2, set_signed_foo2: 7, 0; |
| 853 |
signed_foo3, set_signed_foo3: 8, 1; |
| 854 |
signed_foo4, set_signed_foo4: 19, 4; |
| 855 |
} |
| 856 |
|
| 857 |
impl ArrayBitfieldMsb0<[u8; 3]> { |
| 858 |
bitfield_fields! { |
| 859 |
u8, lsb_msb_inverted, set_lsb_msb_inverted: 3, 4; |
| 860 |
u8, lsb_msb_inverted_array, set_lsb_msb_inverted_array: 4, 5, 2; |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
#[test] |
| 865 |
fn test_arraybitfield_msb0() { |
| 866 |
let mut ab = ArrayBitfieldMsb0([0; 3]); |
| 867 |
|
| 868 |
assert_eq!(0, ab.foo1()); |
| 869 |
assert_eq!(0, ab.foo2()); |
| 870 |
assert_eq!(0, ab.foo3()); |
| 871 |
assert_eq!(0, ab.foo4()); |
| 872 |
assert_eq!(0, ab.signed_foo1()); |
| 873 |
assert_eq!(0, ab.signed_foo2()); |
| 874 |
assert_eq!(0, ab.signed_foo3()); |
| 875 |
assert_eq!(0, ab.signed_foo4()); |
| 876 |
|
| 877 |
ab.set_foo1(1); |
| 878 |
assert_eq!([0b1000_0000, 0, 0], ab.0); |
| 879 |
assert_eq!(1, ab.foo1()); |
| 880 |
assert_eq!(0b1000_0000, ab.foo2()); |
| 881 |
assert_eq!(0, ab.foo3()); |
| 882 |
assert_eq!(0, ab.foo4()); |
| 883 |
assert_eq!(-1, ab.signed_foo1()); |
| 884 |
assert_eq!(-128, ab.signed_foo2()); |
| 885 |
assert_eq!(0, ab.signed_foo3()); |
| 886 |
assert_eq!(0, ab.signed_foo4()); |
| 887 |
|
| 888 |
ab.set_foo1(0); |
| 889 |
ab.set_foo2(0xFF); |
| 890 |
assert_eq!([0b1111_1111, 0, 0], ab.0); |
| 891 |
assert_eq!(1, ab.foo1()); |
| 892 |
assert_eq!(0b1111_1111, ab.foo2()); |
| 893 |
assert_eq!(0b1111_1110, ab.foo3()); |
| 894 |
assert_eq!(0b1111_0000_0000_0000, ab.foo4()); |
| 895 |
assert_eq!(-1, ab.signed_foo1()); |
| 896 |
assert_eq!(-1, ab.signed_foo2()); |
| 897 |
assert_eq!(-2, ab.signed_foo3()); |
| 898 |
assert_eq!(-4096, ab.signed_foo4()); |
| 899 |
|
| 900 |
ab.set_foo2(0); |
| 901 |
ab.set_foo3(0xFF); |
| 902 |
assert_eq!([0b0111_1111, 0b1000_0000, 0], ab.0); |
| 903 |
assert_eq!(0, ab.foo1()); |
| 904 |
assert_eq!(0b0111_1111, ab.foo2()); |
| 905 |
assert_eq!(0xFF, ab.foo3()); |
| 906 |
assert_eq!(0b1111_1000_0000_0000, ab.foo4()); |
| 907 |
assert_eq!(0, ab.signed_foo1()); |
| 908 |
assert_eq!(127, ab.signed_foo2()); |
| 909 |
assert_eq!(-1, ab.signed_foo3()); |
| 910 |
assert_eq!(-2048, ab.signed_foo4()); |
| 911 |
|
| 912 |
ab.set_foo3(0); |
| 913 |
ab.set_foo4(0xFFFF); |
| 914 |
assert_eq!([0x0F, 0xFF, 0xF0], ab.0); |
| 915 |
assert_eq!(0, ab.foo1()); |
| 916 |
assert_eq!(0x0F, ab.foo2()); |
| 917 |
assert_eq!(0b0001_1111, ab.foo3()); |
| 918 |
assert_eq!(0xFFFF, ab.foo4()); |
| 919 |
assert_eq!(0, ab.signed_foo1()); |
| 920 |
assert_eq!(0x0F, ab.signed_foo2()); |
| 921 |
assert_eq!(0b0001_1111, ab.signed_foo3()); |
| 922 |
assert_eq!(-1, ab.signed_foo4()); |
| 923 |
|
| 924 |
ab.set_foo4(0x0); |
| 925 |
ab.set_signed_foo1(0); |
| 926 |
assert_eq!([0x00, 0x00, 0x00], ab.0); |
| 927 |
|
| 928 |
ab.set_signed_foo1(-1); |
| 929 |
assert_eq!([0b1000_0000, 0x00, 0x00], ab.0); |
| 930 |
|
| 931 |
ab.set_signed_foo1(0); |
| 932 |
ab.set_signed_foo2(127); |
| 933 |
assert_eq!([0x7F, 0x00, 0x00], ab.0); |
| 934 |
|
| 935 |
ab.set_signed_foo2(-128); |
| 936 |
assert_eq!([0x80, 0x00, 0x00], ab.0); |
| 937 |
|
| 938 |
ab.set_signed_foo2(1); |
| 939 |
assert_eq!([0x01, 0x00, 0x00], ab.0); |
| 940 |
|
| 941 |
ab.set_signed_foo2(-1); |
| 942 |
assert_eq!([0xFF, 0x00, 0x00], ab.0); |
| 943 |
|
| 944 |
ab.set_signed_foo2(0); |
| 945 |
ab.set_signed_foo3(127); |
| 946 |
assert_eq!([0b0011_1111, 0b1000_0000, 0], ab.0); |
| 947 |
|
| 948 |
ab.set_signed_foo3(-1); |
| 949 |
assert_eq!([0b0111_1111, 0b1000_0000, 0], ab.0); |
| 950 |
|
| 951 |
ab.set_signed_foo3(0); |
| 952 |
ab.set_signed_foo4(-1); |
| 953 |
assert_eq!([0x0F, 0xFF, 0xF0], ab.0); |
| 954 |
} |
| 955 |
|
| 956 |
#[test] |
| 957 |
#[should_panic(expected = "the MSB (3) is smaller than the LSB (4), you likely inverted them")] |
| 958 |
fn lsb_msb_inverted_should_panic_on_access_with_array_msb0_bitfield_1() { |
| 959 |
let ab = ArrayBitfieldMsb0([0; 3]); |
| 960 |
ab.lsb_msb_inverted(); |
| 961 |
} |
| 962 |
|
| 963 |
#[test] |
| 964 |
#[should_panic(expected = "the MSB (3) is smaller than the LSB (4), you likely inverted them")] |
| 965 |
fn lsb_msb_inverted_should_panic_on_access_with_array_msb0_bitfield_2() { |
| 966 |
let mut ab = ArrayBitfieldMsb0([0; 3]); |
| 967 |
ab.set_lsb_msb_inverted(1); |
| 968 |
} |
| 969 |
|
| 970 |
#[test] |
| 971 |
#[should_panic(expected = "the MSB (4) is smaller than the LSB (5), you likely inverted them")] |
| 972 |
fn lsb_msb_inverted_should_panic_on_access_with_array_msb0_bitfield_3() { |
| 973 |
let ab = ArrayBitfieldMsb0([0; 3]); |
| 974 |
ab.lsb_msb_inverted_array(0); |
| 975 |
} |
| 976 |
|
| 977 |
#[test] |
| 978 |
#[should_panic(expected = "the MSB (4) is smaller than the LSB (5), you likely inverted them")] |
| 979 |
fn lsb_msb_inverted_should_panic_on_access_with_array_msb0_bitfield_4() { |
| 980 |
let mut ab = ArrayBitfieldMsb0([0; 3]); |
| 981 |
ab.set_lsb_msb_inverted_array(1, 1); |
| 982 |
} |
| 983 |
|
| 984 |
#[test] |
| 985 |
fn test_arraybitfield_bitops() { |
| 986 |
let mut a = ArrayBitfield([1u8; 3]); |
| 987 |
let b = ArrayBitfield([1u8, 2u8, 4u8]); |
| 988 |
|
| 989 |
let c = a | b; |
| 990 |
assert_eq!(c.0, [1, 3, 5]); |
| 991 |
|
| 992 |
let d = a & b; |
| 993 |
assert_eq!(d.0, [1, 0, 0]); |
| 994 |
|
| 995 |
let e = a ^ b; |
| 996 |
assert_eq!(e.0, [0, 3, 5]); |
| 997 |
|
| 998 |
a ^= b; |
| 999 |
assert_eq!(a.0, [0, 3, 5]); |
| 1000 |
|
| 1001 |
let mut vec_a = ArrayBitfield(vec![1u8; 3]); |
| 1002 |
let vec_b = ArrayBitfield(vec![1u8, 2u8, 4u8]); |
| 1003 |
|
| 1004 |
let vec_c = vec_a.clone() | vec_b.clone(); |
| 1005 |
assert_eq!(vec_c.0, [1, 3, 5]); |
| 1006 |
|
| 1007 |
let vec_d = vec_a.clone() & vec_b.clone(); |
| 1008 |
assert_eq!(vec_d.0, [1, 0, 0]); |
| 1009 |
|
| 1010 |
let vec_e = vec_a.clone() ^ vec_b.clone(); |
| 1011 |
assert_eq!(vec_e.0, [0, 3, 5]); |
| 1012 |
|
| 1013 |
vec_a ^= vec_b; |
| 1014 |
assert_eq!(vec_a.0, [0, 3, 5]); |
| 1015 |
} |
| 1016 |
|
| 1017 |
#[test] |
| 1018 |
fn test_arraybitfield_constructor() { |
| 1019 |
let a: ArrayBitfield<[u8; 4]> = |
| 1020 |
ArrayBitfield::new(1, 2, 3, 4, -1, -2, -3, -4, 0b0001_0000, Foo(1), 2u8, Foo(3)); |
| 1021 |
println!("{:b}", a.0[0]); |
| 1022 |
assert_eq!(a.foo1(), 0); |
| 1023 |
assert_eq!(a.foo2(), 10); |
| 1024 |
assert_eq!(a.foo3(), 133); |
| 1025 |
assert_eq!(a.foo4(), 16); |
| 1026 |
assert_eq!(a.into_from_foo1(), Foo(1)); |
| 1027 |
assert_eq!(a.into_foo2(), Foo(2)); |
| 1028 |
assert_eq!(a.from_foo3(), 3); |
| 1029 |
} |
| 1030 |
|
| 1031 |
mod some_module { |
| 1032 |
use bitfield::bitfield; |
| 1033 |
bitfield! { |
| 1034 |
pub(super) struct PubBitFieldInAModule(u32); |
| 1035 |
impl Debug; |
| 1036 |
/// Attribute works on pub fields |
| 1037 |
pub field1, set_field1: 1; |
| 1038 |
pub field2, _: 1; |
| 1039 |
pub _, set_field3: 1; |
| 1040 |
pub u16, field4, set_field4: 1; |
| 1041 |
/// Check if multiple attributes are applied |
| 1042 |
#[cfg(not(test))] |
| 1043 |
pub u16, field4, set_field4: 1; |
| 1044 |
pub u16, _, set_field5: 1; |
| 1045 |
pub u16, field6, _: 1; |
| 1046 |
pub field7, set_field7: 1; |
| 1047 |
pub field8, set_field8: 1, 1; |
| 1048 |
#[cfg(not(test))] |
| 1049 |
/// And make sure not only the last attributes is applied |
| 1050 |
pub field8, set_field8: 1, 1; |
| 1051 |
pub field9, set_field9: 1, 1, 1; |
| 1052 |
pub u32, field10, set_field10: 1; |
| 1053 |
pub u32, field11, set_field11: 1, 1; |
| 1054 |
pub u32, field12, set_field12: 1, 1, 1; |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
#[test] |
| 1059 |
fn struct_can_be_public() { |
| 1060 |
let _ = some_module::PubBitFieldInAModule(0); |
| 1061 |
} |
| 1062 |
#[test] |
| 1063 |
fn field_can_be_public() { |
| 1064 |
let mut a = some_module::PubBitFieldInAModule(0); |
| 1065 |
let _ = a.field1(); |
| 1066 |
a.set_field1(true); |
| 1067 |
let _ = a.field2(); |
| 1068 |
a.set_field3(true); |
| 1069 |
let _ = a.field4(); |
| 1070 |
a.set_field4(true); |
| 1071 |
a.set_field5(true); |
| 1072 |
let _ = a.field6(); |
| 1073 |
let _ = a.field7(); |
| 1074 |
a.set_field7(true); |
| 1075 |
let _ = a.field8(); |
| 1076 |
a.set_field8(0); |
| 1077 |
let _ = a.field9(0); |
| 1078 |
a.set_field9(0, 0); |
| 1079 |
let _ = a.field10(); |
| 1080 |
a.set_field10(true); |
| 1081 |
let _ = a.field11(); |
| 1082 |
a.set_field11(0); |
| 1083 |
let _ = a.field12(0); |
| 1084 |
a.set_field12(0, 0); |
| 1085 |
} |
| 1086 |
|
| 1087 |
// Everything in this module is to make sure that its possible to specify types |
| 1088 |
// in most of the possible ways. |
| 1089 |
#[allow(dead_code)] |
| 1090 |
mod test_types { |
| 1091 |
use bitfield::{bitfield_fields, BitRange, BitRangeMut}; |
| 1092 |
use std::sync::atomic::{self, AtomicUsize}; |
| 1093 |
|
| 1094 |
struct Foo; |
| 1095 |
|
| 1096 |
impl Foo { |
| 1097 |
bitfield_fields! { |
| 1098 |
std::sync::atomic::AtomicUsize, field1, set_field1: 0, 0; |
| 1099 |
std::sync::atomic::AtomicUsize; |
| 1100 |
field2, set_field2: 0, 0; |
| 1101 |
::std::sync::atomic::AtomicUsize, field3, set_field3: 0, 0; |
| 1102 |
::std::sync::atomic::AtomicUsize; |
| 1103 |
field4, set_field4: 0, 0; |
| 1104 |
atomic::AtomicUsize, field5, set_field5: 0, 0; |
| 1105 |
atomic::AtomicUsize; |
| 1106 |
field6, set_field6: 0, 0; |
| 1107 |
AtomicUsize, field7, set_field7: 0, 0; |
| 1108 |
AtomicUsize; |
| 1109 |
field8, set_field8: 0, 0; |
| 1110 |
Vec<std::sync::atomic::AtomicUsize>, field9, set_field9: 0, 0; |
| 1111 |
Vec<std::sync::atomic::AtomicUsize>; |
| 1112 |
field10, set_field10: 0, 0; |
| 1113 |
Vec<::std::sync::atomic::AtomicUsize>, field11, set_field11: 0, 0; |
| 1114 |
Vec<::std::sync::atomic::AtomicUsize>; |
| 1115 |
field12, set_field12: 0, 0; |
| 1116 |
Vec<atomic::AtomicUsize>, field13, set_field13: 0, 0; |
| 1117 |
Vec<atomic::AtomicUsize>; |
| 1118 |
field14, set_field14: 0, 0; |
| 1119 |
Vec<AtomicUsize>, field15, set_field15: 0, 0; |
| 1120 |
Vec<AtomicUsize>; |
| 1121 |
field16, set_field16: 0, 0; |
| 1122 |
&str, field17, set_field17: 0, 0; |
| 1123 |
&str; |
| 1124 |
field18, set_field18: 0, 0; |
| 1125 |
&'static str, field19, set_field19: 0, 0; |
| 1126 |
&'static str; |
| 1127 |
field20, set_field20: 0, 0; |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
impl BitRange<AtomicUsize> for Foo { |
| 1132 |
fn bit_range(&self, _msb: usize, _lsb: usize) -> AtomicUsize { |
| 1133 |
AtomicUsize::new(0) |
| 1134 |
} |
| 1135 |
} |
| 1136 |
|
| 1137 |
impl BitRangeMut<AtomicUsize> for Foo { |
| 1138 |
fn set_bit_range(&mut self, _msb: usize, _lsb: usize, _value: AtomicUsize) {} |
| 1139 |
} |
| 1140 |
|
| 1141 |
impl BitRange<Vec<AtomicUsize>> for Foo { |
| 1142 |
fn bit_range(&self, _msb: usize, _lsb: usize) -> Vec<AtomicUsize> { |
| 1143 |
vec![AtomicUsize::new(0)] |
| 1144 |
} |
| 1145 |
} |
| 1146 |
|
| 1147 |
impl BitRangeMut<Vec<AtomicUsize>> for Foo { |
| 1148 |
fn set_bit_range(&mut self, _msb: usize, _lsb: usize, _value: Vec<AtomicUsize>) {} |
| 1149 |
} |
| 1150 |
|
| 1151 |
impl<'a> BitRange<&'a str> for Foo { |
| 1152 |
fn bit_range(&self, _msb: usize, _lsb: usize) -> &'a str { |
| 1153 |
"" |
| 1154 |
} |
| 1155 |
} |
| 1156 |
|
| 1157 |
impl<'a> BitRangeMut<&'a str> for Foo { |
| 1158 |
fn set_bit_range(&mut self, _msb: usize, _lsb: usize, _value: &'a str) {} |
| 1159 |
} |
| 1160 |
|
| 1161 |
#[test] |
| 1162 |
fn test_field_type() { |
| 1163 |
let test = Foo; |
| 1164 |
let _: AtomicUsize = test.field1(); |
| 1165 |
let _: AtomicUsize = test.field2(); |
| 1166 |
let _: AtomicUsize = test.field3(); |
| 1167 |
let _: AtomicUsize = test.field4(); |
| 1168 |
let _: AtomicUsize = test.field5(); |
| 1169 |
let _: AtomicUsize = test.field6(); |
| 1170 |
let _: AtomicUsize = test.field7(); |
| 1171 |
let _: AtomicUsize = test.field8(); |
| 1172 |
let _: Vec<AtomicUsize> = test.field9(); |
| 1173 |
let _: Vec<AtomicUsize> = test.field10(); |
| 1174 |
let _: Vec<AtomicUsize> = test.field11(); |
| 1175 |
let _: Vec<AtomicUsize> = test.field12(); |
| 1176 |
let _: Vec<AtomicUsize> = test.field13(); |
| 1177 |
let _: Vec<AtomicUsize> = test.field14(); |
| 1178 |
let _: Vec<AtomicUsize> = test.field15(); |
| 1179 |
let _: Vec<AtomicUsize> = test.field16(); |
| 1180 |
let _: &str = test.field17(); |
| 1181 |
let _: &str = test.field18(); |
| 1182 |
let _: &'static str = test.field19(); |
| 1183 |
let _: &'static str = test.field20(); |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
#[allow(dead_code)] |
| 1188 |
mod test_no_default_bitrange { |
| 1189 |
use bitfield::{bitfield, BitRange, BitRangeMut}; |
| 1190 |
use std::fmt::Debug; |
| 1191 |
use std::fmt::Error; |
| 1192 |
use std::fmt::Formatter; |
| 1193 |
|
| 1194 |
use crate::FooBar; |
| 1195 |
bitfield! { |
| 1196 |
#[derive(Eq, PartialEq)] |
| 1197 |
pub struct BitField1(u16); |
| 1198 |
no default BitRange; |
| 1199 |
impl Debug; |
| 1200 |
u8; |
| 1201 |
field1, set_field1: 10, 0; |
| 1202 |
pub field2, _ : 12, 3; |
| 1203 |
field3, set_field3: 2; |
| 1204 |
} |
| 1205 |
|
| 1206 |
impl BitRange<u8> for BitField1 { |
| 1207 |
fn bit_range(&self, msb: usize, lsb: usize) -> u8 { |
| 1208 |
(msb + lsb) as u8 |
| 1209 |
} |
| 1210 |
} |
| 1211 |
|
| 1212 |
impl BitRangeMut<u8> for BitField1 { |
| 1213 |
fn set_bit_range(&mut self, msb: usize, lsb: usize, value: u8) { |
| 1214 |
self.0 = msb as u16 + lsb as u16 + u16::from(value) |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
#[allow(clippy::identity_op)] |
| 1219 |
#[test] |
| 1220 |
fn custom_bitrange_implementation_is_used() { |
| 1221 |
let mut bf = BitField1(0); |
| 1222 |
assert_eq!(bf.field1(), 10 + 0); |
| 1223 |
assert_eq!(bf.field2(), 12 + 3); |
| 1224 |
assert!(bf.field3()); |
| 1225 |
bf.set_field1(42); |
| 1226 |
assert_eq!(bf, BitField1(10 + 0 + 42)); |
| 1227 |
} |
| 1228 |
|
| 1229 |
bitfield! { |
| 1230 |
pub(crate) struct BitField2(u16); |
| 1231 |
no default BitRange; |
| 1232 |
u8; |
| 1233 |
field1, set_field1: 10, 0; |
| 1234 |
pub field2, _ : 12, 3; |
| 1235 |
field3, set_field3: 0; |
| 1236 |
} |
| 1237 |
|
| 1238 |
impl BitRange<u8> for BitField2 { |
| 1239 |
fn bit_range(&self, _msb: usize, _lsb: usize) -> u8 { |
| 1240 |
0 |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
impl BitRangeMut<u8> for BitField2 { |
| 1245 |
fn set_bit_range(&mut self, _msb: usize, _lsb: usize, _value: u8) {} |
| 1246 |
} |
| 1247 |
|
| 1248 |
// Make sure Debug wasn't implemented by implementing it. |
| 1249 |
impl Debug for BitField2 { |
| 1250 |
fn fmt(&self, _: &mut Formatter) -> Result<(), Error> { |
| 1251 |
unimplemented!() |
| 1252 |
} |
| 1253 |
} |
| 1254 |
|
| 1255 |
// Check that we can put `impl Debug` before `no default BitRange` |
| 1256 |
bitfield! { |
| 1257 |
pub struct BitField3(u16); |
| 1258 |
impl Debug; |
| 1259 |
no default BitRange; |
| 1260 |
u8; |
| 1261 |
field1, set_field1: 10, 0; |
| 1262 |
pub field2, _ : 12, 3; |
| 1263 |
field3, set_field3: 0; |
| 1264 |
} |
| 1265 |
|
| 1266 |
impl BitRange<u8> for BitField3 { |
| 1267 |
fn bit_range(&self, _msb: usize, _lsb: usize) -> u8 { |
| 1268 |
0 |
| 1269 |
} |
| 1270 |
} |
| 1271 |
|
| 1272 |
impl BitRangeMut<u8> for BitField3 { |
| 1273 |
fn set_bit_range(&mut self, _msb: usize, _lsb: usize, _value: u8) {} |
| 1274 |
} |
| 1275 |
|
| 1276 |
bitfield! { |
| 1277 |
#[derive(Eq, PartialEq)] |
| 1278 |
pub struct BitField4([u16]); |
| 1279 |
no default BitRange; |
| 1280 |
impl Debug; |
| 1281 |
u8; |
| 1282 |
field1, set_field1: 10, 0; |
| 1283 |
pub field2, _ : 12, 3; |
| 1284 |
field3, set_field3: 2; |
| 1285 |
} |
| 1286 |
|
| 1287 |
impl<T> BitRange<u8> for BitField4<T> { |
| 1288 |
fn bit_range(&self, _msb: usize, _lsb: usize) -> u8 { |
| 1289 |
0 |
| 1290 |
} |
| 1291 |
} |
| 1292 |
|
| 1293 |
impl<T> BitRangeMut<u8> for BitField4<T> { |
| 1294 |
fn set_bit_range(&mut self, _msb: usize, _lsb: usize, _value: u8) {} |
| 1295 |
} |
| 1296 |
|
| 1297 |
bitfield! { |
| 1298 |
pub struct BitField5([u16]); |
| 1299 |
no default BitRange; |
| 1300 |
u8; |
| 1301 |
field1, set_field1: 10, 0; |
| 1302 |
pub field2, _ : 12, 3; |
| 1303 |
field3, set_field3: 0; |
| 1304 |
} |
| 1305 |
|
| 1306 |
impl<T> BitRange<u8> for BitField5<T> { |
| 1307 |
fn bit_range(&self, _msb: usize, _lsb: usize) -> u8 { |
| 1308 |
0 |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
impl<T> BitRangeMut<u8> for BitField5<T> { |
| 1313 |
fn set_bit_range(&mut self, _msb: usize, _lsb: usize, _value: u8) {} |
| 1314 |
} |
| 1315 |
|
| 1316 |
// Make sure Debug wasn't implemented by implementing it. |
| 1317 |
impl<T> Debug for BitField5<T> { |
| 1318 |
fn fmt(&self, _: &mut Formatter) -> Result<(), Error> { |
| 1319 |
unimplemented!() |
| 1320 |
} |
| 1321 |
} |
| 1322 |
|
| 1323 |
// Check that we can put `impl Debug` before `no default BitRange` |
| 1324 |
bitfield! { |
| 1325 |
pub struct BitField6([u16]); |
| 1326 |
impl Debug; |
| 1327 |
no default BitRange; |
| 1328 |
u8; |
| 1329 |
field1, set_field1: 10, 0; |
| 1330 |
pub field2, _ : 12, 3; |
| 1331 |
field3, set_field3: 0; |
| 1332 |
} |
| 1333 |
|
| 1334 |
impl<T> BitRange<u8> for BitField6<T> { |
| 1335 |
fn bit_range(&self, _msb: usize, _lsb: usize) -> u8 { |
| 1336 |
0 |
| 1337 |
} |
| 1338 |
} |
| 1339 |
|
| 1340 |
impl<T> BitRangeMut<u8> for BitField6<T> { |
| 1341 |
fn set_bit_range(&mut self, _msb: usize, _lsb: usize, _value: u8) {} |
| 1342 |
} |
| 1343 |
|
| 1344 |
bitfield! { |
| 1345 |
#[derive(Eq, PartialEq)] |
| 1346 |
pub struct BitField7(MSB0 [u16]); |
| 1347 |
no default BitRange; |
| 1348 |
impl Debug; |
| 1349 |
u8; |
| 1350 |
field1, set_field1: 10, 0; |
| 1351 |
pub field2, _ : 12, 3; |
| 1352 |
field3, set_field3: 2; |
| 1353 |
} |
| 1354 |
|
| 1355 |
impl<T> BitRange<u8> for BitField7<T> { |
| 1356 |
fn bit_range(&self, _msb: usize, _lsb: usize) -> u8 { |
| 1357 |
0 |
| 1358 |
} |
| 1359 |
} |
| 1360 |
|
| 1361 |
impl<T> BitRangeMut<u8> for BitField7<T> { |
| 1362 |
fn set_bit_range(&mut self, _msb: usize, _lsb: usize, _value: u8) {} |
| 1363 |
} |
| 1364 |
|
| 1365 |
bitfield! { |
| 1366 |
pub struct BitField8(MSB0 [u16]); |
| 1367 |
no default BitRange; |
| 1368 |
u8; |
| 1369 |
field1, set_field1: 10, 0; |
| 1370 |
pub field2, _ : 12, 3; |
| 1371 |
field3, set_field3: 0; |
| 1372 |
} |
| 1373 |
|
| 1374 |
impl<T> BitRange<u8> for BitField8<T> { |
| 1375 |
fn bit_range(&self, _msb: usize, _lsb: usize) -> u8 { |
| 1376 |
0 |
| 1377 |
} |
| 1378 |
} |
| 1379 |
|
| 1380 |
impl<T> BitRangeMut<u8> for BitField8<T> { |
| 1381 |
fn set_bit_range(&mut self, _msb: usize, _lsb: usize, _value: u8) {} |
| 1382 |
} |
| 1383 |
|
| 1384 |
// Make sure Debug wasn't implemented by implementing it. |
| 1385 |
impl<T> Debug for BitField8<T> { |
| 1386 |
fn fmt(&self, _: &mut Formatter) -> Result<(), Error> { |
| 1387 |
unimplemented!() |
| 1388 |
} |
| 1389 |
} |
| 1390 |
|
| 1391 |
// Check that we can put `impl Debug` before `no default BitRange` |
| 1392 |
bitfield! { |
| 1393 |
pub struct BitField9([u16]); |
| 1394 |
impl Debug; |
| 1395 |
no default BitRange; |
| 1396 |
u8; |
| 1397 |
field1, set_field1: 10, 0; |
| 1398 |
pub field2, _ : 12, 3; |
| 1399 |
field3, set_field3: 0; |
| 1400 |
} |
| 1401 |
|
| 1402 |
impl<T> BitRange<u8> for BitField9<T> { |
| 1403 |
fn bit_range(&self, _msb: usize, _lsb: usize) -> u8 { |
| 1404 |
0 |
| 1405 |
} |
| 1406 |
} |
| 1407 |
|
| 1408 |
impl<T> BitRangeMut<u8> for BitField9<T> { |
| 1409 |
fn set_bit_range(&mut self, _msb: usize, _lsb: usize, _value: u8) {} |
| 1410 |
} |
| 1411 |
|
| 1412 |
#[test] |
| 1413 |
fn test_debug_is_implemented_with_no_default_bitrange() { |
| 1414 |
let _ = format!("{:?}", BitField1(0)); |
| 1415 |
let _ = format!("{:?}", BitField3(0)); |
| 1416 |
let _ = format!("{:?}", BitField4([0; 1])); |
| 1417 |
let _ = format!("{:?}", BitField6([0; 1])); |
| 1418 |
let _ = format!("{:?}", BitField7([0; 1])); |
| 1419 |
let _ = format!("{:?}", BitField9([0; 1])); |
| 1420 |
} |
| 1421 |
|
| 1422 |
#[test] |
| 1423 |
fn masks() { |
| 1424 |
assert_eq!(FooBar::I128_MASK, 0b111111110i128); |
| 1425 |
assert_eq!(FooBar::U128_MASK, 0b111111110u128); |
| 1426 |
assert_eq!(FooBar::SETTER_MASK, 0b11100u32); |
| 1427 |
assert_eq!(FooBar::GETTER_MASK, 0b1110u32); |
| 1428 |
assert_eq!(FooBar::PUB_SETTER_MASK, 0b11100u32); |
| 1429 |
assert_eq!(FooBar::PUB_GETTER_MASK, 0b1110u32); |
| 1430 |
assert_eq!(FooBar::SINGLE_BIT_MASK, 1 << 3); |
| 1431 |
assert_eq!(FooBar::PUB_MASK, 1 << 31); |
| 1432 |
} |
| 1433 |
} |
| 1434 |
|
| 1435 |
#[deny(missing_docs)] |
| 1436 |
/// A module to test that `impl new` works with `#[deny(missing_docs)]` |
| 1437 |
pub mod deny_missing_docs { |
| 1438 |
use bitfield::bitfield; |
| 1439 |
|
| 1440 |
bitfield! { |
| 1441 |
/// A doc comment for the struct |
| 1442 |
pub struct BitField10(u8); |
| 1443 |
|
| 1444 |
impl new; |
| 1445 |
|
| 1446 |
/// A doc comment for the methods |
| 1447 |
pub field1, set_field1: 0; |
| 1448 |
} |
| 1449 |
} |
| 1450 |
|
| 1451 |
bitfield! { |
| 1452 |
#[derive(Copy, Clone)] |
| 1453 |
/// documentation comments also work! |
| 1454 |
struct FooBarSigned(i32); |
| 1455 |
impl Debug; |
| 1456 |
impl BitOr; |
| 1457 |
foo1, set_foo1: 31, 8; |
| 1458 |
foo2, set_foo2: 10, 10, 2; |
| 1459 |
mask A_MASK(u32), foo3, set_foo3: 3, 1; |
| 1460 |
u8; |
| 1461 |
foo4, set_foo4: THREE, 0; |
| 1462 |
u32, foo5, set_foo5: 31,8; |
| 1463 |
} |
| 1464 |
|
| 1465 |
#[test] |
| 1466 |
fn field_type_signed() { |
| 1467 |
let fbs = FooBarSigned(0); |
| 1468 |
let _: i32 = fbs.foo1(); |
| 1469 |
let _: i32 = fbs.foo2(0); |
| 1470 |
let _: i32 = fbs.foo3(); |
| 1471 |
let _: u8 = fbs.foo4(); |
| 1472 |
let _: u32 = fbs.foo5(); |
| 1473 |
} |
| 1474 |
|
| 1475 |
#[test] |
| 1476 |
fn value_signed() { |
| 1477 |
let initial_value = 0b1101_0101_1010_1010_1010_1010_0110_0010u32 as i32; |
| 1478 |
let fbs = FooBarSigned(initial_value); |
| 1479 |
assert_eq!( |
| 1480 |
fbs.foo1(), |
| 1481 |
0b1111_1111_1101_0101_1010_1010_1010_1010u32 as i32 |
| 1482 |
); |
| 1483 |
assert_eq!(fbs.foo5(), 0b1101_0101_1010_1010_1010_1010u32); |
| 1484 |
} |
| 1485 |
|
| 1486 |
#[test] |
| 1487 |
fn mask_signed() { |
| 1488 |
assert_eq!(FooBarSigned::A_MASK, 0b1110u32); |
| 1489 |
} |