unsigned-varint-0.8.0/.cargo_vcs_info.json0000644000000001360000000000100141560ustar { "git": { "sha1": "10d55e5dbdd7f61b6495bd12a49d06df82e08612" }, "path_in_vcs": "" }unsigned-varint-0.8.0/.editorconfig000064400000000000000000000001441046102023000154220ustar 00000000000000root = true [*] charset=utf-8 end_of_line=lf indent_size=4 indent_style=space max_line_length=100 unsigned-varint-0.8.0/.github/dependabot.yml000064400000000000000000000003111046102023000171310ustar 00000000000000version: 2 updates: - package-ecosystem: "cargo" directory: "/" schedule: interval: "daily" - package-ecosystem: github-actions directory: '/' schedule: interval: daily unsigned-varint-0.8.0/.github/workflows/rust.yml000064400000000000000000000005061046102023000200640ustar 00000000000000name: Rust on: push: branches: [ master ] pull_request: branches: [ master ] env: CARGO_TERM_COLOR: always jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Build run: cargo build --all-features - name: Run tests run: cargo test --all-features unsigned-varint-0.8.0/.gitignore000064400000000000000000000000501046102023000147310ustar 00000000000000/target /Cargo.lock *.swp *.swo tags ID unsigned-varint-0.8.0/CHANGELOG.md000064400000000000000000000037071046102023000145660ustar 00000000000000# 0.8.0 - [2023-11-01] - Update `asynchronous-codec` to `0.7` (#71) # 0.7.2 [2023-09-XX] - Update `tokio-util` to `0.7` (#59) # 0.7.1 [2021-02-08] - Allow conversions from crate errors to std::io equivalents (#52). - Reject non-minimally encoded varints (additional non-significant zero bytes) (#56). # 0.7.0 [2021-02-04] - Update `asynchronous-codec` to `v0.6`. # 0.6.0 [2021-01-11] - Switch `futures_codec` to `asynchronous-codec`. - Upgrade dependencies. # 0.5.1 [2020-09-08] - Improve docs (#36). # 0.5.0 [2020-07-31] - Add support for `no_std` (#32, #33). A new feature flag `std` has been added. It is not enabled by default but implicitly for `codec`, `futures` and `futures-codec` features. It must be explicitly provided in the absence of those features if the `io` module should be enabled. # 0.4.0 [2020-05-18] - Update `tokio-util` and `futures_codec` (#31). # 0.3.3 [2020-04-14] - Optional support for nom has been added (#27). # 0.3.2 [2020-03-04] - Replace the optional `futures` dependency with a `futures` feature that only includes `futures-io` and `futures-util` as dependencies (#26). # 0.3.1 [2020-02-17] - Add modules `io` and `aio` to support direct reading of an unsigned-varint value from a `std::io::Read` or `futures::io::AsyncRead` type. # 0.3.0 [2020-01-02] - Update to `bytes` v0.5. - Add support for `tokio-util` v0.2. - Remove support for `tokio-codec` v0.1. - Use `#[non_exhaustive]` in `decode::Error` and remove `__Nonexhaustive`. # 0.2.3 [2019-10-07] - In addition to `tokio-codec`, `futures_codec` is now supported (#18). - `decode::Error` now implements `Clone` (#19). - Code quality improvements (#20, #21). # 0.2.2 [2019-01-31] - Add package metadata for docs.rs to generate documentation for all features. # 0.2.1 [2018-09-05] - Ensure `codec::Uvi` is `Send` when `T` is. # 0.2.0 [2018-09-03] - Change default value for `UviBytes::max` from `usize::MAX` to 128 MiB. # 0.1.0 [2018-08-08] Initial release unsigned-varint-0.8.0/Cargo.lock0000644000000531750000000000100121440ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "addr2line" version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" dependencies = [ "gimli", ] [[package]] name = "adler" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" [[package]] name = "aho-corasick" version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" dependencies = [ "memchr", ] [[package]] name = "asynchronous-codec" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a860072022177f903e59730004fb5dc13db9275b79bb2aef7ba8ce831956c233" dependencies = [ "bytes", "futures-sink", "futures-util", "memchr", "pin-project-lite", ] [[package]] name = "atty" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ "hermit-abi", "libc", "winapi", ] [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "backtrace" version = "0.3.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837" dependencies = [ "addr2line", "cc", "cfg-if", "libc", "miniz_oxide", "object", "rustc-demangle", ] [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bumpalo" version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" [[package]] name = "bytes" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" [[package]] name = "cast" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" dependencies = [ "libc", ] [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" version = "2.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" dependencies = [ "bitflags", "textwrap", "unicode-width", ] [[package]] name = "criterion" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b01d6de93b2b6c65e17c634a26653a29d107b3c98c607c765bf38d041531cd8f" dependencies = [ "atty", "cast", "clap", "criterion-plot", "csv", "itertools", "lazy_static", "num-traits", "oorandom", "plotters", "rayon", "regex", "serde", "serde_cbor", "serde_derive", "serde_json", "tinytemplate", "walkdir", ] [[package]] name = "criterion-plot" version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2673cc8207403546f45f5fd319a974b1e6983ad1a3ee7e6041650013be041876" dependencies = [ "cast", "itertools", ] [[package]] name = "crossbeam-deque" version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" dependencies = [ "cfg-if", "crossbeam-epoch", "crossbeam-utils", ] [[package]] name = "crossbeam-epoch" version = "0.9.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" dependencies = [ "autocfg", "cfg-if", "crossbeam-utils", "memoffset", "scopeguard", ] [[package]] name = "crossbeam-utils" version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" dependencies = [ "cfg-if", ] [[package]] name = "csv" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac574ff4d437a7b5ad237ef331c17ccca63c46479e5b5453eb8e10bb99a759fe" dependencies = [ "csv-core", "itoa", "ryu", "serde", ] [[package]] name = "csv-core" version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5efa2b3d7902f4b634a20cae3c9c4e6209dc4779feb6863329607560143efa70" dependencies = [ "memchr", ] [[package]] name = "either" version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" [[package]] name = "env_logger" version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" dependencies = [ "log", "regex", ] [[package]] name = "futures-core" version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" [[package]] name = "futures-executor" version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" dependencies = [ "futures-core", "futures-task", "futures-util", ] [[package]] name = "futures-io" version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" [[package]] name = "futures-macro" version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "futures-sink" version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e36d3378ee38c2a36ad710c5d30c2911d752cb941c00c72dbabfb786a7970817" [[package]] name = "futures-task" version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2" [[package]] name = "futures-util" version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" dependencies = [ "futures-core", "futures-io", "futures-macro", "futures-task", "memchr", "pin-project-lite", "pin-utils", "slab", ] [[package]] name = "getrandom" version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ "cfg-if", "libc", "wasi", ] [[package]] name = "gimli" version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" [[package]] name = "half" version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" [[package]] name = "hermit-abi" version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" dependencies = [ "libc", ] [[package]] name = "hex" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" [[package]] name = "itertools" version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" dependencies = [ "either", ] [[package]] name = "itoa" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "js-sys" version = "0.3.65" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "54c0c35952f67de54bb584e9fd912b3023117cbafc0a77d8f3dee1fb5f572fe8" dependencies = [ "wasm-bindgen", ] [[package]] name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" version = "0.2.149" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" [[package]] name = "log" version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] name = "memchr" version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" [[package]] name = "memoffset" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" dependencies = [ "autocfg", ] [[package]] name = "minimal-lexical" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "miniz_oxide" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" dependencies = [ "adler", ] [[package]] name = "nom" version = "7.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" dependencies = [ "memchr", "minimal-lexical", ] [[package]] name = "num-traits" version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" dependencies = [ "autocfg", ] [[package]] name = "object" version = "0.32.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0" dependencies = [ "memchr", ] [[package]] name = "once_cell" version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] name = "oorandom" version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" [[package]] name = "pin-project-lite" version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" [[package]] name = "pin-utils" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "plotters" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45" dependencies = [ "num-traits", "plotters-backend", "plotters-svg", "wasm-bindgen", "web-sys", ] [[package]] name = "plotters-backend" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e76628b4d3a7581389a35d5b6e2139607ad7c75b17aed325f210aa91f4a9609" [[package]] name = "plotters-svg" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38f6d39893cca0701371e3c27294f09797214b86f1fb951b89ade8ec04e2abab" dependencies = [ "plotters-backend", ] [[package]] name = "proc-macro2" version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" dependencies = [ "unicode-ident", ] [[package]] name = "quickcheck" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "588f6378e4dd99458b60ec275b4477add41ce4fa9f64dcba6f15adccb19b50d6" dependencies = [ "env_logger", "log", "rand", ] [[package]] name = "quote" version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] [[package]] name = "rand" version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "rand_core", ] [[package]] name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ "getrandom", ] [[package]] name = "rayon" version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1" dependencies = [ "either", "rayon-core", ] [[package]] name = "rayon-core" version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed" dependencies = [ "crossbeam-deque", "crossbeam-utils", ] [[package]] name = "regex" version = "1.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" dependencies = [ "aho-corasick", "memchr", "regex-automata", "regex-syntax", ] [[package]] name = "regex-automata" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" dependencies = [ "aho-corasick", "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "rustc-demangle" version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "ryu" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" [[package]] name = "same-file" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" dependencies = [ "winapi-util", ] [[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" version = "1.0.190" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "91d3c334ca1ee894a2c6f6ad698fe8c435b76d504b13d436f0685d648d6d96f7" dependencies = [ "serde_derive", ] [[package]] name = "serde_cbor" version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5" dependencies = [ "half", "serde", ] [[package]] name = "serde_derive" version = "1.0.190" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67c5609f394e5c2bd7fc51efda478004ea80ef42fee983d5c67a65e34f32c0e3" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "serde_json" version = "1.0.108" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" dependencies = [ "itoa", "ryu", "serde", ] [[package]] name = "slab" version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" dependencies = [ "autocfg", ] [[package]] name = "syn" version = "2.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "textwrap" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" dependencies = [ "unicode-width", ] [[package]] name = "tinytemplate" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" dependencies = [ "serde", "serde_json", ] [[package]] name = "tokio" version = "1.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4f38200e3ef7995e5ef13baec2f432a6da0aa9ac495b2c0e8f3b7eec2c92d653" dependencies = [ "backtrace", "pin-project-lite", ] [[package]] name = "tokio-util" version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15" dependencies = [ "bytes", "futures-core", "futures-sink", "pin-project-lite", "tokio", "tracing", ] [[package]] name = "tracing" version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ "pin-project-lite", "tracing-core", ] [[package]] name = "tracing-core" version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", ] [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "unicode-width" version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" [[package]] name = "unsigned-varint" version = "0.8.0" dependencies = [ "asynchronous-codec", "bytes", "criterion", "futures-executor", "futures-io", "futures-util", "hex", "nom", "quickcheck", "tokio-util", ] [[package]] name = "walkdir" version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" dependencies = [ "same-file", "winapi-util", ] [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7daec296f25a1bae309c0cd5c29c4b260e510e6d813c286b19eaadf409d40fce" dependencies = [ "cfg-if", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e397f4664c0e4e428e8313a469aaa58310d302159845980fd23b0f22a847f217" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", "syn", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-macro" version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5961017b3b08ad5f3fe39f1e79877f8ee7c23c5e5fd5eb80de95abc41f1f16b2" dependencies = [ "quote", "wasm-bindgen-macro-support", ] [[package]] name = "wasm-bindgen-macro-support" version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c5353b8dab669f5e10f5bd76df26a9360c748f054f862ff5f3f8aae0c7fb3907" dependencies = [ "proc-macro2", "quote", "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d046c5d029ba91a1ed14da14dca44b68bf2f124cfbaf741c54151fdb3e0750b" [[package]] name = "web-sys" version = "0.3.65" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5db499c5f66323272151db0e666cd34f78617522fb0c1604d31a27c50c206a85" dependencies = [ "js-sys", "wasm-bindgen", ] [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" dependencies = [ "winapi", ] [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" unsigned-varint-0.8.0/Cargo.toml0000644000000031140000000000100121530ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "unsigned-varint" version = "0.8.0" authors = ["Parity Technologies "] description = "unsigned varint encoding" readme = "README.md" license = "MIT" repository = "https://github.com/paritytech/unsigned-varint" [package.metadata.docs.rs] all-features = true [[bench]] name = "benchmark" harness = false [dependencies.asynchronous-codec] version = "0.7" optional = true [dependencies.bytes] version = "1" optional = true [dependencies.futures-io] version = "0.3.4" optional = true [dependencies.futures-util] version = "0.3.4" features = ["io"] optional = true [dependencies.nom] version = "7" optional = true [dependencies.tokio-util] version = "0.7" features = ["codec"] optional = true [dev-dependencies.criterion] version = "0.3" [dev-dependencies.futures-executor] version = "0.3.4" [dev-dependencies.hex] version = "0.4" [dev-dependencies.quickcheck] version = "1" [features] asynchronous_codec = [ "std", "bytes", "asynchronous-codec", ] codec = [ "std", "bytes", "tokio-util", ] futures = [ "std", "futures-io", "futures-util", ] std = [] unsigned-varint-0.8.0/Cargo.toml.orig000064400000000000000000000017141046102023000156400ustar 00000000000000[package] name = "unsigned-varint" version = "0.8.0" authors = ["Parity Technologies "] license = "MIT" description = "unsigned varint encoding" repository = "https://github.com/paritytech/unsigned-varint" readme = "README.md" edition = "2018" [package.metadata.docs.rs] all-features = true [features] std = [] codec = ["std", "bytes", "tokio-util"] futures = ["std", "futures-io", "futures-util"] asynchronous_codec = ["std", "bytes", "asynchronous-codec"] [dependencies] bytes = { version = "1", optional = true } futures-io = { version = "0.3.4", optional = true } futures-util = { version = "0.3.4", features = ["io"], optional = true } asynchronous-codec = { version = "0.7", optional = true } tokio-util = { version = "0.7", features = ["codec"], optional = true } nom = { version = "7", optional = true } [dev-dependencies] criterion = "0.3" futures-executor = "0.3.4" hex = "0.4" quickcheck = "1" [[bench]] name = "benchmark" harness = false unsigned-varint-0.8.0/LICENSE000064400000000000000000000020551046102023000137550ustar 00000000000000Copyright 2018 Parity Technologies (UK) Ltd. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. unsigned-varint-0.8.0/README.md000064400000000000000000000004341046102023000142260ustar 00000000000000unsigned-varint encoding ======================== Unsigned varint encodes unsigned integers in 7-bit groups. The most significant bit (MSB) in each byte indicates if another byte follows (MSB = 1), or not (MSB = 0). For details see: https://github.com/multiformats/unsigned-varint unsigned-varint-0.8.0/benches/benchmark.rs000064400000000000000000000047121046102023000166610ustar 00000000000000// Copyright 2018 Parity Technologies (UK) Ltd. // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS // OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. use criterion::{criterion_group, criterion_main, Criterion}; use std::u64; use unsigned_varint::{decode, encode}; fn bench_decode(c: &mut Criterion) { let mut buf = [0; 10]; let len = encode::u64(u64::MAX, &mut buf).len(); c.bench_function("decode", move |b| b.iter(|| { assert_eq!(u64::MAX, decode::u64(&buf[.. len]).unwrap().0) })); } fn bench_encode(c: &mut Criterion) { let mut buf = [0; 10]; let encoded = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 1]; c.bench_function("encode", move |b| b.iter(|| { assert_eq!(&encoded, encode::u64(u64::MAX, &mut buf)); })); } #[cfg(feature = "codec")] fn bench_codec(c: &mut Criterion) { use bytes::{Bytes, BytesMut}; use tokio_util::codec::{Decoder, Encoder}; use unsigned_varint::codec::UviBytes; let data = Bytes::from(vec![1; 8192]); let mut bytes = BytesMut::with_capacity(9000); let mut uvi_bytes = UviBytes::default(); c.bench_function("codec", move |b| b.iter(|| { uvi_bytes.encode(data.clone(), &mut bytes).unwrap(); assert_eq!(data, uvi_bytes.decode(&mut bytes.split_to(bytes.len())).unwrap().unwrap()) })); } #[cfg(feature = "codec")] criterion_group!(benches, bench_encode, bench_decode, bench_codec); #[cfg(not(feature = "codec"))] criterion_group!(benches, bench_encode, bench_decode); criterion_main!(benches); unsigned-varint-0.8.0/examples/uvi.rs000064400000000000000000000026641046102023000157450ustar 00000000000000extern crate hex; extern crate unsigned_varint; use std::{env, process}; use unsigned_varint::{decode, encode}; pub fn main() { let mut args = env::args().skip(1); let mode = if let Some(s) = args.next() { s } else { println!("usage: -d | -e "); process::exit(1) }; match (mode.as_ref(), args.next()) { ("-d", Some(xs)) => { let v = if let Ok(b) = hex::decode(&xs) { b } else { println!("failed to decode hex string"); process::exit(1) }; match decode::u128(&v) { Ok((n, _)) => println!("{}", n), Err(e) => { println!("{}", e); process::exit(2) } } } ("-e", Some(xs)) => { match xs.parse() { Ok(n) => { let mut buf = encode::u128_buffer(); let bytes = encode::u128(n, &mut buf); println!("{}", hex::encode(&bytes)) } Err(e) => { println!("{}", e); process::exit(3) } } } _ => { println!("usage: -d | -e "); process::exit(1) } } } unsigned-varint-0.8.0/src/aio.rs000064400000000000000000000045531046102023000146620ustar 00000000000000// Copyright 2020 Parity Technologies (UK) Ltd. // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS // OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. //! Decode using [`futures_io::AsyncRead`] types. use crate::{decode, encode, io::ReadError}; use futures_io::AsyncRead; use futures_util::io::AsyncReadExt; use std::io; macro_rules! gen { ($($name:ident, $d:expr, $t:ident, $b:ident);*) => { $( #[doc = " Try to read and decode a "] #[doc = $d] #[doc = " from the given `AsyncRead` type."] pub async fn $name(mut reader: R) -> Result<$t, ReadError> { let mut b = encode::$b(); for i in 0 .. b.len() { let n = reader.read(&mut b[i .. i + 1]).await?; if n == 0 { return Err(ReadError::Io(io::ErrorKind::UnexpectedEof.into())) } if decode::is_last(b[i]) { return Ok(decode::$t(&b[..= i])?.0) } } Err(decode::Error::Overflow)? } )* } } gen! { read_u8, "`u8`", u8, u8_buffer; read_u16, "`u16`", u16, u16_buffer; read_u32, "`u32`", u32, u32_buffer; read_u64, "`u64`", u64, u64_buffer; read_u128, "`u128`", u128, u128_buffer; read_usize, "`usize`", usize, usize_buffer } unsigned-varint-0.8.0/src/codec.rs000064400000000000000000000152371046102023000151700ustar 00000000000000// Copyright 2018-2019 Parity Technologies (UK) Ltd. // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS // OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. //! `Encoder`/`Decoder` implementations for tokio or asynchronous_codec. use bytes::{Buf, BufMut, Bytes, BytesMut}; use crate::{encode, decode::{self, Error}}; use std::{io, marker::PhantomData, usize}; /// Encoder/Decoder of unsigned-varint values #[derive(Default)] pub struct Uvi(PhantomData); macro_rules! encoder_decoder_impls { ($typ:ident, $arr:ident) => { impl Uvi<$typ> { fn serialise(&mut self, item: $typ, dst: &mut BytesMut) { let mut buf = encode::$arr(); dst.extend_from_slice(encode::$typ(item, &mut buf)) } fn deserialise(&mut self, src: &mut BytesMut) -> Result, io::Error> { let (number, consumed) = match decode::$typ(src.as_ref()) { Ok((n, rem)) => (n, src.len() - rem.len()), Err(Error::Insufficient) => return Ok(None), Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)) }; src.advance(consumed); Ok(Some(number)) } } #[cfg(feature = "codec")] impl tokio_util::codec::Encoder<$typ> for Uvi<$typ> { type Error = io::Error; fn encode(&mut self, item: $typ, dst: &mut BytesMut) -> Result<(), Self::Error> { self.serialise(item, dst); Ok(()) } } #[cfg(feature = "codec")] impl tokio_util::codec::Decoder for Uvi<$typ> { type Item = $typ; type Error = io::Error; fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { self.deserialise(src) } } #[cfg(feature = "asynchronous_codec")] impl asynchronous_codec::Encoder for Uvi<$typ> { type Item<'a> = $typ; type Error = io::Error; fn encode(&mut self, item: Self::Item<'_>, dst: &mut BytesMut) -> Result<(), Self::Error> { self.serialise(item, dst); Ok(()) } } #[cfg(feature = "asynchronous_codec")] impl asynchronous_codec::Decoder for Uvi<$typ> { type Item = $typ; type Error = io::Error; fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { self.deserialise(src) } } } } encoder_decoder_impls!(u8, u8_buffer); encoder_decoder_impls!(u16, u16_buffer); encoder_decoder_impls!(u32, u32_buffer); encoder_decoder_impls!(u64, u64_buffer); encoder_decoder_impls!(u128, u128_buffer); encoder_decoder_impls!(usize, usize_buffer); /// Encoder/Decoder of unsigned-varint, length-prefixed bytes pub struct UviBytes { /// the variable-length integer encoder/decoder varint_codec: Uvi, /// number of bytes expected in the current frame (for decoding only) len: Option, /// maximum permitted number of bytes per frame max: usize, _ty: PhantomData } impl Default for UviBytes { fn default() -> Self { Self { varint_codec: Default::default(), len: None, max: 128 * 1024 * 1024, _ty: PhantomData } } } impl UviBytes { /// Limit the maximum allowed length of bytes. pub fn set_max_len(&mut self, val: usize) { self.max = val } /// Return the maximum allowed number of bytes to encode/decode. pub fn max_len(&self) -> usize { self.max } fn deserialise(&mut self, src: &mut BytesMut) -> Result, io::Error> { if self.len.is_none() { self.len = self.varint_codec.deserialise(src)? } if let Some(n) = self.len.take() { if n > self.max { return Err(io::Error::new(io::ErrorKind::PermissionDenied, "len > max")) } if n <= src.len() { return Ok(Some(src.split_to(n))) } let add = n - src.len(); src.reserve(add); self.len = Some(n) } Ok(None) } } impl UviBytes { fn serialise(&mut self, item: T, dst: &mut BytesMut) -> Result<(), io::Error> { if item.remaining() > self.max { return Err(io::Error::new(io::ErrorKind::PermissionDenied, "len > max when encoding")); } self.varint_codec.serialise(item.remaining(), dst); dst.reserve(item.remaining()); dst.put(item); Ok(()) } } #[cfg(feature = "codec")] impl tokio_util::codec::Encoder for UviBytes { type Error = io::Error; fn encode(&mut self, item: T, dst: &mut BytesMut) -> Result<(), Self::Error> { self.serialise(item, dst) } } #[cfg(feature = "codec")] impl tokio_util::codec::Decoder for UviBytes { type Item = BytesMut; type Error = io::Error; fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { self.deserialise(src) } } #[cfg(feature = "asynchronous_codec")] impl asynchronous_codec::Encoder for UviBytes { type Item<'a> = T; type Error = io::Error; fn encode(&mut self, item: Self::Item<'_>, dst: &mut BytesMut) -> Result<(), Self::Error> { self.serialise(item, dst) } } #[cfg(feature = "asynchronous_codec")] impl asynchronous_codec::Decoder for UviBytes { type Item = BytesMut; type Error = io::Error; fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { self.deserialise(src) } } unsigned-varint-0.8.0/src/decode.rs000064400000000000000000000112141046102023000153250ustar 00000000000000// Copyright 2018-2019 Parity Technologies (UK) Ltd. // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS // OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. //! Basic unsigned-varint decoding. use core::{self, fmt}; /// Possible decoding errors. /// /// **Note**: The `std` feature is required for the `std::error::Error` impl and the conversion to /// `std::io::Error`. #[non_exhaustive] #[derive(Clone, Debug, PartialEq, Eq)] pub enum Error { /// Not enough input bytes. Insufficient, /// Input bytes exceed maximum. Overflow, /// Encoding is not minimal (has trailing zero bytes). NotMinimal, } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Error::Insufficient => f.write_str("not enough input bytes"), Error::Overflow => f.write_str("input bytes exceed maximum"), Error::NotMinimal => f.write_str("encoding is not minimal"), } } } /// Only available when the feature `std` is present. #[cfg(feature = "std")] impl std::error::Error for Error {} /// Only available when the feature `std` is present. #[cfg(feature = "std")] impl Into for Error { fn into(self) -> std::io::Error { let kind = match self { Error::Insufficient => std::io::ErrorKind::UnexpectedEof, Error::Overflow => std::io::ErrorKind::InvalidData, Error::NotMinimal => std::io::ErrorKind::InvalidData, }; std::io::Error::new(kind, self) } } macro_rules! decode { ($buf:expr, $max_bytes:expr, $typ:ident) => {{ let mut n = 0; for (i, b) in $buf.iter().cloned().enumerate() { let k = $typ::from(b & 0x7F); n |= k << (i * 7); if is_last(b) { if b == 0 && i > 0 { // If last byte (of a multi-byte varint) is zero, it could have been "more // minimally" encoded by dropping that trailing zero. return Err(Error::NotMinimal); } return Ok((n, &$buf[i + 1..])); } if i == $max_bytes { return Err(Error::Overflow); } } Err(Error::Insufficient) }}; } /// Is this the last byte of an unsigned varint? #[inline] pub fn is_last(b: u8) -> bool { b & 0x80 == 0 } /// Decode the given slice as `u8`. /// /// Returns the value and the remaining slice. #[inline] pub fn u8(buf: &[u8]) -> Result<(u8, &[u8]), Error> { decode!(buf, 1, u8) } /// Decode the given slice as `u16`. /// /// Returns the value and the remaining slice. #[inline] pub fn u16(buf: &[u8]) -> Result<(u16, &[u8]), Error> { decode!(buf, 2, u16) } /// Decode the given slice as `u32`. /// /// Returns the value and the remaining slice. #[inline] pub fn u32(buf: &[u8]) -> Result<(u32, &[u8]), Error> { decode!(buf, 4, u32) } /// Decode the given slice as `u64`. /// /// Returns the value and the remaining slice. #[inline] pub fn u64(buf: &[u8]) -> Result<(u64, &[u8]), Error> { decode!(buf, 9, u64) } /// Decode the given slice as `u128`. /// /// Returns the value and the remaining slice. #[inline] pub fn u128(buf: &[u8]) -> Result<(u128, &[u8]), Error> { decode!(buf, 18, u128) } /// Decode the given slice as `usize`. /// /// Returns the value and the remaining slice. #[inline] #[cfg(target_pointer_width = "64")] pub fn usize(buf: &[u8]) -> Result<(usize, &[u8]), Error> { u64(buf).map(|(n, i)| (n as usize, i)) } /// Decode the given slice as `usize`. /// /// Returns the value and the remaining slice. #[inline] #[cfg(target_pointer_width = "32")] pub fn usize(buf: &[u8]) -> Result<(usize, &[u8]), Error> { u32(buf).map(|(n, i)| (n as usize, i)) } unsigned-varint-0.8.0/src/encode.rs000064400000000000000000000077741046102023000153570ustar 00000000000000// Copyright 2018 Parity Technologies (UK) Ltd. // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS // OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. //! Basic unsigned-varint encoding. macro_rules! encode { ($number:expr, $buf:expr) => {{ let mut n = $number; let mut i = 0; for b in $buf.iter_mut() { *b = n as u8 | 0x80; n >>= 7; if n == 0 { *b &= 0x7f; break } i += 1 } debug_assert_eq!(n, 0); &$buf[0..=i] }} } /// Encode the given `u8` into the given byte array. /// /// Returns the slice of encoded bytes. #[inline] pub fn u8(number: u8, buf: &mut [u8; U8_LEN]) -> &[u8] { encode!(number, buf) } /// Encode the given `u16` into the given byte array. /// /// Returns the slice of encoded bytes. #[inline] pub fn u16(number: u16, buf: &mut [u8; U16_LEN]) -> &[u8] { encode!(number, buf) } /// Encode the given `u32` into the given byte array. /// /// Returns the slice of encoded bytes. #[inline] pub fn u32(number: u32, buf: &mut [u8; U32_LEN]) -> &[u8] { encode!(number, buf) } /// Encode the given `u64` into the given byte array. /// /// Returns the slice of encoded bytes. #[inline] pub fn u64(number: u64, buf: &mut [u8; U64_LEN]) -> &[u8] { encode!(number, buf) } /// Encode the given `u128` into the given byte array. /// /// Returns the slice of encoded bytes. #[inline] pub fn u128(number: u128, buf: &mut [u8; U128_LEN]) -> &[u8] { encode!(number, buf) } /// Encode the given `usize` into the given byte array. /// /// Returns the slice of encoded bytes. #[inline] #[cfg(target_pointer_width = "64")] pub fn usize(number: usize, buf: &mut [u8; USIZE_LEN]) -> &[u8] { u64(number as u64, buf) } /// Encode the given `usize` into the given byte array. /// /// Returns the slice of encoded bytes. #[inline] #[cfg(target_pointer_width = "32")] pub fn usize(number: usize, buf: &mut [u8; USIZE_LEN]) -> &[u8] { u32(number as u32, buf) } /// Create new array buffer for encoding of `u8` values. #[inline] pub fn u8_buffer() -> [u8; U8_LEN] { [0; U8_LEN] } /// Create new array buffer for encoding of `u16` values. #[inline] pub fn u16_buffer() -> [u8; U16_LEN] { [0; U16_LEN] } /// Create new array buffer for encoding of `u32` values. #[inline] pub fn u32_buffer() -> [u8; U32_LEN] { [0; U32_LEN] } /// Create new array buffer for encoding of `u64` values. #[inline] pub fn u64_buffer() -> [u8; U64_LEN] { [0; U64_LEN] } /// Create new array buffer for encoding of `u128` values. #[inline] pub fn u128_buffer() -> [u8; U128_LEN] { [0; U128_LEN] } /// Create new array buffer for encoding of `usize` values. #[inline] pub fn usize_buffer() -> [u8; USIZE_LEN] { [0; USIZE_LEN] } // Required lengths of encoding buffers: const U8_LEN: usize = 2; const U16_LEN: usize = 3; const U32_LEN: usize = 5; const U64_LEN: usize = 10; const U128_LEN: usize = 19; #[cfg(target_pointer_width = "64")] const USIZE_LEN: usize = U64_LEN; #[cfg(target_pointer_width = "32")] const USIZE_LEN: usize = U32_LEN; unsigned-varint-0.8.0/src/io.rs000064400000000000000000000064211046102023000145150ustar 00000000000000// Copyright 2020 Parity Technologies (UK) Ltd. // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS // OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. //! Decode using [`std::io::Read`] types. use crate::{decode, encode}; use std::{fmt, io}; macro_rules! gen { ($($name:ident, $d:expr, $t:ident, $b:ident);*) => { $( #[doc = " Try to read and decode a "] #[doc = $d] #[doc = " from the given `Read` type."] pub fn $name(mut reader: R) -> Result<$t, ReadError> { let mut b = encode::$b(); for i in 0 .. b.len() { let n = reader.read(&mut b[i .. i + 1])?; if n == 0 { return Err(ReadError::Io(io::ErrorKind::UnexpectedEof.into())) } if decode::is_last(b[i]) { return Ok(decode::$t(&b[..= i])?.0) } } Err(decode::Error::Overflow.into()) } )* } } gen! { read_u8, "`u8`", u8, u8_buffer; read_u16, "`u16`", u16, u16_buffer; read_u32, "`u32`", u32, u32_buffer; read_u64, "`u64`", u64, u64_buffer; read_u128, "`u128`", u128, u128_buffer; read_usize, "`usize`", usize, usize_buffer } /// Possible read errors. #[non_exhaustive] #[derive(Debug)] pub enum ReadError { Io(io::Error), Decode(decode::Error) } impl fmt::Display for ReadError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ReadError::Io(e) => write!(f, "i/o error: {}", e), ReadError::Decode(e) => write!(f, "decode error: {}", e) } } } impl std::error::Error for ReadError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { if let ReadError::Io(e) = self { Some(e) } else { None } } } impl From for ReadError { fn from(e: io::Error) -> Self { ReadError::Io(e) } } impl From for ReadError { fn from(e: decode::Error) -> Self { ReadError::Decode(e) } } impl Into for ReadError { fn into(self) -> io::Error { match self { ReadError::Io(e) => e, ReadError::Decode(e) => e.into(), } } } unsigned-varint-0.8.0/src/lib.rs000064400000000000000000000031431046102023000146520ustar 00000000000000// Copyright 2018-2019 Parity Technologies (UK) Ltd. // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS // OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. //! Unsigned varint encodes unsigned integers in 7-bit groups. The most //! significant bit (MSB) in each byte indicates if another byte follows //! (MSB = 1), or not (MSB = 0). #![forbid(unsafe_code, unused_imports, unused_variables)] #![cfg_attr(not(feature = "std"), no_std)] pub mod decode; pub mod encode; #[cfg(feature = "std")] pub mod io; #[cfg(feature = "futures")] pub mod aio; #[cfg(any(feature = "codec", feature = "asynchronous_codec"))] pub mod codec; #[cfg(feature = "nom")] pub mod nom; unsigned-varint-0.8.0/src/nom.rs000064400000000000000000000040651046102023000147010ustar 00000000000000// Copyright (c) 2020 Aleksa Sarai // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS // OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. //! `nom` combinators to decode unsigned varints. use crate::decode::{self, Error}; use nom::{error::ErrorKind, Err as NomErr, IResult, Needed}; macro_rules! gen { ($($type:ident, $d:expr);*) => { $( #[doc = " `nom` combinator to decode a variable-length encoded "] #[doc = $d] #[doc = "."] pub fn $type(input: &[u8]) -> IResult<&[u8], $type, (&[u8], ErrorKind)> { let (n, remain) = decode::$type(input).map_err(|err| match err { Error::Insufficient => NomErr::Incomplete(Needed::Unknown), Error::Overflow => NomErr::Error((input, ErrorKind::TooLarge)), Error::NotMinimal => NomErr::Error((input, ErrorKind::Verify)), })?; Ok((remain, n)) } )* } } gen! { u8, "`u8`"; u16, "`u16`"; u32, "`u32`"; u64, "`u64`"; u128, "`u128`"; usize, "`usize`" } unsigned-varint-0.8.0/tests/identity.rs000064400000000000000000000104031046102023000163050ustar 00000000000000// Copyright 2018 Parity Technologies (UK) Ltd. // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS // OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. use quickcheck::QuickCheck; use std::{u8, u16, u32, u64, u128}; use unsigned_varint::{decode::{self, Error}, encode}; #[test] fn identity_u8() { let mut buf = encode::u8_buffer(); for n in 0 .. u8::MAX { assert_eq!(n, decode::u8(encode::u8(n, &mut buf)).unwrap().0) } } #[test] fn identity_u16() { let mut buf = encode::u16_buffer(); for n in 0 .. u16::MAX { assert_eq!(n, decode::u16(encode::u16(n, &mut buf)).unwrap().0) } } #[test] fn identity_u32() { let mut buf = encode::u32_buffer(); for n in 0 .. 1000_000 { assert_eq!(n, decode::u32(encode::u32(n, &mut buf)).unwrap().0) } assert_eq!(u32::MAX, decode::u32(encode::u32(u32::MAX, &mut buf)).unwrap().0) } #[test] fn identity_u64() { let mut buf = encode::u64_buffer(); for n in 0 .. 1000_000 { assert_eq!(n, decode::u64(encode::u64(n, &mut buf)).unwrap().0) } assert_eq!(u64::MAX, decode::u64(encode::u64(u64::MAX, &mut buf)).unwrap().0) } #[test] fn identity_u128() { let mut buf = encode::u128_buffer(); for n in 0 .. 1000_000 { assert_eq!(n, decode::u128(encode::u128(n, &mut buf)).unwrap().0) } assert_eq!(u128::MAX, decode::u128(encode::u128(u128::MAX, &mut buf)).unwrap().0) } #[test] fn identity() { fn prop(n: u64) -> bool { let mut buf = encode::u64_buffer(); Ok(n) == decode::u64(encode::u64(n, &mut buf)).map(|r| r.0) } QuickCheck::new() .tests(1_000_000) .min_tests_passed(1_000_000) .max_tests(1_000_000) .quickcheck(prop as fn(u64) -> bool) } #[test] fn various() { assert_eq!(Some(Error::Insufficient), decode::u8(&[]).err()); assert_eq!(Some(Error::Insufficient), decode::u8(&[0x80]).err()); assert_eq!(1, decode::u8(&[1]).unwrap().0); assert_eq!(127, decode::u8(&[0b0111_1111]).unwrap().0); assert_eq!(128, decode::u8(&[0b1000_0000, 1]).unwrap().0); assert_eq!(255, decode::u8(&[0b1111_1111, 1]).unwrap().0); assert_eq!(16384, decode::u16(&[0x80, 0x80, 1]).unwrap().0); assert_eq!(300, decode::u16(&[0b1010_1100, 0b0000_0010]).unwrap().0); assert_eq!( Some(Error::Overflow), decode::u64(&[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80]).err() ); assert_eq!( Some(Error::Insufficient), decode::u64(&[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80]).err() ); assert_eq!( 0xFFFFFFFFFFFFFFFF, decode::u64(&[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 1]).unwrap().0 ) } #[cfg(feature = "codec")] #[test] fn identity_codec() { use bytes::{Bytes, BytesMut}; use quickcheck::Gen; use tokio_util::codec::{Encoder, Decoder}; use unsigned_varint::codec::UviBytes; fn prop(mut xs: Vec) -> bool { let mut codec = UviBytes::default(); xs.truncate(codec.max_len()); let input = Bytes::from(xs); let mut buffer = BytesMut::with_capacity(input.len()); assert!(codec.encode(input.clone(), &mut buffer).is_ok()); input == codec.decode(&mut buffer).expect("Ok").expect("Some").freeze() } QuickCheck::new().gen(Gen::new(512 * 1024)) .quickcheck(prop as fn(Vec) -> bool) } unsigned-varint-0.8.0/tests/io.rs000064400000000000000000000124011046102023000150630ustar 00000000000000// Copyright 2020 Parity Technologies (UK) Ltd. // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS // OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. use quickcheck::{Arbitrary, Gen}; use unsigned_varint::encode; #[cfg(feature = "std")] #[test] fn read_arbitrary() { use unsigned_varint::io; fn property(n: RandomUvi) { let mut r = std::io::Cursor::new(n.bytes()); match n { RandomUvi::U8(n, _) => assert_eq!(n, io::read_u8(&mut r).unwrap()), RandomUvi::U16(n, _) => assert_eq!(n, io::read_u16(&mut r).unwrap()), RandomUvi::U32(n, _) => assert_eq!(n, io::read_u32(&mut r).unwrap()), RandomUvi::U64(n, _) => assert_eq!(n, io::read_u64(&mut r).unwrap()), RandomUvi::U128(n, _) => assert_eq!(n, io::read_u128(&mut r).unwrap()), RandomUvi::Usize(n, _) => assert_eq!(n, io::read_usize(&mut r).unwrap()) } } quickcheck::quickcheck(property as fn(RandomUvi)) } #[cfg(feature = "futures")] #[test] fn async_read_arbitrary() { use unsigned_varint::aio; fn property(n: RandomUvi) { futures_executor::block_on(async move { let mut r = futures_util::io::Cursor::new(n.bytes()); match n { RandomUvi::U8(n, _) => assert_eq!(n, aio::read_u8(&mut r).await.unwrap()), RandomUvi::U16(n, _) => assert_eq!(n, aio::read_u16(&mut r).await.unwrap()), RandomUvi::U32(n, _) => assert_eq!(n, aio::read_u32(&mut r).await.unwrap()), RandomUvi::U64(n, _) => assert_eq!(n, aio::read_u64(&mut r).await.unwrap()), RandomUvi::U128(n, _) => assert_eq!(n, aio::read_u128(&mut r).await.unwrap()), RandomUvi::Usize(n, _) => assert_eq!(n, aio::read_usize(&mut r).await.unwrap()) } }) } quickcheck::quickcheck(property as fn(RandomUvi)) } #[cfg(feature = "nom")] #[test] fn nom_read_arbitrary() { use unsigned_varint::nom; fn property(n: RandomUvi) { let input = n.bytes(); let empty = &[][..]; match n { RandomUvi::U8(n, _) => assert_eq!((empty, n), nom::u8(input).unwrap()), RandomUvi::U16(n, _) => assert_eq!((empty, n), nom::u16(input).unwrap()), RandomUvi::U32(n, _) => assert_eq!((empty, n), nom::u32(input).unwrap()), RandomUvi::U64(n, _) => assert_eq!((empty, n), nom::u64(input).unwrap()), RandomUvi::U128(n, _) => assert_eq!((empty, n), nom::u128(input).unwrap()), RandomUvi::Usize(n, _) => assert_eq!((empty, n), nom::usize(input).unwrap()), } } quickcheck::quickcheck(property as fn(RandomUvi)) } #[derive(Debug, Clone, PartialEq, Eq)] enum RandomUvi { U8(u8, Vec), U16(u16, Vec), U32(u32, Vec), U64(u64, Vec), U128(u128, Vec), Usize(usize, Vec), } impl RandomUvi { #[cfg(feature = "std")] fn bytes(&self) -> &[u8] { match self { RandomUvi::U8(_, v) => v, RandomUvi::U16(_, v) => v, RandomUvi::U32(_, v) => v, RandomUvi::U64(_, v) => v, RandomUvi::U128(_, v) => v, RandomUvi::Usize(_, v) => v } } } impl Arbitrary for RandomUvi { fn arbitrary(g: &mut Gen) -> Self { let n: u128 = Arbitrary::arbitrary(g); match n % 6 { 0 => { let mut b = encode::u8_buffer(); RandomUvi::U8(n as u8, Vec::from(encode::u8(n as u8, &mut b))) } 1 => { let mut b = encode::u16_buffer(); RandomUvi::U16(n as u16, Vec::from(encode::u16(n as u16, &mut b))) } 2 => { let mut b = encode::u32_buffer(); RandomUvi::U32(n as u32, Vec::from(encode::u32(n as u32, &mut b))) } 3 => { let mut b = encode::u64_buffer(); RandomUvi::U64(n as u64, Vec::from(encode::u64(n as u64, &mut b))) } 4 => { let mut b = encode::u128_buffer(); RandomUvi::U128(n, Vec::from(encode::u128(n, &mut b))) } _ => { let mut b = encode::usize_buffer(); RandomUvi::Usize(n as usize, Vec::from(encode::usize(n as usize, &mut b))) } } } }