Advertisement
Guest User

Untitled

a guest
Aug 26th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.54 KB | None | 0 0
  1. #![feature(unboxed_closures, core)]
  2.  
  3. pub struct Foo<T> { t: T }
  4.  
  5. impl<'a, T> Foo<T> where T: 'a + FnMut() -> &'a [u8] {
  6.    pub fn from_callback(closure: T) -> Self {
  7.        Foo { t: closure }
  8.    }
  9.  
  10.    pub fn work(&mut self) {
  11.      println!("{:?}", self.t.call_mut(()));
  12.      println!("{:?}", self.t.call_mut(()));
  13.    }
  14. }
  15.  
  16. fn main() {
  17.  let mut foo = {
  18.    let buffer = vec![1, 2, 3, 4];
  19.    let mut i = 0;
  20.    Foo::from_callback(move || {
  21.      let j = i;
  22.      i += 1;
  23.      &buffer[j..j+1]
  24.    })
  25.  };
  26.  foo.work();
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement