pub struct Router<State = ()> { /* private fields */ }Expand description
Router mapping command names to handlers.
Implementations§
Source§impl<State> Router<State>
impl<State> Router<State>
Sourcepub fn from_state(state: State) -> Self
pub fn from_state(state: State) -> Self
Create a new router with the provided state.
Examples found in repository?
examples/history.rs (line 45)
40pub async fn main() -> Result<()> {
41 let addr = env::args()
42 .nth(1)
43 .unwrap_or_else(|| "0.0.0.0:6379".to_string());
44
45 let app = Router::from_state(AppState::default())
46 .route("HISTORY", history)
47 .route("COMMAND", history)
48 .route("PING", ping);
49
50 Server::bind(&addr)
51 .with_graceful_shutdown(async {
52 let _ = signal::ctrl_c().await;
53 })
54 .serve(app)
55 .await
56}Sourcepub fn with_state(self, state: State) -> Self
pub fn with_state(self, state: State) -> Self
Replace the state while keeping existing routes.
Sourcepub fn route<H, Args>(self, command: &'static str, handler: H) -> Selfwhere
H: IntoHandler<State, Args>,
pub fn route<H, Args>(self, command: &'static str, handler: H) -> Selfwhere
H: IntoHandler<State, Args>,
Register a command handler.
The handler must be an async function or Fn that returns a Send + 'static future.
Examples found in repository?
More examples
examples/history.rs (line 46)
40pub async fn main() -> Result<()> {
41 let addr = env::args()
42 .nth(1)
43 .unwrap_or_else(|| "0.0.0.0:6379".to_string());
44
45 let app = Router::from_state(AppState::default())
46 .route("HISTORY", history)
47 .route("COMMAND", history)
48 .route("PING", ping);
49
50 Server::bind(&addr)
51 .with_graceful_shutdown(async {
52 let _ = signal::ctrl_c().await;
53 })
54 .serve(app)
55 .await
56}Trait Implementations§
Auto Trait Implementations§
impl<State> Freeze for Router<State>
impl<State = ()> !RefUnwindSafe for Router<State>
impl<State> Send for Router<State>
impl<State> Sync for Router<State>
impl<State> Unpin for Router<State>
impl<State = ()> !UnwindSafe for Router<State>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more