From 39ea216984f4b7b5aa861c9605ccf5de720bee7e Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Mon, 23 Jul 2018 22:03:37 +0300 Subject: Lifetime problems with opening the device --- src/lib.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2a0a330..9468997 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,32 +5,31 @@ const KNOWN_DEVICES: [(u16, u16, &str); 2] = [ (0x0bda, 0x2838, "Generic RTL2832U OEM") ]; -struct Usb { +struct Usb<'a> { ctx: libusb::Context, - desc: Option - // dev: Option + dev: Option> } -pub struct RtlSdr { - usb: Usb +pub struct RtlSdr<'a> { + usb: Usb<'a> } -impl RtlSdr { +impl<'a> RtlSdr<'a> { - pub fn new() -> RtlSdr { + pub fn new() -> RtlSdr<'a> { RtlSdr { usb: Usb { ctx: libusb::Context::new().unwrap(), - desc: None + dev: None } } } pub fn init(&mut self) { - self.usb.desc = self.find_device(); + self.usb.dev = self.find_device(); } - pub fn find_device(&self) -> Option { + pub fn find_device(&self) -> Option> { for mut dev in self.usb.ctx.devices().unwrap().iter() { let desc = dev.device_descriptor().unwrap(); let vid = desc.vendor_id(); @@ -38,7 +37,7 @@ impl RtlSdr { for kd in KNOWN_DEVICES.iter() { if kd.0 == vid && kd.1 == pid { - return Some(desc) + return Some(dev.open().unwrap()) } } } @@ -53,8 +52,8 @@ mod tests { #[test] fn test_init() { let mut rtlsdr = RtlSdr::new(); - assert!(rtlsdr.usb.desc.is_none()); + assert!(rtlsdr.usb.dev.is_none()); rtlsdr.init(); - assert!(rtlsdr.usb.desc.is_some()); + assert!(rtlsdr.usb.dev.is_some()); } } -- cgit v1.3.1