Skip to content

Commit

Permalink
Use our own Ready for IoAction to avoid bumping MSRV
Browse files Browse the repository at this point in the history
std::future::Ready::into_inner requires 1.82, which is still fairly
recent.
  • Loading branch information
kevinmehall committed Jan 20, 2025
1 parent 4bbebd7 commit 3986e70
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
15 changes: 13 additions & 2 deletions src/ioaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,19 @@ pub mod blocking {
}
}

impl<T> IoAction for std::future::Ready<T> {
pub(crate) struct Ready<T>(pub(crate) T);

impl<T> IntoFuture for Ready<T> {
type Output = T;
type IntoFuture = std::future::Ready<T>;

fn into_future(self) -> Self::IntoFuture {
std::future::ready(self.0)
}
}

impl<T> IoAction for Ready<T> {
fn wait(self) -> Self::Output {
self.into_inner()
self.0
}
}
10 changes: 5 additions & 5 deletions src/platform/linux_usbfs/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use log::debug;
use log::warn;

use crate::enumeration::InterfaceInfo;
use crate::ioaction::blocking::Blocking;
use crate::ioaction::Ready;
use crate::IoAction;
use crate::{BusInfo, DeviceInfo, Error, Speed, UsbControllerType};

Expand Down Expand Up @@ -122,7 +122,7 @@ impl FromHexStr for u16 {
const SYSFS_USB_PREFIX: &'static str = "/sys/bus/usb/devices/";

pub fn list_devices() -> impl IoAction<Output = Result<impl Iterator<Item = DeviceInfo>, Error>> {
Blocking::new(move || {
Ready((|| {
Ok(fs::read_dir(SYSFS_USB_PREFIX)?.flat_map(|entry| {
let path = entry.ok()?.path();
let name = path.file_name()?;
Expand All @@ -143,7 +143,7 @@ pub fn list_devices() -> impl IoAction<Output = Result<impl Iterator<Item = Devi
.inspect_err(|e| warn!("{e}; ignoring device"))
.ok()
}))
})
})())
}

pub fn list_root_hubs() -> Result<impl Iterator<Item = DeviceInfo>, Error> {
Expand All @@ -163,7 +163,7 @@ pub fn list_root_hubs() -> Result<impl Iterator<Item = DeviceInfo>, Error> {
}

pub fn list_buses() -> impl IoAction<Output = Result<impl Iterator<Item = BusInfo>, Error>> {
Blocking::new(move || {
Ready((|| {
Ok(list_root_hubs()?.filter_map(|rh| {
// get the parent by following the absolute symlink; root hub in /bus/usb is a symlink to a dir in parent bus
let parent_path = rh
Expand All @@ -186,7 +186,7 @@ pub fn list_buses() -> impl IoAction<Output = Result<impl Iterator<Item = BusInf
root_hub: rh,
})
}))
})
})())
}

pub fn probe_device(path: SysfsPath) -> Result<DeviceInfo, SysfsError> {
Expand Down
6 changes: 3 additions & 3 deletions src/platform/macos_iokit/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use io_kit_sys::{
};
use log::debug;

use crate::{BusInfo, DeviceInfo, Error, InterfaceInfo, IoAction, Speed, UsbControllerType};
use crate::{BusInfo, DeviceInfo, Error, InterfaceInfo, IoAction, Speed, UsbControllerType, ioaction::Ready};

use super::iokit::{IoService, IoServiceIterator};
/// IOKit class name for PCI USB XHCI high-speed controllers (USB 3.0+)
Expand Down Expand Up @@ -77,13 +77,13 @@ fn usb_controller_service_iter(
}

pub fn list_devices() -> impl IoAction<Output = Result<impl Iterator<Item = DeviceInfo>, Error>> {
std::future::ready(usb_service_iter().map(|i| i.filter_map(probe_device)))
Ready(usb_service_iter().map(|i| i.filter_map(probe_device)))
}

pub fn list_buses() -> impl IoAction<Output = Result<impl Iterator<Item = BusInfo>, Error>> {
// Chain all the HCI types into one iterator
// A bit of a hack, could maybe probe IOPCIDevice and filter on children with IOClass.starts_with("AppleUSB")
std::future::ready(Ok([
Ready(Ok([
UsbControllerType::XHCI,
UsbControllerType::EHCI,
UsbControllerType::OHCI,
Expand Down
6 changes: 3 additions & 3 deletions src/platform/windows_winusb/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use windows_sys::Win32::{

use crate::{
descriptors::{validate_config_descriptor, DESCRIPTOR_TYPE_CONFIGURATION},
ioaction::blocking::Blocking,
ioaction::{blocking::Blocking, Ready},
transfer::{Control, Direction, EndpointType, Recipient, TransferError, TransferHandle},
DeviceInfo, Error, IoAction,
};
Expand Down Expand Up @@ -96,7 +96,7 @@ impl WindowsDevice {
&self,
_configuration: u8,
) -> impl IoAction<Output = Result<(), Error>> {
std::future::ready(Err(io::Error::new(
Ready(Err(io::Error::new(
ErrorKind::Unsupported,
"set_configuration not supported by WinUSB",
)))
Expand All @@ -112,7 +112,7 @@ impl WindowsDevice {
}

pub(crate) fn reset(&self) -> impl IoAction<Output = Result<(), Error>> {
std::future::ready(Err(io::Error::new(
Ready(Err(io::Error::new(
ErrorKind::Unsupported,
"reset not supported by WinUSB",
)))
Expand Down

0 comments on commit 3986e70

Please sign in to comment.