Skip to content

Commit d7b5879

Browse files
authored
RUST-1973 Example client usage targeting WasmEdge (#1137)
1 parent 2380bfe commit d7b5879

File tree

7 files changed

+40
-2
lines changed

7 files changed

+40
-2
lines changed

etc/wasmedge/.cargo/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build]
2+
target = "wasm32-wasi"
3+
rustflags = ["--cfg", "wasmedge", "--cfg", "tokio_unstable"]

etc/wasmedge/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target/
2+
Cargo.lock

etc/wasmedge/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "wasmedge_mongodb"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[patch.crates-io]
7+
tokio = { git = "https://github.com/second-state/wasi_tokio.git", branch = "v1.36.x" }
8+
socket2 = { git = "https://github.com/second-state/socket2.git", branch = "v0.5.x" }
9+
10+
[dependencies]
11+
mongodb = { path = "../../", default_features = false, features = ["compat-3-0-0", "rustls-tls"] }
12+
tokio = { version = "1", features = ["io-util", "fs", "net", "time", "rt", "macros"] }

etc/wasmedge/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Please note: using the MongoDB Rust driver on WasmEdge is unsupported!
2+
3+
This example program requires:
4+
* WasmEdge (https://wasmedge.org/docs/start/install/)
5+
* The Rust wasm toolchain (`rustup target add wasm32-wasi`)
6+
7+
To compile: `cargo build --target wasm32-wasi --release`
8+
9+
To run: `wasmedge target/wasm32-wasi/release/wasmedge_mongodb.wasm`

etc/wasmedge/src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[tokio::main(flavor = "current_thread")]
2+
async fn main() {
3+
let client = mongodb::Client::with_uri_str("mongodb://127.0.0.1:27017")
4+
.await
5+
.unwrap();
6+
println!("{:?}", client.list_database_names().await);
7+
}

src/client/auth/oidc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ fn validate_address_with_allowed_hosts(
714714
mechanism_properties: Option<&Document>,
715715
address: &ServerAddress,
716716
) -> Result<()> {
717+
#[allow(irrefutable_let_patterns)]
717718
let hostname = if let ServerAddress::Tcp { host, .. } = address {
718719
host.as_str()
719720
} else {

src/runtime/stream.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use super::{
2020
};
2121

2222
pub(crate) const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
23+
#[cfg(not(target_os = "wasi"))]
2324
const KEEPALIVE_TIME: Duration = Duration::from_secs(120);
2425

2526
/// An async stream possibly using TLS.
@@ -67,8 +68,11 @@ async fn tcp_try_connect(address: &SocketAddr) -> Result<TcpStream> {
6768
stream.set_nodelay(true)?;
6869

6970
let socket = socket2::Socket::from(stream.into_std()?);
70-
let conf = socket2::TcpKeepalive::new().with_time(KEEPALIVE_TIME);
71-
socket.set_tcp_keepalive(&conf)?;
71+
#[cfg(not(target_os = "wasi"))]
72+
{
73+
let conf = socket2::TcpKeepalive::new().with_time(KEEPALIVE_TIME);
74+
socket.set_tcp_keepalive(&conf)?;
75+
}
7276
let std_stream = std::net::TcpStream::from(socket);
7377
Ok(TcpStream::from_std(std_stream)?)
7478
}

0 commit comments

Comments
 (0)