Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exclude = ["fuzz/"]
arrayvec = { version = "0.7", default-features = false, optional = true }
bitcode_derive = { version = "=0.6.9", path = "./bitcode_derive", optional = true }
bytemuck = { version = "1.14", features = [ "min_const_generics", "must_cast" ] }
compact_str = { version = "0.9.0", optional = true }
glam = { version = ">=0.21", default-features = false, optional = true }
rust_decimal = { version = "1.36", default-features = false, optional = true }
serde = { version = "1.0", default-features = false, features = [ "alloc" ], optional = true }
Expand Down
62 changes: 62 additions & 0 deletions src/ext/compact_str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::coder::{Decoder, Encoder};
use crate::derive::{Decode, Encode};
use crate::str::{StrDecoder, StrEncoder};
use compact_str::CompactString;

impl Encoder<CompactString> for StrEncoder {
#[inline(always)]
fn encode(&mut self, t: &CompactString) {
self.encode(t.as_str())
}
}

impl Encode for CompactString {
type Encoder = StrEncoder;
}

impl<'a> Decoder<'a, CompactString> for StrDecoder<'a> {
#[inline(always)]
fn decode(&mut self) -> CompactString {
let s: &str = self.decode();
s.into()
}
}

impl<'a> Decode<'a> for CompactString {
type Decoder = StrDecoder<'a>;
}

#[cfg(test)]
mod tests {
use crate::{decode, encode};
use alloc::vec::Vec;
use compact_str::CompactString;

#[test]
fn compact_string() {
let mut v = CompactString::default();
v.push('0');
v.push('1');
let b = encode(&v);
assert_eq!(decode::<CompactString>(&b).unwrap(), v);
}

#[test]
fn compact_string_long() {
// CompactString inlines up to 23 bytes on 64-bit platforms; the English alphabet exceeds that at 26 bytes.
let v = CompactString::new("abcdefghijklmnopqrstuvwxyz");
let b = encode(&v);
assert_eq!(decode::<CompactString>(&b).unwrap(), v);
}

#[test]
fn compact_string_vec() {
let v = vec![
CompactString::new("01"),
CompactString::new("abcdefghijklmnopqrstuvwxyz"),
];
let b = encode(&v);
let v2 = decode::<Vec<CompactString>>(&b).unwrap();
assert_eq!(v, v2);
}
}
2 changes: 2 additions & 0 deletions src/ext/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#[cfg(feature = "arrayvec")]
mod arrayvec;
#[cfg(feature = "compact_str")]
mod compact_str;
#[cfg(feature = "glam")]
#[rustfmt::skip] // Makes impl_struct! calls way longer.
mod glam;
Expand Down
Loading