Skip to content

Commit b3e7835

Browse files
committed
Fix: Implement p3-runtime wrt wasip3 crate
Signed-off-by: Aditya <aditya.salunkh919@gmail.com>
1 parent 149da63 commit b3e7835

File tree

14 files changed

+349
-229
lines changed

14 files changed

+349
-229
lines changed

Cargo.lock

Lines changed: 24 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ mac_address = "1.1.5"
186186
rust_decimal = { version = "1.26.1", default-features = false, features = ["std"] }
187187
time = { version = "0.3.36", features = ["formatting", "parsing", "macros"] }
188188
uuid = "1.1.2"
189-
189+
tokio-util = { version = "*" }
190190
# Common utility crates
191191
cfg-if = "1.0.0"
192192
dotenvy = { version = "0.15.0", default-features = false }
@@ -208,7 +208,7 @@ default-features = false
208208

209209
[workspace.dependencies.tokio]
210210
version = "1"
211-
features = ["time", "net", "sync", "fs", "io-util", "rt"]
211+
features = ["time", "sync", "io-util", "rt"]
212212
default-features = false
213213

214214
[dependencies]
@@ -255,6 +255,9 @@ cast_sign_loss = 'deny'
255255
# See `clippy.toml`
256256
disallowed_methods = 'deny'
257257

258+
[patch.crates-io]
259+
whoami = { git = "https://github.com/Aditya1404Sal/whoami", branch = "v2" }
260+
258261

259262
[lints.rust.unexpected_cfgs]
260263
level = 'warn'

examples/mysql/todos/Cargo.toml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@ version = "0.1.0"
44
edition = "2021"
55
workspace = "../../../"
66

7+
[lib]
8+
crate-type = ["cdylib"]
9+
710
[dependencies]
811
anyhow = "1.0"
9-
sqlx = { path = "../../../", features = [ "mysql", "runtime-tokio", "tls-native-tls" ] }
12+
futures = "0.3"
13+
sqlx = { path = "../../../", features = [ "mysql", "runtime-tokio" ] }
1014
clap = { version = "4", features = ["derive"] }
11-
tokio = { version = "1.20.0", features = ["rt", "macros"]}
15+
tokio = { version = "1.20.0", features = ["rt"]}
16+
dotenvy = "0.15.0"
17+
wasip3 = "0.2.0+wasi-0.3.0-rc-2025-09-16"
18+
19+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
20+
sqlx = { path = "../../../", features = ["tls-native-tls"] }

examples/mysql/todos/src/main.rs renamed to examples/mysql/todos/src/lib.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,26 @@ enum Command {
1414
Done { id: u64 },
1515
}
1616

17-
#[tokio::main(flavor = "current_thread")]
18-
async fn main() -> anyhow::Result<()> {
19-
let args = Args::parse();
17+
async fn run() -> anyhow::Result<()> {
18+
let args = Args::parse_from(wasip3::cli::environment::get_arguments());
2019
let pool = MySqlPool::connect(&env::var("DATABASE_URL")?).await?;
2120

2221
match args.cmd {
2322
Some(Command::Add { description }) => {
24-
println!("Adding new todo with description '{description}'");
23+
eprintln!("Adding new todo with description '{description}'");
2524
let todo_id = add_todo(&pool, description).await?;
26-
println!("Added new todo with id {todo_id}");
25+
eprintln!("Added new todo with id {todo_id}");
2726
}
2827
Some(Command::Done { id }) => {
29-
println!("Marking todo {id} as done");
28+
eprintln!("Marking todo {id} as done");
3029
if complete_todo(&pool, id).await? {
31-
println!("Todo {id} is marked as done");
30+
eprintln!("Todo {id} is marked as done");
3231
} else {
33-
println!("Invalid id {id}");
32+
eprintln!("Invalid id {id}");
3433
}
3534
}
3635
None => {
37-
println!("Printing list of all todos");
36+
eprintln!("Printing list of all todos");
3837
list_todos(&pool).await?;
3938
}
4039
}
@@ -43,7 +42,6 @@ async fn main() -> anyhow::Result<()> {
4342
}
4443

4544
async fn add_todo(pool: &MySqlPool, description: String) -> anyhow::Result<u64> {
46-
// Insert the task, then obtain the ID of this row
4745
let todo_id = sqlx::query!(
4846
r#"
4947
INSERT INTO todos ( description )
@@ -85,10 +83,8 @@ ORDER BY id
8583
.fetch_all(pool)
8684
.await?;
8785

88-
// NOTE: Booleans in MySQL are stored as `TINYINT(1)` / `i8`
89-
// 0 = false, non-0 = true
9086
for rec in recs {
91-
println!(
87+
eprintln!(
9288
"- [{}] {}: {}",
9389
if rec.done != 0 { "x" } else { " " },
9490
rec.id,
@@ -98,3 +94,27 @@ ORDER BY id
9894

9995
Ok(())
10096
}
97+
98+
wasip3::cli::command::export!(Component);
99+
100+
struct Component;
101+
102+
impl wasip3::exports::cli::run::Guest for Component {
103+
async fn run() -> Result<(), ()> {
104+
if let Err(err) = run().await {
105+
let (mut tx, rx) = wasip3::wit_stream::new();
106+
107+
futures::join!(
108+
async { wasip3::cli::stderr::write_via_stream(rx).await.unwrap() },
109+
async {
110+
let remaining = tx.write_all(format!("{err:#}\n").into_bytes()).await;
111+
assert!(remaining.is_empty());
112+
drop(tx);
113+
}
114+
);
115+
Err(())
116+
} else {
117+
Ok(())
118+
}
119+
}
120+
}

sqlx-core/Cargo.toml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ _unstable-doc = ["sqlx-toml"]
5050
async-global-executor = { workspace = true, optional = true }
5151
async-std = { workspace = true, optional = true }
5252
smol = { workspace = true, optional = true }
53-
tokio = { workspace = true, optional = true }
53+
tokio = { workspace = true, optional = true, features = ["sync","rt"]}
54+
tokio-util = { workspace = true }
5455

5556
# TLS
5657
native-tls = { version = "0.2.10", optional = true }
@@ -94,18 +95,26 @@ toml = { version = "0.8.16", optional = true }
9495
sha2 = { version = "0.10.0", default-features = false, optional = true }
9596
#sqlformat = "0.2.0"
9697
thiserror = "2.0.0"
97-
tokio-stream = { version = "0.1.8", features = ["fs"], optional = true }
98+
tokio-stream = { version = "0.1.8", optional = true }
9899
tracing = { version = "0.1.37", features = ["log"] }
99100
smallvec = "1.7.0"
100101
url = { version = "2.2.2" }
101102
bstr = { version = "1.0", default-features = false, features = ["std"], optional = true }
102103
hashlink = "0.10.0"
103104
indexmap = "2.0"
104105
event-listener = "5.2.0"
105-
hashbrown = "0.15.0"
106-
wasip3 = { workspace = true}
106+
hashbrown = { version = "0.15.0", default-features = false }
107+
[target.'cfg(target_arch = "wasm32")'.dependencies]
108+
tokio = { workspace = true, features = ["sync", "rt"] }
109+
tokio-util = { workspace = true }
110+
wasip3 = { workspace = true }
107111
wit-bindgen = { workspace = true, optional = true }
108112

113+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
114+
tokio = { workspace = true, features = ["net", "fs"], optional = true }
115+
tokio-stream = { version = "0.1.8", features = ["fs"], optional = true }
116+
117+
109118
[dev-dependencies]
110119
sqlx = { workspace = true, features = ["postgres", "sqlite", "mysql", "migrate", "macros", "time", "uuid"] }
111120
tokio = { version = "1", features = ["rt"] }

sqlx-core/src/migrate/source.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,7 @@ impl<'s> MigrationSource<'s> for &'s Path {
3737

3838
impl MigrationSource<'static> for PathBuf {
3939
fn resolve(self) -> BoxFuture<'static, Result<Vec<Migration>, BoxDynError>> {
40-
// Technically this could just be `Box::pin(spawn_blocking(...))`
41-
// but that would actually be a breaking behavior change because it would call
42-
// `spawn_blocking()` on the current thread
43-
Box::pin(async move {
44-
crate::rt::spawn_blocking(move || {
45-
#[cfg(not(target_arch = "wasm32"))]
46-
let migrations_with_paths = resolve_blocking(&self)?;
47-
#[cfg(target_arch = "wasm32")]
48-
let migrations_with_paths = resolve(&canonical).await?;
49-
Ok(migrations_with_paths.into_iter().map(|(m, _p)| m).collect())
50-
})
51-
.await
52-
})
40+
Box::pin(async move { self.as_path().resolve().await })
5341
}
5442
}
5543

sqlx-core/src/net/socket/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ pub async fn connect_tcp<Ws: WithSocket>(
186186
port: u16,
187187
with_socket: Ws,
188188
) -> crate::Result<Ws::Output> {
189+
#[cfg(all(feature = "_rt-tokio", target_arch = "wasm32"))]
190+
{
191+
let res = crate::rt::rt_wasip3::connect_tcp(host, port, with_socket).await;
192+
return res;
193+
}
194+
189195
#[cfg(all(feature = "_rt-tokio", not(target_arch = "wasm32")))]
190196
if crate::rt::rt_tokio::available() {
191197
return Ok(with_socket
@@ -200,12 +206,6 @@ pub async fn connect_tcp<Ws: WithSocket>(
200206
crate::rt::missing_rt((host, port, with_socket))
201207
}
202208
}
203-
204-
#[cfg(all(feature = "_rt-tokio", target_arch = "wasm32"))]
205-
{
206-
let res = crate::rt::rt_wasip3::connect_tcp(host, port, with_socket).await;
207-
return res;
208-
}
209209
}
210210

211211
/// Open a TCP socket to `host` and `port`.

0 commit comments

Comments
 (0)