Skip to main content

ServeMux

Struct ServeMux 

Source
pub struct ServeMux { /* private fields */ }
Expand description

HTTP request multiplexer. Port of Go’s http.ServeMux (Go 1.22+).

Matching rules:

  1. More-specific patterns beat less-specific ones (method + host > host > path).
  2. Literal path segments beat wildcard segments; wildcards beat tail wildcards.
  3. Exact path beats trailing-slash subtree of the same length.
  4. Among equally-specific patterns, longer paths win.
  5. A pattern matching path but not method returns 405 Method Not Allowed.

Implementations§

Source§

impl ServeMux

Source

pub fn new() -> Self

Source

pub fn handle(&self, pattern: &str, handler: impl Handler + 'static)

Register a handler for the given pattern.

Pattern syntax: [METHOD ][HOST]/PATH

ExampleMeaning
/subtree catch-all (old-style)
/api/v2/subtree prefix
/items/{id}single wildcard segment
/files/{path...}tail wildcard (matches rest of path)
GET /api/usersmethod-restricted exact route
example.com/host-restricted subtree
GET example.com/{id}method + host + wildcard
Source

pub fn handle_func<F>(&self, pattern: &str, f: F)
where F: Fn(&mut dyn ResponseWriter, &mut Request) + Send + Sync + 'static,

Register a function as a handler.

Source

pub fn match_handler(&self, path: &str) -> Option<Arc<dyn Handler>>

Find the best matching handler for a bare path (no method/host filtering). Retained for backward compatibility; prefer the full serve_http path for new code.

Trait Implementations§

Source§

impl Default for ServeMux

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Handler for ServeMux

Source§

fn serve_http(&self, w: &mut dyn ResponseWriter, r: &mut Request)

Auto Trait Implementations§

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> 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, 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.