Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use core::cell::RefCell;
- struct Interface {
- buf: RefCell<[u8; 8]>,
- }
- impl Interface {
- pub fn new() -> Self {
- Self { buf: RefCell::new([0; 8]) }
- }
- }
- #[derive(Debug)]
- struct Foo<'a>(&'a [u8]);
- impl Interface {
- fn receive<F>(&self, mut f: F) where F: FnMut(Option<Foo<'_>>) {
- f(Some(Foo(&*self.buf.borrow())));
- }
- fn transmit<'a>(&self, foo: Foo<'a>) {
- (&mut self.buf.borrow_mut()[..foo.0.len()]).copy_from_slice(&foo.0);
- }
- }
- fn main() {
- let interface = Interface::new();
- interface.receive(|foo| println!("{:?}", foo));
- interface.transmit(Foo(&[1; 4][..]));
- interface.receive(|foo| println!("{:?}", foo));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement