Expand description
Yet another string type for Rust 🦀
- no copy and
const
literal wrapping - no alloc small strings (23 bytes on 64-bit platform)
- no copy owned slices
- a niche:
Option<HipStr>
andHipStr
have the same size - zero dependency, except for optional
serde
support
Also byte strings, OS strings, paths, too!
§Examples
use hipstr::HipStr;
let simple_greetings = HipStr::borrowed("Hello world");
let clone = simple_greetings.clone(); // no copy
std::thread::spawn(move || { println!("{}", clone); });
let user = "John";
let greetings = HipStr::from(format!("Hello {}", user));
let user = greetings.slice(6..); // no copy
drop(greetings); // the slice is _owned_, it exists even if greetings disappear
let chars = user.chars().count(); // "inherits" `&str` methods
§Three Representations
Each type has three distinct representations:
- Borrowed slice
- Inline sequence (up to
HipByt::inline_capacity()
) - Shared reference (cheaply clonable) and slice (sliceable)
The shared reference can be thread-safe or not, depending on the backend.
Most operations keep string normalized, that is, if the string is not borrowed, the inline representation is preferred when possible.
§⚠️ Warning!
The used representation of the empty string is unspecified and may change between patch versions! It may be borrowed or inlined but will never be allocated.
§Three Backends
The crate provides three backends:
Arc
(atomic reference counting), formerlyThreadSafe
Rc
(reference counting), formerlyLocal
Unique
(unique reference)
The last backend, Unique
, is mostly an experiment in pushing what
constitutes a hipstr backend and has no practical use.
The crate root also provides some convenience type aliases, typically for strings:
hipstr::HipStr
with theArc
backend,hipstr::LocalHipStr
with theRc
backend.
§Platform Support
This crate is only supported on platforms where:
- pointers have the same memory size as
usize
, - pointer alignment requirement is strictly greater than 2.
For now, most common architectures are like that. However, hipstr
will not
work on new and future architectures relying on large tagged pointers (e.g.
CHERI 128-bit pointers).
§Features
std
(default): usesstd
rather thancore
andalloc
, and also provides more trait implementations (for comparison, conversions)serde
: provides serialization/deserialization support withserde
borsh
: provides serialization/deserialization support withborsh
bstr
: provides compatibility with BurntSushi’s bstr crate makeHipByt
deref to&bstr::BStr
rather than&[u8]
unstable
: do nothing, used to reveal unstable implementation details
Modules§
Structs§
- Arc
target_has_atomic="ptr"
- Atomic (thread-safe) reference counter.
- Rc
- Local (thread-unsafe) reference counter.
- Unique
- Unique reference marker.
Traits§
- Backend
- Sealed marker trait for allocated backend.
Type Aliases§
- HipByt
- Thread-safe shared byte sequence.
- HipOs
Str std
- Thread-safe shared string.
- HipPath
std
- Thread-safe shared path.
- HipStr
- Thread-safe shared string.
- Local
Deprecated - Local
HipByt - Thread-local shared byte sequence.
- Local
HipOs Str std
- Thread-local shared byte sequence.
- Local
HipPath std
- Thread-local shared path.
- Local
HipStr - Thread-local shared string.
- Thread
Safe Deprecated