A flexible, simple to use, clone-efficient String replacement for Rust. It unifies borrowed, inlined, referenced counted and boxed strings into a single type.
If you've used Cow, but you wish cloning owned strings was more performant and that being owned didn't always imply heap allocation, this crate might be what you are looking for. The operations are "lazy" (like Cow), and it tries not to do work the user is not expecting.
Users of previous versions: you should be aware this new version is a ground up rewrite with a solidly different thought process, API and design. Even if the previous versions didn't match your needs, this one might. Users should be aware that nearly all the string construction code is not yet present in this version. The new way to do this (workaround?) is to do the work as a
Stringand then import it into aLocalStrorSharedStr. Moving into and out of the boxed variant (from_owned) should be near zero cost.
- Simple: just an enum. You mostly already know how to use it.
- Borrowed, inlined, reference counted, and boxed strings in a single type
- O(1) clone
- NOTE: first
clonewhen variant isBoxedis O(n)
- NOTE: first
- Mutable (Copy-on-write under the hood, if necessary)
- Inlined string type can be used on its own
- Same size a a
String(3 words wide, even inside anOption) - Lazy instantiation (no unexpected allocations)
- No external dependencies
- NOTE:
serde/sqlxoptional for serialization/deserialization/encode/decode
- NOTE:
- Optional
no_std - Optional
safefeature that forbids anyunsafeusage- NOTE: This does induce a performance penalty, as would be expected
- NOTE 2:
OsStr/Pathsupport on Windows requires at least one unsafe call (win_min_unsafefeature).
- Handles all Rust string types (
str,CStr,OsStr,Path,[u8])
- safe = Use all safe functions and add
forbid(unsafe_code)(performance penalty) - sqlx = Enable sqlx encode/decode/type support
- sqlx_pg_arrays = Enable sqlx Postgres array support (implies
sqlxfeature) - std = Use
std(default) - serde = add
serdedependency and adds serialization/deserialization - win_min_unsafe = enables the minimum necessary unsafe code on Windows to support
OsStr/Path. No other string types or operating systems are impacted (impliessafefeature).- NOTE: The code will refuse to compile if this is not specified when ALL the following conditions are true:
- The
safefeature is enabled - The
osstrand/orpathfeature(s) are enabled - Compiling for Windows
- The
- NOTE: The code will refuse to compile if this is not specified when ALL the following conditions are true:
- str = Enable
str-based strings (default) - bytes = Enable byte-based strings (
[u8]) - cstr = Enable
CStr-based strings - osstr = Enable
OsStr-based strings - path = Enable
Path-based strings (impliesosstrfeature)
It is just an enum that looks like this - you can probably guess much of how it works just by looking at it:
// `S` is just the raw string type (typically `str`)
// `R` is just an `Arc` or an `Rc`.
pub enum FlexStr<'s, S, R> {
Borrowed(&'s S),
Inlined(InlineFlexStr<S>),
RefCounted(R),
Boxed(Box<S>),
}
// You would typically use it via one of the type aliases, for example:
pub type LocalStr<'s> = FlexStr<'s, str, Rc<str>>;
pub type SharedStr<'s> = FlexStr<'s, str, Arc<str>>;Even that you don't really need to concern yourself with. You can just use it how you would expect a simple wrapper to behave.
use flexstr::*;
// This will be a "Borrowed" variant
let hello: SharedStr = "hello".into();
assert!(hello.is_borrowed());
// This will be a "Boxed" variant
let world: SharedStr = "world".to_string().into();
assert!(world.is_boxed());
// This is now "Inlined" (since it is short)
let hello = hello.into_owned();
assert!(hello.is_inlined());
// This is now "Inlined" as well (since it is short)
let world = world.optimize();
assert!(world.is_inlined());
println!("{hello} {world}");In general, it performs quite well given that it is mostly just a thin wrapper over the stdlib. See the benchmarks page for more details.
The code was written by hand with care (although AI tab completion was used). Any contributions should be completely understood by the contributor, whether AI assisted or not.
The tests on the otherhand were 90%+ generated by AI under my instruction. I've done a cursory review for sanity, but they need more work. Volunteers welcome.
This is currently experimental, however, I will be using this at a startup in production code, so it will become production ready at some point.
Contributions are welcome so long as they align to my vision for this crate. Currently, it does most of what I want it to do (outside of string construction and mutation, but I'm not ready to start on that yet).
This project is licensed optionally under either:
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)