Skip to content

Commit

Permalink
Fix cargo-playdate run, add async-std reactor support (#260)
Browse files Browse the repository at this point in the history
add async-std reactor support, fix `cargo-playdate run`
  • Loading branch information
boozook authored Apr 2, 2024
1 parent 993f1fb commit db91c35
Show file tree
Hide file tree
Showing 11 changed files with 508 additions and 48 deletions.
464 changes: 442 additions & 22 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ sys = { version = "0.3", path = "api/sys", package = "playdate-sys", default-fea
tool = { version = "0.1", path = "support/tool", package = "playdate-tool" }
build = { version = "0.2", path = "support/build", package = "playdate-build", default-features = false }
utils = { version = "0.2", path = "support/utils", package = "playdate-build-utils", default-features = false }
device = { version = "0.1", path = "support/device", package = "playdate-device" }
device = { version = "0.2", path = "support/device", package = "playdate-device" }
simulator = { version = "0.1", path = "support/sim-ctrl", package = "playdate-simulator-utils", default-features = false }
bindgen = { version = "0.1", path = "support/bindgen", package = "playdate-bindgen", default-features = false }
bindgen-cfg = { version = "0.1", path = "support/bindgen-cfg", package = "playdate-bindgen-cfg", default-features = false }
Expand All @@ -49,3 +49,4 @@ toml = "0.8"
futures-lite = "2.3"
thiserror = "1.0"
tokio = { version = "1.37", default-features = false }
async-std = { version = "1.12", default-features = false }
5 changes: 2 additions & 3 deletions cargo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-playdate"
version = "0.4.0-alpha.1"
version = "0.4.0-alpha.2"
readme = "README.md"
description = "Build tool for neat yellow console."
keywords = ["playdate", "build", "cargo", "plugin", "cargo-subcommand"]
Expand Down Expand Up @@ -62,10 +62,9 @@ features = ["assets-report", "toml"]

[dependencies.device]
workspace = true
features = ["clap", "async", "tokio-serial", "tokio"]
features = ["clap", "tokio", "async-std", "tokio-serial"]

[dependencies.simulator]
# features = ["tokio"]
workspace = true

[dependencies.clap]
Expand Down
22 changes: 18 additions & 4 deletions support/device/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "playdate-device"
version = "0.1.0"
version = "0.2.0"
readme = "README.md"
description = "Cross-platform interface Playdate device, async & blocking."
keywords = ["playdate", "usb", "serial"]
Expand Down Expand Up @@ -39,6 +39,19 @@ features = ["fs", "process", "time", "io-std"]
workspace = true
optional = true

[dependencies.async-std]
features = [
"std",
"log",
"gloo-timers",
"futures-lite",
"pin-project-lite",
"unstable",
"default",
]
workspace = true
optional = true

[dependencies.futures-lite]
version = "2.3"

Expand Down Expand Up @@ -75,6 +88,7 @@ features = [


[features]
default = ["async"]
async = ["futures", "tokio", "tokio-serial"]
tokio-serial = ["dep:tokio-serial", "tokio?/io-util", "tokio?/rt"]
default = ["tokio", "tokio-serial"]
tokio-serial = ["futures", "dep:tokio-serial", "tokio?/io-util", "tokio?/rt"]
tokio = ["futures", "dep:tokio", "async-std?/tokio1"] # use async-std reactor
async-std = ["futures", "dep:async-std"] # use async-std reactor and features
9 changes: 6 additions & 3 deletions support/device/src/device/methods.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#![cfg(feature = "tokio")]


use crate::retry::{IterTime, Retries};
use crate::usb::mode::Mode;
use crate::error::Error;
Expand Down Expand Up @@ -30,14 +27,20 @@ pub async fn wait_mode_change(mut dev: Device, to: Mode, retry: Retries<impl Ite
debug!("retries: {retries_num} * {iter_ms:?} ≈ {total:?}.");

let mut counter = retries_num;
#[cfg(all(feature = "tokio", not(feature = "async-std")))]
let mut interval = tokio::time::interval(iter_ms);

while {
counter -= 1;
counter
} != 0
{
#[cfg(all(not(feature = "async-std"), feature = "tokio"))]
interval.tick().await;
#[cfg(feature = "async-std")]
async_std::task::sleep(iter_ms).await;
#[cfg(all(not(feature = "tokio"), not(feature = "async-std")))]
std::thread::sleep(iter_ms);

let mode = dev.mode_cached();
trace!(
Expand Down
9 changes: 6 additions & 3 deletions support/device/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ pub async fn install<'dev>(drive: &'dev MountedDevice,
path: &Path,
force: bool)
-> Result<MountedDevicePathBorrowed<'dev>> {
#[cfg(feature = "tokio")]
#[cfg(all(feature = "tokio", not(feature = "async-std")))]
use tokio::process::Command;
#[cfg(not(feature = "tokio"))]
#[cfg(feature = "async-std")]
use async_std::process::Command;
#[cfg(all(not(feature = "tokio"), not(feature = "async-std")))]
use std::process::Command;


Expand All @@ -87,9 +89,10 @@ pub async fn install<'dev>(drive: &'dev MountedDevice,
async {
if cfg!(unix) {
let mut cmd = Command::new("cp");
cmd.arg("-r");

if force {
cmd.arg("-r");
cmd.arg("-f");
}

cmd.arg(path);
Expand Down
6 changes: 4 additions & 2 deletions support/device/src/mount/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ mod unmount {
}
}

#[cfg(feature = "tokio")]
impl UnmountAsync for Volume {
#[cfg_attr(feature = "tracing", tracing::instrument())]
async fn unmount(&self) -> Result<(), Error> {
use tokio::process::Command;
use futures_lite::future::ready;
#[cfg(all(feature = "tokio", not(feature = "async-std")))]
use tokio::process::Command;
#[cfg(feature = "async-std")]
use async_std::process::Command;


Command::from(eject(self)).status()
Expand Down
16 changes: 10 additions & 6 deletions support/device/src/mount/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,19 @@ mod unmount {
}
}

#[cfg(feature = "tokio")]

impl UnmountAsync for Volume {
#[cfg_attr(feature = "tracing", tracing::instrument())]
async fn unmount(&self) -> Result<(), Error> {
tokio::process::Command::from(cmd(self)).status()
.await?
.exit_ok()
.map(|_| trace!("unmounted {self}"))
.map_err(Into::into)
#[cfg(all(feature = "tokio", not(feature = "async-std")))]
use tokio::process::Command;
#[cfg(feature = "async-std")]
use async_std::process::Command;
Command::from(cmd(self)).status()
.await?
.exit_ok()
.map(|_| trace!("unmounted {self}"))
.map_err(Into::into)
}
}

Expand Down
12 changes: 12 additions & 0 deletions support/device/src/mount/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub async fn wait_fs_available<T>(mount: &MountedDevice, retry: Retries<T>) -> R
debug!("retries: {retries_num} * {iter_ms:?} ≈ {total:?}.");

let mut counter = retries_num;
#[cfg(all(feature = "tokio", not(feature = "async-std")))]
let mut interval = tokio::time::interval(iter_ms);

let check = || {
Expand Down Expand Up @@ -70,7 +71,12 @@ pub async fn wait_fs_available<T>(mount: &MountedDevice, retry: Retries<T>) -> R
counter
} != 0
{
#[cfg(all(not(feature = "async-std"), feature = "tokio"))]
interval.tick().await;
#[cfg(feature = "async-std")]
async_std::task::sleep(iter_ms).await;
#[cfg(all(not(feature = "tokio"), not(feature = "async-std")))]
std::thread::sleep(iter_ms);

if check() {
return Ok(());
Expand Down Expand Up @@ -262,6 +268,7 @@ async fn wait_mount_point<T>(dev: Device, retry: Retries<T>) -> Result<MountedDe
debug!("retries: {retries_num} * {iter_ms:?} ≈ {total:?}.");

let mut counter = retries_num;
#[cfg(all(feature = "tokio", not(feature = "async-std")))]
let mut interval = tokio::time::interval(iter_ms);

let sn = dev.info()
Expand All @@ -274,7 +281,12 @@ async fn wait_mount_point<T>(dev: Device, retry: Retries<T>) -> Result<MountedDe
counter
} != 0
{
#[cfg(all(not(feature = "async-std"), feature = "tokio"))]
interval.tick().await;
#[cfg(feature = "async-std")]
async_std::task::sleep(iter_ms).await;
#[cfg(all(not(feature = "tokio"), not(feature = "async-std")))]
std::thread::sleep(iter_ms);

let mode = dev.mode_cached();
trace!(
Expand Down
6 changes: 4 additions & 2 deletions support/device/src/mount/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ mod unmount {
}
}

#[cfg(feature = "tokio")]
impl UnmountAsync for Volume {
#[cfg_attr(feature = "tracing", tracing::instrument())]
async fn unmount(&self) -> Result<(), Error> {
use tokio::process::Command;
use futures_lite::future::ready;
#[cfg(all(feature = "tokio", not(feature = "async-std")))]
use tokio::process::Command;
#[cfg(feature = "async-std")]
use async_std::process::Command;

futures::future::lazy(|_| winapi::unmount(self.letter)).or_else(|err| {
if std::env::var_os("SHELL").is_some() {
Expand Down
4 changes: 2 additions & 2 deletions support/tool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "playdate-tool"
version = "0.2.1"
version = "0.2.2"
readme = "README.md"
description = "Tool for interaction with Playdate device and sim."
keywords = ["playdate", "usb", "utility"]
Expand Down Expand Up @@ -46,7 +46,7 @@ workspace = true

# PD:
[dependencies.device]
features = ["async", "tokio", "clap", "tokio-serial"]
features = ["clap", "tokio", "tokio-serial"]
workspace = true

[dependencies.simulator]
Expand Down

0 comments on commit db91c35

Please sign in to comment.