Use launch-inferred '_' in most example code.

This commit is contained in:
Sergio Benitez 2021-04-13 18:12:39 -07:00
parent c924ff7591
commit fd8c9ce795
21 changed files with 54 additions and 49 deletions

View File

@ -18,7 +18,7 @@ fn hello(name: &str, age: u8) -> String {
}
#[launch]
fn rocket() -> rocket::Rocket {
fn rocket() -> _ {
rocket::build().mount("/hello", routes![hello])
}
```

View File

@ -63,7 +63,7 @@
//! struct LogsDbConn(diesel::SqliteConnection);
//!
//! #[launch]
//! fn rocket() -> rocket::Rocket {
//! fn rocket() -> _ {
//! rocket::build().attach(LogsDbConn::fairing())
//! }
//! # } fn main() {}

View File

@ -203,7 +203,7 @@ impl std::ops::BitOr for Options {
/// use rocket_contrib::serve::StaticFiles;
///
/// #[launch]
/// fn rocket() -> rocket::Rocket {
/// fn rocket() -> _ {
/// rocket::build().mount("/public", StaticFiles::from("/static"))
/// }
/// ```
@ -227,7 +227,7 @@ impl std::ops::BitOr for Options {
/// use rocket_contrib::serve::{StaticFiles, crate_relative};
///
/// #[launch]
/// fn rocket() -> rocket::Rocket {
/// fn rocket() -> _ {
/// rocket::build().mount("/", StaticFiles::from(crate_relative!("static")))
/// }
/// ```
@ -263,7 +263,7 @@ impl StaticFiles {
/// use rocket_contrib::serve::StaticFiles;
///
/// #[launch]
/// fn rocket() -> rocket::Rocket {
/// fn rocket() -> _ {
/// rocket::build().mount("/static", StaticFiles::from("/www/public"))
/// }
/// ```
@ -276,7 +276,7 @@ impl StaticFiles {
/// use rocket_contrib::serve::StaticFiles;
///
/// #[launch]
/// fn rocket() -> rocket::Rocket {
/// fn rocket() -> _ {
/// rocket::build().mount("/static", StaticFiles::from("/www/public").rank(30))
/// }
/// ```
@ -305,7 +305,7 @@ impl StaticFiles {
/// use rocket_contrib::serve::{StaticFiles, Options};
///
/// #[launch]
/// fn rocket() -> rocket::Rocket {
/// fn rocket() -> _ {
/// let options = Options::Index | Options::DotFiles;
/// rocket::build()
/// .mount("/static", StaticFiles::from("/www/public"))

View File

@ -37,7 +37,7 @@ mod b_inferred {
mod c {
// non-async launch.
#[rocket::launch]
fn rocket() -> rocket::Rocket {
fn rocket() -> _ {
rocket::build()
}

View File

@ -108,7 +108,7 @@ note: this function cannot be `main`
error[E0728]: `await` is only allowed inside `async` functions and blocks
--> $DIR/async-entry.rs:75:17
|
74 | fn rocket() -> rocket::Rocket {
72 | fn rocket() -> _ {
| ------ this is not `async`
75 | let _ = rocket::build().launch().await;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ only allowed inside `async` functions and blocks

View File

@ -102,7 +102,7 @@ error: [note] this function cannot be `main`
error[E0728]: `await` is only allowed inside `async` functions and blocks
--> $DIR/async-entry.rs:75:17
|
74 | fn rocket() -> rocket::Rocket {
72 | fn rocket() -> _ {
| ------ this is not `async`
75 | let _ = rocket::build().launch().await;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ only allowed inside `async` functions and blocks

View File

@ -41,7 +41,7 @@ mod launch_a {
mod launch_b {
#[rocket::launch]
async fn rocket() -> rocket::Rocket {
async fn rocket() -> _ {
let _ = rocket::build().launch().await;
"hi".to_string()
}
@ -71,7 +71,7 @@ mod launch_e {
mod launch_f {
#[rocket::launch]
fn rocket() -> rocket::Rocket {
fn rocket() -> _ {
let _ = rocket::build().launch().await;
rocket::build()
}

View File

@ -200,7 +200,10 @@ impl AdHoc {
/// /* and so on.. */
/// }
///
/// let fairing = AdHoc::config::<Config>();
/// #[launch]
/// fn rocket() -> _ {
/// rocket::build().attach(AdHoc::config::<Config>())
/// }
/// ```
pub fn config<'de, T>() -> AdHoc
where T: serde::Deserialize<'de> + Send + Sync + 'static

View File

@ -28,7 +28,7 @@ use crate::{Request, Response};
/// }
///
/// #[launch]
/// fn rocket() -> rocket::Rocket {
/// fn rocket() -> _ {
/// rocket::build().mount("/", routes![hello_world])
/// # .reconfigure(rocket::Config::debug_default())
/// }

View File

@ -25,7 +25,7 @@ use super::Client;
/// }
///
/// #[launch]
/// fn rocket() -> rocket::Rocket {
/// fn rocket() -> _ {
/// rocket::build().mount("/", routes![hello_world])
/// # .reconfigure(rocket::Config::debug_default())
/// }

View File

@ -43,8 +43,7 @@
//!
//! # /*
//! #[launch]
//! # */
//! fn rocket() -> rocket::Rocket {
//! fn rocket() -> _ {
//! rocket::build().mount("/", routes![hello])
//! }
//!

View File

@ -47,9 +47,11 @@ impl Rocket {
/// # Examples
///
/// ```rust
/// # {
/// rocket::build()
/// # };
/// # use rocket::launch;
/// #[launch]
/// fn rocket() -> _ {
/// rocket::build()
/// }
/// ```
#[track_caller]
#[inline(always)]
@ -177,7 +179,7 @@ impl Rocket {
/// }
///
/// #[launch]
/// fn rocket() -> rocket::Rocket {
/// fn rocket() -> _ {
/// rocket::build().mount("/hello", routes![hi])
/// }
/// ```
@ -195,10 +197,11 @@ impl Rocket {
/// Outcome::from(req, "Hello!").pin()
/// }
///
/// # let _ = async { // We don't actually want to launch the server in an example.
/// rocket::build().mount("/hello", vec![Route::new(Get, "/world", hi)])
/// # .launch().await;
/// # };
/// #[launch]
/// fn rocket() -> _ {
/// let hi_route = Route::new(Method::Get, "/world", hi);
/// rocket::build().mount("/hello", vec![hi_route])
/// }
/// ```
pub fn mount<'a, B, R>(mut self, base: B, routes: R) -> Self
where B: TryInto<Origin<'a>> + Clone + Display,
@ -263,7 +266,7 @@ impl Rocket {
/// }
///
/// #[launch]
/// fn rocket() -> rocket::Rocket {
/// fn rocket() -> _ {
/// rocket::build().register("/", catchers![internal_error, not_found])
/// }
/// ```
@ -322,7 +325,7 @@ impl Rocket {
/// }
///
/// #[launch]
/// fn rocket() -> rocket::Rocket {
/// fn rocket() -> _ {
/// rocket::build()
/// .mount("/", routes![index])
/// .manage(MyValue(10))
@ -350,7 +353,7 @@ impl Rocket {
/// use rocket::fairing::AdHoc;
///
/// #[launch]
/// fn rocket() -> rocket::Rocket {
/// fn rocket() -> _ {
/// rocket::build()
/// .attach(AdHoc::on_liftoff("Liftoff Message", |_| Box::pin(async {
/// println!("We have liftoff!");
@ -372,7 +375,7 @@ impl Rocket {
/// use rocket::fairing::AdHoc;
///
/// #[launch]
/// fn rocket() -> rocket::Rocket {
/// fn rocket() -> _ {
/// rocket::build()
/// .attach(AdHoc::on_liftoff("Print Config", |rocket| Box::pin(async move {
/// println!("Rocket launch config: {:?}", rocket.config());

View File

@ -43,7 +43,7 @@ use crate::http::Status;
/// }
///
/// #[launch]
/// fn rocket() -> rocket::Rocket {
/// fn rocket() -> _ {
/// rocket::build()
/// .mount("/", routes![index, raw_config_value])
/// .manage(MyConfig { user_val: "user input".to_string() })

View File

@ -103,7 +103,7 @@ async fn run_migrations(rocket: Rocket) -> Rocket {
}
#[launch]
fn rocket() -> Rocket {
fn rocket() -> _ {
rocket::build()
.attach(DbConn::fairing())
.attach(Template::fairing())

View File

@ -53,7 +53,7 @@ And finally, create a skeleton Rocket application to work off of in
#[macro_use] extern crate rocket;
#[launch]
fn rocket() -> rocket::Rocket {
fn rocket() -> _ {
rocket::build()
}
```
@ -110,7 +110,7 @@ to them. To mount the `index` route, modify the main function so that it reads:
# #[get("/")] fn index() { }
#[launch]
fn rocket() -> rocket::Rocket {
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
}
```
@ -276,7 +276,7 @@ extension trait. Ensure that the route is mounted at the root path:
# #[post("/")] fn upload() {}
#[launch]
fn rocket() -> rocket::Rocket {
fn rocket() -> _ {
rocket::build().mount("/", routes![index, upload])
}
```
@ -340,7 +340,7 @@ Make sure that the route is mounted at the root path:
# #[get("/<id>")] fn retrieve(id: String) {}
#[launch]
fn rocket() -> rocket::Rocket {
fn rocket() -> _ {
rocket::build().mount("/", routes![index, upload, retrieve])
}
```

View File

@ -71,7 +71,7 @@ fn index() -> &'static str {
}
#[launch]
fn rocket() -> rocket::Rocket {
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
}
```

View File

@ -155,7 +155,7 @@ fn world() -> &'static str {
}
#[launch]
fn rocket() -> rocket::Rocket {
fn rocket() -> _ {
rocket::build().mount("/hello", routes![world])
}
```
@ -181,12 +181,10 @@ Running the application, the console shows:
! tip: You can also return `_` from a `#[launch]` function!
If you find it more pleasing, `#[launch]` can infer the return type of
`Rocket` for you by using `_` as the return type:
`
#[launch] fn rocket() -> _ { /* ... */ }
`
Special to Rocket's `#[launch]` attribute, the return type of a function
decorated with `#[launch]` is automatically inferred when the return type is
set to `_`. If you prefer, you can also set the return type explicitly to
`Rocket<Build>`.
If we visit `http://127.0.0.1:8000/hello/world`, we see `Hello, world!`, exactly
as we expected.

View File

@ -200,7 +200,7 @@ fn user_int(id: isize) { /* ... */ }
fn user_str(id: &str) { /* ... */ }
#[launch]
fn rocket() -> rocket::Rocket {
fn rocket() -> _ {
rocket::build().mount("/", routes![user, user_int, user_str])
}
```

View File

@ -291,7 +291,7 @@ use rocket_contrib::databases::diesel;
struct LogsDbConn(diesel::SqliteConnection);
#[launch]
fn rocket() -> rocket::Rocket {
fn rocket() -> _ {
rocket::build().attach(LogsDbConn::fairing())
}
```

View File

@ -51,7 +51,7 @@ example, the following snippet attached two fairings, `req_fairing` and
```rust
# use rocket::launch;
#[launch]
fn rocket() -> rocket::Rocket {
fn rocket() -> _ {
# let req_fairing = rocket::fairing::AdHoc::on_request("example", |_, _| Box::pin(async {}));
# let res_fairing = rocket::fairing::AdHoc::on_response("example", |_, _| Box::pin(async {}));

View File

@ -123,7 +123,7 @@ fn hello() -> &'static str {
}
#[launch]
fn rocket() -> rocket::Rocket {
fn rocket() -> _ {
rocket::build().mount("/", routes![hello])
}
```
@ -164,7 +164,8 @@ To test our "Hello, world!" application, we create a `Client` for our
testing: we _want_ our tests to panic when something goes wrong.
```rust
# fn rocket() -> rocket::Rocket {
# #[rocket::launch]
# fn rocket() -> _ {
# rocket::build().reconfigure(rocket::Config::debug_default())
# }
# use rocket::local::blocking::Client;
@ -176,7 +177,8 @@ Then, we create a new `GET /` request and dispatch it, getting back our
application's response:
```rust
# fn rocket() -> rocket::Rocket {
# #[rocket::launch]
# fn rocket() -> _ {
# rocket::build().reconfigure(rocket::Config::debug_default())
# }
# use rocket::local::blocking::Client;