From ac9f20cf2cf6950a4d357000e0b7e627a01dcdfc Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Mon, 8 Jul 2024 14:04:06 +0100 Subject: [PATCH] Implement ToArrayString for FixedString --- .github/workflows/ci.yml | 2 +- Cargo.lock | 58 ++++++++++++++++++++++++++-------------- Cargo.toml | 4 ++- src/string.rs | 34 ++++++++++++++++++++--- 4 files changed, 72 insertions(+), 26 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4ad19f7..38b4645 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,7 +32,7 @@ jobs: run: rustup toolchain install 1.70 - name: Run Cargo test on MSRV - run: cargo +1.70 minimal-versions test --features typesize,serde + run: cargo +1.70 minimal-versions test --features typesize,serde,to-arraystring clippy-stable: runs-on: ubuntu-latest diff --git a/Cargo.lock b/Cargo.lock index d29c6df..a84aefe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,50 +2,56 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + [[package]] name = "itoa" -version = "1.0.10" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "proc-macro2" -version = "1.0.76" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.35" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] [[package]] name = "ryu" -version = "1.0.16" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "serde" -version = "1.0.195" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.195" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", @@ -54,9 +60,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.111" +version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" dependencies = [ "itoa", "ryu", @@ -69,34 +75,46 @@ version = "0.4.4" dependencies = [ "serde", "serde_json", + "to-arraystring", "typesize", ] [[package]] name = "syn" -version = "2.0.48" +version = "2.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" +checksum = "201fcda3845c23e8212cd466bfebf0bd20694490fc0356ae8e428e0824a915a6" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] +[[package]] +name = "to-arraystring" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7b965ccaf5a96e2477a315e176098f3217dc11f96954157dbc70123ab57a4fc" +dependencies = [ + "arrayvec", + "itoa", + "ryu", +] + [[package]] name = "typesize" -version = "0.1.5" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36924509726e38224322c8c90ddfbf4317324338327b7c11b7cf8672cb786da1" +checksum = "eb704842c709bc76f63e99e704cb208beeccca2abbabd0d9aec02e48ca1cee0f" dependencies = [ "typesize-derive", ] [[package]] name = "typesize-derive" -version = "0.1.3" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b122284365ba8497be951b9a21491f70c9688eb6fddc582931a0703f6a00ece" +checksum = "905e88c2a4cc27686bd57e495121d451f027e441388a67f773be729ad4be1ea8" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index a5bef4f..ac2f104 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "small-fixed-array" description = "A crate providing fixed length immutable collections with a low memory footprint." repository = "https://github.com/GnomedDev/small-fixed-array" keywords = ["array", "string", "collection", "low-memory"] -category = ["data-structures"] +categories = ["data-structures"] rust-version = "1.70" version = "0.4.4" edition = "2021" @@ -11,6 +11,7 @@ license = "MIT" [dependencies] serde = { version = "1.0.193", optional = true } +to-arraystring = { version = "0.2.1", optional = true } typesize = { version = "0.1.3", optional = true, default-features = false } [dev-dependencies] @@ -20,6 +21,7 @@ serde_json = "1" default = ["std"] # Add new features to the jobs in .github/workflows/ci.yml. +to-arraystring = ["dep:to-arraystring"] typesize = ["dep:typesize"] serde = ["dep:serde"] nightly = [] diff --git a/src/string.rs b/src/string.rs index 3e174fa..0e3b03c 100644 --- a/src/string.rs +++ b/src/string.rs @@ -326,6 +326,16 @@ impl From> for Arc { } } +#[cfg(feature = "to-arraystring")] +impl to_arraystring::ToArrayString for &FixedString { + const MAX_LENGTH: usize = 255; + type ArrayString = to_arraystring::ArrayString<255>; + + fn to_arraystring(self) -> Self::ArrayString { + Self::ArrayString::from(self).unwrap() + } +} + #[cfg(feature = "serde")] impl<'de, LenT: ValidLength> serde::Deserialize<'de> for FixedString { fn deserialize>(deserializer: D) -> Result { @@ -364,9 +374,9 @@ impl serde::Serialize for FixedString { mod test { use super::*; - fn check_u8_roundtrip_generic(to_fixed: fn(Box) -> FixedString) { + fn check_u8_roundtrip_generic(to_fixed: fn(String) -> FixedString) { for i in 0..=u8::MAX { - let original = "a".repeat(i.into()).into_boxed_str(); + let original = "a".repeat(i.into()); let fixed = to_fixed(original); assert!(fixed.bytes().all(|c| c == b'a')); @@ -379,13 +389,15 @@ mod test { } #[test] fn check_u8_roundtrip() { - check_u8_roundtrip_generic(|original| FixedString::::try_from(original).unwrap()); + check_u8_roundtrip_generic(|original| { + FixedString::::try_from(original.into_boxed_str()).unwrap() + }); } #[test] fn check_u8_roundtrip_static() { check_u8_roundtrip_generic(|original| { - let static_str = Box::leak(original); + let static_str = String::leak(original); FixedString::from_static_trunc(static_str) }); } @@ -398,6 +410,20 @@ mod test { }); } + #[test] + #[cfg(feature = "to-arraystring")] + fn check_u8_roundtrip_arraystring() { + use to_arraystring::ToArrayString; + + check_u8_roundtrip_generic(|original| { + FixedString::from_str_trunc( + FixedString::from_string_trunc(original) + .to_arraystring() + .as_str(), + ) + }); + } + #[test] fn check_sizes() { type DoubleOpt = Option>;