Repository: actix/actix-web
File: src/router.rs (Rust)

Code before:
    /// This method does not take `prefix` into account.
    /// For example if prefix is `/test` and router contains route `/name`,
    /// following path would be recognizable `/test/name` but `has_route()` call
    /// would return `false`.
    pub fn has_route(&self, path: &str) -> bool {
        let path = if path.is_empty() { "/" } else { path };

        for pattern in &self.router.patterns {
            if pattern.is_match(path) {
                return true;
            }
        }
        false
    }

    /// Check if application contains matching route.
    ///
    /// This method does take `prefix` into account.
    /// For example if prefix is `/test` and router contains route `/name`,
    /// following path would be recognizable `/test/name` and `has_route_prefixed()` call
    /// would return `true`.
    pub fn has_route_prefixed(&self, path: &str) -> bool {
        let path = if path.is_empty() {
            "/".into()
        } else if !self.router.prefix.is_empty() {
            let pb = PathBuf::from(path);
            if let Ok(p) = pb.strip_prefix(Path::new(&self.router.prefix)) {
                format!("/{}", p.to_str().unwrap_or(path))
            } else {
                path.into()
            }
        } else {
            path.into()
        };

        for pattern in &self.router.patterns {
            if pattern.is_match(&path) {
                return true;
            }
        }

        false
    }
}

struct Inner {
    prefix: String,
    prefix_len: usize,
    named: HashMap<String, (Resource, bool)>,
    patterns: Vec<Resource>,
}

Review comment (suggestion):
Use `Path::new()` instead, as argument is string slice, we can get `Path` directly without a need for allocation

Code after:
        }

        self.prefix = info.params.tail;
        self.params = p;
    }

    /// Generate url for named resource
    ///
    /// Check [`HttpRequest::url_for()`](../struct.HttpRequest.html#method.
    /// url_for) for detailed information.
    pub fn url_for<U, I>(
        &self, req: &Request, name: &str, elements: U,
    ) -> Result<Url, UrlGenerationError>
    where
        U: IntoIterator<Item = I>,
        I: AsRef<str>,
    {
        if let Some(pattern) = self.router.named.get(name) {
            let path =
                pattern.resource_path(elements, &req.path()[..(self.prefix as usize)])?;
            if path.starts_with('/') {
                let conn = req.connection_info();
                Ok(Url::parse(&format!(
                    "{}://{}{}",
                    conn.scheme(),
                    conn.host(),
                    path
                ))?)
            } else {
                Ok(Url::parse(&path)?)
            }
        } else {
            Err(UrlGenerationError::ResourceNotFound)
        }
    }

    /// Check if application contains matching route.
    ///
    /// This method does not take `prefix` into account.
    /// For example if prefix is `/test` and router contains route `/name`,
    /// following path would be recognizable `/test/name` but `has_route()` call
    /// would return `false`.
    pub fn has_route(&self, path: &str) -> bool {
        let path = if path.is_empty() { "/" } else { path };

        for pattern in &self.router.patterns {
            if pattern.is_match(path) {
                return true;
            }
        }
        false