Fix ignored App doc-test
@@ -74,44 +74,45 @@ use crate::{
///
/// # Application state
///
-/// ```rust, no_run, ignore
+/// ```rust, no_run
/// #![feature(async_await)]
///
-/// use tide::{Context, EndpointResult, error::ResultExt, response, App};
-/// use http::StatusCode;
+/// use http::status::StatusCode;
+/// use serde::{Deserialize, Serialize};
/// use std::sync::Mutex;
+/// use tide::{error::ResultExt, response, App, Context, EndpointResult};
///
/// #[derive(Default)]
/// struct Database {
/// contents: Mutex<Vec<Message>>,
/// }
///
-/// impl Database {
-/// fn messages(&self) -> std::sync::MutexGuard<Vec<Message>> {
-/// self.contents.lock().unwrap()
-/// }
-/// }
-///
/// #[derive(Serialize, Deserialize, Clone)]
/// struct Message {
/// author: Option<String>,
/// contents: String,
/// }
///
-/// async fn new_message(cx: Context<Database>) -> EndpointResult<String> {
-/// let msg = cx.body_json().await.client_err()?;
+/// impl Database {
+/// fn insert(&self, msg: Message) -> usize {
+/// let mut table = self.contents.lock().unwrap();
+/// table.push(msg);
+/// table.len() - 1
+/// }
///
-/// let mut messages = cx.app_data().messages();
-/// let id = messages.len();
-/// messages.push(msg);
+/// fn get(&self, id: usize) -> Option<Message> {
+/// self.contents.lock().unwrap().get(id).cloned()
+/// }
+/// }
///
-/// Ok(id.to_string())
+/// async fn new_message(mut cx: Context<Database>) -> EndpointResult<String> {
+/// let msg = cx.body_json().await.client_err()?;
+/// Ok(cx.state().insert(msg).to_string())
/// }
///
/// async fn get_message(cx: Context<Database>) -> EndpointResult {
-/// let id: usize = cx.param("id").client_err()?;
-///
-/// if let Some(msg) = cx.app_data().messages().get(id) {
+/// let id = cx.param("id").client_err()?;
+/// if let Some(msg) = cx.state().get(id) {
/// Ok(response::json(msg))
/// } else {
/// Err(StatusCode::NOT_FOUND)?
@@ -119,11 +120,12 @@ use crate::{
/// }
///
/// fn main() {
-/// let mut app = App::new(Database::default());
+/// let mut app = App::with_state(Database::default());
/// app.at("/message").post(new_message);
/// app.at("/message/:id").get(get_message);
/// app.serve("127.0.0.1:8000").unwrap();
/// }
+/// ```
pub struct App<State> {
router: Router<State>,
