Guest User

Untitled

a guest
Mar 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. use std::io::Cursor;
  2.  
  3. // Should check if reading $cur for $length results in a slice equal to one of expected$
  4. macro_rules! matches_header (
  5. ( $cur:expr, $length:expr, $( $expected:expr), *) => {
  6. {
  7. use std::io::Read;
  8.  
  9. let mut buf = [0;$length];
  10. match $cur.read(&mut buf) {
  11. $(
  12. Ok(_) if buf == $expected => true,
  13. )*
  14. _ => false
  15. }
  16. }
  17. };
  18. );
  19.  
  20. fn main() {
  21. let mut cur = Cursor::new([71, 73, 70, 56, 57, 97]);
  22. if matches_header!(cur, 6, [71, 73, 70, 56, 55, 97], [71, 73, 70, 56, 57, 97]) {
  23. println!("Yes!")
  24. } else {
  25. println!("No!")
  26. }
  27. }
Add Comment
Please, Sign In to add comment