Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub struct AsBoolsIter {
- bools: [bool; 8],
- cur: usize
- }
- impl Iterator for AsBoolsIter {
- type Item = bool;
- fn next(&mut self) -> Option<Self::Item> {
- let ret = self.bools.get(self.cur).map(|x| *x);
- self.cur += 1;
- ret
- }
- }
- pub trait AsBools {
- fn is_set(self, mask: u8) -> bool;
- fn is_set_n(self, n: u8) -> bool;
- fn as_bools(self) -> [bool; 8];
- fn as_bools_iter(self) -> AsBoolsIter;
- }
- impl AsBools for u8 {
- #[inline]
- fn is_set(self, mask: u8) -> bool {
- (self & mask) == mask
- }
- #[inline]
- fn is_set_n(self, bit_number: u8) -> bool {
- self.is_set(1u8 << bit_number)
- }
- #[inline]
- fn as_bools_iter(self) -> AsBoolsIter {
- AsBoolsIter {bools: self.as_bools(), cur: 0}
- }
- #[inline]
- fn as_bools(self) -> [bool; 8] {
- [
- self.is_set_n(7),
- self.is_set_n(6),
- self.is_set_n(5),
- self.is_set_n(4),
- self.is_set_n(3),
- self.is_set_n(2),
- self.is_set_n(1),
- self.is_set_n(0),
- ]
- }
- }
- fn main() {
- const SIZE: usize = 32;
- let mut native_pxs = vec![0u8; SIZE*3];
- let chip8_pxs = vec![0b0001_1000u8; SIZE/8];
- for (bool, chunk) in chip8_pxs.iter().map(|byte| byte.as_bools_iter()).flatten().zip(native_pxs.chunks_exact_mut(3)) {
- if let &mut[ref mut r, ref mut g, ref mut b] = chunk {
- let val = if bool {255} else {0};
- *r = val;
- *g = val;
- *b = val;
- }
- }
- dbg!(native_pxs);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement