| Crates.io | waterui-str |
| lib.rs | waterui-str |
| version | 0.2.1 |
| created_at | 2025-08-31 14:06:04.372885+00 |
| updated_at | 2025-12-13 19:48:33.374526+00 |
| description | String utilities for WaterUI |
| homepage | |
| repository | https://github.com/water-rs/waterui |
| max_upload_size | |
| id | 1818585 |
| size | 43,264 |
A memory-efficient, reference-counted string type optimized for both static and owned strings.
waterui-str provides Str, a hybrid string type that automatically chooses between static string references and reference-counted owned strings. This design eliminates unnecessary allocations for static strings while enabling efficient cloning for owned strings through reference counting.
The crate is designed for no_std environments (with alloc), making it suitable for embedded systems and WebAssembly targets. It integrates seamlessly with WaterUI's reactive system through the nami-core integration.
Key features:
&str, works with all standard string operationsWaterUI's nami reactive primitives via impl_constant!serde featureAdd to your Cargo.toml:
[dependencies]
waterui-str = "0.2"
With serde support:
[dependencies]
waterui-str = { version = "0.2", features = ["serde"] }
use waterui_str::Str;
// Static strings - no allocation
let static_str = Str::from("hello");
// Owned strings - reference counted
let owned = Str::from(String::from("world"));
// Cheap cloning
let clone = owned.clone(); // Just increments ref count
// Transparent string operations
assert_eq!(static_str.len(), 5);
assert!(static_str.starts_with("hel"));
// Concatenation
let combined = static_str + " " + &owned;
assert_eq!(combined, "hello world");
Str uses a clever tagged pointer representation:
&'static str)Shared (reference-counted String)This allows zero-overhead discrimination between static and owned strings at runtime.
Owned strings use internal reference counting via Shared:
Empty strings always use a static empty string reference, regardless of how they're created:
use waterui_str::Str;
let empty1 = Str::new();
let empty2 = Str::from("");
let empty3 = Str::from(String::new());
// All three use the same static "" reference
use waterui_str::Str;
// From static string literal
let s1 = Str::from("hello");
// From owned String
let s2 = Str::from(String::from("hello"));
// From UTF-8 bytes
let bytes = vec![104, 101, 108, 108, 111]; // "hello"
let s3 = Str::from_utf8(bytes).unwrap();
// Empty string
let s4 = Str::new();
use waterui_str::Str;
let mut s = Str::from("hello");
s.append(" world");
assert_eq!(s, "hello world");
// Concatenation with +
let s1 = Str::from("foo");
let s2 = s1 + "bar";
assert_eq!(s2, "foobar");
// AddAssign
let mut s3 = Str::from("hello");
s3 += " world";
assert_eq!(s3, "hello world");
use waterui_str::Str;
// Collect from iterator
let words = vec!["hello", " ", "world"];
let s: Str = words.into_iter().collect();
assert_eq!(s, "hello world");
// Extend
let mut s = Str::from("hello");
s.extend(vec![" ", "world"]);
assert_eq!(s, "hello world");
use waterui_str::Str;
let s1 = Str::from(String::from("owned"));
let s2 = s1.clone();
// Convert to String - takes ownership if ref count is 1
let string1 = s1.into_string(); // Copies because s2 still exists
assert_eq!(string1, "owned");
// s2 is now the only reference
let string2 = s2.into_string(); // No copy, takes ownership
assert_eq!(string2, "owned");
Str::new() - Create empty stringStr::from_static(&'static str) - Create from static string literalStr::from_utf8(Vec<u8>) - Create from UTF-8 bytes with validationunsafe Str::from_utf8_unchecked(Vec<u8>) - Create from UTF-8 bytes without validationas_str(&self) -> &str - Get string slicelen(&self) -> usize - Get byte lengthis_empty(&self) -> bool - Check if emptyappend(&mut self, &str) - Append stringinto_string(self) -> String - Convert to owned StringDeref<Target = str> - Transparent access to string methodsClone - Efficient reference-counted cloningDefault - Empty stringDisplay, Debug - FormattingHash, Eq, Ord - Collections and comparisonsAsRef<str>, AsRef<[u8]>, Borrow<str> - ConversionsFromStr, FromIterator - Parsing and collectionAdd, AddAssign - ConcatenationExtend - Extension from iteratorsIndex<I> - Slice indexingstd is available)AsRef<OsStr>, AsRef<Path> - Filesystem operationsTryFrom<OsString> - OS string conversionToSocketAddrs - Network address resolutionCow<'static, str>?While Cow<'static, str> provides similar functionality, Str offers:
Cow::Ownedappend() optimized for the use caseThe internal reference count is deliberately not exposed in the public API. This:
Str as a simple value typeThe crate includes extensive memory safety tests designed for Miri (Rust's undefined behavior detector), covering:
into_string() with unique ownership: Zero copy, takes ownershipinto_string() with shared ownership: Single allocation and copyLicensed under the same terms as the WaterUI project.