Bug: 342499478

Clone this repo:
  1. 79b4cef Migrate 9 crates to monorepo. by James Farrell · 3 months ago main
  2. 37ee048 Adding min_sdk_version for apex nested dependencies by Nouby Mohamed · 3 months ago
  3. 7859b5b Fix licenses for matchit. by James Farrell · 3 months ago
  4. 7f5384a Re-run cargo_embargo to fix license. by Andrew Walbran · 3 months ago
  5. 03d6218 Merge remote-tracking branch 'origin/upstream' am: eac25a1fec am: c6a64b95a3 by Inna Palant · 7 months ago android15-automotiveos-dev android15-platform-release android15-prebuilt-test android15-qpr1-release android15-qpr1-s3-release android15-qpr1-s4-release android15-qpr1-s5-release android15-release android15-s1-release android15-security-release android15-tests-dev android15-tests-release aml_adb_351010000 aml_ads_351017080 aml_ads_351121120 aml_art_350913340 aml_art_351011240 aml_art_351011340 aml_art_351110180 aml_ase_351010000 aml_ase_351112060 aml_ase_351114000 aml_cbr_350910020 aml_cbr_351011020 aml_cbr_351111000 aml_cfg_351010000 aml_con_351010000 aml_con_351110000 aml_doc_350915120 aml_doc_351012120 aml_doc_351113060 aml_ext_350912020 aml_ext_351122080 aml_hef_350921160 aml_hef_351016140 aml_hef_351120040 aml_ips_351010000 aml_ips_351111040 aml_med_350914000 aml_med_351010060 aml_mpr_350914160 aml_mpr_351013100 aml_mpr_351013160 aml_mpr_351113060 aml_mpr_351113100 aml_net_350911020 aml_net_351010000 aml_net_351010020 aml_net_351111100 aml_net_351111140 aml_odp_351020000 aml_odp_351121040 aml_per_350910080 aml_per_351014000 aml_per_351112280 aml_per_351112300 aml_res_351011000 aml_res_351111020 aml_rkp_350910000 aml_rkp_351011000 aml_sch_351010000 aml_sdk_350910000 aml_sdk_351110000 aml_sta_350911020 aml_sta_351110040 aml_tet_350911120 aml_tet_351010220 aml_tet_351110060 aml_tz6_351010000 aml_uwb_350911040 aml_uwb_351011040 aml_wif_350912040 aml_wif_351010040 aml_wif_351110060 android-15.0.0_r1 android-15.0.0_r10 android-15.0.0_r11 android-15.0.0_r12 android-15.0.0_r13 android-15.0.0_r2 android-15.0.0_r3 android-15.0.0_r4 android-15.0.0_r5 android-15.0.0_r6 android-15.0.0_r7 android-15.0.0_r8 android-15.0.0_r9 android-cts-15.0_r1 android-cts-15.0_r2 android-platform-15.0.0_r1 android-platform-15.0.0_r2 android-platform-15.0.0_r3 android-platform-15.0.0_r4 android-security-15.0.0_r1 android-security-15.0.0_r2 android-security-15.0.0_r3 android-security-15.0.0_r4 android-vts-15.0_r1 android-vts-15.0_r2 frc_350820260 frc_350820420 frc_350820440 frc_350820660 frc_350820860 frc_350820960 frc_350822020

matchit

Documentation Version License

A high performance, zero-copy URL router.

use matchit::Router;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut router = Router::new();
    router.insert("/home", "Welcome!")?;
    router.insert("/users/:id", "A User")?;

    let matched = router.at("/users/978")?;
    assert_eq!(matched.params.get("id"), Some("978"));
    assert_eq!(*matched.value, "A User");

    Ok(())
}

Parameters

Along with static routes, the router also supports dynamic route segments. These can either be named or catch-all parameters:

Named Parameters

Named parameters like /:id match anything until the next / or the end of the path:

let mut m = Router::new();
m.insert("/users/:id", true)?;

assert_eq!(m.at("/users/1")?.params.get("id"), Some("1"));
assert_eq!(m.at("/users/23")?.params.get("id"), Some("23"));
assert!(m.at("/users").is_err());

Catch-all Parameters

Catch-all parameters start with * and match everything after the /. They must always be at the end of the route:

let mut m = Router::new();
m.insert("/*p", true)?;

assert_eq!(m.at("/foo.js")?.params.get("p"), Some("foo.js"));
assert_eq!(m.at("/c/bar.css")?.params.get("p"), Some("c/bar.css"));

// note that this would not match:
assert_eq!(m.at("/").is_err());

Routing Priority

Static and dynamic route segments are allowed to overlap. If they do, static segments will be given higher priority:

let mut m = Router::new();
m.insert("/", "Welcome!").unwrap();      // priority: 1
m.insert("/about", "About Me").unwrap(); // priority: 1
m.insert("/*filepath", "...").unwrap();  // priority: 2

How does it work?

The router takes advantage of the fact that URL routes generally follow a hierarchical structure. Routes are stored them in a radix trie that makes heavy use of common prefixes:

Priority   Path             Value
9          \                1
3          s               None
2          |├earch\         2
1          |└upport\        3
2          blog\           4
1          |    └:post      None
1          |         \     5
2          about-us\       6
1          |        team\  7
1          contact\        8

This allows us to reduce the route search to a small number of branches. Child nodes on the same level of the tree are also prioritized by the number of children with registered values, increasing the chance of choosing the correct branch of the first try.

Benchmarks

As it turns out, this method of routing is extremely fast. In a benchmark matching 4 paths against 130 registered routes, matchit find the correct routes in under 200 nanoseconds, an order of magnitude faster than most other routers. You can view the benchmark code here.

Compare Routers/matchit 
time:   [197.57 ns 198.74 ns 199.83 ns]

Compare Routers/actix
time:   [26.805 us 26.811 us 26.816 us]

Compare Routers/path-tree
time:   [468.95 ns 470.34 ns 471.65 ns]

Compare Routers/regex
time:   [22.539 us 22.584 us 22.639 us]

Compare Routers/route-recognizer
time:   [3.7552 us 3.7732 us 3.8027 us]

Compare Routers/routefinder
time:   [5.7313 us 5.7405 us 5.7514 us]

Credits

A lot of the code in this package was based on Julien Schmidt's httprouter.