Add support for wasm32-unknown-unknown target#232
Open
Jaltaire wants to merge 2 commits into
Open
Conversation
tokio_postgres::Socket is not supported for wasm32-unknown-unknown target. tokio_postgres, additionally, may return a Connection type whose first bound is not a tokio_postgres::Socket. Accordingly, let's relax the bounds on try_from_client_and_connection so that we match the loosest type that may be returned by tokio_postgres's connection methods, which enables try_from_client_and_connection to work for a wasm32-unknown-unknown target.
weiznich
reviewed
May 9, 2025
Member
weiznich
left a comment
There was a problem hiding this comment.
Thanks for opening this PR. Generally speaking I'm open to support this.
That written, there are a few things that I would like to clarify/improve before merging.
- The inline comments
- Changing the cfgs to rather use something like
#[cfg(target_family = "wasm")]instead to also support other wasm targets - Adding some test/example
- Adding some sort of CI check that this works. That should at least check if it compiles, ideally run all tests. Maybe diesel-rs/diesel#4411 could serve as an example for the testing?
Comment on lines
+152
to
153
| #[cfg(not(target_arch = "wasm32"))] | ||
| fn establish(database_url: &str) -> impl Future<Output = ConnectionResult<Self>> + Send; |
Member
There was a problem hiding this comment.
I would like to keep this method in all configuration.
It should be possible to provide an implementation for this for wasm32 as you somehow need to establish a connection there as well, right?
Comment on lines
+906
to
+915
| #[cfg(target_arch = "wasm32")] | ||
| wasm_bindgen_futures::spawn_local(async move { | ||
| match futures_util::future::select(shutdown_rx, conn).await { | ||
| Either::Left(_) | Either::Right((Ok(_), _)) => {} | ||
| Either::Right((Err(e), _)) => { | ||
| let _ = error_tx.send(Arc::new(e)); | ||
| } | ||
| } | ||
| }); | ||
|
|
Member
There was a problem hiding this comment.
Given that the only difference here to the tokio::spawn call above is the name of the spawn function we maybe could refactor this to something like that to minimize code duplication:
let background_future = async move {
match futures_util::future::select(shutdown_rx, conn).await {
Either::Left(_) | Either::Right((Ok(_), _)) => {}
Either::Right((Err(e), _)) => {
let _ = error_tx.send(Arc::new(e));
}
}
};
#[cfg(not(target_arch = "wasm32"))]
tokio::spawn(background_future);
#[cfg(target_arch = "wasm32")]
wasm_bindgen_futures::spawn_local(background_future);
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
tokio-postgresincludes support for targetingwasm32-unknown-unknown, and it would be wonderful to have ORM functionality with Diesel in WASM environments.This PR enables
diesel-asynccompilation with thepostgresfeature for thewasm32-unknown-unknowntarget. Please note that I have not explored what is required to supportwasm32-unknown-unknownfor other database or connection types.I am currently using this within a Cloudflare Workers deployment. (As an aside to anyone exploring, I have not yet been able to configure
diesel-asyncto work with Hyperdrive, presumably due to some Postgres feature set thatdiesel-asyncleverages and that Hyperdrive is not currently supporting. Usingdiesel-asyncwith a direct connection string without Hyperdrive, however, works as expected.)