diff --git a/examples/list_devices.rs b/examples/list_devices.rs index 4169c3e..9005aac 100644 --- a/examples/list_devices.rs +++ b/examples/list_devices.rs @@ -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 diff --git a/examples/read_device.rs b/examples/read_device.rs index db70f5c..065e59f 100644 --- a/examples/read_device.rs +++ b/examples/read_device.rs @@ -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 { @@ -129,7 +129,7 @@ fn read_endpoint(handle: &mut libusb::DeviceHandle, endpoint: Endpoint, transfer match configure_endpoint(handle, &endpoint) { Ok(_) => { let mut vec = Vec::::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); @@ -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(()) } diff --git a/src/context.rs b/src/context.rs index 4033cb4..b48e691 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,5 +1,5 @@ use std::marker::PhantomData; -use std::mem; +use std::mem::MaybeUninit; use libc::c_int; use libusb::*; @@ -28,11 +28,11 @@ unsafe impl Send for Context {} impl Context { /// Opens a new `libusb` context. pub fn new() -> ::Result { - 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. @@ -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> { - 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) }) } } diff --git a/src/device.rs b/src/device.rs index cbfda2b..592a58c 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1,5 +1,5 @@ use std::marker::PhantomData; -use std::mem; +use std::mem::MaybeUninit; use libusb::*; @@ -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 { - 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 { - 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 { - 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. @@ -80,11 +80,11 @@ impl<'a> Device<'a> { /// Opens the device. pub fn open(&self) -> ::Result> { - 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()) }) } } diff --git a/src/device_handle.rs b/src/device_handle.rs index eb9c360..5f920c8 100644 --- a/src/device_handle.rs +++ b/src/device_handle.rs @@ -1,5 +1,5 @@ use std::marker::PhantomData; -use std::mem; +use std::mem::MaybeUninit; use std::slice; use std::time::Duration; @@ -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 { - 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. @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -403,16 +407,16 @@ impl<'a> DeviceHandle<'a> { pub fn read_languages(&self, timeout: Duration) -> ::Result> { let mut buf = Vec::::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); @@ -430,16 +434,16 @@ impl<'a> DeviceHandle<'a> { pub fn read_string_descriptor(&self, language: Language, index: u8, timeout: Duration) -> ::Result { let mut buf = Vec::::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);