Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More modern idioms #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/list_devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ fn main() {
fn list_devices() -> libusb::Result<()> {
let timeout = Duration::from_secs(1);

let context = try!(libusb::Context::new());
let context = libusb::Context::new()?;

for device in try!(context.devices()).iter() {
for device in context.devices()?.iter() {
let device_desc = match device.device_descriptor() {
Ok(d) => d,
Err(_) => continue
Expand Down
14 changes: 7 additions & 7 deletions examples/read_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ fn open_device(context: &mut libusb::Context, vid: u16, pid: u16) -> Option<(lib
}

fn read_device(device: &mut libusb::Device, device_desc: &libusb::DeviceDescriptor, handle: &mut libusb::DeviceHandle) -> libusb::Result<()> {
try!(handle.reset());
handle.reset()?;

let timeout = Duration::from_secs(1);
let languages = try!(handle.read_languages(timeout));
let languages = handle.read_languages(timeout)?;

println!("Active configuration: {}", try!(handle.active_configuration()));
println!("Active configuration: {}", handle.active_configuration()?);
println!("Languages: {:?}", languages);

if languages.len() > 0 {
Expand Down Expand Up @@ -129,7 +129,7 @@ fn read_endpoint(handle: &mut libusb::DeviceHandle, endpoint: Endpoint, transfer
match configure_endpoint(handle, &endpoint) {
Ok(_) => {
let mut vec = Vec::<u8>::with_capacity(256);
let mut buf = unsafe { slice::from_raw_parts_mut((&mut vec[..]).as_mut_ptr(), vec.capacity()) };
let buf = unsafe { slice::from_raw_parts_mut((&mut vec[..]).as_mut_ptr(), vec.capacity()) };

let timeout = Duration::from_secs(1);

Expand Down Expand Up @@ -164,8 +164,8 @@ fn read_endpoint(handle: &mut libusb::DeviceHandle, endpoint: Endpoint, transfer
}

fn configure_endpoint<'a>(handle: &'a mut libusb::DeviceHandle, endpoint: &Endpoint) -> libusb::Result<()> {
try!(handle.set_active_configuration(endpoint.config));
try!(handle.claim_interface(endpoint.iface));
try!(handle.set_alternate_setting(endpoint.iface, endpoint.setting));
handle.set_active_configuration(endpoint.config)?;
handle.claim_interface(endpoint.iface)?;
handle.set_alternate_setting(endpoint.iface, endpoint.setting)?;
Ok(())
}
14 changes: 7 additions & 7 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::marker::PhantomData;
use std::mem;
use std::mem::MaybeUninit;

use libc::c_int;
use libusb::*;
Expand Down Expand Up @@ -28,11 +28,11 @@ unsafe impl Send for Context {}
impl Context {
/// Opens a new `libusb` context.
pub fn new() -> ::Result<Self> {
let mut context = unsafe { mem::uninitialized() };
let mut context = MaybeUninit::uninit();

try_unsafe!(libusb_init(&mut context));
try_unsafe!(libusb_init(context.as_mut_ptr()));

Ok(Context { context: context })
Ok(Context { context: unsafe { context.assume_init() } })
}

/// Sets the log level of a `libusb` context.
Expand Down Expand Up @@ -71,15 +71,15 @@ impl Context {

/// Returns a list of the current USB devices. The context must outlive the device list.
pub fn devices<'a>(&'a self) -> ::Result<DeviceList<'a>> {
let mut list: *const *mut libusb_device = unsafe { mem::uninitialized() };
let mut list = MaybeUninit::uninit();

let n = unsafe { libusb_get_device_list(self.context, &mut list) };
let n = unsafe { libusb_get_device_list(self.context, list.as_mut_ptr()) };

if n < 0 {
Err(error::from_libusb(n as c_int))
}
else {
Ok(unsafe { device_list::from_libusb(self, list, n as usize) })
Ok(unsafe { device_list::from_libusb(self, list.assume_init(), n as usize) })
}
}

Expand Down
26 changes: 13 additions & 13 deletions src/device.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::marker::PhantomData;
use std::mem;
use std::mem::MaybeUninit;

use libusb::*;

Expand Down Expand Up @@ -31,30 +31,30 @@ unsafe impl<'a> Sync for Device<'a> {}
impl<'a> Device<'a> {
/// Reads the device descriptor.
pub fn device_descriptor(&self) -> ::Result<DeviceDescriptor> {
let mut descriptor: libusb_device_descriptor = unsafe { mem::uninitialized() };
let mut descriptor = MaybeUninit::uninit();

// since libusb 1.0.16, this function always succeeds
try_unsafe!(libusb_get_device_descriptor(self.device, &mut descriptor));
try_unsafe!(libusb_get_device_descriptor(self.device, descriptor.as_mut_ptr()));

Ok(device_descriptor::from_libusb(descriptor))
Ok(device_descriptor::from_libusb(unsafe { descriptor.assume_init() }))
}

/// Reads a configuration descriptor.
pub fn config_descriptor(&self, config_index: u8) -> ::Result<ConfigDescriptor> {
let mut config: *const libusb_config_descriptor = unsafe { mem::uninitialized() };
let mut config = MaybeUninit::uninit();

try_unsafe!(libusb_get_config_descriptor(self.device, config_index, &mut config));
try_unsafe!(libusb_get_config_descriptor(self.device, config_index, config.as_mut_ptr()));

Ok(unsafe { config_descriptor::from_libusb(config) })
Ok(unsafe { config_descriptor::from_libusb(config.assume_init()) })
}

/// Reads the configuration descriptor for the current configuration.
pub fn active_config_descriptor(&self) -> ::Result<ConfigDescriptor> {
let mut config: *const libusb_config_descriptor = unsafe { mem::uninitialized() };
let mut config = MaybeUninit::uninit();

try_unsafe!(libusb_get_active_config_descriptor(self.device, &mut config));
try_unsafe!(libusb_get_active_config_descriptor(self.device, config.as_mut_ptr()));

Ok(unsafe { config_descriptor::from_libusb(config) })
Ok(unsafe { config_descriptor::from_libusb(config.assume_init()) })
}

/// Returns the number of the bus that the device is connected to.
Expand All @@ -80,11 +80,11 @@ impl<'a> Device<'a> {

/// Opens the device.
pub fn open(&self) -> ::Result<DeviceHandle<'a>> {
let mut handle: *mut libusb_device_handle = unsafe { mem::uninitialized() };
let mut handle = MaybeUninit::uninit();

try_unsafe!(libusb_open(self.device, &mut handle));
try_unsafe!(libusb_open(self.device, handle.as_mut_ptr()));

Ok(unsafe { device_handle::from_libusb(self.context, handle) })
Ok(unsafe { device_handle::from_libusb(self.context, handle.assume_init()) })
}
}

Expand Down
48 changes: 26 additions & 22 deletions src/device_handle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::marker::PhantomData;
use std::mem;
use std::mem::MaybeUninit;
use std::slice;
use std::time::Duration;

Expand Down Expand Up @@ -41,10 +41,10 @@ unsafe impl<'a> Sync for DeviceHandle<'a> {}
impl<'a> DeviceHandle<'a> {
/// Returns the active configuration number.
pub fn active_configuration(&self) -> ::Result<u8> {
let mut config = unsafe { mem::uninitialized() };
let mut config = MaybeUninit::uninit();

try_unsafe!(libusb_get_configuration(self.handle, &mut config));
Ok(config as u8)
try_unsafe!(libusb_get_configuration(self.handle, config.as_mut_ptr()));
Ok(unsafe { config.assume_init() } as u8)
}

/// Sets the device's active configuration.
Expand Down Expand Up @@ -142,17 +142,18 @@ impl<'a> DeviceHandle<'a> {
return Err(Error::InvalidParam);
}

let mut transferred: c_int = unsafe { mem::uninitialized() };
let mut transferred = MaybeUninit::uninit();

let ptr = buf.as_mut_ptr() as *mut c_uchar;
let len = buf.len() as c_int;
let timeout_ms = (timeout.as_secs() * 1000 + timeout.subsec_nanos() as u64 / 1_000_000) as c_uint;

match unsafe { libusb_interrupt_transfer(self.handle, endpoint, ptr, len, &mut transferred, timeout_ms) } {
match unsafe { libusb_interrupt_transfer(self.handle, endpoint, ptr, len, transferred.as_mut_ptr(), timeout_ms) } {
0 => {
Ok(transferred as usize)
Ok(unsafe { transferred.assume_init() } as usize)
},
err => {
let transferred = unsafe { transferred.assume_init() };
if err == LIBUSB_ERROR_INTERRUPTED && transferred > 0 {
Ok(transferred as usize)
}
Expand Down Expand Up @@ -188,17 +189,18 @@ impl<'a> DeviceHandle<'a> {
return Err(Error::InvalidParam);
}

let mut transferred: c_int = unsafe { mem::uninitialized() };
let mut transferred = MaybeUninit::uninit();

let ptr = buf.as_ptr() as *mut c_uchar;
let len = buf.len() as c_int;
let timeout_ms = (timeout.as_secs() * 1000 + timeout.subsec_nanos() as u64 / 1_000_000) as c_uint;

match unsafe { libusb_interrupt_transfer(self.handle, endpoint, ptr, len, &mut transferred, timeout_ms) } {
match unsafe { libusb_interrupt_transfer(self.handle, endpoint, ptr, len, transferred.as_mut_ptr(), timeout_ms) } {
0 => {
Ok(transferred as usize)
Ok(unsafe { transferred.assume_init() } as usize)
},
err => {
let transferred = unsafe { transferred.assume_init() };
if err == LIBUSB_ERROR_INTERRUPTED && transferred > 0 {
Ok(transferred as usize)
}
Expand Down Expand Up @@ -236,17 +238,18 @@ impl<'a> DeviceHandle<'a> {
return Err(Error::InvalidParam);
}

let mut transferred: c_int = unsafe { mem::uninitialized() };
let mut transferred = MaybeUninit::uninit();

let ptr = buf.as_mut_ptr() as *mut c_uchar;
let len = buf.len() as c_int;
let timeout_ms = (timeout.as_secs() * 1000 + timeout.subsec_nanos() as u64 / 1_000_000) as c_uint;

match unsafe { libusb_bulk_transfer(self.handle, endpoint, ptr, len, &mut transferred, timeout_ms) } {
match unsafe { libusb_bulk_transfer(self.handle, endpoint, ptr, len, transferred.as_mut_ptr(), timeout_ms) } {
0 => {
Ok(transferred as usize)
Ok(unsafe { transferred.assume_init() } as usize)
},
err => {
let transferred = unsafe { transferred.assume_init() };
if err == LIBUSB_ERROR_INTERRUPTED && transferred > 0 {
Ok(transferred as usize)
}
Expand Down Expand Up @@ -282,17 +285,18 @@ impl<'a> DeviceHandle<'a> {
return Err(Error::InvalidParam);
}

let mut transferred: c_int = unsafe { mem::uninitialized() };
let mut transferred = MaybeUninit::uninit();

let ptr = buf.as_ptr() as *mut c_uchar;
let len = buf.len() as c_int;
let timeout_ms = (timeout.as_secs() * 1000 + timeout.subsec_nanos() as u64 / 1_000_000) as c_uint;

match unsafe { libusb_bulk_transfer(self.handle, endpoint, ptr, len, &mut transferred, timeout_ms) } {
match unsafe { libusb_bulk_transfer(self.handle, endpoint, ptr, len, transferred.as_mut_ptr(), timeout_ms) } {
0 => {
Ok(transferred as usize)
Ok(unsafe { transferred.assume_init() } as usize)
},
err => {
let transferred = unsafe { transferred.assume_init() };
if err == LIBUSB_ERROR_INTERRUPTED && transferred > 0 {
Ok(transferred as usize)
}
Expand Down Expand Up @@ -403,16 +407,16 @@ impl<'a> DeviceHandle<'a> {
pub fn read_languages(&self, timeout: Duration) -> ::Result<Vec<Language>> {
let mut buf = Vec::<u8>::with_capacity(256);

let mut buf_slice = unsafe {
let buf_slice = unsafe {
slice::from_raw_parts_mut((&mut buf[..]).as_mut_ptr(), buf.capacity())
};

let len = try!(self.read_control(request_type(Direction::In, RequestType::Standard, Recipient::Device),
let len = self.read_control(request_type(Direction::In, RequestType::Standard, Recipient::Device),
LIBUSB_REQUEST_GET_DESCRIPTOR,
(LIBUSB_DT_STRING as u16) << 8,
0,
buf_slice,
timeout));
timeout)?;

unsafe {
buf.set_len(len);
Expand All @@ -430,16 +434,16 @@ impl<'a> DeviceHandle<'a> {
pub fn read_string_descriptor(&self, language: Language, index: u8, timeout: Duration) -> ::Result<String> {
let mut buf = Vec::<u8>::with_capacity(256);

let mut buf_slice = unsafe {
let buf_slice = unsafe {
slice::from_raw_parts_mut((&mut buf[..]).as_mut_ptr(), buf.capacity())
};

let len = try!(self.read_control(request_type(Direction::In, RequestType::Standard, Recipient::Device),
let len = self.read_control(request_type(Direction::In, RequestType::Standard, Recipient::Device),
LIBUSB_REQUEST_GET_DESCRIPTOR,
(LIBUSB_DT_STRING as u16) << 8 | index as u16,
language.lang_id(),
buf_slice,
timeout));
timeout)?;

unsafe {
buf.set_len(len);
Expand Down