|
4 | 4 | #![doc(html_favicon_url = "https://rocket.rs/images/favicon.ico")]
|
5 | 5 | #![doc(html_logo_url = "https://rocket.rs/images/logo-boxed.png")]
|
6 | 6 |
|
7 |
| -#![warn(rust_2018_idioms)] |
| 7 | +#![warn(rust_2018_idioms, missing_docs)] |
8 | 8 |
|
9 | 9 | //! # Rocket - Code Generation
|
10 | 10 | //!
|
@@ -377,11 +377,100 @@ pub fn async_test(args: TokenStream, input: TokenStream) -> TokenStream {
|
377 | 377 | emit!(attribute::entry::async_test_attribute(args, input))
|
378 | 378 | }
|
379 | 379 |
|
| 380 | +/// Retrofits `async fn` support in `main` functions. |
| 381 | +/// |
| 382 | +/// A `main` `async fn` function decorated with `#[rocket::main]` is transformed |
| 383 | +/// into a regular `main` function that internally initalizes a Rocket-specific |
| 384 | +/// tokio runtime and runs the attributed `async fn` inside of it: |
| 385 | +/// |
| 386 | +/// ```rust,no_run |
| 387 | +/// #[rocket::main] |
| 388 | +/// async fn main() -> Result<(), rocket::Error> { |
| 389 | +/// rocket::build() |
| 390 | +/// .ignite().await? |
| 391 | +/// .launch().await |
| 392 | +/// } |
| 393 | +/// ``` |
| 394 | +/// |
| 395 | +/// It should be used only when inspection of an ignited instance of `Rocket` is |
| 396 | +/// required, or when the return value of `launch()` is to be inspected: |
| 397 | +/// |
| 398 | +/// ```rust,no_run |
| 399 | +/// #[rocket::main] |
| 400 | +/// async fn main() -> Result<(), rocket::Error> { |
| 401 | +/// let rocket = rocket::build().ignite().await?; |
| 402 | +/// println!("Hello, Rocket: {:?}", rocket); |
| 403 | +/// |
| 404 | +/// let result = rocket.launch().await; |
| 405 | +/// println!("The server shutdown: {:?}", result); |
| 406 | +/// |
| 407 | +/// result |
| 408 | +/// } |
| 409 | +/// ``` |
| 410 | +/// |
| 411 | +/// For all other cases, use [`#[launch]`](launch) instead. |
| 412 | +/// |
| 413 | +/// The function attributed with `#[rocket::main]` _must_ be `async` and _must_ |
| 414 | +/// be called `main`. Violation of either results in a compile-time error. |
380 | 415 | #[proc_macro_attribute]
|
381 | 416 | pub fn main(args: TokenStream, input: TokenStream) -> TokenStream {
|
382 | 417 | emit!(attribute::entry::main_attribute(args, input))
|
383 | 418 | }
|
384 | 419 |
|
| 420 | +/// Generates a `main` function that launches a returned `Rocket<Build>`. |
| 421 | +/// |
| 422 | +/// When applied to a function that returns a `Rocket<Build>` instance, |
| 423 | +/// `#[launch]` automatically initializes an `async` runtime and |
| 424 | +/// launches the function's returned instance: |
| 425 | +/// |
| 426 | +/// ```rust,no_run |
| 427 | +/// # use rocket::launch; |
| 428 | +/// use rocket::{Rocket, Build}; |
| 429 | +/// |
| 430 | +/// #[launch] |
| 431 | +/// fn rocket() -> Rocket<Build> { |
| 432 | +/// rocket::build() |
| 433 | +/// } |
| 434 | +/// ``` |
| 435 | +/// |
| 436 | +/// This generates code equivalent to the following: |
| 437 | +/// |
| 438 | +/// ```rust,no_run |
| 439 | +/// # use rocket::{Rocket, Build}; |
| 440 | +/// # fn rocket() -> Rocket<Build> { |
| 441 | +/// # rocket::build() |
| 442 | +/// # } |
| 443 | +/// # |
| 444 | +/// #[rocket::main] |
| 445 | +/// async fn main() { |
| 446 | +/// // Recall that an uninspected `Error` will cause a pretty-printed panic, |
| 447 | +/// // so rest assured failures do not go undetected when using `#[launch]`. |
| 448 | +/// let _ = rocket().launch().await; |
| 449 | +/// } |
| 450 | +/// ``` |
| 451 | +/// |
| 452 | +/// To avoid needing to import _any_ items in the common case, the `launch` |
| 453 | +/// attribute will infer a return type written as `_` as `Rocket<Build>`: |
| 454 | +/// |
| 455 | +/// ```rust,no_run |
| 456 | +/// # use rocket::launch; |
| 457 | +/// #[launch] |
| 458 | +/// fn rocket() -> _ { |
| 459 | +/// rocket::build() |
| 460 | +/// } |
| 461 | +/// ``` |
| 462 | +/// |
| 463 | +/// The attributed function may be `async`: |
| 464 | +/// |
| 465 | +/// ```rust,no_run |
| 466 | +/// # use rocket::launch; |
| 467 | +/// # async fn some_async_work() {} |
| 468 | +/// #[launch] |
| 469 | +/// async fn rocket() -> _ { |
| 470 | +/// some_async_work().await; |
| 471 | +/// rocket::build() |
| 472 | +/// } |
| 473 | +/// ``` |
385 | 474 | #[proc_macro_attribute]
|
386 | 475 | pub fn launch(args: TokenStream, input: TokenStream) -> TokenStream {
|
387 | 476 | emit!(attribute::entry::launch_attribute(args, input))
|
|
0 commit comments