mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 16:32:59 +01:00
add state example
This commit is contained in:
parent
d5c54a1867
commit
115b30d9cc
21
src/app.rs
21
src/app.rs
@ -79,6 +79,27 @@ where
|
|||||||
/// multiple times. If you want to share state between different
|
/// multiple times. If you want to share state between different
|
||||||
/// threads, a shared object should be used, e.g. `Arc`. Application
|
/// threads, a shared object should be used, e.g. `Arc`. Application
|
||||||
/// state does not need to be `Send` or `Sync`.
|
/// state does not need to be `Send` or `Sync`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use std::cell::Cell;
|
||||||
|
/// use actix_web::{web, State, App};
|
||||||
|
///
|
||||||
|
/// struct MyState {
|
||||||
|
/// counter: Cell<usize>,
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// fn index(state: State<MyState>) {
|
||||||
|
/// state.counter.set(state.counter.get() + 1);
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// fn main() {
|
||||||
|
/// let app = App::new()
|
||||||
|
/// .state(MyState{ counter: Cell::new(0) })
|
||||||
|
/// .resource(
|
||||||
|
/// "/index.html",
|
||||||
|
/// |r| r.route(web::get().to(index)));
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn state<S: 'static>(mut self, state: S) -> Self {
|
pub fn state<S: 'static>(mut self, state: S) -> Self {
|
||||||
self.state.push(Box::new(State::new(state)));
|
self.state.push(Box::new(State::new(state)));
|
||||||
self
|
self
|
||||||
|
@ -938,66 +938,21 @@ impl<P> FromRequest<P> for () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rustfmt::skip]
|
||||||
|
mod m {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
tuple_from_req!(TupleFromRequest1, (0, A));
|
tuple_from_req!(TupleFromRequest1, (0, A));
|
||||||
tuple_from_req!(TupleFromRequest2, (0, A), (1, B));
|
tuple_from_req!(TupleFromRequest2, (0, A), (1, B));
|
||||||
tuple_from_req!(TupleFromRequest3, (0, A), (1, B), (2, C));
|
tuple_from_req!(TupleFromRequest3, (0, A), (1, B), (2, C));
|
||||||
tuple_from_req!(TupleFromRequest4, (0, A), (1, B), (2, C), (3, D));
|
tuple_from_req!(TupleFromRequest4, (0, A), (1, B), (2, C), (3, D));
|
||||||
tuple_from_req!(TupleFromRequest5, (0, A), (1, B), (2, C), (3, D), (4, E));
|
tuple_from_req!(TupleFromRequest5, (0, A), (1, B), (2, C), (3, D), (4, E));
|
||||||
tuple_from_req!(
|
tuple_from_req!(TupleFromRequest6, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F));
|
||||||
TupleFromRequest6,
|
tuple_from_req!(TupleFromRequest7, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G));
|
||||||
(0, A),
|
tuple_from_req!(TupleFromRequest8, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H));
|
||||||
(1, B),
|
tuple_from_req!(TupleFromRequest9, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H), (8, I));
|
||||||
(2, C),
|
tuple_from_req!(TupleFromRequest10, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H), (8, I), (9, J));
|
||||||
(3, D),
|
}
|
||||||
(4, E),
|
|
||||||
(5, F)
|
|
||||||
);
|
|
||||||
tuple_from_req!(
|
|
||||||
TupleFromRequest7,
|
|
||||||
(0, A),
|
|
||||||
(1, B),
|
|
||||||
(2, C),
|
|
||||||
(3, D),
|
|
||||||
(4, E),
|
|
||||||
(5, F),
|
|
||||||
(6, G)
|
|
||||||
);
|
|
||||||
tuple_from_req!(
|
|
||||||
TupleFromRequest8,
|
|
||||||
(0, A),
|
|
||||||
(1, B),
|
|
||||||
(2, C),
|
|
||||||
(3, D),
|
|
||||||
(4, E),
|
|
||||||
(5, F),
|
|
||||||
(6, G),
|
|
||||||
(7, H)
|
|
||||||
);
|
|
||||||
tuple_from_req!(
|
|
||||||
TupleFromRequest9,
|
|
||||||
(0, A),
|
|
||||||
(1, B),
|
|
||||||
(2, C),
|
|
||||||
(3, D),
|
|
||||||
(4, E),
|
|
||||||
(5, F),
|
|
||||||
(6, G),
|
|
||||||
(7, H),
|
|
||||||
(8, I)
|
|
||||||
);
|
|
||||||
tuple_from_req!(
|
|
||||||
TupleFromRequest10,
|
|
||||||
(0, A),
|
|
||||||
(1, B),
|
|
||||||
(2, C),
|
|
||||||
(3, D),
|
|
||||||
(4, E),
|
|
||||||
(5, F),
|
|
||||||
(6, G),
|
|
||||||
(7, H),
|
|
||||||
(8, I),
|
|
||||||
(9, J)
|
|
||||||
);
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
Loading…
Reference in New Issue
Block a user