diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index ac4b5777..7acaf796 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -- None. +- **fixed:** Make `Router` cheaper to clone ([#1123]) + +[#1123]: https://github.com/tokio-rs/axum/pull/1123 # 0.5.9 (20. June, 2022) diff --git a/axum/src/routing/mod.rs b/axum/src/routing/mod.rs index ca178a75..18f48ec6 100644 --- a/axum/src/routing/mod.rs +++ b/axum/src/routing/mod.rs @@ -64,7 +64,7 @@ impl RouteId { /// The router type for composing handlers and services. pub struct Router { routes: HashMap>, - node: Node, + node: Arc, fallback: Fallback, nested_at_root: bool, } @@ -73,7 +73,7 @@ impl Clone for Router { fn clone(&self) -> Self { Self { routes: self.routes.clone(), - node: self.node.clone(), + node: Arc::clone(&self.node), fallback: self.fallback.clone(), nested_at_root: self.nested_at_root, } @@ -162,9 +162,12 @@ where Err(service) => Endpoint::Route(Route::new(service)), }; - if let Err(err) = self.node.insert(path, id) { + let mut node = + Arc::try_unwrap(Arc::clone(&self.node)).unwrap_or_else(|node| (*node).clone()); + if let Err(err) = node.insert(path, id) { self.panic_on_matchit_error(err); } + self.node = Arc::new(node); self.routes.insert(id, service); @@ -211,12 +214,12 @@ where panic!("Cannot nest `Router`s that has a fallback"); } - for (id, nested_path) in node.route_id_to_path { - let route = routes.remove(&id).unwrap(); - let full_path: Cow = if &*nested_path == "/" { + for (id, nested_path) in &node.route_id_to_path { + let route = routes.remove(id).unwrap(); + let full_path: Cow = if &**nested_path == "/" { path.into() } else if path == "/" { - (&*nested_path).into() + (&**nested_path).into() } else if let Some(path) = path.strip_suffix('/') { format!("{}{}", path, nested_path).into() } else { @@ -632,6 +635,7 @@ impl fmt::Debug for Endpoint { } #[test] +#[allow(warnings)] fn traits() { use crate::test_helpers::*; assert_send::>();