Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(unboxed_closures, fn_traits)]
- struct S {
- capture: i32,
- }
- impl Fn<(i32,)> for S {
- extern "rust-call" fn call(&self, (x,): (i32,)) -> i32 {
- println!("call fn");
- self.capture * x
- }
- }
- impl FnMut<(i32,)> for S {
- extern "rust-call" fn call_mut(&mut self, args: (i32,)) -> i32 {
- println!("call mut");
- self.call(args)
- }
- }
- impl FnOnce<(i32,)> for S {
- type Output = i32;
- extern "rust-call" fn call_once(self, args: (i32,)) -> i32 {
- println!("call once");
- self.call(args)
- }
- }
- fn call_it<F: Fn(i32) -> i32>(f: &F, x: i32) -> i32 {
- f(x)
- }
- fn call_it_mut<F: FnMut(i32) -> i32>(f: &mut F, x: i32) -> i32 {
- f(x)
- }
- fn call_it_once<F: FnOnce(i32) -> i32>(f: F, x: i32) -> i32 {
- f(x)
- }
- fn main() {
- let mut s = S { capture: 5 };
- {
- let x = call_it(&s, 22);
- }
- {
- let y = call_it_mut(&mut s, 22);
- }
- let z = call_it_once(s, 22);
- // assert_eq!(x, y);
- // assert_eq!(y, z);
- }
Add Comment
Please, Sign In to add comment