Skip to content

Commit

Permalink
Use cell for blocking property
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiwalun committed Jul 23, 2023
1 parent 4106f70 commit c75432c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/hidapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl HidDeviceBackendBase for HidDevice {
self.check_size(res)
}

fn set_blocking_mode(&mut self, blocking: bool) -> HidResult<()> {
fn set_blocking_mode(&self, blocking: bool) -> HidResult<()> {
let res = unsafe {
ffi::hid_set_nonblocking(self._hid_device, if blocking { 0i32 } else { 1i32 })
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ trait HidDeviceBackendBase {
fn read_timeout(&self, buf: &mut [u8], timeout: i32) -> HidResult<usize>;
fn send_feature_report(&self, data: &[u8]) -> HidResult<()>;
fn get_feature_report(&self, buf: &mut [u8]) -> HidResult<usize>;
fn set_blocking_mode(&mut self, blocking: bool) -> HidResult<()>;
fn set_blocking_mode(&self, blocking: bool) -> HidResult<()>;
fn get_device_info(&self) -> HidResult<DeviceInfo>;
fn get_manufacturer_string(&self) -> HidResult<Option<String>>;
fn get_product_string(&self) -> HidResult<Option<String>>;
Expand Down
11 changes: 6 additions & 5 deletions src/macos_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use core_foundation::{
use mach2::port::MACH_PORT_NULL;

use std::{
cell::Cell,
collections::VecDeque,
ffi::{c_void, CStr, CString},
sync::{
Expand Down Expand Up @@ -45,7 +46,7 @@ use self::ffi::{kIOMainPortDefault, IOHIDManager};
#[derive(Debug)]
pub struct HidDevice {
/// If set to true, reads will block until data is available
blocking: bool,
blocking: Cell<bool>,

/// Options used to open the device
open_options: IOOptionBits,
Expand Down Expand Up @@ -333,7 +334,7 @@ impl HidDeviceBackendBase for HidDevice {
}

fn read(&self, buf: &mut [u8]) -> HidResult<usize> {
let timeout = if self.blocking { -1 } else { 0 };
let timeout = if self.blocking.get() { -1 } else { 0 };

self.read_timeout(buf, timeout)
}
Expand Down Expand Up @@ -412,8 +413,8 @@ impl HidDeviceBackendBase for HidDevice {
self.get_report(kIOHIDReportType::Feature, buf)
}

fn set_blocking_mode(&mut self, blocking: bool) -> HidResult<()> {
self.blocking = blocking;
fn set_blocking_mode(&self, blocking: bool) -> HidResult<()> {
self.blocking.set(blocking);
Ok(())
}

Expand Down Expand Up @@ -629,7 +630,7 @@ impl HidDevice {

Ok(Self {
// TODO: Default value here
blocking: false,
blocking: Cell::new(false),
// TODO: Set open options
open_options: 0,
reader_thread_handle: Some(reader_handle),
Expand Down

0 comments on commit c75432c

Please sign in to comment.