Skip to content

Commit 4525136

Browse files
committed
Rewrite the comment about the generated main function.
1 parent 257ca19 commit 4525136

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

macro/src/lib.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn attr_macro_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
8484
.into()
8585
}
8686

87-
/// Enables a HTTP-server main function, for creating [HTTP servers].
87+
/// Enables a HTTP server main function, for creating [HTTP servers].
8888
///
8989
/// [HTTP servers]: https://docs.rs/wstd/latest/wstd/http/server/index.html
9090
///
@@ -148,9 +148,38 @@ pub fn attr_macro_http_server(_attr: TokenStream, item: TokenStream) -> TokenStr
148148

149149
::wstd::wasi::http::proxy::export!(TheServer with_types_in ::wstd::wasi);
150150

151-
// In case the user needs it, provide a `main` function so that the
152-
// code compiles.
153-
fn main() { unreachable!("HTTP-server components should be run wth `handle` rather than `main`") }
151+
// Provide an actual function named `main`.
152+
//
153+
// WASI HTTP server components don't use a traditional `main` function.
154+
// They export a function named `handle` which takes a `Request`
155+
// argument, and which may be called multiple times on the same
156+
// instance. To let users write a familiar `fn main` in a file
157+
// named src/main.rs, we provide this `wstd::main` macro, which
158+
// transforms the user's `fn main` into the appropriate `handle`
159+
// function.
160+
//
161+
// However, when the top-level file is named src/main.rs, rustc
162+
// requires there to be a function named `main` somewhere in it. This
163+
// requirement can be disabled using `#![no_main]`, however we can't
164+
// use that automatically because macros can't contain inner
165+
// attributes, and we don't want to require users to add `#![no_main]`
166+
// in their own code.
167+
//
168+
// So, we include a definition of a function named `main` here, which
169+
// isn't intended to ever be called, and exists just to satify the
170+
// requirement for a `main` function.
171+
//
172+
// Users could use `#![no_main]` if they want to. Or, they could name
173+
// their top-level file src/lib.rs and add
174+
// ```toml
175+
// [lib]
176+
// crate-type = ["cdylib"]
177+
// ```
178+
// to their Cargo.toml. With either of these, this "main" function will
179+
// be ignored as dead code.
180+
fn main() {
181+
unreachable!("HTTP server components should be run with `handle` rather than `run`")
182+
}
154183
}
155184
.into()
156185
}

0 commit comments

Comments
 (0)