Skip to content

Commit

Permalink
reduced the number of #[cfg] attributes by using cfg-if
Browse files Browse the repository at this point in the history
  • Loading branch information
sidit77 committed Jan 10, 2024
1 parent 16e5ea0 commit d534a39
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 79 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ windows-native = [

[dependencies]
libc = "0.2"
cfg-if = "1"

[target.'cfg(target_os = "linux")'.dependencies]
udev = { version = "0.7", optional = true }
Expand Down
112 changes: 50 additions & 62 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,44 +58,68 @@
//! [`HidDevice`] handles can access the same physical device. For backward compatibility this is
//! an opt-in that can be enabled with the `macos-shared-device` feature flag.

#[cfg(target_os = "windows")]
use windows_sys::core::GUID;

mod error;
mod ffi;

#[cfg(hidapi)]
mod hidapi;

#[cfg(all(feature = "linux-native", target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "linux-native", target_os = "linux"))))]
mod linux_native;
#[cfg(target_os = "macos")]
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
mod macos;
#[cfg(target_os = "windows")]
#[cfg_attr(docsrs, doc(cfg(target_os = "windows")))]
mod windows;

#[cfg(feature = "windows-native")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "windows-native", target_os = "windows"))))]
mod windows_native;

use libc::wchar_t;
use std::ffi::CStr;
use std::ffi::CString;
use std::fmt;
use std::fmt::Debug;
use std::sync::Mutex;
use cfg_if::cfg_if;

pub use error::HidError;

#[cfg(hidapi)]
use crate::hidapi::HidApiBackend;
#[cfg(all(feature = "linux-native", target_os = "linux"))]
use linux_native::HidApiBackend;
#[cfg(all(feature = "windows-native", target_os = "windows"))]
use windows_native::HidApiBackend;
cfg_if! {
if #[cfg(all(feature = "linux-native", target_os = "linux"))] {
//#[cfg_attr(docsrs, doc(cfg(all(feature = "linux-native", target_os = "linux"))))]
mod linux_native;
use linux_native::HidApiBackend;
} else if #[cfg(all(feature = "windows-native", target_os = "windows"))] {
//#[cfg_attr(docsrs, doc(cfg(all(feature = "windows-native", target_os = "windows"))))]
mod windows_native;
use windows_native::HidApiBackend;
} else if #[cfg(hidapi)] {
mod hidapi;
use hidapi::HidApiBackend;
} else {
compile_error!("No backend selected");
}
}

// Automatically implement the top trait
cfg_if! {
if #[cfg(target_os = "windows")] {
#[cfg_attr(docsrs, doc(cfg(target_os = "windows")))]
mod windows;
use windows::GUID;
/// A trait with the extra methods that are available on Windows
trait HidDeviceBackendWindows {
/// Get the container ID for a HID device
fn get_container_id(&self) -> HidResult<GUID>;
}
trait HidDeviceBackend: HidDeviceBackendBase + HidDeviceBackendWindows + Send {}
impl<T> HidDeviceBackend for T where T: HidDeviceBackendBase + HidDeviceBackendWindows + Send {}
} else if #[cfg(target_os = "macos")] {
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
mod macos;
/// A trait with the extra methods that are available on macOS
trait HidDeviceBackendMacos {
/// Get the location ID for a [`HidDevice`] device.
fn get_location_id(&self) -> HidResult<u32>;

/// Check if the device was opened in exclusive mode.
fn is_open_exclusive(&self) -> HidResult<bool>;
}
trait HidDeviceBackend: HidDeviceBackendBase + HidDeviceBackendMacos + Send {}
impl<T> HidDeviceBackend for T where T: HidDeviceBackendBase + HidDeviceBackendMacos + Send {}
} else {
trait HidDeviceBackend: HidDeviceBackendBase + Send {}
impl<T> HidDeviceBackend for T where T: HidDeviceBackendBase + Send {}
}
}


pub type HidResult<T> = Result<T, HidError>;
pub const MAX_REPORT_DESCRIPTOR_SIZE: usize = 4096;
Expand Down Expand Up @@ -463,42 +487,6 @@ trait HidDeviceBackendBase {
}
}

/// A trait with the extra methods that are available on macOS
#[cfg(target_os = "macos")]
trait HidDeviceBackendMacos {
/// Get the location ID for a [`HidDevice`] device.
fn get_location_id(&self) -> HidResult<u32>;

/// Check if the device was opened in exclusive mode.
fn is_open_exclusive(&self) -> HidResult<bool>;
}

/// A trait with the extra methods that are available on macOS
#[cfg(target_os = "windows")]
trait HidDeviceBackendWindows {
/// Get the container ID for a HID device
fn get_container_id(&self) -> HidResult<GUID>;
}

#[cfg(not(any(target_os = "macos", target_os = "windows")))]
trait HidDeviceBackend: HidDeviceBackendBase + Send {}
#[cfg(target_os = "macos")]
trait HidDeviceBackend: HidDeviceBackendBase + HidDeviceBackendMacos + Send {}
#[cfg(target_os = "windows")]
trait HidDeviceBackend: HidDeviceBackendBase + HidDeviceBackendWindows + Send {}

/// Automatically implement the top trait
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
impl<T> HidDeviceBackend for T where T: HidDeviceBackendBase + Send {}

/// Automatically implement the top trait
#[cfg(target_os = "macos")]
impl<T> HidDeviceBackend for T where T: HidDeviceBackendBase + HidDeviceBackendMacos + Send {}

/// Automatically implement the top trait
#[cfg(target_os = "windows")]
impl<T> HidDeviceBackend for T where T: HidDeviceBackendBase + HidDeviceBackendWindows + Send {}

pub struct HidDevice {
inner: Box<dyn HidDeviceBackend>,
}
Expand Down
2 changes: 1 addition & 1 deletion src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use windows_sys::core::GUID;
pub use windows_sys::core::GUID;
use crate::{HidDevice, HidResult};

impl HidDevice {
Expand Down
1 change: 0 additions & 1 deletion src/windows_native/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod tests;
use std::collections::HashMap;
use std::ffi::c_void;
use std::slice;
use crate::ensure;
use encoder::encode_descriptor;
use typedefs::{Caps, HidpPreparsedData, LinkCollectionNode};
use types::{BitRange, ItemNodeType, MainItemNode, MainItems, ReportType};
Expand Down
1 change: 0 additions & 1 deletion src/windows_native/dev_node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::ptr::null_mut;
use windows_sys::Win32::Devices::DeviceAndDriverInstallation::{CM_Get_DevNode_PropertyW, CM_Get_Parent, CM_LOCATE_DEVNODE_NORMAL, CM_Locate_DevNodeW, CR_BUFFER_SMALL, CR_SUCCESS};
use crate::ensure;
use super::error::{check_config, WinError, WinResult};
use super::string::U16Str;
use super::types::{DeviceProperty, PropertyKey};
Expand Down
1 change: 0 additions & 1 deletion src/windows_native/hid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::ffi::c_void;
use std::mem::{size_of, zeroed};
use windows_sys::core::GUID;
use windows_sys::Win32::Devices::HumanInterfaceDevice::{HIDD_ATTRIBUTES, HidD_FreePreparsedData, HidD_GetAttributes, HidD_GetHidGuid, HidD_GetPreparsedData, HIDP_CAPS, HidP_GetCaps, HIDP_STATUS_SUCCESS};
use crate::ensure;
use super::error::{check_boolean, WinError, WinResult};
use super::types::Handle;

Expand Down
1 change: 0 additions & 1 deletion src/windows_native/interfaces.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ptr::{null, null_mut};
use windows_sys::core::GUID;
use windows_sys::Win32::Devices::DeviceAndDriverInstallation::{CM_GET_DEVICE_INTERFACE_LIST_PRESENT, CM_Get_Device_Interface_List_SizeW, CM_Get_Device_Interface_ListW, CM_Get_Device_Interface_PropertyW, CR_BUFFER_SMALL, CR_SUCCESS};
use crate::ensure;
use super::error::{check_config, WinError, WinResult};
use super::hid::get_interface_guid;
use super::string::{U16Str, U16StringList};
Expand Down
20 changes: 9 additions & 11 deletions src/windows_native/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
//! The implementation which uses the C library to perform operations
//! The implementation which directly uses the win32 api to perform operations

macro_rules! ensure {
($cond:expr, $result:expr) => {
if !($cond) {
return $result;
}
};
}

mod types;
mod error;
Expand Down Expand Up @@ -35,16 +43,6 @@ use types::{Handle, Overlapped};

const STRING_BUF_LEN: usize = 128;

#[macro_export]
macro_rules! ensure {
($cond:expr, $result:expr) => {
if !($cond) {
return $result;
}
};
}


pub struct HidApiBackend;
impl HidApiBackend {
pub fn get_hid_device_info_vector(vid: u16, pid: u16) -> HidResult<Vec<DeviceInfo>> {
Expand Down
2 changes: 1 addition & 1 deletion src/windows_native/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use windows_sys::Win32::Foundation::{CloseHandle, FALSE, HANDLE, INVALID_HANDLE_
use windows_sys::Win32::System::IO::{GetOverlappedResultEx, OVERLAPPED};
use windows_sys::Win32::System::Threading::{CreateEventW, INFINITE};
use windows_sys::Win32::UI::Shell::PropertiesSystem::PROPERTYKEY;
use crate::{BusType, ensure};
use crate::BusType;
use super::error::{WinError, WinResult};

#[allow(clippy::missing_safety_doc)]
Expand Down

0 comments on commit d534a39

Please sign in to comment.