Advertisement
Guest User

Untitled

a guest
May 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. use core::cell::RefCell;
  2.  
  3. struct Interface {
  4. buf: RefCell<[u8; 8]>,
  5. }
  6.  
  7. impl Interface {
  8. pub fn new() -> Self {
  9. Self { buf: RefCell::new([0; 8]) }
  10. }
  11. }
  12.  
  13. #[derive(Debug)]
  14. struct Foo<'a>(&'a [u8]);
  15.  
  16. impl Interface {
  17. fn receive<F>(&self, mut f: F) where F: FnMut(Option<Foo<'_>>) {
  18. f(Some(Foo(&*self.buf.borrow())));
  19. }
  20.  
  21. fn transmit<'a>(&self, foo: Foo<'a>) {
  22. (&mut self.buf.borrow_mut()[..foo.0.len()]).copy_from_slice(&foo.0);
  23. }
  24. }
  25.  
  26. fn main() {
  27. let interface = Interface::new();
  28.  
  29. interface.receive(|foo| println!("{:?}", foo));
  30. interface.transmit(Foo(&[1; 4][..]));
  31. interface.receive(|foo| println!("{:?}", foo));
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement