Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. pub struct AsBoolsIter {
  2. bools: [bool; 8],
  3. cur: usize
  4. }
  5. impl Iterator for AsBoolsIter {
  6. type Item = bool;
  7. fn next(&mut self) -> Option<Self::Item> {
  8. let ret = self.bools.get(self.cur).map(|x| *x);
  9. self.cur += 1;
  10. ret
  11. }
  12. }
  13.  
  14. pub trait AsBools {
  15. fn is_set(self, mask: u8) -> bool;
  16. fn is_set_n(self, n: u8) -> bool;
  17. fn as_bools(self) -> [bool; 8];
  18. fn as_bools_iter(self) -> AsBoolsIter;
  19. }
  20.  
  21. impl AsBools for u8 {
  22. #[inline]
  23. fn is_set(self, mask: u8) -> bool {
  24. (self & mask) == mask
  25. }
  26.  
  27. #[inline]
  28. fn is_set_n(self, bit_number: u8) -> bool {
  29. self.is_set(1u8 << bit_number)
  30. }
  31.  
  32. #[inline]
  33. fn as_bools_iter(self) -> AsBoolsIter {
  34. AsBoolsIter {bools: self.as_bools(), cur: 0}
  35. }
  36.  
  37. #[inline]
  38. fn as_bools(self) -> [bool; 8] {
  39. [
  40. self.is_set_n(7),
  41. self.is_set_n(6),
  42. self.is_set_n(5),
  43. self.is_set_n(4),
  44. self.is_set_n(3),
  45. self.is_set_n(2),
  46. self.is_set_n(1),
  47. self.is_set_n(0),
  48. ]
  49. }
  50. }
  51. fn main() {
  52. const SIZE: usize = 32;
  53. let mut native_pxs = vec![0u8; SIZE*3];
  54. let chip8_pxs = vec![0b0001_1000u8; SIZE/8];
  55. for (bool, chunk) in chip8_pxs.iter().map(|byte| byte.as_bools_iter()).flatten().zip(native_pxs.chunks_exact_mut(3)) {
  56. if let &mut[ref mut r, ref mut g, ref mut b] = chunk {
  57. let val = if bool {255} else {0};
  58. *r = val;
  59. *g = val;
  60. *b = val;
  61. }
  62. }
  63. dbg!(native_pxs);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement