diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:50 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:50 +0000 |
commit | 2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35 (patch) | |
tree | d325add32978dbdc1db975a438b3a77d571b1ab8 /vendor/writeable | |
parent | Releasing progress-linux version 1.68.2+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35.tar.xz rustc-2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35.zip |
Merging upstream version 1.69.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/writeable')
-rw-r--r-- | vendor/writeable/.cargo-checksum.json | 2 | ||||
-rw-r--r-- | vendor/writeable/Cargo.lock | 110 | ||||
-rw-r--r-- | vendor/writeable/Cargo.toml | 12 | ||||
-rw-r--r-- | vendor/writeable/src/impls.rs | 104 | ||||
-rw-r--r-- | vendor/writeable/src/lib.rs | 60 | ||||
-rw-r--r-- | vendor/writeable/src/ops.rs | 5 |
6 files changed, 175 insertions, 118 deletions
diff --git a/vendor/writeable/.cargo-checksum.json b/vendor/writeable/.cargo-checksum.json index fe32e7569..35e2eb919 100644 --- a/vendor/writeable/.cargo-checksum.json +++ b/vendor/writeable/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.lock":"efd045d3270108f64d75d49125e15940cf26b54c2944a13354698223d0c4ce17","Cargo.toml":"32b4d9ebb1a5c179aafb27b212e94203a1a2158b7c0387c712f25de31188b7a6","LICENSE":"4ad7541d66a407234e2c84902124cef325c29f3e966353efdb800bedb8b8da21","README.md":"a47b1fa1adda09e2ca28f7f49878c61d8f2057b8f3c42af7d39d646fedbd9934","benches/writeable.rs":"edc81c5524f98e77f0b3a45545606c4b526f63791e6dd8f90c88679c758c4834","examples/writeable_message.rs":"58bf4007f54f9f80428af7b687531837a294aecde533395a0a4c4cf55c9cad7d","src/impls.rs":"c13310eaf5ecb2f4bb87896ea0ee4f060b7828e7ea07b333e69a397a03ef39ae","src/lib.rs":"056631819550bd2845084f97fa1ee6ae130927c7021af86c8cf83337ae638f77","src/ops.rs":"4e49b1e8a8da46c3bcfecbdcb91f6e2d87afab47b76fcc2c4f005bf3cef675f1","tests/writeable.rs":"6dc3db45174180bcbf8980e640525b441c31b0b9db238721888d8cc0bd998ded"},"package":"f8e6ab4f5da1b24daf2c590cfac801bacb27b15b4f050e84eb60149ea726f06b"}
\ No newline at end of file +{"files":{"Cargo.lock":"4d2a250761f97e955d5c1839cb828fe364db73a6f8e25b4cf3a3e3bc00570cec","Cargo.toml":"e52bc721b0357827fb17713b5eb64791cb152eac5d8abdff35bb1b7a825f524d","LICENSE":"4ad7541d66a407234e2c84902124cef325c29f3e966353efdb800bedb8b8da21","README.md":"a47b1fa1adda09e2ca28f7f49878c61d8f2057b8f3c42af7d39d646fedbd9934","benches/writeable.rs":"edc81c5524f98e77f0b3a45545606c4b526f63791e6dd8f90c88679c758c4834","examples/writeable_message.rs":"58bf4007f54f9f80428af7b687531837a294aecde533395a0a4c4cf55c9cad7d","src/impls.rs":"c8fcad8d4374b28441442fa62c8a1e4c31739fbb3f31ed74a176e7ddd0e08cd0","src/lib.rs":"75edc4baf7f69a20c7d6af54f318f904e798b3d6c58eeb624ddd198114aef09a","src/ops.rs":"f201b36dc6a74d4137cca876b99a871555fe7bed7c784917ede49758a1717359","tests/writeable.rs":"6dc3db45174180bcbf8980e640525b441c31b0b9db238721888d8cc0bd998ded"},"package":"92d74a687e3b9a7a129db0a8c82b4d464eb9c36f5a66ca68572a7e5f1cfdb5bc"}
\ No newline at end of file diff --git a/vendor/writeable/Cargo.lock b/vendor/writeable/Cargo.lock index c4110d507..e6b901786 100644 --- a/vendor/writeable/Cargo.lock +++ b/vendor/writeable/Cargo.lock @@ -8,7 +8,7 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ - "hermit-abi", + "hermit-abi 0.1.19", "libc", "winapi", ] @@ -39,9 +39,9 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.11.0" +version = "3.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1ad822118d20d2c234f427000d5acc36eabe1e29a348c89b63dd60b13f28e5d" +checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" [[package]] name = "cast" @@ -125,26 +125,24 @@ dependencies = [ [[package]] name = "crossbeam-epoch" -version = "0.9.10" +version = "0.9.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "045ebe27666471bb549370b4b0b3e51b07f56325befa4284db65fc89c02511b1" +checksum = "01a9af1f4c2ef74bb8aa1f7e19706bc72d03598c8a570bb5de72243c7a9d9d5a" dependencies = [ "autocfg", "cfg-if", "crossbeam-utils", "memoffset", - "once_cell", "scopeguard", ] [[package]] name = "crossbeam-utils" -version = "0.8.11" +version = "0.8.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51887d4adc7b564537b15adcfb307936f8075dfcd5f00dde9a9f1d29383682bc" +checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f" dependencies = [ "cfg-if", - "once_cell", ] [[package]] @@ -177,9 +175,9 @@ checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" [[package]] name = "getrandom" -version = "0.2.7" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" dependencies = [ "cfg-if", "libc", @@ -202,10 +200,13 @@ dependencies = [ ] [[package]] -name = "icu_benchmark_macros" -version = "0.7.0" +name = "hermit-abi" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c867656f2d9c90b13709ac88e710a9d6afe33998c1dfa22384bab8804e8b3d4" +checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" +dependencies = [ + "libc", +] [[package]] name = "itertools" @@ -224,9 +225,9 @@ checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" [[package]] name = "itoa" -version = "1.0.3" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" +checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440" [[package]] name = "js-sys" @@ -245,9 +246,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.133" +version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0f80d65747a3e43d1596c7c5492d95d5edddaabd45a7fcdb02b95f644164966" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" [[package]] name = "log" @@ -266,9 +267,9 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "memoffset" -version = "0.6.5" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" +checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" dependencies = [ "autocfg", ] @@ -284,19 +285,19 @@ dependencies = [ [[package]] name = "num_cpus" -version = "1.13.1" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" +checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" dependencies = [ - "hermit-abi", + "hermit-abi 0.2.6", "libc", ] [[package]] name = "once_cell" -version = "1.15.0" +version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1" +checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" [[package]] name = "oorandom" @@ -334,24 +335,24 @@ dependencies = [ [[package]] name = "ppv-lite86" -version = "0.2.16" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" -version = "1.0.44" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7bd7356a8122b6c4a24a82b278680c73357984ca2fc79a0f9fa6dea7dced7c58" +checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.21" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" dependencies = [ "proc-macro2", ] @@ -388,21 +389,19 @@ dependencies = [ [[package]] name = "rayon" -version = "1.5.3" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd99e5772ead8baa5215278c9b15bf92087709e9c1b2d1f97cdb5a183c933a7d" +checksum = "6db3a213adf02b3bcfd2d3846bb41cb22857d131789e01df434fb7e7bc0759b7" dependencies = [ - "autocfg", - "crossbeam-deque", "either", "rayon-core", ] [[package]] name = "rayon-core" -version = "1.9.3" +version = "1.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "258bcdb5ac6dad48491bb2992db6b7cf74878b0384908af124823d118c99683f" +checksum = "356a0625f1954f730c0201cdab48611198dc6ce21f4acff55089b5a78e6e835b" dependencies = [ "crossbeam-channel", "crossbeam-deque", @@ -412,9 +411,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.6.0" +version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" +checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" dependencies = [ "regex-syntax", ] @@ -427,15 +426,15 @@ checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" [[package]] name = "regex-syntax" -version = "0.6.27" +version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" [[package]] name = "ryu" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" +checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" [[package]] name = "same-file" @@ -454,9 +453,9 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.145" +version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b" +checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" [[package]] name = "serde_cbor" @@ -470,9 +469,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.145" +version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c" +checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" dependencies = [ "proc-macro2", "quote", @@ -481,20 +480,20 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.85" +version = "1.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" +checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883" dependencies = [ - "itoa 1.0.3", + "itoa 1.0.5", "ryu", "serde", ] [[package]] name = "syn" -version = "1.0.101" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e90cde112c4b9690b8cbe810cba9ddd8bc1d7472e2cae317b69e9438c1cba7d2" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" dependencies = [ "proc-macro2", "quote", @@ -522,9 +521,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.4" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" [[package]] name = "unicode-width" @@ -646,9 +645,8 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "writeable" -version = "0.5.0" +version = "0.5.1" dependencies = [ "criterion", - "icu_benchmark_macros", "rand", ] diff --git a/vendor/writeable/Cargo.toml b/vendor/writeable/Cargo.toml index 1c9d41303..0ec582e48 100644 --- a/vendor/writeable/Cargo.toml +++ b/vendor/writeable/Cargo.toml @@ -12,7 +12,7 @@ [package] edition = "2018" name = "writeable" -version = "0.5.0" +version = "0.5.1" authors = ["The ICU4X Project Developers"] include = [ "src/**/*", @@ -29,9 +29,15 @@ license = "Unicode-DFS-2016" repository = "https://github.com/unicode-org/icu4x" resolver = "2" +[package.metadata.workspaces] +independent = true + [package.metadata.docs.rs] all-features = true +[package.metadata.cargo-all-features] +denylist = ["bench"] + [lib] path = "src/lib.rs" bench = false @@ -43,13 +49,9 @@ harness = false [dev-dependencies.criterion] version = "0.3" -[dev-dependencies.icu_benchmark_macros] -version = "0.7" - [dev-dependencies.rand] version = "0.8" features = ["small_rng"] [features] bench = [] -default = [] diff --git a/vendor/writeable/src/impls.rs b/vendor/writeable/src/impls.rs index 649ede4d5..a2b5201e0 100644 --- a/vendor/writeable/src/impls.rs +++ b/vendor/writeable/src/impls.rs @@ -7,12 +7,12 @@ use alloc::borrow::Cow; use core::fmt; macro_rules! impl_write_num { - ($u:ty, $i:ty, $test:ident, $log10:ident) => { + ($u:ty, $i:ty, $test:ident, $max_ilog_10:expr) => { impl $crate::Writeable for $u { fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result { - let mut buf = [b'0'; $log10(<$u>::MAX) as usize + 1]; + let mut buf = [b'0'; $max_ilog_10 + 1]; let mut n = *self; - let mut i = buf.len(); + let mut i = $max_ilog_10 + 1; #[allow(clippy::indexing_slicing)] // n < 10^i while n != 0 { i -= 1; @@ -29,40 +29,38 @@ macro_rules! impl_write_num { } fn writeable_length_hint(&self) -> $crate::LengthHint { - $crate::LengthHint::exact(if *self == 0 { - 1 - } else { - $log10(*self) as usize + 1 - }) + LengthHint::exact(self.checked_ilog10().unwrap_or(0) as usize + 1) } } - // TODO: use the library functions once stabilized. - // https://github.com/unicode-org/icu4x/issues/1428 - #[inline] - const fn $log10(s: $u) -> u32 { - let b = (<$u>::BITS - 1) - s.leading_zeros(); - // s ∈ [2ᵇ, 2ᵇ⁺¹-1] => ⌊log₁₀(s)⌋ ∈ [⌊log₁₀(2ᵇ)⌋, ⌊log₁₀(2ᵇ⁺¹-1)⌋] - // <=> ⌊log₁₀(s)⌋ ∈ [⌊log₁₀(2ᵇ)⌋, ⌊log₁₀(2ᵇ⁺¹)⌋] - // <=> ⌊log₁₀(s)⌋ ∈ [⌊b log₁₀(2)⌋, ⌊(b+1) log₁₀(2)⌋] - // The second line holds because there is no integer in - // [log₁₀(2ᶜ-1), log₁₀(2ᶜ)], if there were, there'd be some 10ⁿ in - // [2ᶜ-1, 2ᶜ], but it can't be 2ᶜ-1 due to parity nor 2ᶜ due to prime - // factors. - - const M: u32 = (core::f64::consts::LOG10_2 * (1 << 26) as f64) as u32; - let low = (b * M) >> 26; - let high = ((b + 1) * M) >> 26; - - // If the bounds aren't tight (e.g. 87 ∈ [64, 127] ⟹ ⌊log₁₀(87)⌋ ∈ [1,2]), - // compare to 10ʰ (100). This shouldn't happen too often as there are more - // powers of 2 than 10 (it happens for 14% of u32s). - if high == low { - low - } else if s < (10 as $u).pow(high) { - low - } else { - high + impl ILog10Ext for $u { + fn checked_ilog10(self) -> Option<u32> { + if self == 0 { + return None; + } + let b = (<$u>::BITS - 1) - self.leading_zeros(); + // self ∈ [2ᵇ, 2ᵇ⁺¹-1] => ⌊log₁₀(self)⌋ ∈ [⌊log₁₀(2ᵇ)⌋, ⌊log₁₀(2ᵇ⁺¹-1)⌋] + // <=> ⌊log₁₀(self)⌋ ∈ [⌊log₁₀(2ᵇ)⌋, ⌊log₁₀(2ᵇ⁺¹)⌋] + // <=> ⌊log₁₀(self)⌋ ∈ [⌊b log₁₀(2)⌋, ⌊(b+1) log₁₀(2)⌋] + // The second line holds because there is no integer in + // [log₁₀(2ᶜ-1), log₁₀(2ᶜ)], if there were, there'd be some 10ⁿ in + // [2ᶜ-1, 2ᶜ], but it can't be 2ᶜ-1 due to parity nor 2ᶜ due to prime + // factors. + + const M: u32 = (core::f64::consts::LOG10_2 * (1 << 26) as f64) as u32; + let low = (b * M) >> 26; + let high = ((b + 1) * M) >> 26; + + // If the bounds aren't tight (e.g. 87 ∈ [64, 127] ⟹ ⌊log₁₀(87)⌋ ∈ [1,2]), + // compare to 10ʰ (100). This shouldn't happen too often as there are more + // powers of 2 than 10 (it happens for 14% of u32s). + Some(if high == low { + low + } else if self < (10 as $u).pow(high) { + low + } else { + high + }) } } @@ -84,11 +82,14 @@ macro_rules! impl_write_num { fn $test() { use $crate::assert_writeable_eq; assert_writeable_eq!(&(0 as $u), "0"); - 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"); @@ -112,19 +113,24 @@ macro_rules! impl_write_num { }; } -impl_write_num!(u8, i8, test_u8, log10_u8); -impl_write_num!(u16, i16, test_u16, log10_u16); -impl_write_num!(u32, i32, test_u32, log10_u32); -impl_write_num!(u64, i64, test_u64, log10_u64); -impl_write_num!(u128, i128, test_u128, log10_u128); - -#[test] -fn assert_log10_approximation() { - for i in 1..u128::BITS { - assert_eq!(i * 59 / 196, 2f64.powf(i.into()).log10().floor() as u32); - } +/// `checked_ilog10` is added as a method on integer types in 1.67. +/// This extension trait provides it for older compilers. +trait ILog10Ext: Sized { + fn checked_ilog10(self) -> Option<u32>; } +impl_write_num!(u8, i8, test_u8, 2); +impl_write_num!(u16, i16, test_u16, 4); +impl_write_num!(u32, i32, test_u32, 9); +impl_write_num!(u64, i64, test_u64, 19); +impl_write_num!(u128, i128, test_u128, 38); +impl_write_num!( + usize, + isize, + test_usize, + if usize::MAX as u64 == u64::MAX { 19 } else { 9 } +); + impl Writeable for str { #[inline] fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { @@ -194,7 +200,7 @@ impl<'a, T: Writeable + ?Sized> Writeable for &T { #[test] fn test_string_impls() { - fn check_writeable_slice<W: Writeable>(writeables: &[W]) { + fn check_writeable_slice<W: Writeable + core::fmt::Display>(writeables: &[W]) { assert_writeable_eq!(&writeables[0], ""); assert_writeable_eq!(&writeables[1], "abc"); } @@ -204,10 +210,10 @@ fn test_string_impls() { check_writeable_slice(arr); // test String impl - let arr: &[String] = &["".to_string(), "abc".to_string()]; + let arr: &[String] = &[String::new(), "abc".to_owned()]; check_writeable_slice(arr); // test &T impl - let arr: &[&String] = &[&"".to_string(), &"abc".to_string()]; + let arr: &[&String] = &[&String::new(), &"abc".to_owned()]; check_writeable_slice(arr); } diff --git a/vendor/writeable/src/lib.rs b/vendor/writeable/src/lib.rs index 66be7f33b..0eb6be8d6 100644 --- a/vendor/writeable/src/lib.rs +++ b/vendor/writeable/src/lib.rs @@ -136,6 +136,28 @@ impl LengthHint { } } +/// [`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 { @@ -240,7 +262,11 @@ pub trait Writeable { /// } /// ``` fn write_to_string(&self) -> Cow<str> { - let mut output = String::with_capacity(self.writeable_length_hint().capacity()); + 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) } @@ -289,7 +315,10 @@ macro_rules! impl_display_with_writeable { /// /// struct Demo; /// impl Writeable for Demo { -/// fn write_to_parts<S: writeable::PartsWrite + ?Sized>(&self, sink: &mut S) -> fmt::Result { +/// 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 { @@ -303,7 +332,13 @@ macro_rules! impl_display_with_writeable { /// 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"); +/// assert_writeable_parts_eq!( +/// &Demo, +/// "foo", +/// [(0, 3, WORD)], +/// "Message: {}", +/// "Hello World" +/// ); /// ``` #[macro_export] macro_rules! assert_writeable_eq { @@ -316,10 +351,19 @@ macro_rules! assert_writeable_eq { 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(), $($arg)*); + 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, $($arg)*); + 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); }}; } @@ -340,6 +384,7 @@ macro_rules! assert_writeable_parts_eq { if let Some(upper) = length_hint.1 { assert!(actual_str.len() <= upper, $($arg)+); } + assert_eq!(actual_writeable.to_string(), $expected_str); }}; } @@ -371,7 +416,10 @@ pub fn writeable_to_parts_for_test<W: Writeable>( ) -> fmt::Result { let start = self.string.len(); f(self)?; - self.parts.push((start, self.string.len(), part)); + let end = self.string.len(); + if start < end { + self.parts.push((start, end, part)); + } Ok(()) } } diff --git a/vendor/writeable/src/ops.rs b/vendor/writeable/src/ops.rs index 3ed4406d7..2ccad7d6d 100644 --- a/vendor/writeable/src/ops.rs +++ b/vendor/writeable/src/ops.rs @@ -82,7 +82,10 @@ impl core::ops::BitOr<LengthHint> for LengthHint { /// struct NonDeterministicWriteable(String, String); /// /// impl Writeable for NonDeterministicWriteable { - /// fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { + /// fn write_to<W: fmt::Write + ?Sized>( + /// &self, + /// sink: &mut W, + /// ) -> fmt::Result { /// sink.write_str(if coin_flip() { &self.0 } else { &self.1 }) /// } /// |