1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 #![allow(unknown_features)]
13 #![feature(box_syntax)]
14 #![feature(unboxed_closures, fn_traits)]
16 // Test that unboxing shim for calling rust-call ABI methods through a
17 // trait box works and does not cause an ICE.
19 struct Foo { foo: u32 }
21 impl FnMut<()> for Foo {
22 extern "rust-call" fn call_mut(&mut self, _: ()) -> u32 { self.foo }
25 impl FnOnce<()> for Foo {
27 extern "rust-call" fn call_once(mut self, _: ()) -> u32 { self.call_mut(()) }
30 /////////////////////////////////////////////////////////////////////////
32 impl FnMut<(u32,)> for Foo {
33 extern "rust-call" fn call_mut(&mut self, (x,): (u32,)) -> u32 { self.foo + x }
36 impl FnOnce<(u32,)> for Foo {
38 extern "rust-call" fn call_once(mut self, args: (u32,)) -> u32 { self.call_mut(args) }
41 /////////////////////////////////////////////////////////////////////////
43 impl FnMut<(u32,u32)> for Foo {
44 extern "rust-call" fn call_mut(&mut self, (x, y): (u32, u32)) -> u32 { self.foo + x + y }
47 impl FnOnce<(u32,u32)> for Foo {
49 extern "rust-call" fn call_once(mut self, args: (u32,u32)) -> u32 { self.call_mut(args) }
53 let mut f = box Foo { foo: 42 } as Box<FnMut() -> u32>;
54 assert_eq!(f.call_mut(()), 42);
56 let mut f = box Foo { foo: 40 } as Box<FnMut(u32) -> u32>;
57 assert_eq!(f.call_mut((2,)), 42);
59 let mut f = box Foo { foo: 40 } as Box<FnMut(u32, u32) -> u32>;
60 assert_eq!(f.call_mut((1, 1)), 42);