Struct Router

pub struct Router<T> { /* private fields */ }

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(&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, 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>(&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(&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!"));

Trait Implementations

impl<T> Default for Router<T>

fn default() -> Self

impl<T: Clone> Clone for Router<T>

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

impl<T: Debug> Debug for Router<T>

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

Auto Trait Implementations

impl<T> !Freeze for Router<T>

impl<T> !RefUnwindSafe for Router<T>

impl<T> Send for Router<T> where Node<T>: Send,

impl<T> Sync for Router<T> where Node<T>: Sync,

impl<T> Unpin for Router<T> where Node<T>: Unpin,

impl<T> UnsafeUnpin for Router<T> where Node<T>: UnsafeUnpin,

impl<T> UnwindSafe for Router<T> where Node<T>: UnwindSafe,

Blanket Implementations

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

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

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

impl<T> From<T> for Router<T>

fn from(t: T) -> T

Returns the argument unchanged.

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

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

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

fn into(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<U> for Router<T> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

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

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