I'm using glutin
and so have a move closure for my program's main loop and I'm trying to play an audio file with the rodio
crate. With the following code everything works and I get one beep every time the program loops:
...
let sink: rodio::Sink = ...;
event_loop.run(move |event, _, control_flow | {
let sound_file = File::open("beep.ogg").unwrap();
let buf_wrap = BufReader::new(sound_file);
let source = rodio::Decoder::new(buf_wrap).unwrap();
sink.append(source);
...
});
...
However, this is incredibly slow as I'm opening the same file every time it loops, so I try fixing it with the following:
...
let sound_file = File::open("beep.ogg").unwrap();
event_loop.run(move |event, _, control_flow | {
let buf_wrap = BufReader::new(sound_file);
...
});
...
But now the compiler gives me the following error message:
error[E0507]: cannot move out of `sound_file`, a captured variable in an `FnMut` closure
--> src/lib.rs:86:33
|
83 | let sound_file = File::open("beep.ogg").unwrap();
| ---------- captured outer variable
...
86 | let buf_wrap = BufReader::new(sound_file);
| ^^^^^^^^^^ move occurs because `sound_file` has type `File`, which does not implement the `Copy` trait
I've been trying to fix this for a while now with no success, any insight would be greatly appreciated.
Rc<>
orArc<>
depending on if it needs to beSend
. And in order to be mutable -> wrap it inRefCell
(orMutex<>
!!!) thus gettingRc<RefCell<File>>
orArc<Mutex<File>>.
ButFile
is actually a cursor, so it's not enough to just create a newBufReader
wrapper in the closure. You'll have to use theSeek
trait to move the cursor at the beginning of the file on each closure call.