1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-30 19:54:36 +01:00

helper impls for Router

This commit is contained in:
Nikolay Kim 2019-01-05 22:00:38 -08:00
parent 3484007e4e
commit 2b8f41e9e4
3 changed files with 54 additions and 3 deletions

View File

@ -7,7 +7,7 @@ mod router;
pub use self::de::PathDeserializer; pub use self::de::PathDeserializer;
pub use self::path::Path; pub use self::path::Path;
pub use self::pattern::Pattern; pub use self::pattern::Pattern;
pub use self::router::{Router, RouterBuilder}; pub use self::router::{ResourceInfo, Router, RouterBuilder};
pub trait RequestPath { pub trait RequestPath {
fn path(&self) -> &str; fn path(&self) -> &str;
@ -30,3 +30,15 @@ impl<T: AsRef<[u8]>> RequestPath for string::String<T> {
&*self &*self
} }
} }
#[cfg(feature = "http")]
mod http_support {
use super::RequestPath;
use http::Uri;
impl RequestPath for Uri {
fn path(&self) -> &str {
self.path()
}
}
}

View File

@ -12,7 +12,7 @@ pub(crate) enum PathItem {
/// Resource path match information /// Resource path match information
/// ///
/// If resource path contains variable patterns, `Path` stores them. /// If resource path contains variable patterns, `Path` stores them.
#[derive(Debug, Clone)] #[derive(Debug)]
pub struct Path<T> { pub struct Path<T> {
path: T, path: T,
pub(crate) skip: u16, pub(crate) skip: u16,
@ -29,6 +29,16 @@ impl<T: Default> Default for Path<T> {
} }
} }
impl<T: Clone> Clone for Path<T> {
fn clone(&self) -> Self {
Path {
path: self.path.clone(),
skip: self.skip,
segments: self.segments.clone(),
}
}
}
impl<T: RequestPath> Path<T> { impl<T: RequestPath> Path<T> {
pub fn new(path: T) -> Path<T> { pub fn new(path: T) -> Path<T> {
Path { Path {
@ -38,6 +48,16 @@ impl<T: RequestPath> Path<T> {
} }
} }
/// Get reference to inner path instance
pub fn get_ref(&self) -> &T {
&self.path
}
/// Get mutable reference to inner path instance
pub fn get_mut(&mut self) -> &mut T {
&mut self.path
}
pub fn path(&self) -> &str { pub fn path(&self) -> &str {
let skip = self.skip as usize; let skip = self.skip as usize;
let path = self.path.path(); let path = self.path.path();
@ -68,7 +88,8 @@ impl<T: RequestPath> Path<T> {
} }
} }
pub(crate) fn add_static(&mut self, name: &str, value: &'static str) { #[doc(hidden)]
pub fn add_static(&mut self, name: &str, value: &'static str) {
self.segments self.segments
.push((Rc::new(name.to_string()), PathItem::Static(value))); .push((Rc::new(name.to_string()), PathItem::Static(value)));
} }

View File

@ -75,6 +75,24 @@ impl<T> Router<T> {
} }
} }
impl<'a, T> IntoIterator for &'a Router<T> {
type Item = &'a T;
type IntoIter = std::slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.resources.iter()
}
}
impl<'a, T> IntoIterator for &'a mut Router<T> {
type Item = &'a mut T;
type IntoIter = std::slice::IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.resources.iter_mut()
}
}
impl ResourceMap { impl ResourceMap {
fn register(&mut self, pattern: Pattern) { fn register(&mut self, pattern: Pattern) {
self.patterns.push(pattern); self.patterns.push(pattern);