wayfind
A speedy, flexible router for Rust.
Why another router
Real-world projects often need advanced routing: inline parameters, mid-route wildcards, or compatibility with frameworks such as Ruby on Rails and specifications such as the OCI Distribution Specification.
wayfind aims to be competitive with the fastest routers while supporting these features. Unused features don't impact performance.
Showcase
[]
= "*"
use Error;
use RouterBuilder;
let mut builder = new;
builder.insert?;
builder.insert?;
builder.insert?;
builder.insert?;
builder.insert?;
builder.insert?;
builder.insert?;
let router = builder.build;
let search = router.search.unwrap;
assert_eq!;
assert_eq!;
let search = router.search.unwrap;
assert_eq!;
assert_eq!;
let search = router.search.unwrap;
assert_eq!;
assert_eq!;
let search = router.search.unwrap;
assert_eq!;
assert_eq!;
let search = router.search.unwrap;
assert_eq!;
assert_eq!;
let search = router.search.unwrap;
assert_eq!;
assert_eq!;
let search = router.search.unwrap;
assert_eq!;
assert_eq!;
println!;
Ok::
/v2
╰─ /
╰─ <*name>
╰─ /
├─ blobs/
│ ├─ uploads
│ │ ╰─ /
│ │ ╰─ <reference>
│ ╰─ <algorithm>
│ ╰─ :
│ ╰─ <hash>
├─ manifests/
│ ╰─ <reference>
├─ referrers/
│ ╰─ <algorithm>
│ ╰─ :
│ ╰─ <hash>
╰─ tags/list
Implementation Details
wayfind stores routes in a compressed radix trie.
When searching, each node tries its children in priority order:
- static
- dynamic
- wildcard
All parameters are greedy, consuming as much of the path as possible.
Limitations
No backtracking happens across priority levels. Some matches can surprise you.
In the following router:
/api/
├─ <version>
│ ╰─ /
│ ╰─ <*rest>
╰─ <*path>
╰─ /help
The path /api/docs/help would match the first route, not the second.
Even though the second is arguably more specific.
Performance
wayfind is competitive with the fastest Rust routers across all benchmarks we run.
For all benchmarks, we convert any extracted parameters to strings.
All routers offer a way to return parameters as strings, but some delay the actual UTF-8 decoding until post-search.
| Library | Percent Decoding | String Parameters |
|---|---|---|
| wayfind | no | yes |
| actix-router | partial | yes |
| matchit | no | delayed |
| ntex-router | partial | yes |
| path-tree | no | delayed |
| route-recognizer | no | yes |
| xitca-router | no | yes |
As such, we offer 2 sets of results per benchmark:
- one with the default behaviour of the router.
- one with the parameters extracted to
Vec<(&str, &str)>.
See the results at: https://app.codspeed.io/SwornSystems/wayfind/benchmarks
License
Licensed under the terms of both the MIT License and the Apache License (Version 2.0).