| Crates.io | radix_route_matcher |
| lib.rs | radix_route_matcher |
| version | 0.1.0 |
| created_at | 2025-12-05 01:55:07.550974+00 |
| updated_at | 2025-12-05 01:55:07.550974+00 |
| description | A high-performance route matching library based on Radix Tree |
| homepage | |
| repository | https://github.com/ChadCao/radix_route_matcher |
| max_upload_size | |
| id | 1967442 |
| size | 123,638 |
A high-performance route matching library based on Radix Tree (compressed trie) data structure.
rax implementation in CAdd this to your Cargo.toml:
[dependencies]
radix_route_matcher = "0.1"
use radix_route_matcher::RadixTree;
let mut tree = RadixTree::new().expect("Failed to create tree");
// Insert routes
tree.insert("/api", 1).unwrap();
tree.insert("/api/users", 2).unwrap();
tree.insert("/api/posts", 3).unwrap();
// Create an iterator for prefix matching operations
let iter = tree.create_iter().unwrap();
// Exact match
assert_eq!(tree.find_exact("/api/users"), Some(2));
// Longest prefix match
assert_eq!(tree.longest_prefix(&iter, "/api/users/123"), Some(2));
// Get all matching prefixes
let matches = tree.find_all_prefixes(&iter, "/api/users/123/profile");
assert_eq!(matches, vec![2, 1]); // ["/api/users", "/api"]
use radix_route_matcher::RadixTree;
let mut tree = RadixTree::new().unwrap();
tree.insert("/", 1).unwrap();
tree.insert("/api", 2).unwrap();
tree.insert("/api/v1", 3).unwrap();
let iter = tree.create_iter().unwrap();
let path = "/api/v1/users";
if tree.search(&iter, path) {
while let Some(idx) = tree.next_prefix(&iter, path) {
println!("Matched route: {}", idx);
}
}
| Method | Description |
|---|---|
new() |
Creates a new empty Radix Tree |
insert(path, idx) |
Inserts a path with an associated index |
find_exact(path) |
Finds the exact match for a path |
remove(path) |
Removes a path from the tree |
create_iter() |
Creates a new iterator for prefix operations |
longest_prefix(iter, path) |
Finds the longest prefix match |
search(iter, path) |
Initializes iterator for prefix searching |
next_prefix(iter, path) |
Gets the next prefix match |
find_all_prefixes(iter, path) |
Returns all matching prefixes |
This library also exports a C-compatible API for use from other languages:
void* radix_tree_new();
int radix_tree_destroy(void* t);
int radix_tree_insert(void* t, const unsigned char* buf, unsigned long len, int idx);
void* radix_tree_find(void* t, const unsigned char* buf, unsigned long len);
int radix_tree_remove(void* t, const unsigned char* buf, unsigned long len);
void* radix_tree_new_it(void* t);
void* radix_tree_search(void* tree, void* it, const unsigned char* buf, unsigned long len);
int radix_tree_up(void* it, const unsigned char* buf, unsigned long len);
This project is licensed under the MIT License - see the LICENSE file for details.
This project includes code from the Redis rax implementation by Salvatore Sanfilippo, which is also under a BSD-style license.