Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@orclev
Last active January 22, 2017 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orclev/7dbdbd3559b295ce2aa3114f238c736e to your computer and use it in GitHub Desktop.
Save orclev/7dbdbd3559b295ce2aa3114f238c736e to your computer and use it in GitHub Desktop.
pub struct OpenRadioContext<'a> { // public struct
usb_context: Context, // private fields, safety depends on these not being accessed directly
device_handle: DeviceHandle<'a>,
}
... snip ...
impl <'a> OpenRadioContext<'a> {
fn open() -> OpenRadioContext<'a> {
let mut ctx: Box<Context> = Box::new(Context::new().unwrap()); // Has lifetime a' but Rust doesn't know that
let mut handle = ctx.open_device_with_vid_pid(0x1915, 0x7777).and_then(|mut hndl: DeviceHandle| {
hndl.reset();
hndl.set_active_configuration(1).unwrap();
hndl.claim_interface(0).unwrap();
unsafe {
Option::Some(transmute::<DeviceHandle, DeviceHandle<'a>>(hndl)) // Ewww
}
}).unwrap();
OpenRadioContext { // Make sure ctx and handle both have lifetime 'a
usb_context: ctx,
device_handle: handle,
}
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment