Skip to content

Commit

Permalink
Support build on wasm32
Browse files Browse the repository at this point in the history
Signed-off-by: csh <[email protected]>
  • Loading branch information
L-jasmine committed Jul 14, 2024
1 parent a1bdd0b commit f61401e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions tokio-postgres/src/cancel_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ where
config.port,
config.connect_timeout,
config.tcp_user_timeout,
#[cfg(not(target_arch = "wasm32"))]
config.keepalive.as_ref(),
)
.await?;
Expand Down
5 changes: 3 additions & 2 deletions tokio-postgres/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::codec::{BackendMessages, FrontendMessage};
use crate::config::SslMode;
use crate::connection::{Request, RequestMessages};
use crate::copy_out::CopyOutStream;
#[cfg(feature = "runtime")]
#[cfg(all(feature = "runtime", not(target_arch = "wasm32")))]
use crate::keepalive::KeepaliveConfig;
use crate::query::RowStream;
use crate::simple_query::SimpleQueryStream;
Expand All @@ -27,7 +27,7 @@ use std::collections::HashMap;
use std::fmt;
#[cfg(feature = "runtime")]
use std::net::IpAddr;
#[cfg(feature = "runtime")]
#[cfg(all(feature = "runtime", unix))]
use std::path::PathBuf;
use std::sync::Arc;
use std::task::{Context, Poll};
Expand Down Expand Up @@ -160,6 +160,7 @@ pub(crate) struct SocketConfig {
pub port: u16,
pub connect_timeout: Option<Duration>,
pub tcp_user_timeout: Option<Duration>,
#[cfg(not(target_arch = "wasm32"))]
pub keepalive: Option<KeepaliveConfig>,
}

Expand Down
2 changes: 2 additions & 0 deletions tokio-postgres/src/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ where
port,
config.connect_timeout,
config.tcp_user_timeout,
#[cfg(not(target_arch = "wasm32"))]
if config.keepalives {
Some(&config.keepalive_config)
} else {
Expand Down Expand Up @@ -216,6 +217,7 @@ where
port,
connect_timeout: config.connect_timeout,
tcp_user_timeout: config.tcp_user_timeout,
#[cfg(not(target_arch = "wasm32"))]
keepalive: if config.keepalives {
Some(config.keepalive_config.clone())
} else {
Expand Down
6 changes: 5 additions & 1 deletion tokio-postgres/src/connect_socket.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::client::Addr;
#[cfg(not(target_arch = "wasm32"))]
use crate::keepalive::KeepaliveConfig;
use crate::{Error, Socket};
#[cfg(not(target_arch = "wasm32"))]
use socket2::{SockRef, TcpKeepalive};
use std::future::Future;
use std::io;
Expand All @@ -17,7 +19,7 @@ pub(crate) async fn connect_socket(
#[cfg_attr(not(target_os = "linux"), allow(unused_variables))] tcp_user_timeout: Option<
Duration,
>,
keepalive_config: Option<&KeepaliveConfig>,
#[cfg(not(target_arch = "wasm32"))] keepalive_config: Option<&KeepaliveConfig>,
) -> Result<Socket, Error> {
match addr {
Addr::Tcp(ip) => {
Expand All @@ -26,6 +28,7 @@ pub(crate) async fn connect_socket(

stream.set_nodelay(true).map_err(Error::connect)?;

#[cfg(not(target_arch = "wasm32"))]
let sock_ref = SockRef::from(&stream);
#[cfg(target_os = "linux")]
{
Expand All @@ -34,6 +37,7 @@ pub(crate) async fn connect_socket(
.map_err(Error::connect)?;
}

#[cfg(not(target_arch = "wasm32"))]
if let Some(keepalive_config) = keepalive_config {
sock_ref
.set_tcp_keepalive(&TcpKeepalive::from(keepalive_config))
Expand Down

0 comments on commit f61401e

Please sign in to comment.