135
135
#![ forbid( unsafe_code) ]
136
136
#![ warn( rust_2018_idioms, unreachable_pub) ]
137
137
138
- use smallvec:: SmallVec ;
139
138
use std:: str:: from_utf8;
140
139
140
+ use smallvec:: SmallVec ;
141
+
141
142
mod node;
142
143
mod parser;
143
144
144
145
pub use node:: { Node , NodeKind } ;
145
146
pub use parser:: { Kind , Parser , Piece , Position } ;
146
147
147
148
#[ derive( Debug ) ]
148
- pub struct PathTree < ' a , T > {
149
+ pub struct PathTree < T > {
149
150
id : usize ,
150
- routes : Vec < ( T , Vec < Piece < ' a > > ) > ,
151
- pub node : Node < ' a , usize > ,
151
+ routes : Vec < ( T , Vec < Piece > ) > ,
152
+ pub node : Node < usize > ,
152
153
}
153
154
154
- impl < ' a , T > Default for PathTree < ' a , T > {
155
+ impl < T > Default for PathTree < T > {
155
156
fn default ( ) -> Self {
156
157
Self :: new ( )
157
158
}
158
159
}
159
160
160
- impl < ' a , T > PathTree < ' a , T > {
161
+ impl < T > PathTree < T > {
161
162
pub fn new ( ) -> Self {
162
163
Self {
163
164
id : 0 ,
164
165
routes : Vec :: new ( ) ,
165
- node : Node :: new ( NodeKind :: String ( "" . as_bytes ( ) ) , None ) ,
166
+ node : Node :: new ( NodeKind :: String ( "" . as_bytes ( ) . to_vec ( ) ) , None ) ,
166
167
}
167
168
}
168
169
169
- pub fn insert ( & mut self , path : & ' a str , value : T ) -> usize {
170
+ pub fn insert ( & mut self , path : & str , value : T ) -> usize {
170
171
if path. is_empty ( ) {
171
172
return self . id ;
172
173
}
@@ -177,7 +178,7 @@ impl<'a, T> PathTree<'a, T> {
177
178
for piece in & pieces {
178
179
match piece {
179
180
Piece :: String ( s) => {
180
- node = node. insert_bytes ( s ) ;
181
+ node = node. insert_bytes ( & s [ .. ] ) ;
181
182
}
182
183
Piece :: Parameter ( _, k) => {
183
184
node = node. insert_parameter ( * k) ;
@@ -196,7 +197,7 @@ impl<'a, T> PathTree<'a, T> {
196
197
}
197
198
}
198
199
199
- pub fn find < ' b > ( & ' a self , path : & ' b str ) -> Option < Path < ' a , ' b , T > > {
200
+ pub fn find < ' b > ( & self , path : & ' b str ) -> Option < Path < ' _ , ' b , T > > {
200
201
let bytes = path. as_bytes ( ) ;
201
202
self . node . find ( bytes) . and_then ( |( id, ranges) | {
202
203
self . get_route ( * id) . map ( |( value, pieces) | {
@@ -206,8 +207,8 @@ impl<'a, T> PathTree<'a, T> {
206
207
pieces,
207
208
// opt!
208
209
raws : ranges
209
- . chunks ( 2 )
210
- . map ( |r| from_utf8 ( & bytes[ r[ 0 ] ..r [ 1 ] ] ) . unwrap ( ) )
210
+ . into_iter ( )
211
+ . map ( |r| from_utf8 ( & bytes[ r] ) . unwrap ( ) )
211
212
. rev ( )
212
213
. collect ( ) ,
213
214
}
@@ -216,7 +217,7 @@ impl<'a, T> PathTree<'a, T> {
216
217
}
217
218
218
219
#[ inline]
219
- pub fn get_route ( & self , index : usize ) -> Option < & ( T , Vec < Piece < ' a > > ) > {
220
+ pub fn get_route ( & self , index : usize ) -> Option < & ( T , Vec < Piece > ) > {
220
221
self . routes . get ( index)
221
222
}
222
223
@@ -246,7 +247,7 @@ impl<'a, T> PathTree<'a, T> {
246
247
pub struct Path < ' a , ' b , T > {
247
248
pub id : & ' a usize ,
248
249
pub value : & ' a T ,
249
- pub pieces : & ' a [ Piece < ' a > ] ,
250
+ pub pieces : & ' a [ Piece ] ,
250
251
pub raws : SmallVec < [ & ' b str ; 4 ] > ,
251
252
}
252
253
0 commit comments