Skip to content

Commit fde8ed2

Browse files
authored
Remove lifetime (#22)
* Remove lifetime by using `Vec`s * Fix tests and examples * Remove unneeded lifetime from `PathTree::find` * Run rustfmt * Use `Range` directly * Remove added lifetime from merge
1 parent 6c0b846 commit fde8ed2

File tree

7 files changed

+372
-436
lines changed

7 files changed

+372
-436
lines changed

examples/hello.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
7676
// │ ├── rust •3
7777
// │ └── ** •1
7878
// └── POST/login •4
79-
let mut tree = PathTree::<'static, Box<dyn Handler>>::new();
79+
let mut tree = PathTree::<Box<dyn Handler>>::new();
8080
tree.insert("/GET/", Box::new(index));
8181
tree.insert("/GET/*", Box::new(hello_world));
8282
tree.insert("/GET/hello/:name", Box::new(hello_user));

src/lib.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -135,38 +135,39 @@
135135
#![forbid(unsafe_code)]
136136
#![warn(rust_2018_idioms, unreachable_pub)]
137137

138-
use smallvec::SmallVec;
139138
use std::str::from_utf8;
140139

140+
use smallvec::SmallVec;
141+
141142
mod node;
142143
mod parser;
143144

144145
pub use node::{Node, NodeKind};
145146
pub use parser::{Kind, Parser, Piece, Position};
146147

147148
#[derive(Debug)]
148-
pub struct PathTree<'a, T> {
149+
pub struct PathTree<T> {
149150
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>,
152153
}
153154

154-
impl<'a, T> Default for PathTree<'a, T> {
155+
impl<T> Default for PathTree<T> {
155156
fn default() -> Self {
156157
Self::new()
157158
}
158159
}
159160

160-
impl<'a, T> PathTree<'a, T> {
161+
impl<T> PathTree<T> {
161162
pub fn new() -> Self {
162163
Self {
163164
id: 0,
164165
routes: Vec::new(),
165-
node: Node::new(NodeKind::String("".as_bytes()), None),
166+
node: Node::new(NodeKind::String("".as_bytes().to_vec()), None),
166167
}
167168
}
168169

169-
pub fn insert(&mut self, path: &'a str, value: T) -> usize {
170+
pub fn insert(&mut self, path: &str, value: T) -> usize {
170171
if path.is_empty() {
171172
return self.id;
172173
}
@@ -177,7 +178,7 @@ impl<'a, T> PathTree<'a, T> {
177178
for piece in &pieces {
178179
match piece {
179180
Piece::String(s) => {
180-
node = node.insert_bytes(s);
181+
node = node.insert_bytes(&s[..]);
181182
}
182183
Piece::Parameter(_, k) => {
183184
node = node.insert_parameter(*k);
@@ -196,7 +197,7 @@ impl<'a, T> PathTree<'a, T> {
196197
}
197198
}
198199

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>> {
200201
let bytes = path.as_bytes();
201202
self.node.find(bytes).and_then(|(id, ranges)| {
202203
self.get_route(*id).map(|(value, pieces)| {
@@ -206,8 +207,8 @@ impl<'a, T> PathTree<'a, T> {
206207
pieces,
207208
// opt!
208209
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())
211212
.rev()
212213
.collect(),
213214
}
@@ -216,7 +217,7 @@ impl<'a, T> PathTree<'a, T> {
216217
}
217218

218219
#[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>)> {
220221
self.routes.get(index)
221222
}
222223

@@ -246,7 +247,7 @@ impl<'a, T> PathTree<'a, T> {
246247
pub struct Path<'a, 'b, T> {
247248
pub id: &'a usize,
248249
pub value: &'a T,
249-
pub pieces: &'a [Piece<'a>],
250+
pub pieces: &'a [Piece],
250251
pub raws: SmallVec<[&'b str; 4]>,
251252
}
252253

0 commit comments

Comments
 (0)