11use routefinder:: { Captures , Router as MethodRouter } ;
22use std:: collections:: HashMap ;
3+ use std:: sync:: Arc ;
34
45use crate :: endpoint:: DynEndpoint ;
56use crate :: { Request , Response , StatusCode } ;
@@ -10,8 +11,8 @@ use crate::{Request, Response, StatusCode};
1011/// by the method first allows the table itself to be more efficient.
1112#[ allow( missing_debug_implementations) ]
1213pub ( crate ) struct Router {
13- method_map : HashMap < http_types:: Method , MethodRouter < Box < DynEndpoint > > > ,
14- all_method_router : MethodRouter < Box < DynEndpoint > > ,
14+ method_map : HashMap < http_types:: Method , MethodRouter < Arc < DynEndpoint > > > ,
15+ all_method_router : MethodRouter < Arc < DynEndpoint > > ,
1516}
1617
1718impl std:: fmt:: Debug for Router {
@@ -24,8 +25,8 @@ impl std::fmt::Debug for Router {
2425}
2526
2627/// The result of routing a URL
27- pub ( crate ) struct Selection < ' a > {
28- pub ( crate ) endpoint : & ' a DynEndpoint ,
28+ pub ( crate ) struct Selection {
29+ pub ( crate ) endpoint : Arc < DynEndpoint > ,
2930 pub ( crate ) params : Captures < ' static , ' static > ,
3031}
3132
@@ -37,31 +38,31 @@ impl Router {
3738 }
3839 }
3940
40- pub ( crate ) fn add ( & mut self , path : & str , method : http_types:: Method , ep : Box < DynEndpoint > ) {
41+ pub ( crate ) fn add ( & mut self , path : & str , method : http_types:: Method , ep : Arc < DynEndpoint > ) {
4142 self . method_map
4243 . entry ( method)
4344 . or_insert_with ( MethodRouter :: new)
4445 . add ( path, ep)
4546 . unwrap ( )
4647 }
4748
48- pub ( crate ) fn add_all ( & mut self , path : & str , ep : Box < DynEndpoint > ) {
49+ pub ( crate ) fn add_all ( & mut self , path : & str , ep : Arc < DynEndpoint > ) {
4950 self . all_method_router . add ( path, ep) . unwrap ( )
5051 }
5152
52- pub ( crate ) fn route ( & self , path : & str , method : http_types:: Method ) -> Selection < ' _ > {
53+ pub ( crate ) fn route ( & self , path : & str , method : http_types:: Method ) -> Selection {
5354 if let Some ( m) = self
5455 . method_map
5556 . get ( & method)
5657 . and_then ( |r| r. best_match ( path) )
5758 {
5859 Selection {
59- endpoint : m. handler ( ) ,
60+ endpoint : m. handler ( ) . to_owned ( ) ,
6061 params : m. captures ( ) . into_owned ( ) ,
6162 }
6263 } else if let Some ( m) = self . all_method_router . best_match ( path) {
6364 Selection {
64- endpoint : m. handler ( ) ,
65+ endpoint : m. handler ( ) . to_owned ( ) ,
6566 params : m. captures ( ) . into_owned ( ) ,
6667 }
6768 } else if method == http_types:: Method :: Head {
@@ -78,12 +79,12 @@ impl Router {
7879 // If this `path` can be handled by a callback registered with a different HTTP method
7980 // should return 405 Method Not Allowed
8081 Selection {
81- endpoint : & method_not_allowed,
82+ endpoint : Arc :: new ( method_not_allowed) ,
8283 params : Captures :: default ( ) ,
8384 }
8485 } else {
8586 Selection {
86- endpoint : & not_found_endpoint,
87+ endpoint : Arc :: new ( not_found_endpoint) ,
8788 params : Captures :: default ( ) ,
8889 }
8990 }
0 commit comments