Skip to content

Commit 4fbab89

Browse files
committed
move tcp_echo_server from doc to example, test that it builds and parses as component
1 parent 54289b8 commit 4fbab89

File tree

10 files changed

+142
-26
lines changed

10 files changed

+142
-26
lines changed

Cargo.toml

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
[package]
22
name = "wstd"
33
version = "0.4.0"
4-
license = "MIT OR Apache-2.0 OR Apache-2.0 WITH LLVM-exception"
4+
license.workspace = true
55
repository = "https://github.com/yoshuawuyts/wstd"
66
documentation = "https://docs.rs/wstd"
77
description = "An async standard library for Wasm Components and WASI 0.2"
88
readme = "README.md"
9-
edition = "2021"
10-
resolver = "2"
9+
edition.workspace = true
1110
keywords = ["WebAssembly", "async", "stdlib", "Components"]
1211
categories = []
1312
authors = [
@@ -17,8 +16,32 @@ authors = [
1716
[features]
1817

1918
[dependencies]
19+
slab.workspace = true
20+
url.workspace = true
21+
wasi.workspace = true
22+
23+
[dev-dependencies]
24+
test-program-suite.workspace = true
25+
wasmtime.workspace = true
26+
27+
[workspace]
28+
members = [
29+
"test-programs",
30+
"test-programs/suite",
31+
]
32+
resolver = "2"
33+
34+
[workspace.package]
35+
edition = "2021"
36+
license = "MIT OR Apache-2.0 OR Apache-2.0 WITH LLVM-exception"
37+
38+
[workspace.dependencies]
39+
cargo_metadata = "0.18.1"
40+
heck = "0.5"
2041
slab = "0.4.9"
42+
test-programs = { path = "test-programs" }
43+
test-program-suite = { path = "test-programs/suite" }
2144
url = "2.5.0"
2245
wasi = "0.13.1"
23-
24-
[dev-dependencies]
46+
wstd = { path = "." }
47+
wasmtime = "26"

examples/tcp_echo_server.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use wstd::io;
2+
use wstd::iter::AsyncIterator;
3+
use wstd::net::TcpListener;
4+
use wstd::runtime::block_on;
5+
6+
fn main() -> io::Result<()> {
7+
block_on(async move {
8+
let listener = TcpListener::bind("127.0.0.1:8080").await?;
9+
println!("Listening on {}", listener.local_addr()?);
10+
println!("type `nc localhost 8080` to create a TCP client");
11+
12+
let mut incoming = listener.incoming();
13+
while let Some(stream) = incoming.next().await {
14+
let stream = stream?;
15+
println!("Accepted from: {}", stream.peer_addr()?);
16+
io::copy(&stream, &stream).await?;
17+
}
18+
Ok(())
19+
})
20+
}

src/lib.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,7 @@
1313
//! **TCP echo server**
1414
//!
1515
//! ```rust,no_run
16-
//! use wstd::io;
17-
//! use wstd::iter::AsyncIterator;
18-
//! use wstd::net::TcpListener;
19-
//! use wstd::runtime::block_on;
20-
//!
21-
//! fn main() -> io::Result<()> {
22-
//! block_on(async move {
23-
//! let listener = TcpListener::bind("127.0.0.1:8080").await?;
24-
//! println!("Listening on {}", listener.local_addr()?);
25-
//! println!("type `nc localhost 8080` to create a TCP client");
26-
//!
27-
//! let mut incoming = listener.incoming();
28-
//! while let Some(stream) = incoming.next().await {
29-
//! let stream = stream?;
30-
//! println!("Accepted from: {}", stream.peer_addr()?);
31-
//! io::copy(&stream, &stream).await?;
32-
//! }
33-
//! Ok(())
34-
//! })
35-
//! }
16+
#![doc = include_str!("../examples/tcp_echo_server.rs")]
3617
//! ```
3718
//!
3819
//! # Design Decisions

test-programs/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "test-programs"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
8+
wstd.workspace = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include!("../../../examples/tcp_echo_server.rs");

test-programs/suite/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "test-program-suite"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
license.workspace = true
6+
publish = false
7+
8+
[dependencies]
9+
10+
[build-dependencies]
11+
cargo_metadata.workspace = true
12+
heck.workspace = true

test-programs/suite/build.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use heck::ToShoutySnakeCase;
2+
use std::env::var_os;
3+
use std::path::PathBuf;
4+
use std::process::Command;
5+
6+
fn main() {
7+
let out_dir = PathBuf::from(var_os("OUT_DIR").expect("OUT_DIR env var exists"));
8+
9+
let meta = cargo_metadata::MetadataCommand::new()
10+
.exec()
11+
.expect("cargo metadata");
12+
let test_programs_meta = meta
13+
.packages
14+
.iter()
15+
.find(|p| p.name == "test-programs")
16+
.expect("test-programs is in cargo metadata");
17+
let test_programs_root = test_programs_meta.manifest_path.parent().unwrap();
18+
println!(
19+
"cargo:rerun-if-changed={}",
20+
test_programs_root.as_os_str().to_str().unwrap()
21+
);
22+
23+
let status = Command::new("cargo")
24+
.arg("build")
25+
.arg("--target=wasm32-wasip2")
26+
.arg("--package=test-programs")
27+
.env("CARGO_TARGET_DIR", &out_dir)
28+
.env("CARGO_PROFILE_DEV_DEBUG", "2")
29+
.env("RUSTFLAGS", rustflags())
30+
.env_remove("CARGO_ENCODED_RUSTFLAGS")
31+
.status()
32+
.expect("cargo build test programs");
33+
assert!(status.success());
34+
35+
let mut generated_code = "// THIS FILE IS GENERATED CODE\n".to_string();
36+
37+
for binary in test_programs_meta
38+
.targets
39+
.iter()
40+
.filter(|t| t.kind == ["bin"])
41+
{
42+
let component_path = out_dir
43+
.join("wasm32-wasip2")
44+
.join("debug")
45+
.join(format!("{}.wasm", binary.name));
46+
47+
let const_name = binary.name.to_shouty_snake_case();
48+
generated_code += &format!(
49+
"pub const {const_name}: &str = {:?};\n",
50+
component_path.as_os_str().to_str().expect("path is str")
51+
);
52+
}
53+
54+
std::fs::write(out_dir.join("gen.rs"), generated_code).unwrap();
55+
}
56+
57+
fn rustflags() -> &'static str {
58+
match option_env!("RUSTFLAGS") {
59+
Some(s) if s.contains("-D warnings") => "-D warnings",
60+
_ => "",
61+
}
62+
}

test-programs/suite/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include!(concat!(env!("OUT_DIR"), "/gen.rs"));

tests/test-programs.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use wasmtime::{component::Component, Config, Engine};
2+
3+
#[test]
4+
fn tcp_echo_server() {
5+
let wasm = std::fs::read(test_program_suite::TCP_ECHO_SERVER).unwrap();
6+
let config = Config::default();
7+
let engine = Engine::new(&config).unwrap();
8+
let _component = Component::new(&engine, wasm).unwrap();
9+
}

tests/test.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)