diff --git a/src/httprequest.rs b/src/httprequest.rs index d511d090..ffd13919 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -94,10 +94,6 @@ impl HttpInnerMessage { } } -lazy_static! { - static ref RESOURCE: Resource = Resource::unset(); -} - /// An HTTP Request pub struct HttpRequest(SharedHttpInnerMessage, Option>, Option); @@ -345,13 +341,13 @@ impl HttpRequest { /// This method returns reference to matched `Resource` object. #[inline] - pub fn resource(&self) -> &Resource { + pub fn resource(&self) -> Option<&Resource> { if let Some(ref router) = self.2 { if let RouterResource::Normal(idx) = self.as_ref().resource { - return router.get_resource(idx as usize); + return Some(router.get_resource(idx as usize)); } } - &*RESOURCE + None } pub(crate) fn set_resource(&mut self, res: usize) { diff --git a/src/param.rs b/src/param.rs index 1003c642..76262c2a 100644 --- a/src/param.rs +++ b/src/param.rs @@ -1,4 +1,5 @@ use std; +use std::rc::Rc; use std::ops::Index; use std::path::PathBuf; use std::str::FromStr; @@ -32,7 +33,7 @@ pub(crate) enum ParamItem { pub struct Params { url: Url, pub(crate) tail: u16, - segments: SmallVec<[(&'static str, ParamItem); 3]>, + segments: SmallVec<[(Rc, ParamItem); 3]>, } impl Params { @@ -56,12 +57,12 @@ impl Params { self.tail = tail; } - pub(crate) fn add(&mut self, name: &'static str, value: ParamItem) { + pub(crate) fn add(&mut self, name: Rc, value: ParamItem) { self.segments.push((name, value)); } - pub(crate) fn add_static(&mut self, name: &'static str, value: &'static str) { - self.segments.push((name, ParamItem::Static(value))); + pub(crate) fn add_static(&mut self, name: &str, value: &'static str) { + self.segments.push((Rc::new(name.to_string()), ParamItem::Static(value))); } /// Check if there are any matched patterns @@ -77,7 +78,7 @@ impl Params { /// Get matched parameter by name without type conversion pub fn get(&self, key: &str) -> Option<&str> { for item in self.segments.iter() { - if key == item.0 { + if key == item.0.as_str() { return match item.1 { ParamItem::Static(ref s) => Some(&s), ParamItem::UrlSegment(s, e) => { diff --git a/src/router.rs b/src/router.rs index e37e46b4..fcbb0d44 100644 --- a/src/router.rs +++ b/src/router.rs @@ -143,7 +143,7 @@ enum PatternElement { enum PatternType { Static(String), Prefix(String), - Dynamic(Regex, Vec<&'static str>, usize), + Dynamic(Regex, Vec>, usize), } #[derive(Debug, Copy, Clone, PartialEq)] @@ -195,17 +195,6 @@ impl Resource { resource } - /// Unset resource type - pub(crate) fn unset() -> Resource { - Resource { - tp: PatternType::Static("".to_owned()), - rtp: ResourceType::Unset, - name: "".to_owned(), - pattern: "".to_owned(), - elements: Vec::new(), - } - } - /// Parse path pattern and create new `Resource` instance with custom prefix pub fn with_prefix(name: &str, path: &str, prefix: &str, for_prefix: bool) -> Self { let (pattern, elements, is_dynamic, len) = @@ -220,11 +209,7 @@ impl Resource { let names = re .capture_names() .filter_map(|name| { - name.map(|name| { - let s: &'static str = - Box::leak(name.to_owned().into_boxed_str()); - s - }) + name.map(|name| Rc::new(name.to_owned())) }) .collect(); PatternType::Dynamic(re, names, len) @@ -315,7 +300,7 @@ impl Resource { let params = req.match_info_mut(); params.set_tail(len as u16); for (idx, segment) in segments.into_iter().enumerate() { - params.add(names[idx], segment); + params.add(names[idx].clone(), segment); } true } @@ -381,7 +366,7 @@ impl Resource { let params = req.match_info_mut(); params.set_tail(tail_len as u16); for (idx, segment) in segments.into_iter().enumerate() { - params.add(names[idx], segment); + params.add(names[idx].clone(), segment); } Some(tail_len) } @@ -774,13 +759,13 @@ mod tests { let mut req = TestRequest::with_uri("/index.json").finish_with_router(router.clone()); assert_eq!(router.recognize(&mut req), Some(0)); - let resource = req.resource(); + let resource = req.resource().unwrap(); assert_eq!(resource.name(), "r1"); let mut req = TestRequest::with_uri("/test.json").finish_with_router(router.clone()); assert_eq!(router.recognize(&mut req), Some(1)); - let resource = req.resource(); + let resource = req.resource().unwrap(); assert_eq!(resource.name(), "r2"); } }