2015-11-13 23:14:57 -06:00
|
|
|
struct FuncContainer {
|
|
|
|
f1: fn(data: u8),
|
|
|
|
f2: extern "C" fn(data: u8),
|
|
|
|
f3: unsafe fn(data: u8),
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FuncContainerOuter {
|
|
|
|
container: Box<FuncContainer>
|
|
|
|
}
|
|
|
|
|
2015-06-14 19:07:05 -05:00
|
|
|
struct Obj<F> where F: FnOnce() -> u32 {
|
|
|
|
closure: F,
|
|
|
|
not_closure: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BoxedObj {
|
2019-05-28 13:46:13 -05:00
|
|
|
boxed_closure: Box<dyn FnOnce() -> u32>,
|
2015-06-14 19:07:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Wrapper<F> where F: FnMut() -> u32 {
|
|
|
|
wrap: Obj<F>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn func() -> u32 {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2019-05-28 13:46:13 -05:00
|
|
|
fn check_expression() -> Obj<Box<dyn FnOnce() -> u32>> {
|
|
|
|
Obj { closure: Box::new(|| 42_u32) as Box<dyn FnOnce() -> u32>, not_closure: 42 }
|
2015-06-14 19:07:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// test variations of function
|
2015-06-14 20:01:26 -05:00
|
|
|
|
2015-06-14 19:07:05 -05:00
|
|
|
let o_closure = Obj { closure: || 42, not_closure: 42 };
|
|
|
|
o_closure.closure(); //~ ERROR no method named `closure` found
|
|
|
|
|
2017-03-25 01:40:24 -05:00
|
|
|
o_closure.not_closure();
|
|
|
|
//~^ ERROR no method named `not_closure` found
|
2015-06-14 19:07:05 -05:00
|
|
|
|
|
|
|
let o_func = Obj { closure: func, not_closure: 5 };
|
|
|
|
o_func.closure(); //~ ERROR no method named `closure` found
|
|
|
|
|
|
|
|
let boxed_fn = BoxedObj { boxed_closure: Box::new(func) };
|
|
|
|
boxed_fn.boxed_closure();//~ ERROR no method named `boxed_closure` found
|
|
|
|
|
2019-05-28 13:46:13 -05:00
|
|
|
let boxed_closure = BoxedObj { boxed_closure: Box::new(|| 42_u32) as Box<dyn FnOnce() -> u32> };
|
2015-06-14 19:07:05 -05:00
|
|
|
boxed_closure.boxed_closure();//~ ERROR no method named `boxed_closure` found
|
|
|
|
|
|
|
|
// test expression writing in the notes
|
2015-06-14 20:01:26 -05:00
|
|
|
|
2015-06-14 19:07:05 -05:00
|
|
|
let w = Wrapper { wrap: o_func };
|
|
|
|
w.wrap.closure();//~ ERROR no method named `closure` found
|
2015-06-14 20:01:26 -05:00
|
|
|
|
2017-03-25 01:40:24 -05:00
|
|
|
w.wrap.not_closure();
|
|
|
|
//~^ ERROR no method named `not_closure` found
|
2015-06-14 19:07:05 -05:00
|
|
|
|
|
|
|
check_expression().closure();//~ ERROR no method named `closure` found
|
|
|
|
}
|
2015-11-13 23:14:57 -06:00
|
|
|
|
|
|
|
impl FuncContainerOuter {
|
|
|
|
fn run(&self) {
|
|
|
|
unsafe {
|
|
|
|
(*self.container).f1(1); //~ ERROR no method named `f1` found
|
|
|
|
(*self.container).f2(1); //~ ERROR no method named `f2` found
|
|
|
|
(*self.container).f3(1); //~ ERROR no method named `f3` found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|