I'm trying to update my code for Rust 1.0.alpha, and one section I am having trouble with can be reduced to the below example. I already annotated the closure type, and switched to unboxed closures. However I can't find the correct type for fun. I tried fun : FnMut() -> IoResult<u32> but even though the whole point of FnMut, FnOnce, and friends is to provide traits for closures to fulfill; the compiler can't seem to match the types properly.
I've read the following:
- Types of unboxed closures being unique to each
- Error message with unboxed closures
- http://smallcultfollowing.com/babysteps/blog/2014/11/26/purging-proc/
but they don't explain clearly how to deal with this issue
use std::io::File;
use std::io::IoResult;
use std::io::fs::PathExtensions;
use std::iter::range_step;
fn main() {
let path = Path::new("fid");
let mut file = File::open(&path);
let big = true;
let mut v = vec![];
let fun = if big {
|&mut:| file.read_be_u32()
} else {
|&mut:| file.read_le_u32()
};
for _ in range_step(0u64, path.stat().unwrap().size,4u64){
v.push(fun().unwrap());
}
println!("{}",v);
}
This gives:
scratch.rs:11:15: 15:6 error: if and else have incompatible types: expected `closure[scratch.rs:12:9: 12:35]`, found `closure[scratch.rs:14:9: 14:35]` (expected closure, found a different closure)
and using fun : FnMut() -> IoResult<u32> or fun : FnMut<(),IoResult<u32>> gives:
scratch.rs:12:9: 12:35 error: mismatched types: expected `core::ops::FnMut() -> core::result::Result<u32, std::io::IoError>`, found `closure[scratch.rs:12:9: 12:35]` (expected trait core::ops::FnMut, found closure)
scratch.rs:12 |&mut:| file.read_be_u32()
^~~~~~~~~~~~~~~~~~~~~~~~~~
scratch.rs:14:9: 14:35 error: mismatched types: expected `core::ops::FnMut() -> core::result::Result<u32, std::io::IoError>`, found `closure[scratch.rs:14:9: 14:35]` (expected trait core::ops::FnMut, found closure)
scratch.rs:14 |&mut:| file.read_le_u32()
^~~~~~~~~~~~~~~~~~~~~~~~~~