Skip to content

Commit ff3b166

Browse files
committed
add support for wasm32-unknown-unknown target
1 parent c6f9dc8 commit ff3b166

File tree

6 files changed

+27
-2
lines changed

6 files changed

+27
-2
lines changed

Cargo.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ futures-util = { version = "0.3.17", default-features = false, features = [
2121
"std",
2222
"sink",
2323
] }
24-
tokio-postgres = { version = "0.7.10", optional = true }
25-
tokio = { version = "1.26", optional = true }
2624
mysql_async = { version = "0.36.0", optional = true, default-features = false, features = [
2725
"minimal-rust",
2826
] }
@@ -36,6 +34,15 @@ deadpool = { version = "0.12", optional = true, default-features = false, featur
3634
mobc = { version = ">=0.7,<0.10", optional = true }
3735
scoped-futures = { version = "0.1", features = ["std"] }
3836

37+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
38+
tokio-postgres = { version = "0.7.10", optional = true }
39+
tokio = { version = "1.26", optional = true }
40+
41+
[target.'cfg(target_arch = "wasm32")'.dependencies]
42+
tokio-postgres = { version = "0.7.10", optional = true, default-features = false, features = ["js"] }
43+
tokio = { version = "1.26", optional = true, default-features = false }
44+
wasm-bindgen-futures = { version = "0.4.50" }
45+
3946
[dependencies.diesel]
4047
version = "~2.2.0"
4148
default-features = false

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ pub trait AsyncConnection: SimpleAsyncConnection + Sized + Send {
149149
/// The argument to this method and the method's behavior varies by backend.
150150
/// See the documentation for that backend's connection class
151151
/// for details about what it accepts and how it behaves.
152+
#[cfg(not(target_arch = "wasm32"))]
152153
fn establish(database_url: &str) -> impl Future<Output = ConnectionResult<Self>> + Send;
153154

154155
/// Executes the given function inside of a database transaction

src/mysql/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ impl AsyncConnection for AsyncMysqlConnection {
7171

7272
type TransactionManager = AnsiTransactionManager;
7373

74+
#[cfg(not(target_arch = "wasm32"))]
7475
async fn establish(database_url: &str) -> diesel::ConnectionResult<Self> {
7576
let mut instrumentation = DynInstrumentation::default_instrumentation();
7677
instrumentation.on_connection_event(InstrumentationEvent::start_establish_connection(

src/pg/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ impl AsyncConnection for AsyncPgConnection {
161161
type Backend = diesel::pg::Pg;
162162
type TransactionManager = AnsiTransactionManager;
163163

164+
#[cfg(not(target_arch = "wasm32"))]
164165
async fn establish(database_url: &str) -> ConnectionResult<Self> {
165166
let mut instrumentation = DynInstrumentation::default_instrumentation();
166167
instrumentation.on_connection_event(InstrumentationEvent::start_establish_connection(
@@ -396,6 +397,7 @@ impl AsyncPgConnection {
396397

397398
/// Constructs a new `AsyncPgConnection` from an existing [`tokio_postgres::Client`] and
398399
/// [`tokio_postgres::Connection`]
400+
#[cfg(not(target_arch = "wasm32"))]
399401
pub async fn try_from_client_and_connection<S>(
400402
client: tokio_postgres::Client,
401403
conn: tokio_postgres::Connection<tokio_postgres::Socket, S>,
@@ -877,6 +879,7 @@ async fn drive_future<R>(
877879
}
878880
}
879881

882+
#[cfg(not(target_arch = "wasm32"))]
880883
fn drive_connection<S>(
881884
conn: tokio_postgres::Connection<tokio_postgres::Socket, S>,
882885
) -> (
@@ -889,6 +892,7 @@ where
889892
let (error_tx, error_rx) = tokio::sync::broadcast::channel(1);
890893
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
891894

895+
#[cfg(not(target_arch = "wasm32"))]
892896
tokio::spawn(async move {
893897
match futures_util::future::select(shutdown_rx, conn).await {
894898
Either::Left(_) | Either::Right((Ok(_), _)) => {}
@@ -898,6 +902,16 @@ where
898902
}
899903
});
900904

905+
#[cfg(target_arch = "wasm32")]
906+
wasm_bindgen_futures::spawn_local(async move {
907+
match futures_util::future::select(shutdown_rx, conn).await {
908+
Either::Left(_) | Either::Right((Ok(_), _)) => {}
909+
Either::Right((Err(e), _)) => {
910+
let _ = error_tx.send(Arc::new(e));
911+
}
912+
}
913+
});
914+
901915
(error_rx, shutdown_tx)
902916
}
903917

src/pooled_connection/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ where
193193
type TransactionManager =
194194
PoolTransactionManager<<C::Target as AsyncConnection>::TransactionManager>;
195195

196+
#[cfg(not(target_arch = "wasm32"))]
196197
async fn establish(_database_url: &str) -> diesel::ConnectionResult<Self> {
197198
Err(diesel::result::ConnectionError::BadConnection(
198199
String::from("Cannot directly establish a pooled connection"),

src/sync_connection_wrapper/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ where
112112
type Backend = <C as Connection>::Backend;
113113
type TransactionManager = SyncTransactionManagerWrapper<<C as Connection>::TransactionManager>;
114114

115+
#[cfg(not(target_arch = "wasm32"))]
115116
async fn establish(database_url: &str) -> ConnectionResult<Self> {
116117
let database_url = database_url.to_string();
117118
tokio::task::spawn_blocking(move || C::establish(&database_url))

0 commit comments

Comments
 (0)