cancel guard

This commit is contained in:
Magnus Ulimoen 2020-07-24 17:34:57 +02:00
parent 35c3299d51
commit fe6abd7173
1 changed files with 19 additions and 8 deletions

View File

@ -150,7 +150,7 @@ impl Handle {
}) })
.unwrap() .unwrap()
}; };
(0..num_desc).map(move |i| self.get_descriptor(i as _).unwrap()) (1..num_desc).map(move |i| self.get_descriptor(i as _).unwrap())
} }
fn get_descriptor(&self, index: usize) -> Option<Descriptor> { fn get_descriptor(&self, index: usize) -> Option<Descriptor> {
let desc = unsafe { sane_get_option_descriptor(self.0, index as _) }; let desc = unsafe { sane_get_option_descriptor(self.0, index as _) };
@ -165,11 +165,9 @@ impl Handle {
unsafe { checked(|| sane_get_parameters(self.0, parameters.as_mut_ptr()))? } unsafe { checked(|| sane_get_parameters(self.0, parameters.as_mut_ptr()))? }
Ok(Parameters(unsafe { parameters.assume_init() })) Ok(Parameters(unsafe { parameters.assume_init() }))
} }
fn start(&self) -> Result<(), Error> { fn start(&self) -> Result<Acquisition, Error> {
unsafe { checked(|| sane_start(self.0)) } unsafe { checked(|| sane_start(self.0)) };
} Ok(Acquisition { handle: &self })
fn cancel(&self) {
unsafe { sane_cancel(self.0) }
} }
} }
@ -212,6 +210,20 @@ impl Parameters {
} }
} }
struct Acquisition<'a> {
handle: &'a Handle,
}
impl<'a> Acquisition<'a> {
fn cancel(self) {}
}
impl Drop for Acquisition<'_> {
fn drop(&mut self) {
unsafe { sane_cancel(self.handle.0) }
}
}
fn main() { fn main() {
let (context, version) = Context::init().unwrap(); let (context, version) = Context::init().unwrap();
println!( println!(
@ -242,6 +254,5 @@ fn main() {
let parameters = handle.parameters().unwrap(); let parameters = handle.parameters().unwrap();
println!("{:?}", parameters); println!("{:?}", parameters);
handle.start().unwrap(); let acq = handle.start().unwrap();
handle.cancel();
} }