Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. struct Error;
  2.  
  3. trait WriteBytes<'a> {
  4. fn write_byte<'b>(&'b mut self, byte: u8) -> Result<(), Error>
  5. where
  6. 'b: 'a;
  7. }
  8.  
  9. impl<'a> WriteBytes<'a> for &'a mut [u8] {
  10. fn write_byte<'b>(&'b mut self, byte: u8) -> Result<(), Error>
  11. where
  12. 'b: 'a,
  13. {
  14. match self.split_first_mut() {
  15. Some((first, rest)) => {
  16. *first = byte;
  17. *self = rest;
  18. Ok(())
  19. }
  20. None => Err(Error),
  21. }
  22. }
  23. }
  24.  
  25. fn main() {
  26. let mut array = [0, 1, 2, 3, 4];
  27. let mut buf: &mut [u8] = &mut array;
  28. buf.write_byte(5);
  29. println!("{:?}", buf);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement