From 528a006703321f1d063e95d72d3c49ef187931a2 Mon Sep 17 00:00:00 2001 From: LastExceed Date: Sat, 19 Aug 2023 10:06:33 +0200 Subject: [PATCH 1/6] turn `SDKResult` into a type alias --- wooting-analog-common/src/lib.rs | 120 +------------------------------ 1 file changed, 2 insertions(+), 118 deletions(-) diff --git a/wooting-analog-common/src/lib.rs b/wooting-analog-common/src/lib.rs index 93ff2d0..b7275fe 100644 --- a/wooting-analog-common/src/lib.rs +++ b/wooting-analog-common/src/lib.rs @@ -273,61 +273,7 @@ impl Default for WootingAnalogResult { } } -#[derive(Debug)] -pub struct SDKResult(pub std::result::Result); - -impl Default for SDKResult { - fn default() -> Self { - Err(Default::default()).into() - } -} - -impl Deref for SDKResult { - type Target = std::result::Result; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl From> for SDKResult { - fn from(ptr: std::result::Result) -> Self { - SDKResult(ptr) - } -} - -impl Into> for SDKResult { - fn into(self) -> std::result::Result { - self.0 - } -} - -//TODO: Figure out a way to not have to use this for the lib_wrap_option in the sdk -impl<'a> From> for SDKResult> { - fn from(res: FfiStr<'a>) -> Self { - Ok(res).into() - } -} - -impl From for SDKResult { - fn from(res: c_int) -> Self { - if res >= 0 { - Ok(res).into() - } else { - Err(WootingAnalogResult::from_i32(res).unwrap_or(WootingAnalogResult::Failure)).into() - } - } -} - -impl From for SDKResult { - fn from(res: c_int) -> Self { - if res >= 0 { - Ok(res as u32).into() - } else { - Err(WootingAnalogResult::from_i32(res).unwrap_or(WootingAnalogResult::Failure)).into() - } - } -} +pub type SDKResult = Result; impl Into for WootingAnalogResult { fn into(self) -> c_int { @@ -335,74 +281,12 @@ impl Into for WootingAnalogResult { } } -impl From for SDKResult { - fn from(res: u32) -> Self { - Ok(res).into() - } -} - -impl Into for SDKResult { - fn into(self) -> i32 { - match self.0 { - Ok(v) => v as i32, - Err(e) => e.into(), - } - } -} - -impl Into for SDKResult { - fn into(self) -> c_int { - match self.0 { - Ok(v) => v, - Err(e) => e.into(), - } - } -} - -impl From for SDKResult { - fn from(res: f32) -> Self { - if res >= 0.0 { - Ok(res).into() - } else { - Err(WootingAnalogResult::from_f32(res).unwrap_or(WootingAnalogResult::Failure)).into() - } - } -} - impl Into for WootingAnalogResult { fn into(self) -> f32 { (self as i32) as f32 } } -impl Into for SDKResult { - fn into(self) -> f32 { - match self.0 { - Ok(v) => v, - Err(e) => e.into(), - } - } -} - -impl Into for SDKResult<()> { - fn into(self) -> WootingAnalogResult { - match self.0 { - Ok(_) => WootingAnalogResult::Ok, - Err(e) => e, - } - } -} - -impl From for SDKResult<()> { - fn from(res: WootingAnalogResult) -> Self { - if res.is_ok() { - Ok(()).into() - } else { - Err(res).into() - } - } -} - impl Into for WootingAnalogResult { fn into(self) -> bool { self == WootingAnalogResult::Ok @@ -576,4 +460,4 @@ pub enum HIDCodes { RightShift = 0xe5, //SHIFT_RIGHT RightAlt = 0xe6, //ALT_RIGHT RightMeta = 0xe7, //META_RIGHT -} +} \ No newline at end of file From e9207baf0a60488aee9fa5864df698dbdf01fe9e Mon Sep 17 00:00:00 2001 From: LastExceed Date: Sat, 19 Aug 2023 10:07:00 +0200 Subject: [PATCH 2/6] remove obsolete `.into()`s --- wooting-analog-plugin-dev/src/lib.rs | 2 +- wooting-analog-plugin/src/lib.rs | 68 ++++++------- wooting-analog-sdk/src/cplugin.rs | 23 ++--- wooting-analog-sdk/src/ffi.rs | 14 ++- wooting-analog-sdk/src/sdk.rs | 110 ++++++++++----------- wooting-analog-test-plugin/src/lib.rs | 20 ++-- wooting-analog-virtual-control/src/main.rs | 2 +- wooting-analog-wrapper/src/bin/main.rs | 7 +- wooting-analog-wrapper/src/lib.rs | 4 +- 9 files changed, 115 insertions(+), 135 deletions(-) diff --git a/wooting-analog-plugin-dev/src/lib.rs b/wooting-analog-plugin-dev/src/lib.rs index 39f6a65..bab04f4 100644 --- a/wooting-analog-plugin-dev/src/lib.rs +++ b/wooting-analog-plugin-dev/src/lib.rs @@ -103,4 +103,4 @@ mod ffi { }; super::generate_device_id(&serial, vendor_id, product_id) } -} +} \ No newline at end of file diff --git a/wooting-analog-plugin/src/lib.rs b/wooting-analog-plugin/src/lib.rs index 4254ea9..0d999ca 100644 --- a/wooting-analog-plugin/src/lib.rs +++ b/wooting-analog-plugin/src/lib.rs @@ -75,13 +75,13 @@ trait DeviceImplementation: objekt::Clone + Send { Ok(len) => { // If the length is 0 then that means the read timed out, so we shouldn't use it to update values if len == 0 { - return Ok(None).into(); + return Ok(None); } } Err(e) => { error!("Failed to read buffer: {}", e); - return Err(WootingAnalogResult::DeviceDisconnected).into(); + return Err(WootingAnalogResult::DeviceDisconnected); } } //println!("{:?}", buffer); @@ -98,7 +98,6 @@ trait DeviceImplementation: objekt::Clone + Send { }) .collect(), )) - .into() } /// Get the unique device ID from the given `device_info` @@ -276,10 +275,7 @@ impl Device { return 0; } - match device_impl - .get_analog_buffer(&device, ANALOG_MAX_SIZE) - .into() - { + match device_impl.get_analog_buffer(&device, ANALOG_MAX_SIZE) { Ok(data) => { if let Some(data) = data { let mut m = t_buffer.lock().unwrap(); @@ -324,7 +320,7 @@ impl Device { } fn read_analog(&mut self, code: u16) -> SDKResult { - (*self.buffer.lock().unwrap().get(&code).unwrap_or(&0.0)).into() + Ok(*self.buffer.lock().unwrap().get(&code).unwrap_or(&0.0)) } fn read_full_buffer(&mut self, _max_length: usize) -> SDKResult> { @@ -342,7 +338,7 @@ impl Device { //Store the newPressedKeys for the next call self.pressed_keys = new_pressed_keys; - Ok(buffer).into() + Ok(buffer) } } @@ -427,7 +423,7 @@ impl WootingPlugin { } Err(e) => { error!("Error opening HID Device: {}", e); - //return WootingAnalogResult::Failure.into(); + //return WootingAnalogResult::Failure; } } } @@ -456,7 +452,7 @@ impl WootingPlugin { } Err(e) => { error!("Error obtaining HIDAPI: {}", e); - return Err(WootingAnalogResult::Failure).into(); + return Err(WootingAnalogResult::Failure); } }; @@ -493,13 +489,13 @@ impl WootingPlugin { }) }); debug!("Started timer"); - Ok(self.devices.lock().unwrap().len() as u32).into() + Ok(self.devices.lock().unwrap().len() as u32) } } impl Plugin for WootingPlugin { fn name(&mut self) -> SDKResult<&'static str> { - Ok(PLUGIN_NAME).into() + Ok(PLUGIN_NAME) } fn initialise( @@ -530,20 +526,20 @@ impl Plugin for WootingPlugin { fn read_analog(&mut self, code: u16, device_id: DeviceID) -> SDKResult { if !self.initialised { - return Err(WootingAnalogResult::UnInitialized).into(); + return Err(WootingAnalogResult::UnInitialized); } if self.devices.lock().unwrap().is_empty() { - return Err(WootingAnalogResult::NoDevices).into(); + return Err(WootingAnalogResult::NoDevices); } //If the Device ID is 0 we want to go through all the connected devices //and combine the analog values if device_id == 0 { let mut analog: f32 = -1.0; - let mut error: WootingAnalogResult = WootingAnalogResult::Ok; + let mut error = WootingAnalogResult::Ok; for (_id, device) in self.devices.lock().unwrap().iter_mut() { - match device.read_analog(code).into() { + match device.read_analog(code) { Ok(val) => { analog = analog.max(val); } @@ -554,19 +550,19 @@ impl Plugin for WootingPlugin { } if analog < 0.0 { - Err(error).into() + Err(error) } else { - analog.into() + analog } } else //If the device id is not 0, we try and find a connected device with that ID and read from it { match self.devices.lock().unwrap().get_mut(&device_id) { - Some(device) => match device.read_analog(code).into() { - Ok(val) => val.into(), - Err(e) => Err(e).into(), + Some(device) => match device.read_analog(code) { + Ok(val) => val, + Err(e) => Err(e), }, - None => Err(WootingAnalogResult::NoDevices).into(), + None => Err(WootingAnalogResult::NoDevices), } } } @@ -577,11 +573,11 @@ impl Plugin for WootingPlugin { device_id: DeviceID, ) -> SDKResult> { if !self.initialised { - return Err(WootingAnalogResult::UnInitialized).into(); + return Err(WootingAnalogResult::UnInitialized); } if self.devices.lock().unwrap().is_empty() { - return Err(WootingAnalogResult::NoDevices).into(); + return Err(WootingAnalogResult::NoDevices); } //If the Device ID is 0 we want to go through all the connected devices @@ -589,9 +585,9 @@ impl Plugin for WootingPlugin { if device_id == 0 { let mut analog: HashMap = HashMap::new(); let mut any_read = false; - let mut error: WootingAnalogResult = WootingAnalogResult::Ok; + let mut error = WootingAnalogResult::Ok; for (_id, device) in self.devices.lock().unwrap().iter_mut() { - match device.read_full_buffer(max_length).into() { + match device.read_full_buffer(max_length) { Ok(val) => { any_read = true; analog.extend(val); @@ -603,26 +599,26 @@ impl Plugin for WootingPlugin { } if !any_read { - Err(error).into() + Err(error) } else { - Ok(analog).into() + Ok(analog) } } else //If the device id is not 0, we try and find a connected device with that ID and read from it { match self.devices.lock().unwrap().get_mut(&device_id) { - Some(device) => match device.read_full_buffer(max_length).into() { - Ok(val) => Ok(val).into(), - Err(e) => Err(e).into(), + Some(device) => match device.read_full_buffer(max_length) { + Ok(val) => Ok(val), + Err(e) => Err(e), }, - None => Err(WootingAnalogResult::NoDevices).into(), + None => Err(WootingAnalogResult::NoDevices), } } } fn device_info(&mut self) -> SDKResult> { if !self.initialised { - return Err(WootingAnalogResult::UnInitialized).into(); + return Err(WootingAnalogResult::UnInitialized); } let mut devices = vec![]; @@ -630,8 +626,8 @@ impl Plugin for WootingPlugin { devices.push(device.device_info.clone()); } - Ok(devices).into() + Ok(devices) } } -declare_plugin!(WootingPlugin, WootingPlugin::new); +declare_plugin!(WootingPlugin, WootingPlugin::new); \ No newline at end of file diff --git a/wooting-analog-sdk/src/cplugin.rs b/wooting-analog-sdk/src/cplugin.rs index 18fff80..4c77bd2 100644 --- a/wooting-analog-sdk/src/cplugin.rs +++ b/wooting-analog-sdk/src/cplugin.rs @@ -57,7 +57,7 @@ macro_rules! lib_wrap_option { }).ok(); match func { Some(f) => f($($fn_arg_names),*).into(), - _ => Err(WootingAnalogResult::FunctionNotFound).into() + _ => Err(WootingAnalogResult::FunctionNotFound) } } @@ -85,7 +85,7 @@ impl CPlugin { "CPlugin ABI version does not match! Given: {}, Expected: {}", v, CPLUGIN_ABI_VERSION ); - return Err(WootingAnalogResult::IncompatibleVersion).into(); + return Err(WootingAnalogResult::IncompatibleVersion); } } } @@ -94,7 +94,6 @@ impl CPlugin { lib, //funcs: HashMap::new() }) - .into() } lib_wrap_option! { @@ -131,7 +130,7 @@ extern "C" fn call_closure(data: *mut c_void, event: DeviceEventType, device_raw impl Plugin for CPlugin { fn name(&mut self) -> SDKResult<&'static str> { - self._name().0.map(|s| s.as_str()).into() + self._name().map(|s| s.as_str()) } fn initialise( @@ -140,9 +139,7 @@ impl Plugin for CPlugin { ) -> SDKResult { let data = Box::into_raw(Box::new(callback)); self._initialise(data as *mut _, call_closure) - .0 .map(|res| res as u32) - .into() } fn read_full_buffer( @@ -161,11 +158,10 @@ impl Plugin for CPlugin { analog_buffer.as_ptr(), max_length as c_uint, device, - ) - .0; + ); if let Err(e) = ret { //debug!("Error got: {:?}",e); - return Err(e).into(); + return Err(e); } let ret = ret.unwrap(); max_length.min(ret as usize) @@ -177,7 +173,7 @@ impl Plugin for CPlugin { analog_data.insert(code_buffer[i], analog_buffer[i]); } - Ok(analog_data).into() + Ok(analog_data) } fn device_info(&mut self) -> SDKResult> { @@ -185,7 +181,6 @@ impl Plugin for CPlugin { match self ._device_info(device_infos.as_mut_ptr(), device_infos.len() as c_uint) - .0 .map(|no| no as u32) { Ok(num) => unsafe { @@ -203,9 +198,9 @@ impl Plugin for CPlugin { device }) .collect(); - Ok(devices).into() + Ok(devices) }, - Err(e) => Err(e).into(), + Err(e) => Err(e), } } @@ -217,4 +212,4 @@ impl Plugin for CPlugin { fn read_analog(code: u16, device: DeviceID) -> f32; //fn neg(x: u32, y: u32) -> u32; } -} +} \ No newline at end of file diff --git a/wooting-analog-sdk/src/ffi.rs b/wooting-analog-sdk/src/ffi.rs index 84d3559..c723020 100644 --- a/wooting-analog-sdk/src/ffi.rs +++ b/wooting-analog-sdk/src/ffi.rs @@ -32,10 +32,10 @@ pub extern "C" fn wooting_analog_initialise() -> c_int { trace!("catch unwind result: {:?}", result); match result { Ok(c) => c, - Err(e) =>{ + Err(e) => { error!("An error occurred in wooting_analog_initialise: {:?}", e); WootingAnalogResult::Failure.into() - } , + } } } @@ -226,7 +226,7 @@ pub extern "C" fn wooting_analog_get_connected_devices_info( len: c_uint, ) -> c_int { let result: SDKResult> = ANALOG_SDK.lock().unwrap().get_device_info(); - match result.0 { + match result { Ok(mut devices) => { let device_no = (len as usize).min(devices.len()); @@ -258,7 +258,7 @@ pub extern "C" fn wooting_analog_get_connected_devices_info( }); device_no as i32 } - Err(e) => e.into(), + Err(e) => e, } } @@ -327,7 +327,6 @@ pub extern "C" fn wooting_analog_read_full_buffer_device( .lock() .unwrap() .read_full_buffer(len as usize, device_id) - .0 { Ok(analog_data) => { //Fill up given slices @@ -445,8 +444,7 @@ mod tests { assert!(!wooting_analog_is_initialised()); assert_eq!( get_sdk() - .initialise_with_plugin_path(dir.as_str(), !dir.ends_with("debug")) - .0, + .initialise_with_plugin_path(dir.as_str(), !dir.ends_with("debug")), Ok(0) ); assert!(wooting_analog_is_initialised()); @@ -681,4 +679,4 @@ mod tests { wooting_analog_initialise(); assert_eq!(wooting_analog_uninitialise(), WootingAnalogResult::Ok); } -} +} \ No newline at end of file diff --git a/wooting-analog-sdk/src/sdk.rs b/wooting-analog-sdk/src/sdk.rs index 87819cd..e151103 100644 --- a/wooting-analog-sdk/src/sdk.rs +++ b/wooting-analog-sdk/src/sdk.rs @@ -73,7 +73,7 @@ impl AnalogSDK { let plugin_dir = PathBuf::from(plugin_dir); if !plugin_dir.is_dir() { error!("The plugin directory '{:?}' does not exist! Make sure you have it created and have plugins in there", plugin_dir); - return Err(WootingAnalogResult::NoPlugins).into(); + return Err(WootingAnalogResult::NoPlugins); } /*let mut plugin_dir = match plugin_dir { Ok(v) => { @@ -148,7 +148,7 @@ impl AnalogSDK { }, )); debug!("{:?}", ret); - if let Ok(num) = ret.0 { + if let Ok(num) = ret { plugins_initialised += 1; device_no += num; } @@ -157,9 +157,9 @@ impl AnalogSDK { self.initialised = plugins_initialised > 0; if !self.initialised { - Err(WootingAnalogResult::NoPlugins).into() + Err(WootingAnalogResult::NoPlugins) } else { - Ok(device_no).into() + Ok(device_no) } } @@ -269,7 +269,7 @@ impl AnalogSDK { None => { info!("Didn't find _plugin_create, assuming it's a C plugin"); let lib = self.loaded_libraries.pop().unwrap(); - match CPlugin::new(lib).0 { + match CPlugin::new(lib) { Ok(cplugin) => Box::new(cplugin), Err(WootingAnalogResult::IncompatibleVersion) => { bail!( @@ -283,7 +283,7 @@ impl AnalogSDK { } }; let name = plugin.name(); - match name.0 { + match name { Ok(name) => { info!("Loaded plugin: {:?}", name); //plugin.on_plugin_load(); @@ -305,28 +305,28 @@ impl AnalogSDK { cb: impl Fn(DeviceEventType, DeviceInfo) + 'static + Send, ) -> SDKResult<()> { if !self.initialised { - return WootingAnalogResult::UnInitialized.into(); + return Err(WootingAnalogResult::UnInitialized); } self.device_event_callback .lock() .unwrap() .replace(Box::new(cb)); - Ok(()).into() + Ok(()) } pub fn clear_device_event_cb(&mut self) -> SDKResult<()> { if !self.initialised { - return Err(WootingAnalogResult::UnInitialized).into(); + return Err(WootingAnalogResult::UnInitialized); } self.device_event_callback.lock().unwrap().take(); - Ok(()).into() + Ok(()) } pub fn get_device_info(&mut self) -> SDKResult> { if !self.initialised { - return Err(WootingAnalogResult::UnInitialized).into(); + return Err(WootingAnalogResult::UnInitialized); } let mut devices: Vec = vec![]; let mut error: WootingAnalogResult = WootingAnalogResult::Ok; @@ -336,7 +336,7 @@ impl AnalogSDK { } //Give a reference to the buffer at the point where there is free space - match p.device_info().0 { + match p.device_info() { Ok(mut p_devices) => { devices.append(&mut p_devices); } @@ -351,15 +351,15 @@ impl AnalogSDK { } } if devices.is_empty() && !error.is_ok() { - Err(error).into() + Err(error) } else { - Ok(devices).into() + Ok(devices) } } pub fn read_analog(&mut self, code: u16, device_id: DeviceID) -> SDKResult { if !self.initialised { - return Err(WootingAnalogResult::UnInitialized).into(); + return Err(WootingAnalogResult::UnInitialized); } //Try and map the given keycode to HID @@ -369,7 +369,7 @@ impl AnalogSDK { let mut err = WootingAnalogResult::Ok; for p in self.plugins.iter_mut() { - match p.read_analog(hid_code, device_id).into() { + match p.read_analog(hid_code, device_id) { Ok(x) => { value = value.max(x); //If we were looking to read from a specific device, we've found that read, so no need to continue @@ -385,12 +385,12 @@ impl AnalogSDK { } if value < 0.0 { - return Err(err).into(); + return Err(err); } - value.into() + Ok(value) } else { - Err(WootingAnalogResult::NoMapping).into() + Err(WootingAnalogResult::NoMapping) } } @@ -400,7 +400,7 @@ impl AnalogSDK { device_id: DeviceID, ) -> SDKResult> { if !self.initialised { - return Err(WootingAnalogResult::UnInitialized).into(); + return Err(WootingAnalogResult::UnInitialized); } let mut analog_data: HashMap = HashMap::with_capacity(max_length); @@ -410,8 +410,7 @@ impl AnalogSDK { //Read from all and add up for p in self.plugins.iter_mut() { let plugin_data = p - .read_full_buffer(max_length - analog_data.len(), device_id) - .into(); + .read_full_buffer(max_length - analog_data.len(), device_id); match plugin_data { Ok(mut data) => { for (hid_code, analog) in data.drain() { @@ -444,10 +443,10 @@ impl AnalogSDK { } } if !any_success { - return Err(err).into(); + return Err(err); } - Ok(analog_data).into() + Ok(analog_data) } /// Unload all plugins and loaded plugin libraries, making sure to fire @@ -455,7 +454,7 @@ impl AnalogSDK { pub fn unload(&mut self) { debug!("Unloading plugins"); for mut plugin in self.plugins.drain(..) { - let name = plugin.name().0; + let name = plugin.name(); trace!("Firing on_plugin_unload for {:?}", name); plugin.unload(); debug!("Unload successful for {:?}", name); @@ -529,7 +528,7 @@ mod tests { //Don't need to use the Singleton instance of the SDK here as we're not actually gonna initialise it let mut sdk = AnalogSDK::new(); assert_eq!( - sdk.initialise_with_plugin_path(dir, true).0, + sdk.initialise_with_plugin_path(dir, true), Err(WootingAnalogResult::NoPlugins) ); assert!(!sdk.initialised); @@ -546,7 +545,7 @@ mod tests { //Don't need to use the Singleton instance of the SDK here as we're not actually gonna initialise it let mut sdk = AnalogSDK::new(); assert_eq!( - sdk.initialise_with_plugin_path(dir, true).0, + sdk.initialise_with_plugin_path(dir, true), Err(WootingAnalogResult::NoPlugins) ); assert!(!sdk.initialised) @@ -597,8 +596,7 @@ mod tests { assert!(!sdk().initialised); assert_eq!( sdk() - .initialise_with_plugin_path(dir.as_str(), !dir.ends_with("debug")) - .0, + .initialise_with_plugin_path(dir.as_str(), !dir.ends_with("debug")), Ok(0) ); assert!(sdk().initialised); @@ -626,13 +624,12 @@ mod tests { *got_connected_borrow.lock().unwrap() = event == DeviceEventType::Connected; if event == DeviceEventType::Connected { debug!("Started reading"); - assert_eq!(sdk_borrow.lock().unwrap().read_analog(1, 0).0, Ok(0.0)); + assert_eq!(sdk_borrow.lock().unwrap().read_analog(1, 0), Ok(0.0)); assert_eq!( sdk_borrow .lock() .unwrap() .get_device_info() - .0 .map(|dev| dev.len()), Ok(1) ); @@ -651,7 +648,7 @@ mod tests { //Check that we now have one device { - assert_eq!(sdk().get_device_info().0.map(|dev| dev.len()), Ok(1)); + assert_eq!(sdk().get_device_info().map(|dev| dev.len()), Ok(1)); } //Check the cb is called with disconnected @@ -665,7 +662,7 @@ mod tests { //Check that we now have no devices { - assert_eq!(sdk().get_device_info().0.map(|dev| dev.len()), Ok(0)); + assert_eq!(sdk().get_device_info().map(|dev| dev.len()), Ok(0)); } let analog_val = 0xF4; @@ -682,20 +679,20 @@ mod tests { wait_for_connected(&got_connected, 5, true); //Check we get the val with no id specified - assert_eq!(sdk().read_analog(analog_key as u16, 0).0, Ok(f_analog_val)); + assert_eq!(sdk().read_analog(analog_key as u16, 0), Ok(f_analog_val)); //Check we get the val with the device_id we use assert_eq!( - sdk().read_analog(analog_key as u16, device_id).0, + sdk().read_analog(analog_key as u16, device_id), Ok(f_analog_val) ); //Check we don't get a val with invalid device id assert_eq!( - sdk().read_analog(analog_key as u16, device_id + 1).0, + sdk().read_analog(analog_key as u16, device_id + 1), Err(WootingAnalogResult::NoDevices) ); //Check if the next value is 0 assert_eq!( - sdk().read_analog((analog_key + 1) as u16, device_id).0, + sdk().read_analog((analog_key + 1) as u16, device_id), Ok(0.0) ); @@ -706,14 +703,13 @@ mod tests { .read_analog( hid_to_code(analog_key as u16, &KeycodeType::ScanCode1).unwrap(), device_id - ) - .0, + ), Ok(f_analog_val) ); sdk().keycode_mode = KeycodeType::HID; let buffer_len = 5; - let analog_data = sdk().read_full_buffer(buffer_len, 0).0.unwrap(); + let analog_data = sdk().read_full_buffer(buffer_len, 0).unwrap(); //Check it reads buffer properly with no device id assert_eq!(analog_data.len(), 1); assert_eq!( @@ -721,7 +717,7 @@ mod tests { Some((&(analog_key as u16), &f_analog_val)) ); - let analog_data = sdk().read_full_buffer(buffer_len, device_id).0.unwrap(); + let analog_data = sdk().read_full_buffer(buffer_len, device_id).unwrap(); //Check it reads buffer properly with proper device_id assert_eq!(analog_data.len(), 1); assert_eq!( @@ -731,13 +727,13 @@ mod tests { //Check it errors on read buffer with invalid device_id assert_eq!( - sdk().read_full_buffer(buffer_len, device_id + 1).0, + sdk().read_full_buffer(buffer_len, device_id + 1), Err(WootingAnalogResult::NoDevices) ); //Check that it does code mapping sdk().keycode_mode = KeycodeType::ScanCode1; - let analog_data = sdk().read_full_buffer(buffer_len, device_id).0.unwrap(); + let analog_data = sdk().read_full_buffer(buffer_len, device_id).unwrap(); assert_eq!(analog_data.len(), 1); assert_eq!( analog_data.iter().next(), @@ -753,14 +749,14 @@ mod tests { shared_state.analog_values[analog_key] = 0; } ::std::thread::sleep(Duration::from_secs(1)); - let analog_data = sdk().read_full_buffer(buffer_len, device_id).0.unwrap(); + let analog_data = sdk().read_full_buffer(buffer_len, device_id).unwrap(); //Check that it is returning the released key in the next call assert_eq!(analog_data.len(), 1); assert_eq!(analog_data[&(analog_key as u16)], 0.0); - assert_eq!(sdk().read_analog(analog_key as u16, 0).0, Ok(0.0)); + assert_eq!(sdk().read_analog(analog_key as u16, 0), Ok(0.0)); - let analog_data = sdk().read_full_buffer(buffer_len, device_id).0; + let analog_data = sdk().read_full_buffer(buffer_len, device_id); assert_eq!(analog_data.unwrap().len(), 0); sdk().clear_device_event_cb(); @@ -794,7 +790,7 @@ mod tests { let dir = format!("./{}/build/", TEST_PLUGIN_DIR); info!("Loading plugins from: {:?}", dir); assert!(!sdk.initialised); - assert_eq!(sdk.initialise_with_plugin_path(dir.as_str(), true).0, Ok(1)); + assert_eq!(sdk.initialise_with_plugin_path(dir.as_str(), true), Ok(1)); assert!(sdk.initialised); let got_cb = Arc::new(AtomicBool::new(false)); let got_cb_inner = got_cb.clone(); @@ -808,11 +804,11 @@ mod tests { assert_eq!(device.device_id, 7); }); - assert_eq!(sdk.read_analog(30, 0).0, Ok(0.56)); + assert_eq!(sdk.read_analog(30, 0), Ok(0.56)); //We told it to execute the callback when read_analog is called so let's just call it a second time to ensure it can be called multiple times without dying - assert_eq!(sdk.read_analog(30, 0).0, Ok(0.56)); - assert_eq!(sdk.read_full_buffer(30, 0).0.unwrap().get(&5), Some(&0.4)); - let device = sdk.get_device_info().0.unwrap().first().unwrap().clone(); + assert_eq!(sdk.read_analog(30, 0), Ok(0.56)); + assert_eq!(sdk.read_full_buffer(30, 0).unwrap().get(&5), Some(&0.4)); + let device = sdk.get_device_info().unwrap().first().unwrap().clone(); assert_eq!(device.device_id, 7); //Wait a wee bit to ensure the callback has been executed @@ -841,25 +837,25 @@ mod tests { fn uninitialised_sdk_functions(sdk: &mut AnalogSDK) { assert_eq!(sdk.initialised, false); assert_eq!( - sdk.set_device_event_cb(cb).0, + sdk.set_device_event_cb(cb), Err(WootingAnalogResult::UnInitialized) ); assert_eq!( - sdk.clear_device_event_cb().0, + sdk.clear_device_event_cb(), Err(WootingAnalogResult::UnInitialized) ); assert_eq!( - sdk.read_analog(0, 0).0, + sdk.read_analog(0, 0), Err(WootingAnalogResult::UnInitialized) ); assert_eq!( - sdk.get_device_info().0.err(), + sdk.get_device_info().err(), Some(WootingAnalogResult::UnInitialized) ); assert_eq!( - sdk.read_full_buffer(0, 0).0, + sdk.read_full_buffer(0, 0), Err(WootingAnalogResult::UnInitialized) ); } -} +} \ No newline at end of file diff --git a/wooting-analog-test-plugin/src/lib.rs b/wooting-analog-test-plugin/src/lib.rs index 31d476c..d7937ba 100644 --- a/wooting-analog-test-plugin/src/lib.rs +++ b/wooting-analog-test-plugin/src/lib.rs @@ -224,7 +224,7 @@ fn from_ut8f_to_null(bytes: &[u8], max_len: usize) -> &str { impl Plugin for WootingAnalogTestPlugin { fn name(&mut self) -> SDKResult<&'static str> { - Ok("Wooting Analog Test Plugin").into() + Ok("Wooting Analog Test Plugin") } fn initialise( @@ -235,8 +235,7 @@ impl Plugin for WootingAnalogTestPlugin { Ok(1) } else { Ok(0) - } - .into(); + }; self.device_event_cb.lock().unwrap().replace(cb); ret } @@ -267,12 +266,12 @@ impl Plugin for WootingAnalogTestPlugin { } debug!("Finished with devices"); - Ok(devices).into() + Ok(devices) } fn read_analog(&mut self, code: u16, device: u64) -> SDKResult { if !*self.device_connected.lock().unwrap() { - return Err(WootingAnalogResult::NoDevices).into(); + return Err(WootingAnalogResult::NoDevices); } if device == 0 || device == *self.device_id.lock().unwrap() { @@ -284,9 +283,8 @@ impl Plugin for WootingAnalogTestPlugin { .cloned() .or(Some(0.0)) .unwrap()) - .into() } else { - Err(WootingAnalogResult::NoDevices).into() + Err(WootingAnalogResult::NoDevices) } } @@ -296,7 +294,7 @@ impl Plugin for WootingAnalogTestPlugin { device: u64, ) -> SDKResult> { if !*self.device_connected.lock().unwrap() { - return Err(WootingAnalogResult::NoDevices).into(); + return Err(WootingAnalogResult::NoDevices); } if device == 0 || device == *self.device_id.lock().unwrap() { @@ -314,9 +312,9 @@ impl Plugin for WootingAnalogTestPlugin { //Store the newPressedKeys for the next call self.pressed_keys = new_pressed_keys; - Ok(buffer).into() + Ok(buffer) } else { - Err(WootingAnalogResult::NoDevices).into() + Err(WootingAnalogResult::NoDevices) } } } @@ -329,4 +327,4 @@ mod tests { // fn it_works() { // assert_eq!(2 + 2, 4); // } -} +} \ No newline at end of file diff --git a/wooting-analog-virtual-control/src/main.rs b/wooting-analog-virtual-control/src/main.rs index 8ba9b3b..e1e9824 100644 --- a/wooting-analog-virtual-control/src/main.rs +++ b/wooting-analog-virtual-control/src/main.rs @@ -436,4 +436,4 @@ fn main() -> Result<(), iced::Error> { exit_on_close_request: true, ..Settings::default() }) -} +} \ No newline at end of file diff --git a/wooting-analog-wrapper/src/bin/main.rs b/wooting-analog-wrapper/src/bin/main.rs index 43de501..78df22d 100644 --- a/wooting-analog-wrapper/src/bin/main.rs +++ b/wooting-analog-wrapper/src/bin/main.rs @@ -11,7 +11,7 @@ fn main() { assert!(!sdk::is_initialised()); let init_result: SDKResult = sdk::initialise(); - match init_result.0 { + match init_result { Ok(device_num) => { assert!(sdk::is_initialised()); println!("SDK Successfully initialised with {} devices", device_num); @@ -51,7 +51,6 @@ fn use_sdk(device_num: u32) { // get_connected_devices_info() -> SDKResult> let devices: Vec = sdk::get_connected_devices_info(DEVICE_BUFFER_MAX) - .0 .unwrap(); assert_eq!(device_num, devices.len() as u32); for (i, device) in devices.iter().enumerate() { @@ -63,7 +62,7 @@ fn use_sdk(device_num: u32) { let mut new_output: Option = None; let read_result: SDKResult> = sdk::read_full_buffer(ANALOG_BUFFER_READ_MAX); - match read_result.0 { + match read_result { Ok(analog_data) => { let mut sorted_data = analog_data.iter().collect::>(); sorted_data.sort_by_key(|x| x.0); @@ -91,4 +90,4 @@ fn use_sdk(device_num: u32) { } } } -} +} \ No newline at end of file diff --git a/wooting-analog-wrapper/src/lib.rs b/wooting-analog-wrapper/src/lib.rs index 1e85ce2..983688f 100644 --- a/wooting-analog-wrapper/src/lib.rs +++ b/wooting-analog-wrapper/src/lib.rs @@ -140,7 +140,6 @@ pub fn get_connected_devices_info(max_devices: usize) -> SDKResult SDKResult> { return read_full_buffer_device(max_items, 0); -} +} \ No newline at end of file From 2c0d8a731d5c305cd532e0d88240524840982684 Mon Sep 17 00:00:00 2001 From: LastExceed Date: Sat, 19 Aug 2023 10:07:46 +0200 Subject: [PATCH 3/6] derive `Default` --- wooting-analog-common/src/lib.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/wooting-analog-common/src/lib.rs b/wooting-analog-common/src/lib.rs index b7275fe..5e79a1d 100644 --- a/wooting-analog-common/src/lib.rs +++ b/wooting-analog-common/src/lib.rs @@ -217,7 +217,7 @@ pub enum DeviceEventType { } #[cfg_attr(feature = "serdes", derive(Serialize, Deserialize))] -#[derive(Debug, PartialEq, Clone, Primitive, Error)] +#[derive(Debug, Default, PartialEq, Clone, Primitive, Error)] #[repr(C)] pub enum WootingAnalogResult { #[error("All OK")] @@ -241,6 +241,7 @@ pub enum WootingAnalogResult { #[error("No Plugins were found")] NoPlugins = -1995isize, /// The specified function was not found in the library + #[default] #[error("The specified function was not found in the library")] FunctionNotFound = -1994isize, /// No Keycode mapping to HID was found for the given Keycode @@ -267,12 +268,6 @@ impl WootingAnalogResult { } } -impl Default for WootingAnalogResult { - fn default() -> Self { - WootingAnalogResult::FunctionNotFound - } -} - pub type SDKResult = Result; impl Into for WootingAnalogResult { From 4475742db2a918a9242ed516202a343a421447a1 Mon Sep 17 00:00:00 2001 From: LastExceed Date: Mon, 21 Aug 2023 20:45:07 +0200 Subject: [PATCH 4/6] re-add EOF linebreaks --- wooting-analog-common/src/lib.rs | 2 +- wooting-analog-plugin-dev/src/lib.rs | 2 +- wooting-analog-plugin/src/lib.rs | 2 +- wooting-analog-sdk/src/cplugin.rs | 2 +- wooting-analog-sdk/src/ffi.rs | 2 +- wooting-analog-sdk/src/sdk.rs | 2 +- wooting-analog-test-plugin/src/lib.rs | 2 +- wooting-analog-virtual-control/src/main.rs | 2 +- wooting-analog-wrapper/src/bin/main.rs | 2 +- wooting-analog-wrapper/src/lib.rs | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/wooting-analog-common/src/lib.rs b/wooting-analog-common/src/lib.rs index 5e79a1d..652e63a 100644 --- a/wooting-analog-common/src/lib.rs +++ b/wooting-analog-common/src/lib.rs @@ -455,4 +455,4 @@ pub enum HIDCodes { RightShift = 0xe5, //SHIFT_RIGHT RightAlt = 0xe6, //ALT_RIGHT RightMeta = 0xe7, //META_RIGHT -} \ No newline at end of file +} diff --git a/wooting-analog-plugin-dev/src/lib.rs b/wooting-analog-plugin-dev/src/lib.rs index bab04f4..39f6a65 100644 --- a/wooting-analog-plugin-dev/src/lib.rs +++ b/wooting-analog-plugin-dev/src/lib.rs @@ -103,4 +103,4 @@ mod ffi { }; super::generate_device_id(&serial, vendor_id, product_id) } -} \ No newline at end of file +} diff --git a/wooting-analog-plugin/src/lib.rs b/wooting-analog-plugin/src/lib.rs index 0d999ca..54898db 100644 --- a/wooting-analog-plugin/src/lib.rs +++ b/wooting-analog-plugin/src/lib.rs @@ -630,4 +630,4 @@ impl Plugin for WootingPlugin { } } -declare_plugin!(WootingPlugin, WootingPlugin::new); \ No newline at end of file +declare_plugin!(WootingPlugin, WootingPlugin::new); diff --git a/wooting-analog-sdk/src/cplugin.rs b/wooting-analog-sdk/src/cplugin.rs index 4c77bd2..0011ec8 100644 --- a/wooting-analog-sdk/src/cplugin.rs +++ b/wooting-analog-sdk/src/cplugin.rs @@ -212,4 +212,4 @@ impl Plugin for CPlugin { fn read_analog(code: u16, device: DeviceID) -> f32; //fn neg(x: u32, y: u32) -> u32; } -} \ No newline at end of file +} diff --git a/wooting-analog-sdk/src/ffi.rs b/wooting-analog-sdk/src/ffi.rs index c723020..937da64 100644 --- a/wooting-analog-sdk/src/ffi.rs +++ b/wooting-analog-sdk/src/ffi.rs @@ -679,4 +679,4 @@ mod tests { wooting_analog_initialise(); assert_eq!(wooting_analog_uninitialise(), WootingAnalogResult::Ok); } -} \ No newline at end of file +} diff --git a/wooting-analog-sdk/src/sdk.rs b/wooting-analog-sdk/src/sdk.rs index e151103..3f50ca9 100644 --- a/wooting-analog-sdk/src/sdk.rs +++ b/wooting-analog-sdk/src/sdk.rs @@ -858,4 +858,4 @@ mod tests { Err(WootingAnalogResult::UnInitialized) ); } -} \ No newline at end of file +} diff --git a/wooting-analog-test-plugin/src/lib.rs b/wooting-analog-test-plugin/src/lib.rs index d7937ba..1e115d3 100644 --- a/wooting-analog-test-plugin/src/lib.rs +++ b/wooting-analog-test-plugin/src/lib.rs @@ -327,4 +327,4 @@ mod tests { // fn it_works() { // assert_eq!(2 + 2, 4); // } -} \ No newline at end of file +} diff --git a/wooting-analog-virtual-control/src/main.rs b/wooting-analog-virtual-control/src/main.rs index e1e9824..8ba9b3b 100644 --- a/wooting-analog-virtual-control/src/main.rs +++ b/wooting-analog-virtual-control/src/main.rs @@ -436,4 +436,4 @@ fn main() -> Result<(), iced::Error> { exit_on_close_request: true, ..Settings::default() }) -} \ No newline at end of file +} diff --git a/wooting-analog-wrapper/src/bin/main.rs b/wooting-analog-wrapper/src/bin/main.rs index 78df22d..2bba8bd 100644 --- a/wooting-analog-wrapper/src/bin/main.rs +++ b/wooting-analog-wrapper/src/bin/main.rs @@ -90,4 +90,4 @@ fn use_sdk(device_num: u32) { } } } -} \ No newline at end of file +} diff --git a/wooting-analog-wrapper/src/lib.rs b/wooting-analog-wrapper/src/lib.rs index 983688f..1b0845c 100644 --- a/wooting-analog-wrapper/src/lib.rs +++ b/wooting-analog-wrapper/src/lib.rs @@ -215,4 +215,4 @@ pub fn read_full_buffer_device( /// * `Err(NoDevices)`: Indicates no devices are connected pub fn read_full_buffer(max_items: usize) -> SDKResult> { return read_full_buffer_device(max_items, 0); -} \ No newline at end of file +} From 10b0e8d956dd6e6c6354feee4de45dabf5b8b5e0 Mon Sep 17 00:00:00 2001 From: LastExceed Date: Tue, 10 Oct 2023 20:42:07 +0200 Subject: [PATCH 5/6] add explicit result conversions --- wooting-analog-common/src/lib.rs | 64 +++++++++++++++++++++++++++++-- wooting-analog-plugin/src/lib.rs | 4 +- wooting-analog-sdk/src/ffi.rs | 10 ++--- wooting-analog-wrapper/src/lib.rs | 30 +++++++++------ 4 files changed, 87 insertions(+), 21 deletions(-) diff --git a/wooting-analog-common/src/lib.rs b/wooting-analog-common/src/lib.rs index 652e63a..de1e664 100644 --- a/wooting-analog-common/src/lib.rs +++ b/wooting-analog-common/src/lib.rs @@ -3,13 +3,12 @@ extern crate enum_primitive_derive; extern crate ffi_support; extern crate num_traits; -use ffi_support::FfiStr; +use std::convert::TryFrom; pub use num_traits::{FromPrimitive, ToPrimitive}; #[cfg(feature = "serdes")] use serde::{Deserialize, Serialize}; use std::ffi::{CStr, CString}; -use std::ops::Deref; -use std::os::raw::{c_char, c_int}; +use std::os::raw::{c_char, c_float, c_int}; use thiserror::Error; #[cfg(target_os = "macos")] @@ -266,10 +265,69 @@ impl WootingAnalogResult { pub fn is_ok_or_no_device(&self) -> bool { *self == WootingAnalogResult::Ok || *self == WootingAnalogResult::NoDevices } + + pub fn into_sdk_result(self) -> SDKResult<()> { + if self.is_ok() { Ok(()) } else { Err(self) } + } } pub type SDKResult = Result; +pub trait IntoSDKResultExt { + fn into_sdk_result(self) -> SDKResult; +} + +impl IntoSDKResultExt for c_int { + fn into_sdk_result(self) -> SDKResult { + u32::try_from(self).map_err(|_| + WootingAnalogResult::try_from(self) + .unwrap_or(WootingAnalogResult::Failure) + ) + } +} + +impl IntoSDKResultExt for f32 { + fn into_sdk_result(self) -> SDKResult { + match self { + //the RFC was made with matching exact values in mind, + //to prevent problems with structural vs semantic equality (e.g. -0 vs +0). + //there was never any consensus about how ranges should be treated. + //none of this matters though, as the RFC got rejected. the lint just hasn't been removed yet + #[allow(illegal_floating_point_literal_pattern)] + 0_f32..=1_f32 => Ok(self), + _ => (self as c_int).into_sdk_result().map(|v| v as f32) //wtf + } + } +} + +pub trait IntoWootingAnalogResultExt { + fn into_wooting_analog_result(self) -> WootingAnalogResult; +} + +impl IntoWootingAnalogResultExt for SDKResult<()> { + fn into_wooting_analog_result(self) -> WootingAnalogResult { + self.map(|_| WootingAnalogResult::Ok).unwrap_or_else(|e| e) //for some reason rust doesnt have .unwrap_err_or() + } +} + +pub trait IntoCResultExt { + fn into_c_result(self) -> T; +} + +impl IntoCResultExt for SDKResult { + fn into_c_result(self) -> c_int { + self.map(|value| value as _) + .unwrap_or_else(|error| error as _) + } +} + +impl IntoCResultExt for SDKResult { + fn into_c_result(self) -> c_float { + self.map(|value| value as _) + .unwrap_or_else(|error| error as isize as _) + } +} + impl Into for WootingAnalogResult { fn into(self) -> c_int { self as c_int diff --git a/wooting-analog-plugin/src/lib.rs b/wooting-analog-plugin/src/lib.rs index 54898db..49e62ef 100644 --- a/wooting-analog-plugin/src/lib.rs +++ b/wooting-analog-plugin/src/lib.rs @@ -552,14 +552,14 @@ impl Plugin for WootingPlugin { if analog < 0.0 { Err(error) } else { - analog + analog.into_sdk_result() } } else //If the device id is not 0, we try and find a connected device with that ID and read from it { match self.devices.lock().unwrap().get_mut(&device_id) { Some(device) => match device.read_analog(code) { - Ok(val) => val, + Ok(val) => val.into_sdk_result(), Err(e) => Err(e), }, None => Err(WootingAnalogResult::NoDevices), diff --git a/wooting-analog-sdk/src/ffi.rs b/wooting-analog-sdk/src/ffi.rs index 937da64..d2339e6 100644 --- a/wooting-analog-sdk/src/ffi.rs +++ b/wooting-analog-sdk/src/ffi.rs @@ -27,7 +27,7 @@ lazy_static! { pub extern "C" fn wooting_analog_initialise() -> c_int { let result = panic::catch_unwind(|| { trace!("wooting_analog_initialise called"); - ANALOG_SDK.lock().unwrap().initialise().into() + ANALOG_SDK.lock().unwrap().initialise().into_c_result() }); trace!("catch unwind result: {:?}", result); match result { @@ -166,7 +166,7 @@ pub extern "C" fn wooting_analog_read_analog_device( .lock() .unwrap() .read_analog(code, device_id) - .into() + .into_c_result() } /// Set the callback which is called when there is a DeviceEvent. Currently these events can either be Disconnected or Connected(Currently not properly implemented). @@ -196,7 +196,7 @@ pub extern "C" fn wooting_analog_set_device_event_cb( Box::from_raw(device_raw); } }) - .into() + .into_wooting_analog_result() } /// Clears the device event callback that has been set @@ -206,7 +206,7 @@ pub extern "C" fn wooting_analog_set_device_event_cb( /// * `UnInitialized`: The SDK is not initialised #[no_mangle] pub extern "C" fn wooting_analog_clear_device_event_cb() -> WootingAnalogResult { - ANALOG_SDK.lock().unwrap().clear_device_event_cb().into() + ANALOG_SDK.lock().unwrap().clear_device_event_cb().into_wooting_analog_result() } thread_local!(static CONNECTED_DEVICES: RefCell>> = RefCell::new(None)); @@ -258,7 +258,7 @@ pub extern "C" fn wooting_analog_get_connected_devices_info( }); device_no as i32 } - Err(e) => e, + Err(e) => e as _, } } diff --git a/wooting-analog-wrapper/src/lib.rs b/wooting-analog-wrapper/src/lib.rs index 1b0845c..5dab682 100644 --- a/wooting-analog-wrapper/src/lib.rs +++ b/wooting-analog-wrapper/src/lib.rs @@ -14,7 +14,8 @@ pub(crate) const SDK_ABI_VERSION: u32 = 0; /// Provides the major version of the SDK, a difference in this value to what is expected (SDK_ABI_VERSION) indicates that /// there may be some breaking changes that have been made so the SDK should not be attempted to be used pub fn version() -> SDKResult { - return unsafe { wooting_analog_version().into() }; + unsafe { wooting_analog_version() } + .into_sdk_result() } /// Initialises the Analog SDK, this needs to be successfully called before any other functions @@ -26,19 +27,21 @@ pub fn version() -> SDKResult { /// * `Err(FunctionNotFound)`: The SDK is either not installed or could not be found /// * `Err(IncompatibleVersion)`: The installed SDK is incompatible with this wrapper as they are on different Major versions pub fn initialise() -> SDKResult { - return unsafe { wooting_analog_initialise().into() }; + unsafe { wooting_analog_initialise() } + .into_sdk_result() } /// Returns a bool indicating if the Analog SDK has been initialised pub fn is_initialised() -> bool { - return unsafe { wooting_analog_is_initialised() }; + unsafe { wooting_analog_is_initialised() } } /// Uninitialises the SDK, returning it to an empty state, similar to how it would be before first initialisation /// # Expected Returns /// * `Ok(())`: Indicates that the SDK was successfully uninitialised pub fn uninitialise() -> SDKResult<()> { - return unsafe { wooting_analog_uninitialise().into() }; + unsafe { wooting_analog_uninitialise() } + .into_sdk_result() } /// Sets the type of Keycodes the Analog SDK will receive (in `read_analog`) and output (in `read_full_buffer`). @@ -56,7 +59,8 @@ pub fn uninitialise() -> SDKResult<()> { /// * `Err(NotAvailable)`: The given `KeycodeType` is present, but not supported on the current platform /// * `Err(UnInitialized)`: The SDK is not initialised pub fn set_keycode_mode(mode: KeycodeType) -> SDKResult<()> { - return unsafe { wooting_analog_set_keycode_mode(mode).into() }; + unsafe { wooting_analog_set_keycode_mode(mode) } + .into_sdk_result() } /// Reads the Analog value of the key with identifier `code` from any connected device. The set of key identifiers that is used @@ -80,7 +84,8 @@ pub fn set_keycode_mode(mode: KeycodeType) -> SDKResult<()> { /// * `Err(UnInitialized)`: The SDK is not initialised /// * `Err(NoDevices)`: There are no connected devices pub fn read_analog(code: u16) -> SDKResult { - return unsafe { wooting_analog_read_analog(code).into() }; + unsafe { wooting_analog_read_analog(code) } + .into_sdk_result() } /// Reads the Analog value of the key with identifier `code` from the device with id `device_id`. The set of key identifiers that is used @@ -94,7 +99,8 @@ pub fn read_analog(code: u16) -> SDKResult { /// * `Err(UnInitialized)`: The SDK is not initialised /// * `Err(NoDevices)`: There are no connected devices with id `device_id` pub fn read_analog_device(code: u16, device_id: DeviceID) -> SDKResult { - return unsafe { wooting_analog_read_analog_device(code, device_id).into() }; + unsafe { wooting_analog_read_analog_device(code, device_id) } + .into_sdk_result() } /// Set the callback which is called when there is a DeviceEvent. Currently these events can either be Disconnected or Connected(Currently not properly implemented). @@ -110,7 +116,8 @@ pub fn read_analog_device(code: u16, device_id: DeviceID) -> SDKResult { pub fn set_device_event_cb( cb: extern "C" fn(DeviceEventType, *mut DeviceInfo_FFI), //TODO: Make this accept a closure ) -> SDKResult<()> { - return unsafe { wooting_analog_set_device_event_cb(cb).into() }; + return unsafe { wooting_analog_set_device_event_cb(cb) } + .into_sdk_result(); } /// Clears the device event callback that has been set @@ -119,7 +126,8 @@ pub fn set_device_event_cb( /// * `Ok(())`: The callback was cleared successfully /// * `Err(UnInitialized)`: The SDK is not initialised pub fn clear_device_event_cb() -> SDKResult<()> { - return unsafe { wooting_analog_clear_device_event_cb().into() }; + unsafe { wooting_analog_clear_device_event_cb() } + .into_sdk_result() } /// Returns all connected devices with a max Vector return length of `max_devices` (as many that can fit in the buffer) @@ -137,7 +145,7 @@ pub fn get_connected_devices_info(max_devices: usize) -> SDKResult = wooting_analog_get_connected_devices_info(buffer.as_mut_ptr(), max_devices as c_uint) - .into(); + .into_sdk_result(); return ret .clone() @@ -182,7 +190,7 @@ pub fn read_full_buffer_device( max_items as u32, device_id, ) - .into(); + .into_sdk_result(); return ret .clone() From b6e05f71f11c19590c8969d57a2b51876e748f87 Mon Sep 17 00:00:00 2001 From: LastExceed Date: Tue, 10 Oct 2023 20:57:45 +0200 Subject: [PATCH 6/6] fix type mismatches originating from `lib_wrap_option!` macro --- wooting-analog-sdk/src/cplugin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wooting-analog-sdk/src/cplugin.rs b/wooting-analog-sdk/src/cplugin.rs index 0011ec8..6bd670e 100644 --- a/wooting-analog-sdk/src/cplugin.rs +++ b/wooting-analog-sdk/src/cplugin.rs @@ -56,7 +56,7 @@ macro_rules! lib_wrap_option { error!("{}", e); }).ok(); match func { - Some(f) => f($($fn_arg_names),*).into(), + Some(f) => Ok(f($($fn_arg_names),*)), _ => Err(WootingAnalogResult::FunctionNotFound) }