Skip to content

Remove lifetime #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// │ ├── rust •3
// │ └── ** •1
// └── POST/login •4
let mut tree = PathTree::<'static, Box<dyn Handler>>::new();
let mut tree = PathTree::<Box<dyn Handler>>::new();
tree.insert("/GET/", Box::new(index));
tree.insert("/GET/*", Box::new(hello_world));
tree.insert("/GET/hello/:name", Box::new(hello_user));
Expand Down
29 changes: 15 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,38 +135,39 @@
#![forbid(unsafe_code)]
#![warn(rust_2018_idioms, unreachable_pub)]

use smallvec::SmallVec;
use std::str::from_utf8;

use smallvec::SmallVec;

mod node;
mod parser;

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

#[derive(Debug)]
pub struct PathTree<'a, T> {
pub struct PathTree<T> {
id: usize,
routes: Vec<(T, Vec<Piece<'a>>)>,
pub node: Node<'a, usize>,
routes: Vec<(T, Vec<Piece>)>,
pub node: Node<usize>,
}

impl<'a, T> Default for PathTree<'a, T> {
impl<T> Default for PathTree<T> {
fn default() -> Self {
Self::new()
}
}

impl<'a, T> PathTree<'a, T> {
impl<T> PathTree<T> {
pub fn new() -> Self {
Self {
id: 0,
routes: Vec::new(),
node: Node::new(NodeKind::String("".as_bytes()), None),
node: Node::new(NodeKind::String("".as_bytes().to_vec()), None),
}
}

pub fn insert(&mut self, path: &'a str, value: T) -> usize {
pub fn insert(&mut self, path: &str, value: T) -> usize {
if path.is_empty() {
return self.id;
}
Expand All @@ -177,7 +178,7 @@ impl<'a, T> PathTree<'a, T> {
for piece in &pieces {
match piece {
Piece::String(s) => {
node = node.insert_bytes(s);
node = node.insert_bytes(&s[..]);
}
Piece::Parameter(_, k) => {
node = node.insert_parameter(*k);
Expand All @@ -196,7 +197,7 @@ impl<'a, T> PathTree<'a, T> {
}
}

pub fn find<'b>(&'a self, path: &'b str) -> Option<Path<'a, 'b, T>> {
pub fn find<'b>(&self, path: &'b str) -> Option<Path<'_, 'b, T>> {
let bytes = path.as_bytes();
self.node.find(bytes).and_then(|(id, ranges)| {
self.get_route(*id).map(|(value, pieces)| {
Expand All @@ -206,8 +207,8 @@ impl<'a, T> PathTree<'a, T> {
pieces,
// opt!
raws: ranges
.chunks(2)
.map(|r| from_utf8(&bytes[r[0]..r[1]]).unwrap())
.into_iter()
.map(|r| from_utf8(&bytes[r]).unwrap())
.rev()
.collect(),
}
Expand All @@ -216,7 +217,7 @@ impl<'a, T> PathTree<'a, T> {
}

#[inline]
pub fn get_route(&self, index: usize) -> Option<&(T, Vec<Piece<'a>>)> {
pub fn get_route(&self, index: usize) -> Option<&(T, Vec<Piece>)> {
self.routes.get(index)
}

Expand Down Expand Up @@ -246,7 +247,7 @@ impl<'a, T> PathTree<'a, T> {
pub struct Path<'a, 'b, T> {
pub id: &'a usize,
pub value: &'a T,
pub pieces: &'a [Piece<'a>],
pub pieces: &'a [Piece],
pub raws: SmallVec<[&'b str; 4]>,
}

Expand Down
Loading