├── .github └── workflows │ └── rust.yml ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── benches └── like_bench.rs ├── src └── lib.rs └── tests ├── test_basic.rs └── test_like.rs /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: Rust 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | env: 10 | CARGO_TERM_COLOR: always 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | - name: Build 20 | run: cargo build --verbose 21 | - name: Run tests 22 | run: cargo test --verbose 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "1.1.3" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "anes" 16 | version = "0.1.6" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" 19 | 20 | [[package]] 21 | name = "anstyle" 22 | version = "1.0.6" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" 25 | 26 | [[package]] 27 | name = "auto_enums" 28 | version = "0.8.5" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | checksum = "1899bfcfd9340ceea3533ea157360ba8fa864354eccbceab58e1006ecab35393" 31 | dependencies = [ 32 | "derive_utils", 33 | "proc-macro2", 34 | "quote", 35 | "syn", 36 | ] 37 | 38 | [[package]] 39 | name = "autocfg" 40 | version = "1.2.0" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" 43 | 44 | [[package]] 45 | name = "bumpalo" 46 | version = "3.15.4" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa" 49 | 50 | [[package]] 51 | name = "cast" 52 | version = "0.3.0" 53 | source = "registry+https://github.com/rust-lang/crates.io-index" 54 | checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" 55 | 56 | [[package]] 57 | name = "cfg-if" 58 | version = "1.0.0" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 61 | 62 | [[package]] 63 | name = "ciborium" 64 | version = "0.2.2" 65 | source = "registry+https://github.com/rust-lang/crates.io-index" 66 | checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" 67 | dependencies = [ 68 | "ciborium-io", 69 | "ciborium-ll", 70 | "serde", 71 | ] 72 | 73 | [[package]] 74 | name = "ciborium-io" 75 | version = "0.2.2" 76 | source = "registry+https://github.com/rust-lang/crates.io-index" 77 | checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" 78 | 79 | [[package]] 80 | name = "ciborium-ll" 81 | version = "0.2.2" 82 | source = "registry+https://github.com/rust-lang/crates.io-index" 83 | checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" 84 | dependencies = [ 85 | "ciborium-io", 86 | "half", 87 | ] 88 | 89 | [[package]] 90 | name = "clap" 91 | version = "4.5.4" 92 | source = "registry+https://github.com/rust-lang/crates.io-index" 93 | checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" 94 | dependencies = [ 95 | "clap_builder", 96 | ] 97 | 98 | [[package]] 99 | name = "clap_builder" 100 | version = "4.5.2" 101 | source = "registry+https://github.com/rust-lang/crates.io-index" 102 | checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" 103 | dependencies = [ 104 | "anstyle", 105 | "clap_lex", 106 | ] 107 | 108 | [[package]] 109 | name = "clap_lex" 110 | version = "0.7.0" 111 | source = "registry+https://github.com/rust-lang/crates.io-index" 112 | checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" 113 | 114 | [[package]] 115 | name = "const-currying" 116 | version = "0.0.5" 117 | dependencies = [ 118 | "auto_enums", 119 | "criterion", 120 | "darling", 121 | "itertools 0.12.1", 122 | "proc-macro2", 123 | "quote", 124 | "syn", 125 | ] 126 | 127 | [[package]] 128 | name = "criterion" 129 | version = "0.5.1" 130 | source = "registry+https://github.com/rust-lang/crates.io-index" 131 | checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" 132 | dependencies = [ 133 | "anes", 134 | "cast", 135 | "ciborium", 136 | "clap", 137 | "criterion-plot", 138 | "is-terminal", 139 | "itertools 0.10.5", 140 | "num-traits", 141 | "once_cell", 142 | "oorandom", 143 | "plotters", 144 | "rayon", 145 | "regex", 146 | "serde", 147 | "serde_derive", 148 | "serde_json", 149 | "tinytemplate", 150 | "walkdir", 151 | ] 152 | 153 | [[package]] 154 | name = "criterion-plot" 155 | version = "0.5.0" 156 | source = "registry+https://github.com/rust-lang/crates.io-index" 157 | checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" 158 | dependencies = [ 159 | "cast", 160 | "itertools 0.10.5", 161 | ] 162 | 163 | [[package]] 164 | name = "crossbeam-deque" 165 | version = "0.8.5" 166 | source = "registry+https://github.com/rust-lang/crates.io-index" 167 | checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" 168 | dependencies = [ 169 | "crossbeam-epoch", 170 | "crossbeam-utils", 171 | ] 172 | 173 | [[package]] 174 | name = "crossbeam-epoch" 175 | version = "0.9.18" 176 | source = "registry+https://github.com/rust-lang/crates.io-index" 177 | checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" 178 | dependencies = [ 179 | "crossbeam-utils", 180 | ] 181 | 182 | [[package]] 183 | name = "crossbeam-utils" 184 | version = "0.8.19" 185 | source = "registry+https://github.com/rust-lang/crates.io-index" 186 | checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" 187 | 188 | [[package]] 189 | name = "crunchy" 190 | version = "0.2.2" 191 | source = "registry+https://github.com/rust-lang/crates.io-index" 192 | checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" 193 | 194 | [[package]] 195 | name = "darling" 196 | version = "0.20.8" 197 | source = "registry+https://github.com/rust-lang/crates.io-index" 198 | checksum = "54e36fcd13ed84ffdfda6f5be89b31287cbb80c439841fe69e04841435464391" 199 | dependencies = [ 200 | "darling_core", 201 | "darling_macro", 202 | ] 203 | 204 | [[package]] 205 | name = "darling_core" 206 | version = "0.20.8" 207 | source = "registry+https://github.com/rust-lang/crates.io-index" 208 | checksum = "9c2cf1c23a687a1feeb728783b993c4e1ad83d99f351801977dd809b48d0a70f" 209 | dependencies = [ 210 | "fnv", 211 | "ident_case", 212 | "proc-macro2", 213 | "quote", 214 | "strsim", 215 | "syn", 216 | ] 217 | 218 | [[package]] 219 | name = "darling_macro" 220 | version = "0.20.8" 221 | source = "registry+https://github.com/rust-lang/crates.io-index" 222 | checksum = "a668eda54683121533a393014d8692171709ff57a7d61f187b6e782719f8933f" 223 | dependencies = [ 224 | "darling_core", 225 | "quote", 226 | "syn", 227 | ] 228 | 229 | [[package]] 230 | name = "derive_utils" 231 | version = "0.14.1" 232 | source = "registry+https://github.com/rust-lang/crates.io-index" 233 | checksum = "61bb5a1014ce6dfc2a378578509abe775a5aa06bff584a547555d9efdb81b926" 234 | dependencies = [ 235 | "proc-macro2", 236 | "quote", 237 | "syn", 238 | ] 239 | 240 | [[package]] 241 | name = "either" 242 | version = "1.10.0" 243 | source = "registry+https://github.com/rust-lang/crates.io-index" 244 | checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" 245 | 246 | [[package]] 247 | name = "fnv" 248 | version = "1.0.7" 249 | source = "registry+https://github.com/rust-lang/crates.io-index" 250 | checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" 251 | 252 | [[package]] 253 | name = "half" 254 | version = "2.4.0" 255 | source = "registry+https://github.com/rust-lang/crates.io-index" 256 | checksum = "b5eceaaeec696539ddaf7b333340f1af35a5aa87ae3e4f3ead0532f72affab2e" 257 | dependencies = [ 258 | "cfg-if", 259 | "crunchy", 260 | ] 261 | 262 | [[package]] 263 | name = "hermit-abi" 264 | version = "0.3.9" 265 | source = "registry+https://github.com/rust-lang/crates.io-index" 266 | checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" 267 | 268 | [[package]] 269 | name = "ident_case" 270 | version = "1.0.1" 271 | source = "registry+https://github.com/rust-lang/crates.io-index" 272 | checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" 273 | 274 | [[package]] 275 | name = "is-terminal" 276 | version = "0.4.12" 277 | source = "registry+https://github.com/rust-lang/crates.io-index" 278 | checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" 279 | dependencies = [ 280 | "hermit-abi", 281 | "libc", 282 | "windows-sys", 283 | ] 284 | 285 | [[package]] 286 | name = "itertools" 287 | version = "0.10.5" 288 | source = "registry+https://github.com/rust-lang/crates.io-index" 289 | checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" 290 | dependencies = [ 291 | "either", 292 | ] 293 | 294 | [[package]] 295 | name = "itertools" 296 | version = "0.12.1" 297 | source = "registry+https://github.com/rust-lang/crates.io-index" 298 | checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" 299 | dependencies = [ 300 | "either", 301 | ] 302 | 303 | [[package]] 304 | name = "itoa" 305 | version = "1.0.11" 306 | source = "registry+https://github.com/rust-lang/crates.io-index" 307 | checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" 308 | 309 | [[package]] 310 | name = "js-sys" 311 | version = "0.3.69" 312 | source = "registry+https://github.com/rust-lang/crates.io-index" 313 | checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" 314 | dependencies = [ 315 | "wasm-bindgen", 316 | ] 317 | 318 | [[package]] 319 | name = "libc" 320 | version = "0.2.153" 321 | source = "registry+https://github.com/rust-lang/crates.io-index" 322 | checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" 323 | 324 | [[package]] 325 | name = "log" 326 | version = "0.4.21" 327 | source = "registry+https://github.com/rust-lang/crates.io-index" 328 | checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" 329 | 330 | [[package]] 331 | name = "memchr" 332 | version = "2.7.2" 333 | source = "registry+https://github.com/rust-lang/crates.io-index" 334 | checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" 335 | 336 | [[package]] 337 | name = "num-traits" 338 | version = "0.2.18" 339 | source = "registry+https://github.com/rust-lang/crates.io-index" 340 | checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" 341 | dependencies = [ 342 | "autocfg", 343 | ] 344 | 345 | [[package]] 346 | name = "once_cell" 347 | version = "1.19.0" 348 | source = "registry+https://github.com/rust-lang/crates.io-index" 349 | checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" 350 | 351 | [[package]] 352 | name = "oorandom" 353 | version = "11.1.3" 354 | source = "registry+https://github.com/rust-lang/crates.io-index" 355 | checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" 356 | 357 | [[package]] 358 | name = "plotters" 359 | version = "0.3.5" 360 | source = "registry+https://github.com/rust-lang/crates.io-index" 361 | checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45" 362 | dependencies = [ 363 | "num-traits", 364 | "plotters-backend", 365 | "plotters-svg", 366 | "wasm-bindgen", 367 | "web-sys", 368 | ] 369 | 370 | [[package]] 371 | name = "plotters-backend" 372 | version = "0.3.5" 373 | source = "registry+https://github.com/rust-lang/crates.io-index" 374 | checksum = "9e76628b4d3a7581389a35d5b6e2139607ad7c75b17aed325f210aa91f4a9609" 375 | 376 | [[package]] 377 | name = "plotters-svg" 378 | version = "0.3.5" 379 | source = "registry+https://github.com/rust-lang/crates.io-index" 380 | checksum = "38f6d39893cca0701371e3c27294f09797214b86f1fb951b89ade8ec04e2abab" 381 | dependencies = [ 382 | "plotters-backend", 383 | ] 384 | 385 | [[package]] 386 | name = "proc-macro2" 387 | version = "1.0.79" 388 | source = "registry+https://github.com/rust-lang/crates.io-index" 389 | checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" 390 | dependencies = [ 391 | "unicode-ident", 392 | ] 393 | 394 | [[package]] 395 | name = "quote" 396 | version = "1.0.35" 397 | source = "registry+https://github.com/rust-lang/crates.io-index" 398 | checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" 399 | dependencies = [ 400 | "proc-macro2", 401 | ] 402 | 403 | [[package]] 404 | name = "rayon" 405 | version = "1.10.0" 406 | source = "registry+https://github.com/rust-lang/crates.io-index" 407 | checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" 408 | dependencies = [ 409 | "either", 410 | "rayon-core", 411 | ] 412 | 413 | [[package]] 414 | name = "rayon-core" 415 | version = "1.12.1" 416 | source = "registry+https://github.com/rust-lang/crates.io-index" 417 | checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" 418 | dependencies = [ 419 | "crossbeam-deque", 420 | "crossbeam-utils", 421 | ] 422 | 423 | [[package]] 424 | name = "regex" 425 | version = "1.10.4" 426 | source = "registry+https://github.com/rust-lang/crates.io-index" 427 | checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" 428 | dependencies = [ 429 | "aho-corasick", 430 | "memchr", 431 | "regex-automata", 432 | "regex-syntax", 433 | ] 434 | 435 | [[package]] 436 | name = "regex-automata" 437 | version = "0.4.6" 438 | source = "registry+https://github.com/rust-lang/crates.io-index" 439 | checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" 440 | dependencies = [ 441 | "aho-corasick", 442 | "memchr", 443 | "regex-syntax", 444 | ] 445 | 446 | [[package]] 447 | name = "regex-syntax" 448 | version = "0.8.3" 449 | source = "registry+https://github.com/rust-lang/crates.io-index" 450 | checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" 451 | 452 | [[package]] 453 | name = "ryu" 454 | version = "1.0.17" 455 | source = "registry+https://github.com/rust-lang/crates.io-index" 456 | checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" 457 | 458 | [[package]] 459 | name = "same-file" 460 | version = "1.0.6" 461 | source = "registry+https://github.com/rust-lang/crates.io-index" 462 | checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" 463 | dependencies = [ 464 | "winapi-util", 465 | ] 466 | 467 | [[package]] 468 | name = "serde" 469 | version = "1.0.197" 470 | source = "registry+https://github.com/rust-lang/crates.io-index" 471 | checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" 472 | dependencies = [ 473 | "serde_derive", 474 | ] 475 | 476 | [[package]] 477 | name = "serde_derive" 478 | version = "1.0.197" 479 | source = "registry+https://github.com/rust-lang/crates.io-index" 480 | checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" 481 | dependencies = [ 482 | "proc-macro2", 483 | "quote", 484 | "syn", 485 | ] 486 | 487 | [[package]] 488 | name = "serde_json" 489 | version = "1.0.115" 490 | source = "registry+https://github.com/rust-lang/crates.io-index" 491 | checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd" 492 | dependencies = [ 493 | "itoa", 494 | "ryu", 495 | "serde", 496 | ] 497 | 498 | [[package]] 499 | name = "strsim" 500 | version = "0.10.0" 501 | source = "registry+https://github.com/rust-lang/crates.io-index" 502 | checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" 503 | 504 | [[package]] 505 | name = "syn" 506 | version = "2.0.58" 507 | source = "registry+https://github.com/rust-lang/crates.io-index" 508 | checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" 509 | dependencies = [ 510 | "proc-macro2", 511 | "quote", 512 | "unicode-ident", 513 | ] 514 | 515 | [[package]] 516 | name = "tinytemplate" 517 | version = "1.2.1" 518 | source = "registry+https://github.com/rust-lang/crates.io-index" 519 | checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" 520 | dependencies = [ 521 | "serde", 522 | "serde_json", 523 | ] 524 | 525 | [[package]] 526 | name = "unicode-ident" 527 | version = "1.0.12" 528 | source = "registry+https://github.com/rust-lang/crates.io-index" 529 | checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" 530 | 531 | [[package]] 532 | name = "walkdir" 533 | version = "2.5.0" 534 | source = "registry+https://github.com/rust-lang/crates.io-index" 535 | checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" 536 | dependencies = [ 537 | "same-file", 538 | "winapi-util", 539 | ] 540 | 541 | [[package]] 542 | name = "wasm-bindgen" 543 | version = "0.2.92" 544 | source = "registry+https://github.com/rust-lang/crates.io-index" 545 | checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" 546 | dependencies = [ 547 | "cfg-if", 548 | "wasm-bindgen-macro", 549 | ] 550 | 551 | [[package]] 552 | name = "wasm-bindgen-backend" 553 | version = "0.2.92" 554 | source = "registry+https://github.com/rust-lang/crates.io-index" 555 | checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" 556 | dependencies = [ 557 | "bumpalo", 558 | "log", 559 | "once_cell", 560 | "proc-macro2", 561 | "quote", 562 | "syn", 563 | "wasm-bindgen-shared", 564 | ] 565 | 566 | [[package]] 567 | name = "wasm-bindgen-macro" 568 | version = "0.2.92" 569 | source = "registry+https://github.com/rust-lang/crates.io-index" 570 | checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" 571 | dependencies = [ 572 | "quote", 573 | "wasm-bindgen-macro-support", 574 | ] 575 | 576 | [[package]] 577 | name = "wasm-bindgen-macro-support" 578 | version = "0.2.92" 579 | source = "registry+https://github.com/rust-lang/crates.io-index" 580 | checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" 581 | dependencies = [ 582 | "proc-macro2", 583 | "quote", 584 | "syn", 585 | "wasm-bindgen-backend", 586 | "wasm-bindgen-shared", 587 | ] 588 | 589 | [[package]] 590 | name = "wasm-bindgen-shared" 591 | version = "0.2.92" 592 | source = "registry+https://github.com/rust-lang/crates.io-index" 593 | checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" 594 | 595 | [[package]] 596 | name = "web-sys" 597 | version = "0.3.69" 598 | source = "registry+https://github.com/rust-lang/crates.io-index" 599 | checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" 600 | dependencies = [ 601 | "js-sys", 602 | "wasm-bindgen", 603 | ] 604 | 605 | [[package]] 606 | name = "winapi" 607 | version = "0.3.9" 608 | source = "registry+https://github.com/rust-lang/crates.io-index" 609 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 610 | dependencies = [ 611 | "winapi-i686-pc-windows-gnu", 612 | "winapi-x86_64-pc-windows-gnu", 613 | ] 614 | 615 | [[package]] 616 | name = "winapi-i686-pc-windows-gnu" 617 | version = "0.4.0" 618 | source = "registry+https://github.com/rust-lang/crates.io-index" 619 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 620 | 621 | [[package]] 622 | name = "winapi-util" 623 | version = "0.1.6" 624 | source = "registry+https://github.com/rust-lang/crates.io-index" 625 | checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" 626 | dependencies = [ 627 | "winapi", 628 | ] 629 | 630 | [[package]] 631 | name = "winapi-x86_64-pc-windows-gnu" 632 | version = "0.4.0" 633 | source = "registry+https://github.com/rust-lang/crates.io-index" 634 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 635 | 636 | [[package]] 637 | name = "windows-sys" 638 | version = "0.52.0" 639 | source = "registry+https://github.com/rust-lang/crates.io-index" 640 | checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" 641 | dependencies = [ 642 | "windows-targets", 643 | ] 644 | 645 | [[package]] 646 | name = "windows-targets" 647 | version = "0.52.4" 648 | source = "registry+https://github.com/rust-lang/crates.io-index" 649 | checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" 650 | dependencies = [ 651 | "windows_aarch64_gnullvm", 652 | "windows_aarch64_msvc", 653 | "windows_i686_gnu", 654 | "windows_i686_msvc", 655 | "windows_x86_64_gnu", 656 | "windows_x86_64_gnullvm", 657 | "windows_x86_64_msvc", 658 | ] 659 | 660 | [[package]] 661 | name = "windows_aarch64_gnullvm" 662 | version = "0.52.4" 663 | source = "registry+https://github.com/rust-lang/crates.io-index" 664 | checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" 665 | 666 | [[package]] 667 | name = "windows_aarch64_msvc" 668 | version = "0.52.4" 669 | source = "registry+https://github.com/rust-lang/crates.io-index" 670 | checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" 671 | 672 | [[package]] 673 | name = "windows_i686_gnu" 674 | version = "0.52.4" 675 | source = "registry+https://github.com/rust-lang/crates.io-index" 676 | checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" 677 | 678 | [[package]] 679 | name = "windows_i686_msvc" 680 | version = "0.52.4" 681 | source = "registry+https://github.com/rust-lang/crates.io-index" 682 | checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" 683 | 684 | [[package]] 685 | name = "windows_x86_64_gnu" 686 | version = "0.52.4" 687 | source = "registry+https://github.com/rust-lang/crates.io-index" 688 | checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" 689 | 690 | [[package]] 691 | name = "windows_x86_64_gnullvm" 692 | version = "0.52.4" 693 | source = "registry+https://github.com/rust-lang/crates.io-index" 694 | checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" 695 | 696 | [[package]] 697 | name = "windows_x86_64_msvc" 698 | version = "0.52.4" 699 | source = "registry+https://github.com/rust-lang/crates.io-index" 700 | checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" 701 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "const-currying" 3 | description = "A procedural macro for speeding up your function using currying" 4 | version = "0.0.5" 5 | edition = "2021" 6 | repository = "https://github.com/TennyZhuang/const-currying-rs" 7 | license = "Apache-2.0 OR MIT" 8 | 9 | [lib] 10 | proc-macro = true 11 | 12 | [dependencies] 13 | auto_enums = "0.8.5" 14 | darling = "0.20.8" 15 | itertools = "0.12.1" 16 | proc-macro2 = "1.0.79" 17 | quote = "1.0.35" 18 | syn = { version = "2.0.52", features = ["full", "extra-traits"] } 19 | 20 | [dev-dependencies] 21 | criterion = { version = "0.5.1", features = ["html_reports"] } 22 | 23 | [[bench]] 24 | name = "like_bench" 25 | harness = false 26 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any 2 | person obtaining a copy of this software and associated 3 | documentation files (the "Software"), to deal in the 4 | Software without restriction, including without 5 | limitation the rights to use, copy, modify, merge, 6 | publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software 8 | is furnished to do so, subject to the following 9 | conditions: 10 | 11 | The above copyright notice and this permission notice 12 | shall be included in all copies or substantial portions 13 | of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 16 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 17 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 18 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 19 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 22 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Version]][crates.io] 2 | [![Documentation]][docs.rs] 3 | 4 | [Latest Version]: https://img.shields.io/crates/v/const-currying.svg 5 | [crates.io]: https://crates.io/crates/const-currying 6 | [Documentation]: https://img.shields.io/docsrs/const-currying 7 | [docs.rs]: https://docs.rs/const-currying 8 | 9 | # Const-currying 10 | 11 | A rust proc macro that helps you improve your function's performance using [curring techniques](https://en.wikipedia.org/wiki/Currying). 12 | 13 | ## Motivation 14 | 15 | Const generics is a feature of rust that allows you to pass const values as generic parameters. This feature is very useful to improve the performance of your code by generating multiple versions of the same function at compile time. However, sometimes you have to call the function with a runtime-dependent value, even if it's likely a known const value, or a part of the calls are with const values. This macro helps you to generate multiple versions of the same function, with either const or runtime values, at compile time. 16 | 17 | There is an exciting crate [partial_const](https://crates.io/crates/partial_const), which already provides a similar feature. It's fully based on the wonderful type system of rust; however, it requires caller to specify the const value explicitly. Our crate provides a proc-macro based solution, which introduces no invasive changes to the caller's code. 18 | 19 | ## Usage 20 | 21 | You can specify the potential constant values of the function's arguments using `maybe_const` attribute. 22 | 23 | ```rust 24 | use const_currying::const_currying; 25 | 26 | #[const_currying] 27 | fn f1( 28 | #[maybe_const(dispatch = x, consts = [0, 1])] x: i32, 29 | #[maybe_const(dispatch = y, consts = [true, false])] y: bool, 30 | z: &str, 31 | ) -> i32 { 32 | if y { 33 | x 34 | } else { 35 | -x 36 | } 37 | } 38 | ``` 39 | 40 | There are two arguments `x` and `y` which are potentially passed as const values. The optional `dispatch` attribute specifies the suffix of the generated function name. As an example, we can see the full generated codes here. 41 | 42 | ```rust 43 | #[allow(warnings)] 44 | fn f1_orig(x: i32, y: bool, z: &str) -> (i32, String) { 45 | if y { (x, z.to_string()) } else { (-x, z.chars().rev().collect()) } 46 | } 47 | #[allow(warnings)] 48 | fn f1_x(y: bool, z: &str) -> (i32, String) { 49 | if y { (x, z.to_string()) } else { (-x, z.chars().rev().collect()) } 50 | } 51 | #[allow(warnings)] 52 | fn f1_y(x: i32, z: &str) -> (i32, String) { 53 | if y { (x, z.to_string()) } else { (-x, z.chars().rev().collect()) } 54 | } 55 | #[allow(warnings)] 56 | fn f1_x_y(z: &str) -> (i32, String) { 57 | if y { (x, z.to_string()) } else { (-x, z.chars().rev().collect()) } 58 | } 59 | #[inline(always)] 60 | fn f1(x: i32, y: bool, z: &str) -> (i32, String) { 61 | match (x, y) { 62 | (1, false) => f1_x_y::<1, false>(z), 63 | (1, true) => f1_x_y::<1, true>(z), 64 | (0, false) => f1_x_y::<0, false>(z), 65 | (0, true) => f1_x_y::<0, true>(z), 66 | (x, false) => f1_y::(x, z), 67 | (x, true) => f1_y::(x, z), 68 | (1, y) => f1_x::<1>(y, z), 69 | (0, y) => f1_x::<0>(y, z), 70 | (x, y) => f1_orig(x, y, z), 71 | } 72 | } 73 | ``` 74 | 75 | The original function `f1` is renamed to `f1_orig`, and a powerset of two const arguments `x` and `y` are generated as different functions `f1_x`, `f1_y` and `f1_x_y`. Finally, the original function `f1` is replaced by a dispatcher function, which calls the generated functions according to the runtime values of `x` and `y`. 76 | 77 | ## Benefits 78 | 79 | In most cases, compiler optimization is trustworthy enough to generate the best codes for you. However, when your function is too complicated to inline, the compiler may not be able to get enough information to optimize the function. 80 | 81 | The macro generates multiple versions of the function, and matches the consts argument explicitly in the dispatcher function. This enforces the compiler to generate the best codes for each const value. This is also why const-generics is introduced to rust, and the macro make it easier to use with a runtime-dependent value. 82 | 83 | "No silver bullet" is a well-known principle in software engineering. This macro is not a silver bullet, and it's not always the best choice to use it. At least, it may heavily increase you binary size. You should always profile your code before and after using the macro, and make sure the performance is improved. 84 | 85 | ## License 86 | 87 | Licensed under either of [Apache License, Version 88 | 2.0](LICENSE-APACHE) or [MIT license](LICENSE-MIT) at your option. 89 | -------------------------------------------------------------------------------- /benches/like_bench.rs: -------------------------------------------------------------------------------- 1 | use criterion::{black_box, criterion_group, criterion_main, Criterion}; 2 | 3 | use const_currying::const_currying; 4 | 5 | #[const_currying] 6 | fn like( 7 | s: &str, 8 | p: &str, 9 | #[maybe_const(consts = [b'\\'])] escape: u8, 10 | #[maybe_const(consts = [true, false])] case_sensitive: bool, 11 | ) -> bool { 12 | let (mut px, mut sx) = (0, 0); 13 | let (mut next_px, mut next_sx) = (0, 0); 14 | let (pbytes, sbytes) = (p.as_bytes(), s.as_bytes()); 15 | while px < pbytes.len() || sx < sbytes.len() { 16 | if px < pbytes.len() { 17 | let c = pbytes[px]; 18 | match c { 19 | b'_' => { 20 | if sx < sbytes.len() { 21 | px += 1; 22 | sx += 1; 23 | continue; 24 | } 25 | } 26 | b'%' => { 27 | next_px = px; 28 | next_sx = sx + 1; 29 | px += 1; 30 | continue; 31 | } 32 | mut pc => { 33 | if ((!case_sensitive && pc == escape) 34 | || (case_sensitive && pc.eq_ignore_ascii_case(&escape))) 35 | && px + 1 < pbytes.len() 36 | { 37 | px += 1; 38 | pc = pbytes[px]; 39 | } 40 | if sx < sbytes.len() 41 | && ((!case_sensitive && sbytes[sx] == pc) 42 | || (case_sensitive && sbytes[sx].eq_ignore_ascii_case(&pc))) 43 | { 44 | px += 1; 45 | sx += 1; 46 | continue; 47 | } 48 | } 49 | } 50 | } 51 | if 0 < next_sx && next_sx <= sbytes.len() { 52 | px = next_px; 53 | sx = next_sx; 54 | continue; 55 | } 56 | return false; 57 | } 58 | true 59 | } 60 | 61 | fn criterion_benchmark(c: &mut Criterion) { 62 | c.bench_function("like base", |b| { 63 | b.iter(|| { 64 | like_orig( 65 | black_box("ab\\bbccddeefff"), 66 | black_box("ab\\%%%ccd*f"), 67 | black_box(b'\\'), 68 | black_box(true), 69 | ) 70 | }) 71 | }); 72 | 73 | c.bench_function("like optimized", |b| { 74 | b.iter(|| { 75 | like( 76 | black_box("ab\\bbccddeefff"), 77 | black_box("ab\\%%%ccd*f"), 78 | black_box(b'\\'), 79 | black_box(true), 80 | ) 81 | }) 82 | }); 83 | } 84 | 85 | criterion_group!(benches, criterion_benchmark); 86 | criterion_main!(benches); 87 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![doc = include_str!("../README.md")] 2 | 3 | use std::collections::HashSet; 4 | 5 | use auto_enums::auto_enum; 6 | use darling::{FromAttributes, FromMeta}; 7 | use itertools::Itertools; 8 | use proc_macro2::TokenStream; 9 | use quote::{format_ident, quote}; 10 | use syn::{ 11 | parse_macro_input, parse_quote, punctuated::Punctuated, Attribute, Block, ConstParam, Expr, 12 | FnArg, GenericParam, Generics, Ident, ItemFn, Pat, PatIdent, PatType, Result, Signature, Token, 13 | Type, 14 | }; 15 | 16 | #[proc_macro_attribute] 17 | pub fn const_currying( 18 | attr: proc_macro::TokenStream, 19 | item: proc_macro::TokenStream, 20 | ) -> proc_macro::TokenStream { 21 | let input = parse_macro_input!(item as ItemFn); 22 | match inner(attr.into(), input) { 23 | Ok(output) => output.into(), 24 | Err(err) => err.to_compile_error().into(), 25 | } 26 | } 27 | 28 | #[derive(Debug, Clone, darling::FromAttributes)] 29 | #[darling(attributes(maybe_const))] 30 | struct FieldAttr { 31 | #[darling(default)] 32 | dispatch: Option, 33 | #[darling(default)] 34 | consts: ConstsArray, 35 | } 36 | 37 | #[derive(Debug, Clone, Default)] 38 | struct ConstsArray { 39 | inner: Punctuated, 40 | } 41 | 42 | impl FromMeta for ConstsArray { 43 | fn from_expr(expr: &Expr) -> darling::Result { 44 | if let Expr::Array(array) = expr { 45 | Ok(Self { 46 | inner: array.elems.clone(), 47 | }) 48 | } else { 49 | Err(darling::Error::unexpected_expr_type(expr)) 50 | } 51 | } 52 | } 53 | 54 | #[derive(Clone, Debug)] 55 | struct GenTarget { 56 | attr: FieldAttr, 57 | idx: usize, 58 | arg_name: Ident, 59 | input: PatType, 60 | ty: Type, 61 | } 62 | 63 | fn remove_attr(arg: FnArg) -> FnArg { 64 | match arg { 65 | FnArg::Typed(mut typed) => { 66 | typed.attrs.clear(); 67 | FnArg::Typed(typed) 68 | } 69 | FnArg::Receiver(receiver) => FnArg::Receiver(receiver), 70 | } 71 | } 72 | 73 | fn contains_attr(attrs: &[Attribute]) -> bool { 74 | attrs.iter().any(|attr| attr.path().is_ident("maybe_const")) 75 | } 76 | 77 | #[auto_enum] 78 | fn inner(_attr: TokenStream, item: ItemFn) -> Result { 79 | let item2 = item.clone(); 80 | let ItemFn { sig, .. } = item; 81 | 82 | let Signature { 83 | ident, 84 | inputs, 85 | generics, 86 | .. 87 | } = &sig; 88 | 89 | let targets = inputs 90 | .iter() 91 | .enumerate() 92 | .filter_map(|(idx, input)| match input { 93 | FnArg::Receiver(..) => None, 94 | FnArg::Typed(typed) => { 95 | let PatType { attrs, ty, pat, .. } = typed; 96 | let Pat::Ident(PatIdent { 97 | ident: arg_name, .. 98 | }) = &**pat 99 | else { 100 | return None; 101 | }; 102 | if !contains_attr(attrs) { 103 | return None; 104 | } 105 | let attr = FieldAttr::from_attributes(attrs).ok()?; 106 | Some(GenTarget { 107 | attr, 108 | idx, 109 | arg_name: arg_name.clone(), 110 | input: typed.clone(), 111 | ty: *ty.clone(), 112 | }) 113 | } 114 | }) 115 | .collect::>(); 116 | 117 | let old_fn_name = format_ident!("{ident}_orig"); 118 | 119 | let orig_const_args: Vec<_> = generics 120 | .const_params() 121 | .map(|param| param.ident.clone()) 122 | .collect(); 123 | 124 | let fns = targets 125 | .iter() 126 | .cloned() 127 | .powerset() 128 | .zip(std::iter::from_fn(|| { 129 | let item = item2.clone(); 130 | Some(item) 131 | })) 132 | .map(|(set, item)| { 133 | let ItemFn { sig, .. } = item.clone(); 134 | let Signature { 135 | ident, 136 | inputs, 137 | generics, 138 | .. 139 | } = &sig; 140 | let new_fn_name = [ident.to_string()] 141 | .into_iter() 142 | .chain(set.iter().map(|t| { 143 | t.attr 144 | .dispatch 145 | .as_ref() 146 | .map(ToString::to_string) 147 | .unwrap_or(t.arg_name.to_string()) 148 | })) 149 | .join("_"); 150 | let new_fn_ident = if set.is_empty() { 151 | old_fn_name.clone() 152 | } else { 153 | Ident::new(&new_fn_name, ident.span()) 154 | }; 155 | 156 | let added_generic_params = set 157 | .iter() 158 | .map(|t: &GenTarget| { 159 | let GenTarget { 160 | attr: _, 161 | idx: _, 162 | arg_name, 163 | input, 164 | ty, 165 | } = t; 166 | ConstParam { 167 | attrs: vec![], 168 | const_token: Token![const](arg_name.span()), 169 | ident: arg_name.clone(), 170 | colon_token: input.colon_token, 171 | ty: ty.clone(), 172 | default: None, 173 | eq_token: None, 174 | } 175 | }) 176 | .map(GenericParam::Const); 177 | 178 | let mut old_generics_pararms = generics.params.clone(); 179 | for new_param in added_generic_params { 180 | old_generics_pararms.push(new_param); 181 | } 182 | let new_generics = Generics { 183 | params: old_generics_pararms, 184 | ..generics.clone() 185 | }; 186 | let new_inputs = { 187 | let args_to_remove: HashSet<_> = set.iter().map(|t| t.idx).collect(); 188 | inputs 189 | .iter() 190 | .cloned() 191 | .enumerate() 192 | .filter(|(idx, _)| !args_to_remove.contains(idx)) 193 | .map(|(_idx, input)| input) 194 | .map(remove_attr) 195 | .collect::>() 196 | }; 197 | let sig = sig.clone(); 198 | let new_sig = Signature { 199 | ident: new_fn_ident, 200 | inputs: new_inputs, 201 | generics: new_generics, 202 | ..sig 203 | }; 204 | let item = item.clone(); 205 | let mut new_attrs = item.attrs.clone(); 206 | let new_attr: Attribute = parse_quote!(#[allow(warnings)]); 207 | new_attrs.push(new_attr); 208 | ItemFn { 209 | sig: new_sig, 210 | attrs: new_attrs, 211 | ..item 212 | } 213 | }) 214 | .collect::>(); 215 | 216 | // Generate the dispatch function 217 | let all_target_names = targets 218 | .iter() 219 | .map(|target| target.arg_name.clone()) 220 | .collect::>(); 221 | 222 | let mut branches = targets 223 | .iter() 224 | .cloned() 225 | .enumerate() 226 | .powerset() 227 | .flat_map(|set| { 228 | let new_fn_name = [ident.to_string()] 229 | .into_iter() 230 | .chain(set.iter().map(|(_, t)| { 231 | t.attr 232 | .dispatch 233 | .as_ref() 234 | .map(ToString::to_string) 235 | .unwrap_or(t.arg_name.to_string()) 236 | })) 237 | .join("_"); 238 | let new_fn_ident = if set.is_empty() { 239 | old_fn_name.clone() 240 | } else { 241 | Ident::new(&new_fn_name, ident.span()) 242 | }; 243 | 244 | let remain_args = { 245 | let args_to_remove: HashSet<_> = set.iter().map(|(_, t)| t.idx).collect(); 246 | inputs 247 | .iter() 248 | .cloned() 249 | .enumerate() 250 | .filter(|(idx, _)| !args_to_remove.contains(idx)) 251 | .map(|(_idx, input)| input) 252 | .map(|input| match input { 253 | FnArg::Receiver(_reciver) => quote! { self }, 254 | FnArg::Typed(typed) => match *typed.pat { 255 | Pat::Ident(pat_ident) => { 256 | let name = pat_ident.ident; 257 | quote! { #name } 258 | } 259 | _ => panic!("Only support simple pattern"), 260 | }, 261 | }) 262 | .collect::>() 263 | }; 264 | 265 | #[auto_enum(Iterator)] 266 | let const_sets = if set.is_empty() { 267 | std::iter::once(vec![]) 268 | } else { 269 | Itertools::multi_cartesian_product(set.iter().map(|(idx, target)| { 270 | itertools::izip!(std::iter::repeat(idx), target.attr.consts.inner.iter(),) 271 | })) 272 | }; 273 | 274 | const_sets 275 | .map(|const_set| { 276 | let mut match_args = all_target_names 277 | .iter() 278 | .map(|target_name| quote! { #target_name }) 279 | .collect::>(); 280 | let mut added_const_args = Vec::with_capacity(const_set.len()); 281 | for (idx_in_target, r#const) in const_set { 282 | match_args[*idx_in_target] = quote! { #r#const }; 283 | added_const_args.push(quote! { #r#const }); 284 | } 285 | let const_args = orig_const_args 286 | .iter() 287 | .map(|ident| quote! { #ident }) 288 | .chain(added_const_args.into_iter()); 289 | if remain_args.is_empty() { 290 | quote! { 291 | (#(#match_args),*) => { 292 | #new_fn_ident::<#(#const_args),*>() 293 | } 294 | } 295 | } else { 296 | quote! { 297 | (#(#match_args),*) => { 298 | #new_fn_ident::<#(#const_args),*>(#(#remain_args),*,) 299 | } 300 | } 301 | } 302 | }) 303 | .collect::>() 304 | }) 305 | .collect::>(); 306 | branches.reverse(); 307 | 308 | let dispatch_fn = { 309 | let body: Block = parse_quote! { 310 | { 311 | match (#(#all_target_names),*) { 312 | #(#branches),* 313 | } 314 | } 315 | }; 316 | let new_inputs = sig 317 | .inputs 318 | .iter() 319 | .cloned() 320 | .map(remove_attr) 321 | .collect::>(); 322 | let new_sig = Signature { 323 | inputs: new_inputs, 324 | ..sig 325 | }; 326 | let mut new_attrs = item2.attrs.clone(); 327 | new_attrs.push(parse_quote! { #[inline(always)] }); 328 | ItemFn { 329 | sig: new_sig, 330 | block: Box::new(body), 331 | attrs: new_attrs, 332 | ..item2 333 | } 334 | }; 335 | 336 | Ok(quote! { 337 | #dispatch_fn 338 | #(#fns)* 339 | }) 340 | } 341 | -------------------------------------------------------------------------------- /tests/test_basic.rs: -------------------------------------------------------------------------------- 1 | use const_currying::const_currying; 2 | 3 | #[const_currying] 4 | fn f1( 5 | #[maybe_const(dispatch = x, consts = [0, 1])] x: i32, 6 | #[maybe_const(dispatch = y, consts = [true, false])] y: bool, 7 | z: &str, 8 | ) -> (i32, String) { 9 | if y { 10 | (x, z.to_string()) 11 | } else { 12 | (-x, z.chars().rev().collect()) 13 | } 14 | } 15 | 16 | fn main() { 17 | f1_orig(3, true, "3"); 18 | f1(3, true, "3"); 19 | f1_x::<3>(false, "3"); 20 | f1_y::(3, "3"); 21 | f1_x_y::<3, true>("3"); 22 | } 23 | -------------------------------------------------------------------------------- /tests/test_like.rs: -------------------------------------------------------------------------------- 1 | use const_currying::const_currying; 2 | 3 | #[const_currying] 4 | fn like_impl( 5 | s: &str, 6 | p: &str, 7 | #[maybe_const(consts = [b'\\'])] escape: u8, 8 | ) -> bool { 9 | let (mut px, mut sx) = (0, 0); 10 | let (mut next_px, mut next_sx) = (0, 0); 11 | let (pbytes, sbytes) = (p.as_bytes(), s.as_bytes()); 12 | while px < pbytes.len() || sx < sbytes.len() { 13 | if px < pbytes.len() { 14 | let c = pbytes[px]; 15 | match c { 16 | b'_' => { 17 | if sx < sbytes.len() { 18 | px += 1; 19 | sx += 1; 20 | continue; 21 | } 22 | } 23 | b'%' => { 24 | next_px = px; 25 | next_sx = sx + 1; 26 | px += 1; 27 | continue; 28 | } 29 | mut pc => { 30 | if ((!CASE_INSENSITIVE && pc == escape) 31 | || (CASE_INSENSITIVE && pc.eq_ignore_ascii_case(&escape))) 32 | && px + 1 < pbytes.len() 33 | { 34 | px += 1; 35 | pc = pbytes[px]; 36 | } 37 | if sx < sbytes.len() 38 | && ((!CASE_INSENSITIVE && sbytes[sx] == pc) 39 | || (CASE_INSENSITIVE && sbytes[sx].eq_ignore_ascii_case(&pc))) 40 | { 41 | px += 1; 42 | sx += 1; 43 | continue; 44 | } 45 | } 46 | } 47 | } 48 | if 0 < next_sx && next_sx <= sbytes.len() { 49 | px = next_px; 50 | sx = next_sx; 51 | continue; 52 | } 53 | return false; 54 | } 55 | true 56 | } 57 | 58 | pub fn like_default(s: &str, p: &str) -> bool { 59 | like_impl_escape::(s, p) 60 | } 61 | 62 | pub fn i_like_default(s: &str, p: &str) -> bool { 63 | like_impl_escape::(s, p) 64 | } 65 | --------------------------------------------------------------------------------