Guest User

Untitled

a guest
Nov 13th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. #![feature(fn_traits)]
  2. #![feature(unboxed_closures)]
  3.  
  4. struct Closure {
  5. a: u32,
  6. b: u32
  7. }
  8.  
  9. impl FnOnce<(u32,)> for Closure {
  10. type Output = u32;
  11.  
  12. extern "rust-call" fn call_once(self, (c, ): (u32, )) -> Self::Output {
  13. self.a + self.b + c
  14. }
  15. }
  16.  
  17.  
  18. fn main() {
  19. use std::mem;
  20.  
  21. let a = 1;
  22. let b = 2;
  23.  
  24. let with_lambda = move |c: u32| a + b + c;
  25. let with_struct = Closure { a, b };
  26.  
  27. let lambda_size = mem::size_of_val(&with_lambda);
  28. let struct_size =mem::size_of_val(&with_struct);
  29.  
  30. assert_eq!(lambda_size, struct_size);
  31. assert_eq!(lambda_size, mem::size_of_val(&a) + mem::size_of_val(&b));
  32.  
  33. assert_eq!(with_lambda(3), 6);
  34. assert_eq!(with_struct(3), 6);
  35. }
Add Comment
Please, Sign In to add comment