Skip to content

Commit

Permalink
Implement ToArrayString for FixedString<u8>
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Jul 8, 2024
1 parent 9834597 commit ac9f20c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 38 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ 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"
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]
Expand All @@ -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 = []
Expand Down
34 changes: 30 additions & 4 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,16 @@ impl<LenT: ValidLength> From<FixedString<LenT>> for Arc<str> {
}
}

#[cfg(feature = "to-arraystring")]
impl to_arraystring::ToArrayString for &FixedString<u8> {
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<LenT> {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Expand Down Expand Up @@ -364,9 +374,9 @@ impl<LenT: ValidLength> serde::Serialize for FixedString<LenT> {
mod test {
use super::*;

fn check_u8_roundtrip_generic(to_fixed: fn(Box<str>) -> FixedString<u8>) {
fn check_u8_roundtrip_generic(to_fixed: fn(String) -> FixedString<u8>) {
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'));
Expand All @@ -379,13 +389,15 @@ mod test {
}
#[test]
fn check_u8_roundtrip() {
check_u8_roundtrip_generic(|original| FixedString::<u8>::try_from(original).unwrap());
check_u8_roundtrip_generic(|original| {
FixedString::<u8>::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)
});
}
Expand All @@ -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<T> = Option<Option<T>>;
Expand Down

0 comments on commit ac9f20c

Please sign in to comment.