2018-08-30 12:18:55 +00:00
|
|
|
// run-pass
|
2015-12-03 01:31:49 +00:00
|
|
|
#![feature(unboxed_closures, fn_traits)]
|
2014-10-17 04:56:53 +00:00
|
|
|
|
|
|
|
// Test that unboxing shim for calling rust-call ABI methods through a
|
2014-11-20 17:12:38 +00:00
|
|
|
// trait box works and does not cause an ICE.
|
2014-10-17 04:56:53 +00:00
|
|
|
|
2015-01-12 15:27:25 +00:00
|
|
|
struct Foo { foo: u32 }
|
2014-10-17 04:56:53 +00:00
|
|
|
|
2015-01-12 15:27:25 +00:00
|
|
|
impl FnMut<()> for Foo {
|
|
|
|
extern "rust-call" fn call_mut(&mut self, _: ()) -> u32 { self.foo }
|
2014-10-17 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 14:08:33 +00:00
|
|
|
impl FnOnce<()> for Foo {
|
2015-01-12 15:27:25 +00:00
|
|
|
type Output = u32;
|
2015-03-11 14:08:33 +00:00
|
|
|
extern "rust-call" fn call_once(mut self, _: ()) -> u32 { self.call_mut(()) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FnMut<(u32,)> for Foo {
|
2015-01-12 15:27:25 +00:00
|
|
|
extern "rust-call" fn call_mut(&mut self, (x,): (u32,)) -> u32 { self.foo + x }
|
2014-10-17 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 14:08:33 +00:00
|
|
|
impl FnOnce<(u32,)> for Foo {
|
2015-01-12 15:27:25 +00:00
|
|
|
type Output = u32;
|
2015-03-11 14:08:33 +00:00
|
|
|
extern "rust-call" fn call_once(mut self, args: (u32,)) -> u32 { self.call_mut(args) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FnMut<(u32,u32)> for Foo {
|
2015-01-12 15:27:25 +00:00
|
|
|
extern "rust-call" fn call_mut(&mut self, (x, y): (u32, u32)) -> u32 { self.foo + x + y }
|
2014-10-17 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 14:08:33 +00:00
|
|
|
impl FnOnce<(u32,u32)> for Foo {
|
|
|
|
type Output = u32;
|
|
|
|
extern "rust-call" fn call_once(mut self, args: (u32,u32)) -> u32 { self.call_mut(args) }
|
|
|
|
}
|
|
|
|
|
2014-10-17 04:56:53 +00:00
|
|
|
fn main() {
|
2022-07-07 02:36:10 +00:00
|
|
|
let mut f = Box::new(Foo { foo: 42 }) as Box<dyn FnMut() -> u32>;
|
2014-11-20 17:12:38 +00:00
|
|
|
assert_eq!(f.call_mut(()), 42);
|
2014-10-17 04:56:53 +00:00
|
|
|
|
2022-07-07 02:36:10 +00:00
|
|
|
let mut f = Box::new(Foo { foo: 40 }) as Box<dyn FnMut(u32) -> u32>;
|
2014-11-20 17:12:38 +00:00
|
|
|
assert_eq!(f.call_mut((2,)), 42);
|
2014-10-17 04:56:53 +00:00
|
|
|
|
2022-07-07 02:36:10 +00:00
|
|
|
let mut f = Box::new(Foo { foo: 40 }) as Box<dyn FnMut(u32, u32) -> u32>;
|
2014-11-20 17:12:38 +00:00
|
|
|
assert_eq!(f.call_mut((1, 1)), 42);
|
2014-10-17 04:56:53 +00:00
|
|
|
}
|