From 94d674942feb6736605cf5958a7c71cdf9f14e8e Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Sat, 26 Oct 2024 10:46:14 +0800 Subject: [PATCH] Add derive Debug, closing #17 --- src/cdc.rs | 2 +- src/device.rs | 6 +++--- src/hid.rs | 6 +++--- src/host.rs | 20 ++++++++++++++++++-- src/interface.rs | 4 ++-- src/lib.rs | 2 +- 6 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/cdc.rs b/src/cdc.rs index 467378e..9b57c4b 100644 --- a/src/cdc.rs +++ b/src/cdc.rs @@ -2,7 +2,7 @@ use super::*; /// A handler of a CDC ACM(Abstract Control Model) -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct UsbCdcAcmHandler { pub tx_buffer: Vec, } diff --git a/src/device.rs b/src/device.rs index afd9c74..42f606b 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1,7 +1,7 @@ use super::*; use rusb::Version as rusbVersion; -#[derive(Clone, Default)] +#[derive(Clone, Default, Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Version { pub major: u8, @@ -37,7 +37,7 @@ impl From for Version { } /// Represent a USB device -#[derive(Clone, Default)] +#[derive(Clone, Default, Debug)] #[cfg_attr(feature = "serde", derive(Serialize))] pub struct UsbDevice { pub path: String, @@ -529,7 +529,7 @@ impl UsbDevice { } /// A handler for URB targeting the device -pub trait UsbDeviceHandler { +pub trait UsbDeviceHandler: std::fmt::Debug { /// Handle a URB(USB Request Block) targeting at this device /// /// When the lower 4 bits of `bmRequestType` is zero and the URB is not handled by the library, this function is called. diff --git a/src/hid.rs b/src/hid.rs index c0d9786..0dc84ea 100644 --- a/src/hid.rs +++ b/src/hid.rs @@ -5,7 +5,7 @@ use super::*; // HID 1.11: https://www.usb.org/sites/default/files/documents/hid1_11.pdf // HID Usage Tables 1.12: https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf -#[derive(Clone)] +#[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] enum UsbHidKeyboardHandlerState { Idle, @@ -13,7 +13,7 @@ enum UsbHidKeyboardHandlerState { } /// A handler of a HID keyboard -#[derive(Clone)] +#[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct UsbHidKeyboardHandler { pub report_descriptor: Vec, @@ -24,7 +24,7 @@ pub struct UsbHidKeyboardHandler { /// A report of a HID keyboard /// /// For definition of key codes, see [HID Usage Tables](https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf) -#[derive(Clone)] +#[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct UsbHidKeyboardReport { /// Key modifier diff --git a/src/host.rs b/src/host.rs index 45d91f0..5cdae7a 100644 --- a/src/host.rs +++ b/src/host.rs @@ -2,7 +2,7 @@ use super::*; /// A handler to pass requests to a rusb USB device of the host -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct RusbUsbHostInterfaceHandler { handle: Arc>>, } @@ -93,7 +93,7 @@ impl UsbInterfaceHandler for RusbUsbHostInterfaceHandler { } /// A handler to pass requests to a USB device of the host -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct RusbUsbHostDeviceHandler { handle: Arc>>, } @@ -155,6 +155,14 @@ pub struct NusbUsbHostInterfaceHandler { handle: Arc>, } +impl std::fmt::Debug for NusbUsbHostInterfaceHandler { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("NusbUsbHostInterfaceHandler") + .field("handle", &"Opaque") + .finish() + } +} + impl NusbUsbHostInterfaceHandler { pub fn new(handle: Arc>) -> Self { Self { handle } @@ -231,6 +239,14 @@ pub struct NusbUsbHostDeviceHandler { handle: Arc>, } +impl std::fmt::Debug for NusbUsbHostDeviceHandler { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("NusbUsbHostDeviceHandler") + .field("handle", &"Opaque") + .finish() + } +} + impl NusbUsbHostDeviceHandler { pub fn new(handle: Arc>) -> Self { Self { handle } diff --git a/src/interface.rs b/src/interface.rs index 2d1fcd0..1a9733a 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -1,7 +1,7 @@ use super::*; /// Represent a USB interface -#[derive(Clone)] +#[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(Serialize))] pub struct UsbInterface { pub interface_class: u8, @@ -16,7 +16,7 @@ pub struct UsbInterface { } /// A handler of a custom usb interface -pub trait UsbInterfaceHandler { +pub trait UsbInterfaceHandler: std::fmt::Debug { /// Return the class specific descriptor which is inserted between interface descriptor and endpoint descriptor fn get_class_specific_descriptor(&self) -> Vec; diff --git a/src/lib.rs b/src/lib.rs index 695d745..e2a4da1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ pub use util::*; use crate::usbip_protocol::{UsbIpResponse, USBIP_RET_SUBMIT, USBIP_RET_UNLINK}; /// Main struct of a USB/IP server -#[derive(Default)] +#[derive(Default, Debug)] pub struct UsbIpServer { available_devices: RwLock>, used_devices: RwLock>,