pub struct Router<T> { /* private fields */ }
Expand description
A zero-copy URL router.
See the crate documentation for details.
Implementations§
Source§impl<T> Router<T>
impl<T> Router<T>
Sourcepub fn insert(
&mut self,
route: impl Into<String>,
value: T,
) -> Result<(), InsertError>
pub fn insert( &mut self, route: impl Into<String>, value: T, ) -> Result<(), InsertError>
Insert a route into the router.
§Examples
let mut router = Router::new();
router.insert("/home", "Welcome!")?;
router.insert("/users/{id}", "A User")?;
Sourcepub fn at<'path>(
&self,
path: &'path str,
) -> Result<Match<'_, 'path, &T>, MatchError>
pub 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
let mut router = Router::new();
router.insert("/home", "Welcome!")?;
let matched = router.at("/home").unwrap();
assert_eq!(*matched.value, "Welcome!");
Sourcepub fn at_mut<'path>(
&mut self,
path: &'path str,
) -> Result<Match<'_, 'path, &mut T>, MatchError>
pub 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
let mut router = Router::new();
router.insert("/", 1)?;
*router.at_mut("/").unwrap().value += 1;
assert_eq!(*router.at("/").unwrap().value, 2);
Sourcepub fn remove(&mut self, path: impl Into<String>) -> Option<T>
pub 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
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§
Auto Trait Implementations§
impl<T> !Freeze for Router<T>
impl<T> !RefUnwindSafe for Router<T>
impl<T> Send for Router<T>where
T: Send,
impl<T> Sync for Router<T>where
T: Sync,
impl<T> Unpin for Router<T>where
T: Unpin,
impl<T> UnwindSafe for Router<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more