use easy_bitfield::*; type A = BitField; type B = BitField; type C = BitField; fn main() { let mut storage; storage = A::encode(true); assert!(A::decode(storage)); storage = B::update(-1, storage); assert_eq!(B::decode(storage), -1); storage = B::update(2, storage); assert_eq!(B::decode(storage), 2); assert!(A::decode(storage)); assert_eq!(C::decode(storage), 0); assert!(B::is_valid(7)); assert!(!B::is_valid(8)); assert!(B::is_valid(-8)); assert!(!B::is_valid(-9)); println!("Creating atomic bitfield storage"); type LockBit = BitField; type DataBits = BitField; let container = std::sync::Arc::new(AtomicBitfieldContainer::new(0usize)); println!( "lock bit: {}, data bits: {}", container.read::(), container.read::() ); let thread = { let container = container.clone(); std::thread::spawn(move || { println!("thread 1 spawned"); while !container.try_acquire::() {} container.update::(42); println!( "thread 1: set data bits to {}", container.read::() ); assert!(container.try_release::()); }) }; loop { while !container.try_acquire::() { std::thread::yield_now(); } if container.read::() != 0 { println!("data bits is not zero, break"); assert_eq!(container.read::(), 42); assert!(container.try_release::()); break; } println!("data bits is zero, loop"); assert!(container.try_release::()); } println!("{}", container.read::()); thread.join().unwrap(); }