Struct treemux::Treemux

source ·
pub struct Treemux { /* private fields */ }

Implementations§

source§

impl Treemux

source

pub fn builder() -> Builder<Identity>

source

pub fn lookup<'b, P: AsRef<str>>( &'b self, method: &'b Method, path: P ) -> Result<(RequestHandler, Params), bool>

Lookup allows the manual lookup of handler for a specific method and path. If the handler is not found, it returns a Err(bool) indicating whether a redirection should be performed to the same path with a trailing slash

use treemux::{Treemux, RouterBuilder};
use hyper::{Response, Body, Method};

let mut router = Treemux::builder();
router.get("/home", |_| {
    async { Ok(Response::new(Body::from("Welcome!"))) }
});
let router: Treemux = router.into();

let res = router.lookup(&Method::GET, "/home").unwrap();
assert!(res.1.is_empty());
source

pub fn into_service_with_context<T: Send + Sync + 'static>( self, context: T ) -> MakeRouterService<T>

Converts the Treemux into a Service which you can serve directly with Hyper. If you have an existing Service that you want to incorporate a Treemux into, see Treemux::serve.

// Our router...
let router = Treemux::builder();

// Convert it into a service...
let service = router.into_service();

// Serve with hyper
hyper::Server::bind(&([127, 0, 0, 1], 3030).into())
    .serve(service)
    .await?;
source

pub fn into_service(self) -> MakeRouterService<()>

Converts the Treemux into a Service which you can serve directly with Hyper. If you have an existing Service that you want to incorporate a Treemux into, see Treemux::serve.

// Our router...
let router = Treemux::builder();

// Convert it into a service...
let service = router.into_service();

// Serve with hyper
hyper::Server::bind(&([127, 0, 0, 1], 3030).into())
    .serve(service)
    .await?;
source

pub async fn serve(&self, req: Request<Body>) -> ResponseResult

An asynchronous function from a Request to a Response. You will generally not need to use this function directly, and instead use Treemux::into_service. However, it may be useful when incorporating the router into a larger service.


let mut router = Treemux::builder();

let router: Arc<Treemux> = Arc::new(router.into());

let make_svc = make_service_fn(move |_| {
    let router = router.clone();
    async move {
        Ok::<_, Infallible>(service_fn(move |req: Request<Body>| {
            let router = router.clone();
            async move { router.serve(req).await }
        }))
    }
});

let server = Server::bind(&([127, 0, 0, 1], 3000).into())
    .serve(make_svc)
    .await;

Trait Implementations§

source§

impl Debug for Treemux

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Treemux

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<M> From<Builder<M>> for Treemux

source§

fn from(b: Builder<M>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

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

const: unstable · 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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more