Struct Router

struct Router<T> { ... }

A zero-copy URL router.

See the crate documentation for details.

Implementations

impl<T> Router<T>

fn new() -> Self

Construct a new router.

fn insert<impl Into<String>: Into<String>>(self: &mut Self, route: impl Into<String>, value: T) -> Result<(), InsertError>

Insert a route into the router.

Examples

# use matchit::Router;
# fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut router = Router::new();
router.insert("/home", "Welcome!")?;
router.insert("/users/{id}", "A User")?;
# Ok(())
# }
fn at<'path>(self: &Self, path: &'path str) -> Result<Match<'_, 'path, &T>, MatchError>

Tries to find a value in the router matching the given path.

Examples

# use matchit::Router;
# fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut router = Router::new();
router.insert("/home", "Welcome!")?;

let matched = router.at("/home").unwrap();
assert_eq!(*matched.value, "Welcome!");
# Ok(())
# }
fn at_mut<'path>(self: &mut Self, path: &'path str) -> Result<Match<'_, 'path, &mut T>, MatchError>

Tries to find a value in the router matching the given path, returning a mutable reference.

Examples

# use matchit::Router;
# fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut router = Router::new();
router.insert("/", 1)?;

*router.at_mut("/").unwrap().value += 1;
assert_eq!(*router.at("/").unwrap().value, 2);
# Ok(())
# }
fn remove<impl Into<String>: Into<String>>(self: &mut Self, path: impl Into<String>) -> Option<T>

Remove a given route from the router.

Returns the value stored under the route if it was found. If the route was not found or invalid, None is returned.

Examples

# use matchit::Router;
let mut router = Router::new();

router.insert("/home", "Welcome!");
assert_eq!(router.remove("/home"), Some("Welcome!"));
assert_eq!(router.remove("/home"), None);

router.insert("/home/{id}/", "Hello!");
assert_eq!(router.remove("/home/{id}/"), Some("Hello!"));
assert_eq!(router.remove("/home/{id}/"), None);

router.insert("/home/{id}/", "Hello!");
// the route does not match
assert_eq!(router.remove("/home/{user}"), None);
assert_eq!(router.remove("/home/{id}/"), Some("Hello!"));

router.insert("/home/{id}/", "Hello!");
// invalid route
assert_eq!(router.remove("/home/{id"), None);
assert_eq!(router.remove("/home/{id}/"), Some("Hello!"));

impl<T> Any for Router<T>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Router<T>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Router<T>

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

impl<T> CloneToUninit for Router<T>

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

impl<T> Default for Router<T>

fn default() -> Self

impl<T> Freeze for Router<T>

impl<T> From for Router<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> RefUnwindSafe for Router<T>

impl<T> Send for Router<T>

impl<T> Sync for Router<T>

impl<T> ToOwned for Router<T>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> Unpin for Router<T>

impl<T> UnsafeUnpin for Router<T>

impl<T> UnwindSafe for Router<T>

impl<T, U> Into for Router<T>

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for Router<T>

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

impl<T, U> TryInto for Router<T>

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

impl<T: $crate::clone::Clone> Clone for Router<T>

fn clone(self: &Self) -> Router<T>

impl<T: $crate::fmt::Debug> Debug for Router<T>

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