I am new to Rust having gone through only the Rust book, working on a project which requires capturing a screenshot of the primary display. I am using the scrap crate for this.
fn screen_shot_and_save_image(iter:i32) {
let one_second = Duration::new(1, 0);
let one_frame = one_second / 60;
let mut buffer = None;
let display = Display::primary().expect("Couldn't find primary display.");
let mut capturer = Capturer::new(display).expect("Couldn't begin capture.");
let (mut w, mut h) = (capturer.width(), capturer.height());;
while buffer.is_none() {
// Wait until there's a frame.
match capturer.frame() {
Ok(buf) => {
buffer = Some(buf);
}
Err(error) => {
if error.kind() == WouldBlock {
// Keep spinning.
thread::sleep(one_frame);
continue;
} else {
panic!("Error: {}", error);
}
}
};
}
//work with buffer
}
the capturer.frame() has a signature of
scrap::common::dxgi::Capturer
pub fn frame<'a>(&'a mut self) -> io::Result<Frame<'a>>
Error:
error[E0499]: cannot borrow `capturer` as mutable more than once at a time
--> src\main.rs:103:15
|
103 | match capturer.frame() {
| ^^^^^^^^^^^^^^^^ `capturer` was mutably borrowed here in the previous iteration of the loop
Based on similar questions, I understand that Rust does not allow mutable borrows in a loop. However I need to use this function in a while loop, incase I get the WouldBlock error, which returns a blank image intermittently. I cannot make good sense of the compiler error or suggestion so any help will be much appreciated, thank you!