Guest User

Untitled

a guest
Oct 17th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #![feature(unboxed_closures, fn_traits)]
  2.  
  3. struct S {
  4. capture: i32,
  5. }
  6.  
  7. impl Fn<(i32,)> for S {
  8. extern "rust-call" fn call(&self, (x,): (i32,)) -> i32 {
  9. println!("call fn");
  10. self.capture * x
  11. }
  12. }
  13. impl FnMut<(i32,)> for S {
  14. extern "rust-call" fn call_mut(&mut self, args: (i32,)) -> i32 {
  15. println!("call mut");
  16. self.call(args)
  17. }
  18. }
  19. impl FnOnce<(i32,)> for S {
  20. type Output = i32;
  21. extern "rust-call" fn call_once(self, args: (i32,)) -> i32 {
  22. println!("call once");
  23. self.call(args)
  24. }
  25. }
  26. fn call_it<F: Fn(i32) -> i32>(f: &F, x: i32) -> i32 {
  27. f(x)
  28. }
  29. fn call_it_mut<F: FnMut(i32) -> i32>(f: &mut F, x: i32) -> i32 {
  30. f(x)
  31. }
  32. fn call_it_once<F: FnOnce(i32) -> i32>(f: F, x: i32) -> i32 {
  33. f(x)
  34. }
  35. fn main() {
  36. let mut s = S { capture: 5 };
  37. {
  38. let x = call_it(&s, 22);
  39. }
  40. {
  41. let y = call_it_mut(&mut s, 22);
  42. }
  43. let z = call_it_once(s, 22);
  44. // assert_eq!(x, y);
  45. // assert_eq!(y, z);
  46. }
Add Comment
Please, Sign In to add comment