Skip to content

Commit

Permalink
Rename IoAction -> MaybeFuture
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmehall committed Feb 12, 2025
1 parent a4662e6 commit 11b8fe4
Show file tree
Hide file tree
Showing 20 changed files with 95 additions and 81 deletions.
2 changes: 1 addition & 1 deletion examples/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Duration;

use nusb::{
transfer::{Control, ControlType, Recipient},
IoAction,
MaybeFuture,
};

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/bulk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use futures_lite::future::block_on;
use nusb::{transfer::RequestBuffer, IoAction};
use nusb::{transfer::RequestBuffer, MaybeFuture};

fn main() {
env_logger::init();
Expand Down
2 changes: 1 addition & 1 deletion examples/buses.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use nusb::IoAction;
use nusb::MaybeFuture;

fn main() {
env_logger::init();
Expand Down
2 changes: 1 addition & 1 deletion examples/control.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use futures_lite::future::block_on;
use nusb::{
transfer::{ControlIn, ControlOut, ControlType, Recipient},
IoAction,
MaybeFuture,
};

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/descriptors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use nusb::{DeviceInfo, IoAction};
use nusb::{DeviceInfo, MaybeFuture};

fn main() {
env_logger::init();
Expand Down
2 changes: 1 addition & 1 deletion examples/detach.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Detach the kernel driver for an FTDI device and then reattach it.
use std::{thread::sleep, time::Duration};

use nusb::IoAction;
use nusb::MaybeFuture;
fn main() {
env_logger::init();
let di = nusb::list_devices()
Expand Down
2 changes: 1 addition & 1 deletion examples/detach_claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! then reattach it.
use std::{thread::sleep, time::Duration};

use nusb::IoAction;
use nusb::MaybeFuture;
fn main() {
env_logger::init();
let di = nusb::list_devices()
Expand Down
2 changes: 1 addition & 1 deletion examples/list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use nusb::IoAction;
use nusb::MaybeFuture;

fn main() {
env_logger::init();
Expand Down
2 changes: 1 addition & 1 deletion examples/string_descriptors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use nusb::{descriptors::language_id::US_ENGLISH, DeviceInfo, IoAction};
use nusb::{descriptors::language_id::US_ENGLISH, DeviceInfo, MaybeFuture};

fn main() {
env_logger::init();
Expand Down
30 changes: 16 additions & 14 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
Control, ControlIn, ControlOut, EndpointType, Queue, RequestBuffer, TransferError,
TransferFuture,
},
DeviceInfo, Error, IoAction, Speed,
DeviceInfo, Error, MaybeFuture, Speed,
};
use log::error;
use std::{io::ErrorKind, sync::Arc, time::Duration};
Expand All @@ -18,7 +18,7 @@ use std::{io::ErrorKind, sync::Arc, time::Duration};
/// Obtain a `Device` by calling [`DeviceInfo::open`]:
///
/// ```no_run
/// use nusb::{self, IoAction};
/// use nusb::{self, MaybeFuture};
/// let device_info = nusb::list_devices().wait().unwrap()
/// .find(|dev| dev.vendor_id() == 0xAAAA && dev.product_id() == 0xBBBB)
/// .expect("device not connected");
Expand All @@ -43,21 +43,23 @@ impl Device {
Device { backend }
}

pub(crate) fn open(d: &DeviceInfo) -> impl IoAction<Output = Result<Device, std::io::Error>> {
pub(crate) fn open(
d: &DeviceInfo,
) -> impl MaybeFuture<Output = Result<Device, std::io::Error>> {
platform::Device::from_device_info(d)
}

/// Wraps a device that is already open.
#[cfg(any(target_os = "android", target_os = "linux"))]
pub fn from_fd(fd: std::os::fd::OwnedFd) -> impl IoAction<Output = Result<Device, Error>> {
pub fn from_fd(fd: std::os::fd::OwnedFd) -> impl MaybeFuture<Output = Result<Device, Error>> {
platform::Device::from_fd(fd)
}

/// Open an interface of the device and claim it for exclusive use.
pub fn claim_interface(
&self,
interface: u8,
) -> impl IoAction<Output = Result<Interface, Error>> {
) -> impl MaybeFuture<Output = Result<Interface, Error>> {
self.backend.clone().claim_interface(interface)
}

Expand All @@ -69,7 +71,7 @@ impl Device {
pub fn detach_and_claim_interface(
&self,
interface: u8,
) -> impl IoAction<Output = Result<Interface, Error>> {
) -> impl MaybeFuture<Output = Result<Interface, Error>> {
self.backend.clone().detach_and_claim_interface(interface)
}

Expand Down Expand Up @@ -146,7 +148,7 @@ impl Device {
pub fn set_configuration(
&self,
configuration: u8,
) -> impl IoAction<Output = Result<(), Error>> {
) -> impl MaybeFuture<Output = Result<(), Error>> {
self.backend.clone().set_configuration(configuration)
}

Expand Down Expand Up @@ -256,7 +258,7 @@ impl Device {
///
/// ### Platform-specific notes
/// * Not supported on Windows
pub fn reset(&self) -> impl IoAction<Output = Result<(), Error>> {
pub fn reset(&self) -> impl MaybeFuture<Output = Result<(), Error>> {
self.backend.clone().reset()
}

Expand Down Expand Up @@ -303,7 +305,7 @@ impl Device {
/// ```no_run
/// use futures_lite::future::block_on;
/// use nusb::transfer::{ ControlIn, ControlType, Recipient };
/// # use nusb::IoAction;
/// # use nusb::MaybeFuture;
/// # fn main() -> Result<(), std::io::Error> {
/// # let di = nusb::list_devices().wait().unwrap().next().unwrap();
/// # let device = di.open().wait().unwrap();
Expand Down Expand Up @@ -337,7 +339,7 @@ impl Device {
/// ```no_run
/// use futures_lite::future::block_on;
/// use nusb::transfer::{ ControlOut, ControlType, Recipient };
/// # use nusb::IoAction;
/// # use nusb::MaybeFuture;
/// # fn main() -> Result<(), std::io::Error> {
/// # let di = nusb::list_devices().wait().unwrap().next().unwrap();
/// # let device = di.open().wait().unwrap();
Expand Down Expand Up @@ -386,7 +388,7 @@ impl Interface {
/// An alternate setting is a mode of the interface that makes particular endpoints available
/// and may enable or disable functionality of the device. The OS resets the device to the default
/// alternate setting when the interface is released or the program exits.
pub fn set_alt_setting(&self, alt_setting: u8) -> impl IoAction<Output = Result<(), Error>> {
pub fn set_alt_setting(&self, alt_setting: u8) -> impl MaybeFuture<Output = Result<(), Error>> {
self.backend.clone().set_alt_setting(alt_setting)
}

Expand Down Expand Up @@ -437,7 +439,7 @@ impl Interface {
/// ```no_run
/// use futures_lite::future::block_on;
/// use nusb::transfer::{ ControlIn, ControlType, Recipient };
/// # use nusb::IoAction;
/// # use nusb::MaybeFuture;
/// # fn main() -> Result<(), std::io::Error> {
/// # let di = nusb::list_devices().wait().unwrap().next().unwrap();
/// # let device = di.open().wait().unwrap();
Expand Down Expand Up @@ -473,7 +475,7 @@ impl Interface {
/// ```no_run
/// use futures_lite::future::block_on;
/// use nusb::transfer::{ ControlOut, ControlType, Recipient };
/// # use nusb::IoAction;
/// # use nusb::MaybeFuture;
/// # fn main() -> Result<(), std::io::Error> {
/// # let di = nusb::list_devices().wait().unwrap().next().unwrap();
/// # let device = di.open().wait().unwrap();
Expand Down Expand Up @@ -582,7 +584,7 @@ impl Interface {
/// resume use of the endpoint.
///
/// This should not be called when transfers are pending on the endpoint.
pub fn clear_halt(&self, endpoint: u8) -> impl IoAction<Output = Result<(), Error>> {
pub fn clear_halt(&self, endpoint: u8) -> impl MaybeFuture<Output = Result<(), Error>> {
self.backend.clone().clear_halt(endpoint)
}

Expand Down
4 changes: 2 additions & 2 deletions src/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ffi::{OsStr, OsString};
#[cfg(any(target_os = "linux", target_os = "android"))]
use crate::platform::SysfsPath;

use crate::{Device, Error, IoAction};
use crate::{Device, Error, MaybeFuture};

/// Opaque device identifier
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
Expand Down Expand Up @@ -277,7 +277,7 @@ impl DeviceInfo {
}

/// Open the device
pub fn open(&self) -> impl IoAction<Output = Result<Device, Error>> {
pub fn open(&self) -> impl MaybeFuture<Output = Result<Device, Error>> {
Device::open(self)
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ pub mod transfer;

pub mod hotplug;

mod ioaction;
pub use ioaction::IoAction;
mod maybe_future;
pub use maybe_future::MaybeFuture;

/// OS error returned from operations other than transfers.
pub type Error = io::Error;
Expand All @@ -140,12 +140,13 @@ pub type Error = io::Error;
/// ### Example
///
/// ```no_run
/// use nusb::{self, IoAction};
/// use nusb::{self, MaybeFuture};
/// let device = nusb::list_devices().wait().unwrap()
/// .find(|dev| dev.vendor_id() == 0xAAAA && dev.product_id() == 0xBBBB)
/// .expect("device not connected");
/// ```
pub fn list_devices() -> impl IoAction<Output = Result<impl Iterator<Item = DeviceInfo>, Error>> {
pub fn list_devices() -> impl MaybeFuture<Output = Result<impl Iterator<Item = DeviceInfo>, Error>>
{
platform::list_devices()
}

Expand All @@ -157,7 +158,7 @@ pub fn list_devices() -> impl IoAction<Output = Result<impl Iterator<Item = Devi
///
/// ```no_run
/// use std::collections::HashMap;
/// use nusb::IoAction;
/// use nusb::MaybeFuture;
///
/// let devices = nusb::list_devices().wait().unwrap().collect::<Vec<_>>();
/// let buses: HashMap<String, (nusb::BusInfo, Vec::<nusb::DeviceInfo>)> = nusb::list_buses().wait().unwrap()
Expand All @@ -171,7 +172,7 @@ pub fn list_devices() -> impl IoAction<Output = Result<impl Iterator<Item = Devi
/// ### Platform-specific notes
/// * On Linux, the abstraction of the "bus" is a phony device known as the root hub. This device is available at bus.root_hub()
/// * On Android, this will only work on rooted devices due to sysfs path usage
pub fn list_buses() -> impl IoAction<Output = Result<impl Iterator<Item = BusInfo>, Error>> {
pub fn list_buses() -> impl MaybeFuture<Output = Result<impl Iterator<Item = BusInfo>, Error>> {
platform::list_buses()
}

Expand All @@ -188,7 +189,7 @@ pub fn list_buses() -> impl IoAction<Output = Result<impl Iterator<Item = BusInf
///
/// ```no_run
/// use std::collections::HashMap;
/// use nusb::{IoAction, DeviceInfo, DeviceId, hotplug::HotplugEvent};
/// use nusb::{MaybeFuture, DeviceInfo, DeviceId, hotplug::HotplugEvent};
/// let watch = nusb::watch_devices().unwrap();
/// let mut devices: HashMap<DeviceId, DeviceInfo> = nusb::list_devices().wait().unwrap()
/// .map(|d| (d.id(), d)).collect();
Expand Down
10 changes: 5 additions & 5 deletions src/ioaction.rs → src/maybe_future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::future::IntoFuture;

/// IO that may be performed synchronously or asynchronously.
///
/// An `IOAction` can be run asynchronously with `.await`, or
/// A `MaybeFuture` can be run asynchronously with `.await`, or
/// run synchronously (blocking the current thread) with `.wait()`.
pub trait IoAction: IntoFuture {
pub trait MaybeFuture: IntoFuture {
/// Block waiting for the action to complete
#[cfg(not(target_arch = "wasm32"))]
fn wait(self) -> Self::Output;
Expand All @@ -17,7 +17,7 @@ pub trait IoAction: IntoFuture {
target_os = "macos"
))]
pub mod blocking {
use super::IoAction;
use super::MaybeFuture;
use std::future::IntoFuture;

/// Wrapper that invokes a FnOnce on a background thread when
Expand Down Expand Up @@ -46,7 +46,7 @@ pub mod blocking {
}
}

impl<F, R> IoAction for Blocking<F>
impl<F, R> MaybeFuture for Blocking<F>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
Expand All @@ -68,7 +68,7 @@ impl<T> IntoFuture for Ready<T> {
}
}

impl<T> IoAction for Ready<T> {
impl<T> MaybeFuture for Ready<T> {
fn wait(self) -> Self::Output {
self.0
}
Expand Down
22 changes: 12 additions & 10 deletions src/platform/linux_usbfs/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ use super::{
SysfsPath,
};
use crate::descriptors::{validate_device_descriptor, Configuration, DeviceDescriptor};
use crate::ioaction::blocking::Blocking;
use crate::maybe_future::{blocking::Blocking, MaybeFuture};
use crate::platform::linux_usbfs::events::Watch;
use crate::transfer::{ControlType, Recipient};
use crate::IoAction;
use crate::{
descriptors::{parse_concatenated_config_descriptors, DESCRIPTOR_LEN_DEVICE},
transfer::{
Expand All @@ -52,7 +51,7 @@ pub(crate) struct LinuxDevice {
impl LinuxDevice {
pub(crate) fn from_device_info(
d: &DeviceInfo,
) -> impl IoAction<Output = Result<crate::Device, Error>> {
) -> impl MaybeFuture<Output = Result<crate::Device, Error>> {
let busnum = d.busnum();
let devnum = d.device_address();
let sysfs_path = d.path.clone();
Expand All @@ -71,7 +70,7 @@ impl LinuxDevice {
})
}

pub(crate) fn from_fd(fd: OwnedFd) -> impl IoAction<Output = Result<crate::Device, Error>> {
pub(crate) fn from_fd(fd: OwnedFd) -> impl MaybeFuture<Output = Result<crate::Device, Error>> {
Blocking::new(move || {
debug!("Wrapping fd {} as usbfs device", fd.as_raw_fd());
Self::create_inner(fd, None, None)
Expand Down Expand Up @@ -193,15 +192,15 @@ impl LinuxDevice {
pub(crate) fn set_configuration(
self: Arc<Self>,
configuration: u8,
) -> impl IoAction<Output = Result<(), Error>> {
) -> impl MaybeFuture<Output = Result<(), Error>> {
Blocking::new(move || {
usbfs::set_configuration(&self.fd, configuration)?;
self.active_config.store(configuration, Ordering::SeqCst);
Ok(())
})
}

pub(crate) fn reset(self: Arc<Self>) -> impl IoAction<Output = Result<(), Error>> {
pub(crate) fn reset(self: Arc<Self>) -> impl MaybeFuture<Output = Result<(), Error>> {
Blocking::new(move || {
usbfs::reset(&self.fd)?;
Ok(())
Expand Down Expand Up @@ -282,7 +281,7 @@ impl LinuxDevice {
pub(crate) fn claim_interface(
self: Arc<Self>,
interface_number: u8,
) -> impl IoAction<Output = Result<crate::Interface, Error>> {
) -> impl MaybeFuture<Output = Result<crate::Interface, Error>> {
Blocking::new(move || {
usbfs::claim_interface(&self.fd, interface_number).inspect_err(|e| {
warn!(
Expand All @@ -305,7 +304,7 @@ impl LinuxDevice {
pub(crate) fn detach_and_claim_interface(
self: Arc<Self>,
interface_number: u8,
) -> impl IoAction<Output = Result<crate::Interface, Error>> {
) -> impl MaybeFuture<Output = Result<crate::Interface, Error>> {
Blocking::new(move || {
usbfs::detach_and_claim_interface(&self.fd, interface_number)?;
debug!(
Expand Down Expand Up @@ -500,7 +499,7 @@ impl LinuxInterface {
pub fn set_alt_setting(
self: Arc<Self>,
alt_setting: u8,
) -> impl IoAction<Output = Result<(), Error>> {
) -> impl MaybeFuture<Output = Result<(), Error>> {
Blocking::new(move || {
debug!(
"Set interface {} alt setting to {alt_setting}",
Expand All @@ -514,7 +513,10 @@ impl LinuxInterface {
})
}

pub fn clear_halt(self: Arc<Self>, endpoint: u8) -> impl IoAction<Output = Result<(), Error>> {
pub fn clear_halt(
self: Arc<Self>,
endpoint: u8,
) -> impl MaybeFuture<Output = Result<(), Error>> {
Blocking::new(move || {
debug!("Clear halt, endpoint {endpoint:02x}");
Ok(usbfs::clear_halt(&self.device.fd, endpoint)?)
Expand Down
Loading

0 comments on commit 11b8fe4

Please sign in to comment.