Skip to main content

Router

Struct Router 

Source
pub struct Router<S, R = Response> { /* private fields */ }
Expand description

Fixed-path router with middleware chain.

Implementations§

Source§

impl<S, R> Router<S, R>
where S: Clone + Send + Sync + 'static, R: From<Response> + Send + 'static,

Source

pub fn new(state: S) -> Self

Creates an empty router bound to a clonable state value.

Source

pub fn get<F, Fut>(self, path: impl Into<String>, handler: F) -> Self
where F: Fn(RoutedRequest, S) -> Fut + Send + Sync + 'static, Fut: Future<Output = R> + Send + 'static,

Registers a GET route.

Source

pub fn post<F, Fut>(self, path: impl Into<String>, handler: F) -> Self
where F: Fn(RoutedRequest, S) -> Fut + Send + Sync + 'static, Fut: Future<Output = R> + Send + 'static,

Registers a POST route.

Source

pub fn put<F, Fut>(self, path: impl Into<String>, handler: F) -> Self
where F: Fn(RoutedRequest, S) -> Fut + Send + Sync + 'static, Fut: Future<Output = R> + Send + 'static,

Registers a PUT route.

Source

pub fn delete<F, Fut>(self, path: impl Into<String>, handler: F) -> Self
where F: Fn(RoutedRequest, S) -> Fut + Send + Sync + 'static, Fut: Future<Output = R> + Send + 'static,

Registers a DELETE route.

Source

pub fn patch<F, Fut>(self, path: impl Into<String>, handler: F) -> Self
where F: Fn(RoutedRequest, S) -> Fut + Send + Sync + 'static, Fut: Future<Output = R> + Send + 'static,

Registers a PATCH route.

Source

pub fn head<F, Fut>(self, path: impl Into<String>, handler: F) -> Self
where F: Fn(RoutedRequest, S) -> Fut + Send + Sync + 'static, Fut: Future<Output = R> + Send + 'static,

Registers a HEAD route.

Source

pub fn options<F, Fut>(self, path: impl Into<String>, handler: F) -> Self
where F: Fn(RoutedRequest, S) -> Fut + Send + Sync + 'static, Fut: Future<Output = R> + Send + 'static,

Registers an OPTIONS route.

Source

pub fn any<F, Fut>(self, path: impl Into<String>, handler: F) -> Self
where F: Fn(RoutedRequest, S) -> Fut + Send + Sync + 'static, Fut: Future<Output = R> + Send + 'static,

Registers a route for any method.

Source

pub fn on<F, Fut>( self, method: Method, path: impl Into<String>, handler: F, ) -> Self
where F: Fn(RoutedRequest, S) -> Fut + Send + Sync + 'static, Fut: Future<Output = R> + Send + 'static,

Registers a route for an explicit method token.

Source

pub fn middleware<F, Fut>(self, middleware: F) -> Self
where F: Fn(RoutedRequest, S, Next<S, R>) -> Fut + Send + Sync + 'static, Fut: Future<Output = R> + Send + 'static,

Adds a middleware to the end of the chain.

The middleware applies to all routes currently registered on this router and to any routes added later on the same router value. Middleware attached to another router does not leak across Self::merge.

Source

pub fn merge(self, other: Self) -> Self

Merges routes and middleware from another router with the same state.

Source

pub async fn handle(&self, request: Request) -> R

Dispatches a request entirely in-process.

Source

pub async fn handle_routed(&self, request: RoutedRequest) -> R

Dispatches a routed request entirely in-process.

Trait Implementations§

Source§

impl<S, R> Clone for Router<S, R>
where S: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<S, R> Freeze for Router<S, R>
where S: Freeze,

§

impl<S, R = Response> !RefUnwindSafe for Router<S, R>

§

impl<S, R> Send for Router<S, R>
where S: Send,

§

impl<S, R> Sync for Router<S, R>
where S: Sync,

§

impl<S, R> Unpin for Router<S, R>
where S: Unpin,

§

impl<S, R> UnsafeUnpin for Router<S, R>
where S: UnsafeUnpin,

§

impl<S, R = Response> !UnwindSafe for Router<S, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.