diff options
Diffstat (limited to 'third_party/rust/writeable')
-rw-r--r-- | third_party/rust/writeable/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | third_party/rust/writeable/Cargo.lock | 643 | ||||
-rw-r--r-- | third_party/rust/writeable/Cargo.toml | 57 | ||||
-rw-r--r-- | third_party/rust/writeable/LICENSE | 44 | ||||
-rw-r--r-- | third_party/rust/writeable/README.md | 56 | ||||
-rw-r--r-- | third_party/rust/writeable/benches/writeable.rs | 162 | ||||
-rw-r--r-- | third_party/rust/writeable/examples/writeable_message.rs | 60 | ||||
-rw-r--r-- | third_party/rust/writeable/src/impls.rs | 178 | ||||
-rw-r--r-- | third_party/rust/writeable/src/lib.rs | 438 | ||||
-rw-r--r-- | third_party/rust/writeable/src/ops.rs | 294 | ||||
-rw-r--r-- | third_party/rust/writeable/tests/writeable.rs | 34 |
11 files changed, 1967 insertions, 0 deletions
diff --git a/third_party/rust/writeable/.cargo-checksum.json b/third_party/rust/writeable/.cargo-checksum.json new file mode 100644 index 0000000000..0c558a7845 --- /dev/null +++ b/third_party/rust/writeable/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.lock":"d58a334c85666e902b45b204aaa9273495fd29e68cc5151d3aeda24e74011d2f","Cargo.toml":"5dac443822a68c73ca5fe0b3697f818e610ff64888992e770aa18d6d3c395841","LICENSE":"853f87c96f3d249f200fec6db1114427bc8bdf4afddc93c576956d78152ce978","README.md":"8667ddc9209c9e148fdc3ca3ba9c5a46061e95a042a4e5abae29e9e3848d9278","benches/writeable.rs":"edc81c5524f98e77f0b3a45545606c4b526f63791e6dd8f90c88679c758c4834","examples/writeable_message.rs":"58bf4007f54f9f80428af7b687531837a294aecde533395a0a4c4cf55c9cad7d","src/impls.rs":"e0bafeea27e423a27d0d9e4fb8157b221ade1c5bb9616ae078e83fda6d496f1a","src/lib.rs":"75edc4baf7f69a20c7d6af54f318f904e798b3d6c58eeb624ddd198114aef09a","src/ops.rs":"3d47d9c81652fafc1baf98c747d8be6b2145b26171a37d41307f84763c47f271","tests/writeable.rs":"6dc3db45174180bcbf8980e640525b441c31b0b9db238721888d8cc0bd998ded"},"package":"dad7bb64b8ef9c0aa27b6da38b452b0ee9fd82beaf276a87dd796fb55cbae14e"}
\ No newline at end of file diff --git a/third_party/rust/writeable/Cargo.lock b/third_party/rust/writeable/Cargo.lock new file mode 100644 index 0000000000..527fc99cb9 --- /dev/null +++ b/third_party/rust/writeable/Cargo.lock @@ -0,0 +1,643 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab" +dependencies = [ + "memchr", +] + +[[package]] +name = "anes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" + +[[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 = "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 = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "ciborium" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "effd91f6c78e5a4ace8a5d3c0b6bfaec9e2baaef55f3efc00e45fb2e477ee926" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdf919175532b369853f5d5e20b26b43112613fd6fe7aee757e35f7a44642656" + +[[package]] +name = "ciborium-ll" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "defaa24ecc093c77630e6c15e17c51f5e187bf35ee514f4e2d67baaa96dae22b" +dependencies = [ + "ciborium-io", + "half", +] + +[[package]] +name = "clap" +version = "3.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" +dependencies = [ + "bitflags", + "clap_lex", + "indexmap", + "textwrap", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "criterion" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c76e09c1aae2bc52b3d2f29e13c6572553b30c4aa1b8a49fd70de6412654cb" +dependencies = [ + "anes", + "atty", + "cast", + "ciborium", + "clap", + "criterion-plot", + "itertools", + "lazy_static", + "num-traits", + "oorandom", + "plotters", + "rayon", + "regex", + "serde", + "serde_derive", + "serde_json", + "tinytemplate", + "walkdir", +] + +[[package]] +name = "criterion-plot" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" +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 = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[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 = "half" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[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.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5f195fe497f702db0f318b07fdd68edb16955aed830df8363d837542f8f935a" +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.148" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b" + +[[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.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" + +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num-traits" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" +dependencies = [ + "autocfg", +] + +[[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 = "os_str_bytes" +version = "6.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d5d9eb14b174ee9aa2ef96dc2b94637a2d4b6e7cb873c7e171f0c20c6cf3eac" + +[[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 = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "proc-macro2" +version = "1.0.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" +dependencies = [ + "unicode-ident", +] + +[[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 = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "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.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" + +[[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.188" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.188" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "2.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "textwrap" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" + +[[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 = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[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.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" + +[[package]] +name = "web-sys" +version = "0.3.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b" +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" + +[[package]] +name = "writeable" +version = "0.5.4" +dependencies = [ + "criterion", + "rand", +] diff --git a/third_party/rust/writeable/Cargo.toml b/third_party/rust/writeable/Cargo.toml new file mode 100644 index 0000000000..31856ca791 --- /dev/null +++ b/third_party/rust/writeable/Cargo.toml @@ -0,0 +1,57 @@ +# 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 = "2021" +rust-version = "1.67" +name = "writeable" +version = "0.5.4" +authors = ["The ICU4X Project Developers"] +include = [ + "data/**/*", + "src/**/*", + "examples/**/*", + "benches/**/*", + "tests/**/*", + "Cargo.toml", + "LICENSE", + "README.md", +] +description = "A more efficient alternative to fmt::Display" +readme = "README.md" +license-file = "LICENSE" +repository = "https://github.com/unicode-org/icu4x" + +[package.metadata.cargo-all-features] +denylist = ["bench"] + +[package.metadata.docs.rs] +all-features = true + +[package.metadata.workspaces] +independent = true + +[lib] +bench = false + +[[bench]] +name = "writeable" +harness = false + +[dev-dependencies.rand] +version = "0.8" +features = ["small_rng"] + +[features] +bench = [] + +[target."cfg(not(target_arch = \"wasm32\"))".dev-dependencies.criterion] +version = "0.4" diff --git a/third_party/rust/writeable/LICENSE b/third_party/rust/writeable/LICENSE new file mode 100644 index 0000000000..9845aa5f48 --- /dev/null +++ b/third_party/rust/writeable/LICENSE @@ -0,0 +1,44 @@ +UNICODE LICENSE V3 + +COPYRIGHT AND PERMISSION NOTICE + +Copyright © 2020-2023 Unicode, Inc. + +NOTICE TO USER: Carefully read the following legal agreement. BY +DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR +SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE +TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT +DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of data files and any associated documentation (the "Data Files") or +software and any associated documentation (the "Software") to deal in the +Data Files or Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, and/or sell +copies of the Data Files or Software, and to permit persons to whom the +Data Files or Software are furnished to do so, provided that either (a) +this copyright and permission notice appear with all copies of the Data +Files or Software, or (b) this copyright and permission notice appear in +associated Documentation. + +THE DATA FILES AND SOFTWARE ARE 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 OF +THIRD PARTY RIGHTS. + +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE +BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, +OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA +FILES OR SOFTWARE. + +Except as contained in this notice, the name of a copyright holder shall +not be used in advertising or otherwise to promote the sale, use or other +dealings in these Data Files or Software without prior written +authorization of the copyright holder. + +— + +Portions of ICU4X may have been adapted from ICU4C and/or ICU4J. +ICU 1.8.1 to ICU 57.1 © 1995-2016 International Business Machines Corporation and others. diff --git a/third_party/rust/writeable/README.md b/third_party/rust/writeable/README.md new file mode 100644 index 0000000000..c69b91cf3f --- /dev/null +++ b/third_party/rust/writeable/README.md @@ -0,0 +1,56 @@ +# writeable [![crates.io](https://img.shields.io/crates/v/writeable)](https://crates.io/crates/writeable) + +<!-- cargo-rdme start --> + +`writeable` is a utility crate of the [`ICU4X`] project. + +It includes [`Writeable`], a core trait representing an object that can be written to a +sink implementing `std::fmt::Write`. It is an alternative to `std::fmt::Display` with the +addition of a function indicating the number of bytes to be written. + +`Writeable` improves upon `std::fmt::Display` in two ways: + +1. More efficient, since the sink can pre-allocate bytes. +2. Smaller code, since the format machinery can be short-circuited. + +## Examples + +```rust +use std::fmt; +use writeable::assert_writeable_eq; +use writeable::LengthHint; +use writeable::Writeable; + +struct WelcomeMessage<'s> { + pub name: &'s str, +} + +impl<'s> Writeable for WelcomeMessage<'s> { + fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { + sink.write_str("Hello, ")?; + sink.write_str(self.name)?; + sink.write_char('!')?; + Ok(()) + } + + fn writeable_length_hint(&self) -> LengthHint { + // "Hello, " + '!' + length of name + LengthHint::exact(8 + self.name.len()) + } +} + +let message = WelcomeMessage { name: "Alice" }; +assert_writeable_eq!(&message, "Hello, Alice!"); + +// Types implementing `Writeable` are recommended to also implement `fmt::Display`. +// This can be simply done by redirecting to the `Writeable` implementation: +writeable::impl_display_with_writeable!(WelcomeMessage<'_>); +``` + +[`ICU4X`]: ../icu/index.html + +<!-- cargo-rdme end --> + +## More Information + +For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x). diff --git a/third_party/rust/writeable/benches/writeable.rs b/third_party/rust/writeable/benches/writeable.rs new file mode 100644 index 0000000000..fc433f8953 --- /dev/null +++ b/third_party/rust/writeable/benches/writeable.rs @@ -0,0 +1,162 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use std::fmt; +use writeable::LengthHint; +use writeable::Writeable; + +/// A sample type implementing Writeable +struct WriteableMessage<'s> { + message: &'s str, +} + +impl Writeable for WriteableMessage<'_> { + fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { + sink.write_str(self.message) + } + + fn writeable_length_hint(&self) -> LengthHint { + LengthHint::exact(self.message.len()) + } +} + +writeable::impl_display_with_writeable!(WriteableMessage<'_>); + +/// A sample type implementing Display +struct DisplayMessage<'s> { + message: &'s str, +} + +impl fmt::Display for DisplayMessage<'_> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str(self.message) + } +} + +const SHORT_STR: &str = "short"; +const MEDIUM_STR: &str = "this is a medium-length string"; +const LONG_STR: &str = "this string is very very very very very very very very very very very very very very very very very very very very very very very very long"; + +fn overview_bench(c: &mut Criterion) { + c.bench_function("writeable/overview", |b| { + b.iter(|| { + // This benchmark runs to_string on short, medium, and long strings in one batch. + WriteableMessage { + message: black_box(SHORT_STR), + } + .write_to_string(); + WriteableMessage { + message: black_box(MEDIUM_STR), + } + .write_to_string(); + WriteableMessage { + message: black_box(LONG_STR), + } + .write_to_string(); + }); + }); + + #[cfg(feature = "bench")] + { + writeable_benches(c); + writeable_dyn_benches(c); + display_benches(c); + } +} + +#[cfg(feature = "bench")] +fn writeable_benches(c: &mut Criterion) { + c.bench_function("writeable/to_string/short", |b| { + b.iter(|| { + WriteableMessage { + message: black_box(SHORT_STR), + } + .write_to_string() + .into_owned() + }); + }); + c.bench_function("writeable/to_string/medium", |b| { + b.iter(|| { + WriteableMessage { + message: black_box(MEDIUM_STR), + } + .write_to_string() + .into_owned() + }); + }); + c.bench_function("writeable/to_string/long", |b| { + b.iter(|| { + WriteableMessage { + message: black_box(LONG_STR), + } + .write_to_string() + .into_owned() + }); + }); +} + +#[cfg(feature = "bench")] +fn writeable_dyn_benches(c: &mut Criterion) { + // Same as `write_to_string`, but casts to a `dyn fmt::Write` + fn writeable_dyn_to_string(w: &impl Writeable) -> String { + let mut output = String::with_capacity(w.writeable_length_hint().capacity()); + w.write_to(&mut output as &mut dyn fmt::Write) + .expect("impl Write for String is infallible"); + output + } + + c.bench_function("writeable_dyn/to_string/short", |b| { + b.iter(|| { + writeable_dyn_to_string(&WriteableMessage { + message: black_box(SHORT_STR), + }) + }); + }); + c.bench_function("writeable_dyn/to_string/medium", |b| { + b.iter(|| { + writeable_dyn_to_string(&WriteableMessage { + message: black_box(MEDIUM_STR), + }) + }); + }); + c.bench_function("writeable_dyn/to_string/long", |b| { + b.iter(|| { + writeable_dyn_to_string(&WriteableMessage { + message: black_box(LONG_STR), + }) + }); + }); +} + +#[cfg(feature = "bench")] +fn display_benches(c: &mut Criterion) { + c.bench_function("display/to_string/short", |b| { + b.iter(|| { + DisplayMessage { + message: black_box(SHORT_STR), + } + .to_string() + }); + }); + c.bench_function("display/to_string/medium", |b| { + b.iter(|| { + DisplayMessage { + message: black_box(MEDIUM_STR), + } + .to_string() + }); + }); + c.bench_function("display/to_string/long", |b| { + b.iter(|| { + DisplayMessage { + message: black_box(LONG_STR), + } + .to_string() + }); + }); +} + +criterion_group!(benches, overview_bench,); +criterion_main!(benches); diff --git a/third_party/rust/writeable/examples/writeable_message.rs b/third_party/rust/writeable/examples/writeable_message.rs new file mode 100644 index 0000000000..b342fdb261 --- /dev/null +++ b/third_party/rust/writeable/examples/writeable_message.rs @@ -0,0 +1,60 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +// This example illustrates a very simple type implementing Writeable. +icu_benchmark_macros::static_setup!(); + +use std::fmt; +use writeable::*; + +struct WriteableMessage<W: Writeable>(W); + +const GREETING: Part = Part { + category: "meaning", + value: "greeting", +}; + +const EMOJI: Part = Part { + category: "meaning", + value: "emoji", +}; + +impl<V: Writeable> Writeable for WriteableMessage<V> { + fn write_to_parts<W: PartsWrite + ?Sized>(&self, sink: &mut W) -> fmt::Result { + use fmt::Write; + sink.with_part(GREETING, |g| { + g.write_str("Hello")?; + g.write_str(" ")?; + self.0.write_to(g) + })?; + sink.write_char(' ')?; + sink.with_part(EMOJI, |e| e.write_char('😅')) + } + + fn writeable_length_hint(&self) -> LengthHint { + LengthHint::exact(11) + self.0.writeable_length_hint() + } +} + +impl<V: Writeable> fmt::Display for WriteableMessage<V> { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.write_to(f) + } +} + +fn main() { + icu_benchmark_macros::main_setup!(); + + let (string, parts) = writeable_to_parts_for_test(&WriteableMessage("world")).unwrap(); + + assert_eq!(string, "Hello world 😅"); + + // Print the greeting only + let (start, end, _) = parts + .into_iter() + .find(|(_, _, part)| part == &GREETING) + .unwrap(); + println!("{}", &string[start..end]); +} diff --git a/third_party/rust/writeable/src/impls.rs b/third_party/rust/writeable/src/impls.rs new file mode 100644 index 0000000000..a1410fb948 --- /dev/null +++ b/third_party/rust/writeable/src/impls.rs @@ -0,0 +1,178 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +use crate::*; +use alloc::borrow::Cow; +use core::fmt; + +macro_rules! impl_write_num { + ($u:ty, $i:ty, $test:ident) => { + impl $crate::Writeable for $u { + fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result { + const MAX_LEN: usize = <$u>::MAX.ilog10() as usize + 1; + let mut buf = [b'0'; MAX_LEN]; + let mut n = *self; + let mut i = MAX_LEN; + #[allow(clippy::indexing_slicing)] // n < 10^i + while n != 0 { + i -= 1; + buf[i] = b'0' + (n % 10) as u8; + n /= 10; + } + if i == MAX_LEN { + debug_assert_eq!(*self, 0); + i -= 1; + } + #[allow(clippy::indexing_slicing)] // buf is ASCII + let s = unsafe { core::str::from_utf8_unchecked(&buf[i..]) }; + sink.write_str(s) + } + + fn writeable_length_hint(&self) -> $crate::LengthHint { + LengthHint::exact(self.checked_ilog10().unwrap_or(0) as usize + 1) + } + } + + impl $crate::Writeable for $i { + fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result { + if self.is_negative() { + sink.write_str("-")?; + } + self.unsigned_abs().write_to(sink) + } + + fn writeable_length_hint(&self) -> $crate::LengthHint { + $crate::LengthHint::exact(if self.is_negative() { 1 } else { 0 }) + + self.unsigned_abs().writeable_length_hint() + } + } + + #[test] + fn $test() { + use $crate::assert_writeable_eq; + assert_writeable_eq!(&(0 as $u), "0"); + assert_writeable_eq!(&(0 as $i), "0"); + assert_writeable_eq!(&(-0 as $i), "0"); + assert_writeable_eq!(&(1 as $u), "1"); + assert_writeable_eq!(&(1 as $i), "1"); + assert_writeable_eq!(&(-1 as $i), "-1"); + assert_writeable_eq!(&(9 as $u), "9"); + assert_writeable_eq!(&(9 as $i), "9"); + assert_writeable_eq!(&(-9 as $i), "-9"); + assert_writeable_eq!(&(10 as $u), "10"); + assert_writeable_eq!(&(10 as $i), "10"); + assert_writeable_eq!(&(-10 as $i), "-10"); + assert_writeable_eq!(&(99 as $u), "99"); + assert_writeable_eq!(&(99 as $i), "99"); + assert_writeable_eq!(&(-99 as $i), "-99"); + assert_writeable_eq!(&(100 as $u), "100"); + assert_writeable_eq!(&(-100 as $i), "-100"); + assert_writeable_eq!(&<$u>::MAX, <$u>::MAX.to_string()); + assert_writeable_eq!(&<$i>::MAX, <$i>::MAX.to_string()); + assert_writeable_eq!(&<$i>::MIN, <$i>::MIN.to_string()); + + use rand::{rngs::SmallRng, Rng, SeedableRng}; + let mut rng = SmallRng::seed_from_u64(4); // chosen by fair dice roll. + // guaranteed to be random. + for _ in 0..1000 { + let rand = rng.gen::<$u>(); + assert_writeable_eq!(rand, rand.to_string()); + } + } + }; +} + +impl_write_num!(u8, i8, test_u8); +impl_write_num!(u16, i16, test_u16); +impl_write_num!(u32, i32, test_u32); +impl_write_num!(u64, i64, test_u64); +impl_write_num!(u128, i128, test_u128); +impl_write_num!(usize, isize, test_usize); + +impl Writeable for str { + #[inline] + fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { + sink.write_str(self) + } + + #[inline] + fn writeable_length_hint(&self) -> LengthHint { + LengthHint::exact(self.len()) + } + + /// Returns a borrowed `str`. + /// + /// # Examples + /// + /// ``` + /// use std::borrow::Cow; + /// use writeable::Writeable; + /// + /// let cow = "foo".write_to_string(); + /// assert!(matches!(cow, Cow::Borrowed(_))); + /// ``` + #[inline] + fn write_to_string(&self) -> Cow<str> { + Cow::Borrowed(self) + } +} + +impl Writeable for String { + #[inline] + fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { + sink.write_str(self) + } + + #[inline] + fn writeable_length_hint(&self) -> LengthHint { + LengthHint::exact(self.len()) + } + + #[inline] + fn write_to_string(&self) -> Cow<str> { + Cow::Borrowed(self) + } +} + +impl<T: Writeable + ?Sized> Writeable for &T { + #[inline] + fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { + (*self).write_to(sink) + } + + #[inline] + fn write_to_parts<W: PartsWrite + ?Sized>(&self, sink: &mut W) -> fmt::Result { + (*self).write_to_parts(sink) + } + + #[inline] + fn writeable_length_hint(&self) -> LengthHint { + (*self).writeable_length_hint() + } + + #[inline] + fn write_to_string(&self) -> Cow<str> { + (*self).write_to_string() + } +} + +#[test] +fn test_string_impls() { + fn check_writeable_slice<W: Writeable + core::fmt::Display>(writeables: &[W]) { + assert_writeable_eq!(&writeables[0], ""); + assert_writeable_eq!(&writeables[1], "abc"); + } + + // test str impl + let arr: &[&str] = &["", "abc"]; + check_writeable_slice(arr); + + // test String impl + let arr: &[String] = &[String::new(), "abc".to_owned()]; + check_writeable_slice(arr); + + // test &T impl + let arr: &[&String] = &[&String::new(), &"abc".to_owned()]; + check_writeable_slice(arr); +} diff --git a/third_party/rust/writeable/src/lib.rs b/third_party/rust/writeable/src/lib.rs new file mode 100644 index 0000000000..0eb6be8d6d --- /dev/null +++ b/third_party/rust/writeable/src/lib.rs @@ -0,0 +1,438 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations +#![cfg_attr(all(not(test), not(doc)), no_std)] +#![cfg_attr( + not(test), + deny( + clippy::indexing_slicing, + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::exhaustive_structs, + clippy::exhaustive_enums, + missing_debug_implementations, + ) +)] + +//! `writeable` is a utility crate of the [`ICU4X`] project. +//! +//! It includes [`Writeable`], a core trait representing an object that can be written to a +//! sink implementing `std::fmt::Write`. It is an alternative to `std::fmt::Display` with the +//! addition of a function indicating the number of bytes to be written. +//! +//! `Writeable` improves upon `std::fmt::Display` in two ways: +//! +//! 1. More efficient, since the sink can pre-allocate bytes. +//! 2. Smaller code, since the format machinery can be short-circuited. +//! +//! # Examples +//! +//! ``` +//! use std::fmt; +//! use writeable::assert_writeable_eq; +//! use writeable::LengthHint; +//! use writeable::Writeable; +//! +//! struct WelcomeMessage<'s> { +//! pub name: &'s str, +//! } +//! +//! impl<'s> Writeable for WelcomeMessage<'s> { +//! fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { +//! sink.write_str("Hello, ")?; +//! sink.write_str(self.name)?; +//! sink.write_char('!')?; +//! Ok(()) +//! } +//! +//! fn writeable_length_hint(&self) -> LengthHint { +//! // "Hello, " + '!' + length of name +//! LengthHint::exact(8 + self.name.len()) +//! } +//! } +//! +//! let message = WelcomeMessage { name: "Alice" }; +//! assert_writeable_eq!(&message, "Hello, Alice!"); +//! +//! // Types implementing `Writeable` are recommended to also implement `fmt::Display`. +//! // This can be simply done by redirecting to the `Writeable` implementation: +//! writeable::impl_display_with_writeable!(WelcomeMessage<'_>); +//! ``` +//! +//! [`ICU4X`]: ../icu/index.html + +extern crate alloc; + +mod impls; +mod ops; + +use alloc::borrow::Cow; +use alloc::string::String; +use alloc::vec::Vec; +use core::fmt; + +/// A hint to help consumers of `Writeable` pre-allocate bytes before they call +/// [`write_to`](Writeable::write_to). +/// +/// This behaves like `Iterator::size_hint`: it is a tuple where the first element is the +/// lower bound, and the second element is the upper bound. If the upper bound is `None` +/// either there is no known upper bound, or the upper bound is larger than `usize`. +/// +/// `LengthHint` implements std`::ops::{Add, Mul}` and similar traits for easy composition. +/// During computation, the lower bound will saturate at `usize::MAX`, while the upper +/// bound will become `None` if `usize::MAX` is exceeded. +#[derive(Debug, PartialEq, Eq, Copy, Clone)] +#[non_exhaustive] +pub struct LengthHint(pub usize, pub Option<usize>); + +impl LengthHint { + pub fn undefined() -> Self { + Self(0, None) + } + + /// `write_to` will use exactly n bytes. + pub fn exact(n: usize) -> Self { + Self(n, Some(n)) + } + + /// `write_to` will use at least n bytes. + pub fn at_least(n: usize) -> Self { + Self(n, None) + } + + /// `write_to` will use at most n bytes. + pub fn at_most(n: usize) -> Self { + Self(0, Some(n)) + } + + /// `write_to` will use between `n` and `m` bytes. + pub fn between(n: usize, m: usize) -> Self { + Self(Ord::min(n, m), Some(Ord::max(n, m))) + } + + /// Returns a recommendation for the number of bytes to pre-allocate. + /// If an upper bound exists, this is used, otherwise the lower bound + /// (which might be 0). + /// + /// # Examples + /// + /// ``` + /// use writeable::Writeable; + /// + /// fn pre_allocate_string(w: &impl Writeable) -> String { + /// String::with_capacity(w.writeable_length_hint().capacity()) + /// } + /// ``` + pub fn capacity(&self) -> usize { + self.1.unwrap_or(self.0) + } + + /// Returns whether the `LengthHint` indicates that the string is exactly 0 bytes long. + pub fn is_zero(&self) -> bool { + self.1 == Some(0) + } +} + +/// [`Part`]s are used as annotations for formatted strings. For example, a string like +/// `Alice, Bob` could assign a `NAME` part to the substrings `Alice` and `Bob`, and a +/// `PUNCTUATION` part to `, `. This allows for example to apply styling only to names. +/// +/// `Part` contains two fields, whose usage is left up to the producer of the [`Writeable`]. +/// Conventionally, the `category` field will identify the formatting logic that produces +/// the string/parts, whereas the `value` field will have semantic meaning. `NAME` and +/// `PUNCTUATION` could thus be defined as +/// ``` +/// # use writeable::Part; +/// const NAME: Part = Part { +/// category: "userlist", +/// value: "name", +/// }; +/// const PUNCTUATION: Part = Part { +/// category: "userlist", +/// value: "punctuation", +/// }; +/// ``` +/// +/// That said, consumers should not usually have to inspect `Part` internals. Instead, +/// formatters should expose the `Part`s they produces as constants. +#[derive(Clone, Copy, Debug, PartialEq)] +#[allow(clippy::exhaustive_structs)] // stable +pub struct Part { + pub category: &'static str, + pub value: &'static str, +} + +/// A sink that supports annotating parts of the string with `Part`s. +pub trait PartsWrite: fmt::Write { + type SubPartsWrite: PartsWrite + ?Sized; + + fn with_part( + &mut self, + part: Part, + f: impl FnMut(&mut Self::SubPartsWrite) -> fmt::Result, + ) -> fmt::Result; +} + +/// `Writeable` is an alternative to `std::fmt::Display` with the addition of a length function. +pub trait Writeable { + /// Writes a string to the given sink. Errors from the sink are bubbled up. + /// The default implementation delegates to `write_to_parts`, and discards any + /// `Part` annotations. + fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { + struct CoreWriteAsPartsWrite<W: fmt::Write + ?Sized>(W); + impl<W: fmt::Write + ?Sized> fmt::Write for CoreWriteAsPartsWrite<W> { + fn write_str(&mut self, s: &str) -> fmt::Result { + self.0.write_str(s) + } + + fn write_char(&mut self, c: char) -> fmt::Result { + self.0.write_char(c) + } + } + + impl<W: fmt::Write + ?Sized> PartsWrite for CoreWriteAsPartsWrite<W> { + type SubPartsWrite = CoreWriteAsPartsWrite<W>; + + fn with_part( + &mut self, + _part: Part, + mut f: impl FnMut(&mut Self::SubPartsWrite) -> fmt::Result, + ) -> fmt::Result { + f(self) + } + } + + self.write_to_parts(&mut CoreWriteAsPartsWrite(sink)) + } + + /// Write bytes and `Part` annotations to the given sink. Errors from the + /// sink are bubbled up. The default implementation delegates to `write_to`, + /// and doesn't produce any `Part` annotations. + fn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> fmt::Result { + self.write_to(sink) + } + + /// Returns a hint for the number of UTF-8 bytes that will be written to the sink. + /// + /// Override this method if it can be computed quickly. + fn writeable_length_hint(&self) -> LengthHint { + LengthHint::undefined() + } + + /// Creates a new `String` with the data from this `Writeable`. Like `ToString`, + /// but smaller and faster. + /// + /// The default impl allocates an owned `String`. However, if it is possible to return a + /// borrowed string, overwrite this method to return a `Cow::Borrowed`. + /// + /// To remove the `Cow` wrapper, call `.into_owned()` or `.as_str()` as appropriate. + /// + /// # Examples + /// + /// Inspect a `Writeable` before writing it to the sink: + /// + /// ``` + /// use core::fmt::{Result, Write}; + /// use writeable::Writeable; + /// + /// fn write_if_ascii<W, S>(w: &W, sink: &mut S) -> Result + /// where + /// W: Writeable + ?Sized, + /// S: Write + ?Sized, + /// { + /// let s = w.write_to_string(); + /// if s.is_ascii() { + /// sink.write_str(&s) + /// } else { + /// Ok(()) + /// } + /// } + /// ``` + /// + /// Convert the `Writeable` into a fully owned `String`: + /// + /// ``` + /// use writeable::Writeable; + /// + /// fn make_string(w: &impl Writeable) -> String { + /// w.write_to_string().into_owned() + /// } + /// ``` + fn write_to_string(&self) -> Cow<str> { + let hint = self.writeable_length_hint(); + if hint.is_zero() { + return Cow::Borrowed(""); + } + let mut output = String::with_capacity(hint.capacity()); + let _ = self.write_to(&mut output); + Cow::Owned(output) + } +} + +/// Implements [`Display`](core::fmt::Display) for types that implement [`Writeable`]. +/// +/// It's recommended to do this for every [`Writeable`] type, as it will add +/// support for `core::fmt` features like [`fmt!`](std::fmt), +/// [`print!`](std::print), [`write!`](std::write), etc. +#[macro_export] +macro_rules! impl_display_with_writeable { + ($type:ty) => { + /// This trait is implemented for compatibility with [`fmt!`](alloc::fmt). + /// To create a string, [`Writeable::write_to_string`] is usually more efficient. + impl core::fmt::Display for $type { + #[inline] + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { + $crate::Writeable::write_to(&self, f) + } + } + }; +} + +/// Testing macros for types implementing Writeable. The first argument should be a +/// `Writeable`, the second argument a string, and the third argument (*_parts_eq only) +/// a list of parts (`[(usize, usize, Part)]`). +/// +/// The macros tests for equality of string content, parts (*_parts_eq only), and +/// verify the size hint. +/// +/// # Examples +/// +/// ``` +/// # use writeable::Writeable; +/// # use writeable::LengthHint; +/// # use writeable::Part; +/// # use writeable::assert_writeable_eq; +/// # use writeable::assert_writeable_parts_eq; +/// # use std::fmt::{self, Write}; +/// +/// const WORD: Part = Part { +/// category: "foo", +/// value: "word", +/// }; +/// +/// struct Demo; +/// impl Writeable for Demo { +/// fn write_to_parts<S: writeable::PartsWrite + ?Sized>( +/// &self, +/// sink: &mut S, +/// ) -> fmt::Result { +/// sink.with_part(WORD, |w| w.write_str("foo")) +/// } +/// fn writeable_length_hint(&self) -> LengthHint { +/// LengthHint::exact(3) +/// } +/// } +/// +/// writeable::impl_display_with_writeable!(Demo); +/// +/// assert_writeable_eq!(&Demo, "foo"); +/// assert_writeable_eq!(&Demo, "foo", "Message: {}", "Hello World"); +/// +/// assert_writeable_parts_eq!(&Demo, "foo", [(0, 3, WORD)]); +/// assert_writeable_parts_eq!( +/// &Demo, +/// "foo", +/// [(0, 3, WORD)], +/// "Message: {}", +/// "Hello World" +/// ); +/// ``` +#[macro_export] +macro_rules! assert_writeable_eq { + ($actual_writeable:expr, $expected_str:expr $(,)?) => { + $crate::assert_writeable_eq!($actual_writeable, $expected_str, ""); + }; + ($actual_writeable:expr, $expected_str:expr, $($arg:tt)+) => {{ + let actual_writeable = &$actual_writeable; + let (actual_str, _) = $crate::writeable_to_parts_for_test(actual_writeable).unwrap(); + assert_eq!(actual_str, $expected_str, $($arg)*); + assert_eq!(actual_str, $crate::Writeable::write_to_string(actual_writeable), $($arg)+); + let length_hint = $crate::Writeable::writeable_length_hint(actual_writeable); + assert!( + length_hint.0 <= actual_str.len(), + "hint lower bound {} larger than actual length {}: {}", + length_hint.0, actual_str.len(), format!($($arg)*), + ); + if let Some(upper) = length_hint.1 { + assert!( + actual_str.len() <= upper, + "hint upper bound {} smaller than actual length {}: {}", + length_hint.0, actual_str.len(), format!($($arg)*), + ); + } + assert_eq!(actual_writeable.to_string(), $expected_str); + }}; +} + +/// See [`assert_writeable_eq`]. +#[macro_export] +macro_rules! assert_writeable_parts_eq { + ($actual_writeable:expr, $expected_str:expr, $expected_parts:expr $(,)?) => { + $crate::assert_writeable_parts_eq!($actual_writeable, $expected_str, $expected_parts, ""); + }; + ($actual_writeable:expr, $expected_str:expr, $expected_parts:expr, $($arg:tt)+) => {{ + let actual_writeable = &$actual_writeable; + let (actual_str, actual_parts) = $crate::writeable_to_parts_for_test(actual_writeable).unwrap(); + assert_eq!(actual_str, $expected_str, $($arg)+); + assert_eq!(actual_str, $crate::Writeable::write_to_string(actual_writeable), $($arg)+); + assert_eq!(actual_parts, $expected_parts, $($arg)+); + let length_hint = $crate::Writeable::writeable_length_hint(actual_writeable); + assert!(length_hint.0 <= actual_str.len(), $($arg)+); + if let Some(upper) = length_hint.1 { + assert!(actual_str.len() <= upper, $($arg)+); + } + assert_eq!(actual_writeable.to_string(), $expected_str); + }}; +} + +#[doc(hidden)] +#[allow(clippy::type_complexity)] +pub fn writeable_to_parts_for_test<W: Writeable>( + writeable: &W, +) -> Result<(String, Vec<(usize, usize, Part)>), fmt::Error> { + struct State { + string: alloc::string::String, + parts: Vec<(usize, usize, Part)>, + } + + impl fmt::Write for State { + fn write_str(&mut self, s: &str) -> fmt::Result { + self.string.write_str(s) + } + fn write_char(&mut self, c: char) -> fmt::Result { + self.string.write_char(c) + } + } + + impl PartsWrite for State { + type SubPartsWrite = Self; + fn with_part( + &mut self, + part: Part, + mut f: impl FnMut(&mut Self::SubPartsWrite) -> fmt::Result, + ) -> fmt::Result { + let start = self.string.len(); + f(self)?; + let end = self.string.len(); + if start < end { + self.parts.push((start, end, part)); + } + Ok(()) + } + } + + let mut state = State { + string: alloc::string::String::new(), + parts: Vec::new(), + }; + writeable.write_to_parts(&mut state)?; + + // Sort by first open and last closed + state + .parts + .sort_unstable_by_key(|(begin, end, _)| (*begin, end.wrapping_neg())); + Ok((state.string, state.parts)) +} diff --git a/third_party/rust/writeable/src/ops.rs b/third_party/rust/writeable/src/ops.rs new file mode 100644 index 0000000000..a4c1ffd759 --- /dev/null +++ b/third_party/rust/writeable/src/ops.rs @@ -0,0 +1,294 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +use crate::LengthHint; + +impl core::ops::Add<LengthHint> for LengthHint { + type Output = Self; + + fn add(self, other: LengthHint) -> Self { + LengthHint( + self.0.saturating_add(other.0), + match (self.1, other.1) { + (Some(c), Some(d)) => c.checked_add(d), + _ => None, + }, + ) + } +} + +impl core::ops::AddAssign<LengthHint> for LengthHint { + fn add_assign(&mut self, other: Self) { + *self = *self + other; + } +} + +impl core::iter::Sum<LengthHint> for LengthHint { + fn sum<I>(iter: I) -> Self + where + I: Iterator<Item = LengthHint>, + { + iter.fold(LengthHint::exact(0), core::ops::Add::add) + } +} + +impl core::ops::Add<usize> for LengthHint { + type Output = Self; + + fn add(self, other: usize) -> Self { + Self( + self.0.saturating_add(other), + self.1.and_then(|upper| upper.checked_add(other)), + ) + } +} + +impl core::ops::AddAssign<usize> for LengthHint { + fn add_assign(&mut self, other: usize) { + *self = *self + other; + } +} + +impl core::ops::Mul<usize> for LengthHint { + type Output = Self; + + fn mul(self, other: usize) -> Self { + Self( + self.0.saturating_mul(other), + self.1.and_then(|upper| upper.checked_mul(other)), + ) + } +} + +impl core::ops::MulAssign<usize> for LengthHint { + fn mul_assign(&mut self, other: usize) { + *self = *self * other; + } +} + +impl core::ops::BitOr<LengthHint> for LengthHint { + type Output = Self; + + /// Returns a new hint that is correct wherever `self` is correct, and wherever + /// `other` is correct. + /// + /// Example: + /// ``` + /// # use writeable::{LengthHint, Writeable}; + /// # use core::fmt; + /// # fn coin_flip() -> bool { true } + /// + /// struct NonDeterministicWriteable(String, String); + /// + /// impl Writeable for NonDeterministicWriteable { + /// fn write_to<W: fmt::Write + ?Sized>( + /// &self, + /// sink: &mut W, + /// ) -> fmt::Result { + /// sink.write_str(if coin_flip() { &self.0 } else { &self.1 }) + /// } + /// + /// fn writeable_length_hint(&self) -> LengthHint { + /// LengthHint::exact(self.0.len()) | LengthHint::exact(self.1.len()) + /// } + /// } + /// + /// writeable::impl_display_with_writeable!(NonDeterministicWriteable); + /// ``` + fn bitor(self, other: LengthHint) -> Self { + LengthHint( + Ord::min(self.0, other.0), + match (self.1, other.1) { + (Some(c), Some(d)) => Some(Ord::max(c, d)), + _ => None, + }, + ) + } +} + +impl core::ops::BitOrAssign<LengthHint> for LengthHint { + fn bitor_assign(&mut self, other: Self) { + *self = *self | other; + } +} + +impl core::iter::Sum<usize> for LengthHint { + fn sum<I>(iter: I) -> Self + where + I: Iterator<Item = usize>, + { + LengthHint::exact(iter.sum::<usize>()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_add() { + assert_eq!(LengthHint::exact(3) + 2, LengthHint::exact(5)); + assert_eq!( + LengthHint::exact(3) + LengthHint::exact(2), + LengthHint::exact(5) + ); + assert_eq!( + LengthHint::exact(3) + LengthHint::undefined(), + LengthHint::at_least(3) + ); + + assert_eq!(LengthHint::undefined() + 2, LengthHint::at_least(2)); + assert_eq!( + LengthHint::undefined() + LengthHint::exact(2), + LengthHint::at_least(2) + ); + assert_eq!( + LengthHint::undefined() + LengthHint::undefined(), + LengthHint::undefined() + ); + + assert_eq!( + LengthHint::at_least(15) + LengthHint::exact(3), + LengthHint::at_least(18) + ); + + assert_eq!( + LengthHint::at_least(15) + LengthHint::at_most(3), + LengthHint::at_least(15) + ); + + assert_eq!(LengthHint::between(48, 92) + 5, LengthHint::between(53, 97)); + + let mut len = LengthHint::exact(5); + len += LengthHint::exact(3); + assert_eq!(len, LengthHint::exact(8)); + len += 2; + assert_eq!(len, LengthHint::exact(10)); + len += LengthHint::undefined(); + assert_eq!(len, LengthHint::at_least(10)); + + len += LengthHint::exact(3); + assert_eq!(len, LengthHint::at_least(13)); + len += 2; + assert_eq!(len, LengthHint::at_least(15)); + len += LengthHint::undefined(); + assert_eq!(len, LengthHint::at_least(15)); + + assert_eq!( + LengthHint::between(usize::MAX - 10, usize::MAX - 5) + LengthHint::exact(20), + LengthHint::at_least(usize::MAX) + ); + } + + #[test] + fn test_sum() { + let lens = [ + LengthHint::exact(4), + LengthHint::exact(1), + LengthHint::exact(1), + ]; + assert_eq!( + lens.iter().copied().sum::<LengthHint>(), + LengthHint::exact(6) + ); + + let lens = [ + LengthHint::exact(4), + LengthHint::undefined(), + LengthHint::at_least(1), + ]; + assert_eq!( + lens.iter().copied().sum::<LengthHint>(), + LengthHint::at_least(5) + ); + + let lens = [ + LengthHint::exact(4), + LengthHint::undefined(), + LengthHint::at_most(1), + ]; + assert_eq!( + lens.iter().copied().sum::<LengthHint>(), + LengthHint::at_least(4) + ); + + let lens = [4, 1, 1]; + assert_eq!( + lens.iter().copied().sum::<LengthHint>(), + LengthHint::exact(6) + ); + } + + #[test] + fn test_mul() { + assert_eq!(LengthHint::exact(3) * 2, LengthHint::exact(6)); + + assert_eq!(LengthHint::undefined() * 2, LengthHint::undefined()); + + assert_eq!( + LengthHint::between(48, 92) * 2, + LengthHint::between(96, 184) + ); + + let mut len = LengthHint::exact(5); + len *= 2; + assert_eq!(len, LengthHint::exact(10)); + + assert_eq!( + LengthHint::between(usize::MAX - 10, usize::MAX - 5) * 2, + LengthHint::at_least(usize::MAX) + ); + } + + #[test] + fn test_bitor() { + assert_eq!( + LengthHint::exact(3) | LengthHint::exact(2), + LengthHint::between(2, 3) + ); + assert_eq!( + LengthHint::exact(3) | LengthHint::undefined(), + LengthHint::undefined() + ); + + assert_eq!( + LengthHint::undefined() | LengthHint::undefined(), + LengthHint::undefined() + ); + + assert_eq!( + LengthHint::exact(10) | LengthHint::exact(10), + LengthHint::exact(10) + ); + + assert_eq!( + LengthHint::at_least(15) | LengthHint::exact(3), + LengthHint::at_least(3) + ); + + assert_eq!( + LengthHint::at_least(15) | LengthHint::at_most(18), + LengthHint::undefined() + ); + + assert_eq!( + LengthHint::at_least(15) | LengthHint::at_least(18), + LengthHint::at_least(15) + ); + + assert_eq!( + LengthHint::at_most(15) | LengthHint::at_most(18), + LengthHint::at_most(18) + ); + + assert_eq!( + LengthHint::between(5, 10) | LengthHint::at_most(3), + LengthHint::at_most(10) + ); + + let mut len = LengthHint::exact(5); + len |= LengthHint::exact(3); + assert_eq!(len, LengthHint::between(5, 3)); + } +} diff --git a/third_party/rust/writeable/tests/writeable.rs b/third_party/rust/writeable/tests/writeable.rs new file mode 100644 index 0000000000..b48310e88b --- /dev/null +++ b/third_party/rust/writeable/tests/writeable.rs @@ -0,0 +1,34 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +use std::fmt; +use writeable::assert_writeable_eq; +use writeable::LengthHint; +use writeable::Writeable; + +/// A sample type implementing Writeable +struct WriteableMessage<'s> { + message: &'s str, +} + +impl Writeable for WriteableMessage<'_> { + fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { + sink.write_str(self.message) + } + + fn writeable_length_hint(&self) -> LengthHint { + LengthHint::exact(self.message.len()) + } +} + +writeable::impl_display_with_writeable!(WriteableMessage<'_>); + +#[test] +fn test_basic() { + let input_string = "hello world"; + let message = WriteableMessage { + message: input_string, + }; + assert_writeable_eq!(&message, input_string); +} |