From fec5473244c45eeef20007cbded1b81b79555c33 Mon Sep 17 00:00:00 2001 From: Gabriele Musco Date: Tue, 27 Aug 2024 23:22:56 +0200 Subject: [PATCH] fix: use from trait for xrdevicerole to u32 --- src/ui/factories/device_row_factory.rs | 13 ++-- src/xr_devices.rs | 84 +++++++++++++------------- 2 files changed, 49 insertions(+), 48 deletions(-) diff --git a/src/ui/factories/device_row_factory.rs b/src/ui/factories/device_row_factory.rs index c7b5ef2..bacd4ce 100644 --- a/src/ui/factories/device_row_factory.rs +++ b/src/ui/factories/device_row_factory.rs @@ -69,13 +69,12 @@ impl From<&XRDevice> for DeviceRowModelInit { }), subtitle: Some(d.name.clone()), battery_status: d.battery.map(EnvisionBatteryStatus::from), - sort_index: d - .roles - .iter() - .min() - .unwrap_or(&XRDeviceRole::GenericTracker) - .as_number() - * 1000 + sort_index: u32::from( + d.roles + .iter() + .min() + .unwrap_or(&XRDeviceRole::GenericTracker), + ) * 1000 + d.index, ..Default::default() } diff --git a/src/xr_devices.rs b/src/xr_devices.rs index 5f8ee4c..814d2c4 100644 --- a/src/xr_devices.rs +++ b/src/xr_devices.rs @@ -65,18 +65,6 @@ impl Display for XRDeviceRole { } } -impl PartialOrd for XRDeviceRole { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.as_number().cmp(&other.as_number())) - } -} - -impl Ord for XRDeviceRole { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.partial_cmp(other).unwrap() - } -} - impl XRDeviceRole { pub fn iter() -> Iter<'static, Self> { [ @@ -134,35 +122,6 @@ impl XRDeviceRole { } } - pub fn as_number(&self) -> u32 { - match self { - Self::Head => 0, - Self::Eyes => 1, - Self::Left => 2, - Self::Right => 3, - Self::Gamepad => 4, - Self::HandTrackingLeft => 5, - Self::HandTrackingRight => 6, - - // the following are not in libmonado - Self::HandheldObject => 7, - Self::LeftFoot => 8, - Self::RightFoot => 9, - Self::LeftShoulder => 10, - Self::RightShoulder => 11, - Self::LeftElbow => 12, - Self::RightElbow => 13, - Self::LeftKnee => 14, - Self::RightKnee => 15, - Self::Waist => 16, - Self::Chest => 17, - Self::Camera => 18, - Self::Keyboard => 19, - - Self::GenericTracker => 20, - } - } - pub fn from_monado_str(s: &str) -> Option { match s { "head" => Some(Self::Head), @@ -219,6 +178,49 @@ impl XRDeviceRole { } } +impl From<&XRDeviceRole> for u32 { + fn from(value: &XRDeviceRole) -> Self { + match value { + XRDeviceRole::Head => 0, + XRDeviceRole::Eyes => 1, + XRDeviceRole::Left => 2, + XRDeviceRole::Right => 3, + XRDeviceRole::Gamepad => 4, + XRDeviceRole::HandTrackingLeft => 5, + XRDeviceRole::HandTrackingRight => 6, + + // the following are not in libmonado + XRDeviceRole::HandheldObject => 7, + XRDeviceRole::LeftFoot => 8, + XRDeviceRole::RightFoot => 9, + XRDeviceRole::LeftShoulder => 10, + XRDeviceRole::RightShoulder => 11, + XRDeviceRole::LeftElbow => 12, + XRDeviceRole::RightElbow => 13, + XRDeviceRole::LeftKnee => 14, + XRDeviceRole::RightKnee => 15, + XRDeviceRole::Waist => 16, + XRDeviceRole::Chest => 17, + XRDeviceRole::Camera => 18, + XRDeviceRole::Keyboard => 19, + + XRDeviceRole::GenericTracker => 20, + } + } +} + +impl PartialOrd for XRDeviceRole { + fn partial_cmp(&self, other: &Self) -> Option { + Some(u32::from(self).cmp(&other.into())) + } +} + +impl Ord for XRDeviceRole { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.partial_cmp(other).unwrap() + } +} + impl From for XRDeviceRole { fn from(value: DeviceRole) -> Self { match value {